djblets.util.http¶
HTTP-related utilities.
- class HttpResponseNotAcceptable(content=b'', *args, **kwargs)¶
Bases:
HttpResponse
- status_code = 406¶
- __annotations__ = {}¶
- set_last_modified(response, timestamp)¶
Sets the Last-Modified header in a response based on a DateTimeField.
- get_modified_since(request, last_modified)¶
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.
- set_etag(response, etag)¶
Sets the ETag header in a response.
- encode_etag(etag)¶
Encode a string as a SHA1 value, for use in an ETag.
- etag_if_none_match(request, etag)¶
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)¶
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)¶
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)¶
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)¶
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.
- is_mimetype_a(mimetype, parent_mimetype)¶
Returns whether or not a given mimetype is a subset of another.
This is generally used to determine if vendor-specific mimetypes is a subset of another type. For example, application/vnd.djblets.foo+json is a subset of application/json.
- get_url_params_except(query, *params)¶
Return a URL query string that filters out some params.
This is used often when one wants to preserve some GET parameters and not others.