djblets.util.http¶
HTTP-related utilities.
- class HttpResponseNotAcceptable(content=b'', *args, **kwargs)[source]¶
Bases:
HttpResponse
- __annotations__ = {}¶
- set_last_modified(response, timestamp)[source]¶
Sets the Last-Modified header in a response based on a DateTimeField.
- get_modified_since(request, last_modified)[source]¶
Checks if a Last-Modified timestamp is newer than the requested HTTP_IF_MODIFIED_SINCE from the browser. This can be used to bail early if no updates have been performed since the last access to the page.
This can take a DateField, datetime, HTTP date-formatted string, or a function for the last_modified timestamp. If a function is passed, it will only be called if the HTTP_IF_MODIFIED_SINCE header is present.
- etag_if_none_match(request, etag)[source]¶
Checks if an ETag matches the If-None-Match header sent by the browser. This can be used to bail early if no updates have been performed since the last access to the page.
- etag_if_match(request, etag)[source]¶
Checks if an ETag matches the If-Match header sent by the browser. This is used by PUT requests to to indicate that the update should only happen if the specified ETag matches the header.
- build_not_modified_from_response(response)[source]¶
Build a Not Modified response from an existing response.
This will create a new
HttpResponseNotModified
containing headers from an original response. It should be used in conjunction with functions likeetag_if_none_match()
when checking against an otherwise valid response that’s been built.- Parameters:
response (
django.http.HttpResponse
) – The HTTP response to build the Not Modified response from.- Returns:
The new Not Modified response.
- Return type:
- get_http_accept_lists(request)[source]¶
Returns lists of mimetypes from the request’s Accept header.
This will return two lists, a list of acceptable mimetypes in order of requested priority, and a list of unacceptable mimetypes.
- get_http_requested_mimetype(request, supported_mimetypes)[source]¶
Gets the mimetype that should be used for returning content.
This is based on the client’s requested list of mimetypes (in the HTTP Accept header) and the supported list of mimetypes that can be returned in this request.
If a valid mimetype that can be used is found, it will be returned. Otherwise, None is returned, and the caller is expected to return HttpResponseNotAccepted.