bareutils.header
module bareutils.header ¶
Summary¶
Header utilities
Description¶
A collection of functions to extract headers from the ASGI scope.
function bareutils.header.accept ¶
Summary¶
Returns the accept header if it exists.
Description¶
Where quality is not given it defaults to 1.0.
```python
accept([(b'accept', b'text/html, application/xhtml+xml, application/xml;q=0.9, /;q=0.8')]) {b'text/html': {b'q': 1.0}, b'application/xhtml+xml': {b'q': 1.0}, b'application/xml': {b'q': 0.9}, b'/': {b'q': 0.8}} ```
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
If true add the implicit wildcard '*'. Defaults to False.
An optional default. Defaults to None.
Returns¶
Optional[Mapping[bytes, Mapping[bytes, Any]]]: A dictionary where the key is media type and the value is a mapping of the parameters.function bareutils.header.accept_ch ¶
Summary¶
The Accept-CH header is set by the server to specify which Client Hints
Description¶
headers client should include in subsequent requests.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
An optional default. Defaults to None.
function bareutils.header.accept_ch_lifetime ¶
Summary¶
The Accept-CH-Lifetime header is set by the server to specify the
Description¶
persistence of Accept-CH header value that specifies for which Client Hints headers client should include in subsequent requests.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
An optional default. Defaults to None.
function bareutils.header.accept_charset ¶
Summary¶
Extracts the accept encoding header if it exists into a mapping of the
Description¶
encoding and the quality value which defaults to 1.0 if missing.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
If True ensures the '*' charset is included. Defaults to False.
An optional default. Defaults to None.
function bareutils.header.accept_encoding ¶
Summary¶
Extracts the accept encoding header if it exists into a mapping of the encoding
Description¶
and the quality value which defaults to 1.0 if missing.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers to search.
If True ensures the 'identity' encoding is included.. Defaults to False.
An optional default. Defaults to None.
function bareutils.header.accept_language ¶
Summary¶
Extracts the accept language header if it exists into a mapping of the
Description¶
encoding and the quality value which defaults to 1.0 if missing.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers to search.
If True ensures the '*' charset is included. Defaults to False.
[description]. Defaults to None.
function bareutils.header.accept_patch ¶
Summary¶
The Accept-Patch response HTTP header advertises which media-type the
Description¶
server is able to understand.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers to search.
An optional default value. Defaults to None.
Returns¶
Optional[list[tuple[bytes, bytes | None]]]: A list of tuples of media type and optional charset.function bareutils.header.accept_ranges ¶
Summary¶
Returns the value of the accept ranges header of None if missing
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
An optional default value. Defaults to None.
function bareutils.header.access_control_allow_credentials ¶
Summary¶
Extracts the access control allow credentials header as a bool or None if
Description¶
missing.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.access_control_allow_headers ¶
Summary¶
The Access-Control-Allow-Headers response header is used in response to
Description¶
a preflight request which includes the Access-Control-Request-Headers to indicate which HTTP headers can be used during the actual request.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
Returns¶
list[bytes] | None: list[bytes] | None: A list of the allowed headers or '*' for all headers.function bareutils.header.access_control_allow_methods ¶
Summary¶
The Access-Control-Allow-Methods response header specifies the method or
Description¶
methods allowed when accessing the resource in response to a preflight request.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
Returns¶
list[bytes] | None: list[bytes] | None: A list of the allowed methods, or '*' for all methods.function bareutils.header.access_control_allow_origin ¶
Summary¶
The Access-Control-Allow-Origin response header indicates whether the
Description¶
response can be shared with requesting code from the given origin.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.access_control_expose_headers ¶
Summary¶
[summary]
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
If true add the safelisted headers. Defaults to False.
An optional default value. Defaults to None.
function bareutils.header.access_control_max_age ¶
Summary¶
The Access-Control-Max-Age response header indicates how long the results
Description¶
of a preflight request (that is the information contained in the Access-Control-Allow-Methods and Access-Control-Allow-Headers headers) can be cached.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
An optional default value. Defaults to None.
function bareutils.header.access_control_request_headers ¶
Summary¶
The Access-Control-Request-Headers request header is used by browsers
Description¶
when issuing a preflight request, to let the server know which HTTP headers the client might send when the actual request is made.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
An optional default value. Defaults to None.
function bareutils.header.access_control_request_method ¶
Summary¶
The Access-Control-Request-Method request header is used by browsers when
Description¶
issuing a preflight request, to let the server know which HTTP method will be used when the actual request is made. This header is necessary as the preflight request is always an OPTIONS and doesn't use the same method as the actual request.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.age ¶
Summary¶
The Age header contains the time in seconds the object has been in a
Description¶
proxy cache.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.allow ¶
Summary¶
The Allow header lists the set of methods supported by a resource.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.authorization ¶
Summary¶
The HTTP Authorization request header contains the credentials to
Description¶
authenticate a user agent with a server, usually after the server has responded with a 401 Unauthorized status and the WWW-Authenticate header.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.cache_control ¶
Summary¶
The Cache-Control general-header field is used to specify directives for
Description¶
caching mechanisms in both requests andresponses. Caching directives are unidirectional, meaning that a given directive in a request is not implying that the same directive is to be given in the response.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.clear_site_data ¶
Summary¶
The Clear-Site-Data header clears browsing data (cookies, storage, cache)
Description¶
associated with the requesting website. It allows web developers to have more control over the data stored locally by a browser for their origins.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.collect ¶
Summary¶
Collect all headers into a mapping
function bareutils.header.connection ¶
Summary¶
The Connection general header controls whether or not the network
Description¶
connection stays open after the current transaction finishes. If the value sent is keep-alive, the connection is persistent and not closed, allowing for subsequent requests to the same server to be done.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.content_disposition ¶
Summary¶
Returns the content type if any otherwise None
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default. Defaults to None.
Returns¶
tuple[bytesOptionalMapping[bytes, bytes]]] | None: tuple[bytes, Mapping[bytes, bytes] | None] | None: A tuple of the media type and a mapping of the parameters.function bareutils.header.content_encoding ¶
Summary¶
Returns the content encodings in a list or None if they were not
Description¶
specified.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
If True ensures the 'identity' encoding is included. Defaults to False.
An optional default value. Defaults to None.
function bareutils.header.content_language ¶
Summary¶
The Content-Language entity header is used to describe the language(s)
Description¶
intended for the audience, so that it allows a user to differentiate according to the users' own preferred language.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.content_length ¶
Summary¶
[summary]
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.content_location ¶
Summary¶
The Content-Location header indicates an alternate location for the
Description¶
returned data. The principal use is to indicate the URL of a resource transmitted as the result of content negotiation.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.content_range ¶
Summary¶
The Content-Range response HTTP header indicates where in a full body
Description¶
message a partial message belongs.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
Returns¶
tuple[bytes, tuple[int, int] | None, int | None] | None: tuple[bytes, tuple[int, int] | None, int | None] | None: The content-range header if found, or the default.function bareutils.header.content_security_policy ¶
Summary¶
The HTTP Content-Security-Policy response header allows web site
Description¶
administrators to control resources the user agent is allowed to load for a given page. With a few exceptions, policies mostly involve specifying server origins and script endpoints. This helps guard against cross-site scripting attacks (XSS).
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
An optional default. Defaults to None.
Returns¶
list[tuple[bytes, list[bytes]]] | None: list[tuple[bytes, list[bytes]]]] | None The policy or the default.function bareutils.header.content_security_policy_report_only ¶
Summary¶
The HTTP Content-Security-Policy-Report-Only response header allows web
Description¶
developers to experiment with policies by monitoring (but not enforcing) their effects. These violation reports consist of JSON documents sent via an HTTP POST request to the specified URI.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
Returns¶
list[tuple[bytes, list[bytes]]] | None: list[tuple[bytes, list[bytes]]]] | None The policy, or the default.function bareutils.header.content_type ¶
Summary¶
Returns the content type if any otherwise None
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
An optional default value. Defaults to None.
Returns¶
tuple[bytesOptionalMapping[bytes, bytes]]] | None: tuple[bytes, Mapping[bytes, bytes] | None] | None: A tuple of the media type and a mapping of the parameters or the default if absent.function bareutils.header.cookie ¶
Summary¶
Returns the cookies as a name-value mapping.
function bareutils.header.cross_origin_resource_policy ¶
Summary¶
The HTTP Cross-Origin-Resource-Policy response header conveys a desire
Description¶
that the browser blocks no-cors cross-origin/cross-site requests to the given resource.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.date ¶
Summary¶
The Date general HTTP header contains the date and time at which the
Description¶
message was originated.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
Returns¶
datetime.datetime | None: datetime | None: The date and time at which the message was originatedfunction bareutils.header.device_memory ¶
Summary¶
The Device-Memory header is a Device Memory API header that works like
Description¶
Client Hints header which represents the approximate amount of RAM client device has.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
An optional default value. Defaults to None.
function bareutils.header.dnt ¶
Summary¶
The DNT (Do Not Track) request header indicates the user's tracking
Description¶
preference. It lets users indicate whether they would prefer privacy rather than personalized content.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.dpr ¶
Summary¶
The DPR header is a Client Hints headers which represents the client
Description¶
device pixel ratio (DPR), which is the the number of physical device pixels corresponding to every CSS pixel.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.expect ¶
Summary¶
Returns the expect header
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.expires ¶
Summary¶
The Expires header contains the date/time after which the response is
Description¶
considered stale.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
Returns¶
datetime.datetime | None: datetime | None: The date/time after which the response is considered stale, or the default value.function bareutils.header.find ¶
Summary¶
Find the value of a header, or return a default value.
Parameters¶
name: bytesThe header name.
The headers to search.
An optional default value. Defaults to None.
function bareutils.header.find_all ¶
Summary¶
Find all the values for a given header.
Parameters¶
name: bytesThe header name.
The headers to search.
function bareutils.header.find_date ¶
Summary¶
Find a header containing a date.
Parameters¶
name: bytesThe name of the header.
The headers.
The headers, Defaults to None.
function bareutils.header.find_exact ¶
Summary¶
Find the value of a header, or raise an error.
Parameters¶
name: bytesThe header name.
The headers to search.
function bareutils.header.host ¶
Summary¶
Returns the host header as a name, port tuple
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
Returns¶
tuple[bytes, int | None] | None: tuple[bytes, int | None] | None: The host as a name, port tuple.function bareutils.header.if_modified_since ¶
Summary¶
The If-Modified-Since request HTTP header makes the request conditional:
Description¶
the server will send back the requested resource, with a 200 status, only if it has been last modified after the given date. If the request has not been modified since, the response will be a 304 without any body; the Last-Modified response header of a previous request will contain the date of last modification. Unlike If-Unmodified-Since, If-Modified-Since can only be used with a GET or HEAD.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers
[description]. Defaults to None.
Returns¶
datetime.datetime | None: datetime | None: The timestamp if present, otherwise the default value.function bareutils.header.index ¶
Summary¶
Find the index of the header in the list.
Parameters¶
name: bytesThe header name.
The headers to search.
function bareutils.header.last_modified ¶
Summary¶
The Last-Modified response HTTP header contains the date and time at
Description¶
which the origin server believes the resource was last modified. It is used as a validator to determine if a resource received or stored is the same. Less accurate than an ETag header, it is a fallback mechanism. Conditional requests containing If-Modified-Since or If-Unmodified-Since headers make use of this field.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
Returns¶
datetime.datetime | None: datetime | None: The timestamp if present, otherwise the default value.function bareutils.header.location ¶
Summary¶
The Location response header indicates the URL to redirect a page to. It
Description¶
only provides a meaning when served with a 3xx (redirection) or 201 (created) status response.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.origin ¶
Summary¶
The Origin request header indicates where a fetch originates from. It
Description¶
doesn't include any path information, but only the server name. It is sent with CORS requests, as well as with POST requests. It is similar to the Referer header, but, unlike this header, it doesn't disclose the whole path.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.proxy_authorization ¶
Summary¶
The HTTP Proxy-Authorization request header contains the credentials to
Description¶
authenticate a user agent to a proxy server, usually after the server has responded with a 407 Proxy Authentication Required status and the Proxy-Authenticate header.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.referer ¶
Summary¶
The Referer request header contains the address of the previous web page
Description¶
from which a link to the currently requested page was followed. The Referer header allows servers to identify where people are visiting them from and may use that data for analytics, logging, or optimized caching, for example.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.server ¶
Summary¶
The Server header contains information about the software used by the
Description¶
origin server to handle the request.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.
function bareutils.header.set_cookie ¶
Summary¶
Returns the cookies as a name-value mapping.
function bareutils.header.to_dict ¶
Summary¶
Convert a list of headers into a dictionary where the key is the header
Description¶
name and the value is a list of the values of the headers for that name
Returns¶
dict[bytes, list[bytes]]: A dictionary where the key is the header name and the value is a list of the values of the headers for that namefunction bareutils.header.upsert ¶
Summary¶
If the header exists overwrite the value, otherwise append a new value.
Parameters¶
name: bytesThe header name.
The header value.
The headers.
function bareutils.header.vary ¶
Summary¶
Returns the vary header value as a list of headers.
Parameters¶
headers: Iterable[tuple[bytes, bytes]]The headers.
An optional default value. Defaults to None.