| 60 | |
| 61 | class HeadMiddleware(object): |
| 62 | """ Make a sensible HEAD response """ |
| 63 | |
| 64 | def process_response(self, request, response): |
| 65 | """ |
| 66 | If the request method is HEAD and the response is 200 OK, set the |
| 67 | 'Content-Length' header, and remove the content body. |
| 68 | """ |
| 69 | if request.method == 'HEAD': |
| 70 | # set the Content-Length header if not already set |
| 71 | if not response.has_header('Content-Length'): |
| 72 | response['Content-Length'] = str(len(response.content)) |
| 73 | |
| 74 | # remove the content |
| 75 | response.content = '' |
| 76 | |
| 77 | return response |