Skip to main content

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

AttributeTypeDescription
max_random_bytesint = 100The 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

NameTypeDescription
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

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The original or gzipped response object with updated headers and content.