You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

29 lines
1023 B

  1. """
  2. Functions that modify an HTTP request or response in some way.
  3. """
  4. # This group of functions are run as part of the response handling, after
  5. # everything else, including all response middleware. Think of them as
  6. # "compulsory response middleware". Be careful about what goes here, because
  7. # it's a little fiddly to override this behavior, so they should be truly
  8. # universally applicable.
  9. def conditional_content_removal(request, response):
  10. """
  11. Removes the content of responses for HEAD requests, 1xx, 204 and 304
  12. responses. Ensures compliance with RFC 7230, section 3.3.3.
  13. """
  14. if 100 <= response.status_code < 200 or response.status_code in (204, 304):
  15. if response.streaming:
  16. response.streaming_content = []
  17. else:
  18. response.content = b''
  19. response['Content-Length'] = '0'
  20. if request.method == 'HEAD':
  21. if response.streaming:
  22. response.streaming_content = []
  23. else:
  24. response.content = b''
  25. return response