GZipMiddleware
Compress content if the browser allows gzip compression. Set the Vary header accordingly, so that caches will base their storage on the Accept-Encoding header.
Attributes
| Attribute | Type | Description |
|---|---|---|
| max_random_bytes | int = 100 | The maximum number of random bytes to append to the compressed content to prevent BREACH attacks. |
Methods
process_response()
@classmethod
def process_response(
request: [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest),
response: [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
Compresses the HTTP response content using gzip if the client supports it and the content meets size and encoding criteria. It updates the 'Vary', 'Content-Encoding', and 'Content-Length' headers, and weakens any existing strong ETags to comply with RFC 9110.
Parameters
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest) | The incoming HTTP request object used to check for 'Accept-Encoding' support. |
| response | [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The outgoing HTTP response object to be evaluated for compression. |
Returns
| Type | Description |
|---|---|
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The original or gzipped response object with updated headers and content. |