module documentation

Undocumented

Class COEP Cross Origin Embedder Policies
Class COOP Cross Origin Opener Policies
Function dump_age Formats the duration as a base-10 integer.
Function dump_cookie Create a Set-Cookie header without the ``Set-Cookie`` prefix.
Function dump_csp_header Dump a Content Security Policy header.
Function dump_header Dump an HTTP header again. This is the reversal of :func:`parse_list_header`, :func:`parse_set_header` and :func:`parse_dict_header`. This also quotes strings that include an equals sign unless you pass it as dict of key, value pairs.
Function dump_options_header The reverse function to :func:`parse_options_header`.
Function generate_etag Generate an etag for some data.
Function http_date Format a datetime object or timestamp into an :rfc:`2822` date string.
Function is_byte_range_valid Checks if a given byte content range is valid for the given length.
Function is_entity_header Check if a header is an entity header.
Function is_hop_by_hop_header Check if a header is an HTTP/1.1 "Hop-by-Hop" header.
Function is_resource_modified Convenience method for conditional requests.
Function parse_accept_header Parses an HTTP Accept-* header. This does not implement a complete valid algorithm but one that supports at least value and quality extraction.
Function parse_age Parses a base-10 integer count of seconds into a timedelta.
Function parse_authorization_header Parse an HTTP basic/digest authorization header transmitted by the web browser. The return value is either `None` if the header was invalid or not given, otherwise an :class:`~werkzeug.datastructures.
Function parse_cache_control_header Parse a cache control header. The RFC differs between response and request cache control, this method does not. It's your responsibility to not use the wrong control statements.
Function parse_content_range_header Parses a range header into a :class:`~werkzeug.datastructures.ContentRange` object or `None` if parsing is not possible.
Function parse_cookie Parse a cookie from a string or WSGI environ.
Function parse_csp_header Parse a Content Security Policy header.
Function parse_date Parse an :rfc:`2822` date into a timezone-aware :class:`datetime.datetime` object, or ``None`` if parsing fails.
Function parse_dict_header Parse lists of key, value pairs as described by RFC 2068 Section 2 and convert them into a python dict (or any other mapping object created from the type with a dict like interface provided by the `cls` argument):...
Function parse_etags Parse an etag header.
Function parse_if_range_header Parses an if-range header which can be an etag or a date. Returns a :class:`~werkzeug.datastructures.IfRange` object.
Function parse_list_header Parse lists as described by RFC 2068 Section 2.
Function parse_options_header Parse a ``Content-Type``-like header into a tuple with the value and any options:
Function parse_range_header Parses a range header into a :class:`~werkzeug.datastructures.Range` object. If the header is missing or malformed `None` is returned. `ranges` is a list of ``(start, stop)`` tuples where the ranges are non-inclusive.
Function parse_set_header Parse a set-like header and return a :class:`~werkzeug.datastructures.HeaderSet` object:
Function parse_www_authenticate_header Parse an HTTP WWW-Authenticate header into a :class:`~werkzeug.datastructures.WWWAuthenticate` object.
Function quote_etag Quote an etag.
Function quote_header_value Quote a header value if necessary.
Function remove_entity_headers Remove all entity headers from a list or :class:`Headers` object. This operation works in-place. `Expires` and `Content-Location` headers are by default not removed. The reason for this is :rfc:`2616` section 10...
Function remove_hop_by_hop_headers Remove all HTTP/1.1 "Hop-by-Hop" headers from a list or :class:`Headers` object. This operation works in-place.
Function unquote_etag Unquote a single etag:
Function unquote_header_value Unquotes a header value. (Reversal of :func:`quote_header_value`). This does not use the real unquoting but what browsers are actually using for quoting.
Constant HTTP_STATUS_CODES Undocumented
Function _is_extended_parameter Per RFC 5987/8187, "extended" values may *not* be quoted. This is in keeping with browser implementations. So we test using this function to see if the key indicates this parameter follows the `ext-parameter` syntax (using a trailing '*').
Type Variable _TAnyAccept Undocumented
Type Variable _TAnyCC Undocumented
Type Variable _TAnyCSP Undocumented
Type Alias _t_cc_update Undocumented
Type Alias _t_csp_update Undocumented
Variable _accept_re Undocumented
Variable _entity_headers Undocumented
Variable _etag_re Undocumented
Variable _hop_by_hop_headers Undocumented
Variable _option_header_piece_re Undocumented
Variable _option_header_start_mime_type Undocumented
Variable _token_chars Undocumented

Formats the duration as a base-10 integer. :param age: should be an integer number of seconds, a :class:`datetime.timedelta` object, or, if the age is unknown, `None` (default).

def dump_cookie(key: str, value: t.Union[bytes, str] = '', max_age: t.Optional[t.Union[timedelta, int]] = None, expires: t.Optional[t.Union[str, datetime, int, float]] = None, path: t.Optional[str] = '/', domain: t.Optional[str] = None, secure: bool = False, httponly: bool = False, charset: str = 'utf-8', sync_expires: bool = True, max_size: int = 4093, samesite: t.Optional[str] = None) -> str: (source)

Create a Set-Cookie header without the ``Set-Cookie`` prefix. The return value is usually restricted to ascii as the vast majority of values are properly escaped, but that is no guarantee. It's tunneled through latin1 as required by :pep:`3333`. The return value is not ASCII safe if the key contains unicode characters. This is technically against the specification but happens in the wild. It's strongly recommended to not use non-ASCII values for the keys. :param max_age: should be a number of seconds, or `None` (default) if the cookie should last only as long as the client's browser session. Additionally `timedelta` objects are accepted, too. :param expires: should be a `datetime` object or unix timestamp. :param path: limits the cookie to a given path, per default it will span the whole domain. :param domain: Use this if you want to set a cross-domain cookie. For example, ``domain=".example.com"`` will set a cookie that is readable by the domain ``www.example.com``, ``foo.example.com`` etc. Otherwise, a cookie will only be readable by the domain that set it. :param secure: The cookie will only be available via HTTPS :param httponly: disallow JavaScript to access the cookie. This is an extension to the cookie standard and probably not supported by all browsers. :param charset: the encoding for string values. :param sync_expires: automatically set expires if max_age is defined but expires not. :param max_size: Warn if the final header value exceeds this size. The default, 4093, should be safely `supported by most browsers <cookie_>`_. Set to 0 to disable this check. :param samesite: Limits the scope of the cookie such that it will only be attached to requests if those requests are same-site. .. _`cookie`: http://browsercookielimits.squawky.net/ .. versionchanged:: 1.0.0 The string ``'None'`` is accepted for ``samesite``.

def dump_csp_header(header: ds.ContentSecurityPolicy) -> str: (source)

Dump a Content Security Policy header. These are structured into policies such as "default-src 'self'; script-src 'self'". .. versionadded:: 1.0.0 Support for Content Security Policy headers was added.

def dump_header(iterable: t.Union[t.Dict[str, t.Union[str, int]], t.Iterable[str]], allow_token: bool = True) -> str: (source)

Dump an HTTP header again. This is the reversal of :func:`parse_list_header`, :func:`parse_set_header` and :func:`parse_dict_header`. This also quotes strings that include an equals sign unless you pass it as dict of key, value pairs. >>> dump_header({'foo': 'bar baz'}) 'foo="bar baz"' >>> dump_header(('foo', 'bar baz')) 'foo, "bar baz"' :param iterable: the iterable or dict of values to quote. :param allow_token: if set to `False` tokens as values are disallowed. See :func:`quote_header_value` for more details.

def dump_options_header(header: t.Optional[str], options: t.Mapping[str, t.Optional[t.Union[str, int]]]) -> str: (source)

The reverse function to :func:`parse_options_header`. :param header: the header to dump :param options: a dict of options to append.

def generate_etag(data: bytes) -> str: (source)

Generate an etag for some data. .. versionchanged:: 2.0 Use SHA-1. MD5 may not be available in some environments.

def http_date(timestamp: t.Optional[t.Union[datetime, date, int, float, struct_time]] = None) -> str: (source)

Format a datetime object or timestamp into an :rfc:`2822` date string. This is a wrapper for :func:`email.utils.format_datetime`. It assumes naive datetime objects are in UTC instead of raising an exception. :param timestamp: The datetime or timestamp to format. Defaults to the current time. .. versionchanged:: 2.0 Use ``email.utils.format_datetime``. Accept ``date`` objects.

def is_byte_range_valid(start: t.Optional[int], stop: t.Optional[int], length: t.Optional[int]) -> bool: (source)

Checks if a given byte content range is valid for the given length. .. versionadded:: 0.7

def is_entity_header(header: str) -> bool: (source)

Check if a header is an entity header. .. versionadded:: 0.5 :param header: the header to test. :return: `True` if it's an entity header, `False` otherwise.

def is_hop_by_hop_header(header: str) -> bool: (source)

Check if a header is an HTTP/1.1 "Hop-by-Hop" header. .. versionadded:: 0.5 :param header: the header to test. :return: `True` if it's an HTTP/1.1 "Hop-by-Hop" header, `False` otherwise.

def is_resource_modified(environ: WSGIEnvironment, etag: t.Optional[str] = None, data: t.Optional[bytes] = None, last_modified: t.Optional[t.Union[datetime, str]] = None, ignore_if_range: bool = True) -> bool: (source)

Convenience method for conditional requests. :param environ: the WSGI environment of the request to be checked. :param etag: the etag for the response for comparison. :param data: or alternatively the data of the response to automatically generate an etag using :func:`generate_etag`. :param last_modified: an optional date of the last modification. :param ignore_if_range: If `False`, `If-Range` header will be taken into account. :return: `True` if the resource was modified, otherwise `False`. .. versionchanged:: 2.0 SHA-1 is used to generate an etag value for the data. MD5 may not be available in some environments. .. versionchanged:: 1.0.0 The check is run for methods other than ``GET`` and ``HEAD``.

@typing.overload
def parse_accept_header(value: t.Optional[str]) -> ds.Accept:
@typing.overload
def parse_accept_header(value: t.Optional[str], cls: t.Type[_TAnyAccept]) -> _TAnyAccept:
(source)

Parses an HTTP Accept-* header. This does not implement a complete valid algorithm but one that supports at least value and quality extraction. Returns a new :class:`Accept` object (basically a list of ``(value, quality)`` tuples sorted by the quality with some additional accessor methods). The second parameter can be a subclass of :class:`Accept` that is created with the parsed values and returned. :param value: the accept header string to be parsed. :param cls: the wrapper class for the return value (can be :class:`Accept` or a subclass thereof) :return: an instance of `cls`.

def parse_age(value: t.Optional[str] = None) -> t.Optional[timedelta]: (source)

Parses a base-10 integer count of seconds into a timedelta. If parsing fails, the return value is `None`. :param value: a string consisting of an integer represented in base-10 :return: a :class:`datetime.timedelta` object or `None`.

def parse_authorization_header(value: t.Optional[str]) -> t.Optional[ds.Authorization]: (source)

Parse an HTTP basic/digest authorization header transmitted by the web browser. The return value is either `None` if the header was invalid or not given, otherwise an :class:`~werkzeug.datastructures.Authorization` object. :param value: the authorization header to parse. :return: a :class:`~werkzeug.datastructures.Authorization` object or `None`.

@typing.overload
def parse_cache_control_header(value: t.Optional[str], on_update: _t_cc_update, cls: None = None) -> ds.RequestCacheControl:
@typing.overload
def parse_cache_control_header(value: t.Optional[str], on_update: _t_cc_update, cls: t.Type[_TAnyCC]) -> _TAnyCC:
(source)

Parse a cache control header. The RFC differs between response and request cache control, this method does not. It's your responsibility to not use the wrong control statements. .. versionadded:: 0.5 The `cls` was added. If not specified an immutable :class:`~werkzeug.datastructures.RequestCacheControl` is returned. :param value: a cache control header to be parsed. :param on_update: an optional callable that is called every time a value on the :class:`~werkzeug.datastructures.CacheControl` object is changed. :param cls: the class for the returned object. By default :class:`~werkzeug.datastructures.RequestCacheControl` is used. :return: a `cls` object.

def parse_content_range_header(value: t.Optional[str], on_update: t.Optional[t.Callable[[ds.ContentRange], None]] = None) -> t.Optional[ds.ContentRange]: (source)

Parses a range header into a :class:`~werkzeug.datastructures.ContentRange` object or `None` if parsing is not possible. .. versionadded:: 0.7 :param value: a content range header to be parsed. :param on_update: an optional callable that is called every time a value on the :class:`~werkzeug.datastructures.ContentRange` object is changed.

def parse_cookie(header: t.Union[WSGIEnvironment, str, bytes, None], charset: str = 'utf-8', errors: str = 'replace', cls: t.Optional[t.Type[ds.MultiDict]] = None) -> ds.MultiDict[str, str]: (source)

Parse a cookie from a string or WSGI environ. The same key can be provided multiple times, the values are stored in-order. The default :class:`MultiDict` will have the first value first, and all values can be retrieved with :meth:`MultiDict.getlist`. :param header: The cookie header as a string, or a WSGI environ dict with a ``HTTP_COOKIE`` key. :param charset: The charset for the cookie values. :param errors: The error behavior for the charset decoding. :param cls: A dict-like class to store the parsed cookies in. Defaults to :class:`MultiDict`. .. versionchanged:: 1.0.0 Returns a :class:`MultiDict` instead of a ``TypeConversionDict``. .. versionchanged:: 0.5 Returns a :class:`TypeConversionDict` instead of a regular dict. The ``cls`` parameter was added.

@typing.overload
def parse_csp_header(value: t.Optional[str], on_update: _t_csp_update, cls: None = None) -> ds.ContentSecurityPolicy:
@typing.overload
def parse_csp_header(value: t.Optional[str], on_update: _t_csp_update, cls: t.Type[_TAnyCSP]) -> _TAnyCSP:
(source)

Parse a Content Security Policy header. .. versionadded:: 1.0.0 Support for Content Security Policy headers was added. :param value: a csp header to be parsed. :param on_update: an optional callable that is called every time a value on the object is changed. :param cls: the class for the returned object. By default :class:`~werkzeug.datastructures.ContentSecurityPolicy` is used. :return: a `cls` object.

def parse_date(value: t.Optional[str]) -> t.Optional[datetime]: (source)

Parse an :rfc:`2822` date into a timezone-aware :class:`datetime.datetime` object, or ``None`` if parsing fails. This is a wrapper for :func:`email.utils.parsedate_to_datetime`. It returns ``None`` if parsing fails instead of raising an exception, and always returns a timezone-aware datetime object. If the string doesn't have timezone information, it is assumed to be UTC. :param value: A string with a supported date format. .. versionchanged:: 2.0 Return a timezone-aware datetime object. Use ``email.utils.parsedate_to_datetime``.

def parse_dict_header(value: str, cls: t.Type[dict] = dict) -> t.Dict[str, str]: (source)

Parse lists of key, value pairs as described by RFC 2068 Section 2 and convert them into a python dict (or any other mapping object created from the type with a dict like interface provided by the `cls` argument): >>> d = parse_dict_header('foo="is a fish", bar="as well"') >>> type(d) is dict True >>> sorted(d.items()) [('bar', 'as well'), ('foo', 'is a fish')] If there is no value for a key it will be `None`: >>> parse_dict_header('key_without_value') {'key_without_value': None} To create a header from the :class:`dict` again, use the :func:`dump_header` function. .. versionchanged:: 0.9 Added support for `cls` argument. :param value: a string with a dict header. :param cls: callable to use for storage of parsed results. :return: an instance of `cls`

def parse_etags(value: t.Optional[str]) -> ds.ETags: (source)

Parse an etag header. :param value: the tag header to parse :return: an :class:`~werkzeug.datastructures.ETags` object.

def parse_if_range_header(value: t.Optional[str]) -> ds.IfRange: (source)

Parses an if-range header which can be an etag or a date. Returns a :class:`~werkzeug.datastructures.IfRange` object. .. versionchanged:: 2.0 If the value represents a datetime, it is timezone-aware. .. versionadded:: 0.7

def parse_list_header(value: str) -> t.List[str]: (source)

Parse lists as described by RFC 2068 Section 2. In particular, parse comma-separated lists where the elements of the list may include quoted-strings. A quoted-string could contain a comma. A non-quoted string could have quotes in the middle. Quotes are removed automatically after parsing. It basically works like :func:`parse_set_header` just that items may appear multiple times and case sensitivity is preserved. The return value is a standard :class:`list`: >>> parse_list_header('token, "quoted value"') ['token', 'quoted value'] To create a header from the :class:`list` again, use the :func:`dump_header` function. :param value: a string with a list header. :return: :class:`list`

def parse_options_header(value: t.Optional[str]) -> t.Tuple[str, t.Dict[str, str]]: (source)

Parse a ``Content-Type``-like header into a tuple with the value and any options: >>> parse_options_header('text/html; charset=utf8') ('text/html', {'charset': 'utf8'}) This should is not for ``Cache-Control``-like headers, which use a different format. For those, use :func:`parse_dict_header`. :param value: The header value to parse. .. versionchanged:: 2.2 Option names are always converted to lowercase. .. versionchanged:: 2.1 The ``multiple`` parameter is deprecated and will be removed in Werkzeug 2.2. .. versionchanged:: 0.15 :rfc:`2231` parameter continuations are handled. .. versionadded:: 0.5

def parse_range_header(value: t.Optional[str], make_inclusive: bool = True) -> t.Optional[ds.Range]: (source)

Parses a range header into a :class:`~werkzeug.datastructures.Range` object. If the header is missing or malformed `None` is returned. `ranges` is a list of ``(start, stop)`` tuples where the ranges are non-inclusive. .. versionadded:: 0.7

def parse_set_header(value: t.Optional[str], on_update: t.Optional[t.Callable[[ds.HeaderSet], None]] = None) -> ds.HeaderSet: (source)

Parse a set-like header and return a :class:`~werkzeug.datastructures.HeaderSet` object: >>> hs = parse_set_header('token, "quoted value"') The return value is an object that treats the items case-insensitively and keeps the order of the items: >>> 'TOKEN' in hs True >>> hs.index('quoted value') 1 >>> hs HeaderSet(['token', 'quoted value']) To create a header from the :class:`HeaderSet` again, use the :func:`dump_header` function. :param value: a set header to be parsed. :param on_update: an optional callable that is called every time a value on the :class:`~werkzeug.datastructures.HeaderSet` object is changed. :return: a :class:`~werkzeug.datastructures.HeaderSet`

def parse_www_authenticate_header(value: t.Optional[str], on_update: t.Optional[t.Callable[[ds.WWWAuthenticate], None]] = None) -> ds.WWWAuthenticate: (source)

Parse an HTTP WWW-Authenticate header into a :class:`~werkzeug.datastructures.WWWAuthenticate` object. :param value: a WWW-Authenticate header to parse. :param on_update: an optional callable that is called every time a value on the :class:`~werkzeug.datastructures.WWWAuthenticate` object is changed. :return: a :class:`~werkzeug.datastructures.WWWAuthenticate` object.

def quote_etag(etag: str, weak: bool = False) -> str: (source)

Quote an etag. :param etag: the etag to quote. :param weak: set to `True` to tag it "weak".

def quote_header_value(value: t.Union[str, int], extra_chars: str = '', allow_token: bool = True) -> str: (source)

Quote a header value if necessary. .. versionadded:: 0.5 :param value: the value to quote. :param extra_chars: a list of extra characters to skip quoting. :param allow_token: if this is enabled token values are returned unchanged.

def remove_entity_headers(headers: t.Union[ds.Headers, t.List[t.Tuple[str, str]]], allowed: t.Iterable[str] = ('expires', 'content-location')): (source)

Remove all entity headers from a list or :class:`Headers` object. This operation works in-place. `Expires` and `Content-Location` headers are by default not removed. The reason for this is :rfc:`2616` section 10.3.5 which specifies some entity headers that should be sent. .. versionchanged:: 0.5 added `allowed` parameter. :param headers: a list or :class:`Headers` object. :param allowed: a list of headers that should still be allowed even though they are entity headers.

def remove_hop_by_hop_headers(headers: t.Union[ds.Headers, t.List[t.Tuple[str, str]]]): (source)

Remove all HTTP/1.1 "Hop-by-Hop" headers from a list or :class:`Headers` object. This operation works in-place. .. versionadded:: 0.5 :param headers: a list or :class:`Headers` object.

def unquote_etag(etag: t.Optional[str]) -> t.Union[t.Tuple[str, bool], t.Tuple[None, None]]: (source)

Unquote a single etag: >>> unquote_etag('W/"bar"') ('bar', True) >>> unquote_etag('"bar"') ('bar', False) :param etag: the etag identifier to unquote. :return: a ``(etag, weak)`` tuple.

def unquote_header_value(value: str, is_filename: bool = False) -> str: (source)

Unquotes a header value. (Reversal of :func:`quote_header_value`). This does not use the real unquoting but what browsers are actually using for quoting. .. versionadded:: 0.5 :param value: the header value to unquote. :param is_filename: The value represents a filename or path.

HTTP_STATUS_CODES: dict[int, str] = (source)

Undocumented

Value
{100: 'Continue',
 101: 'Switching Protocols',
 102: 'Processing',
 103: 'Early Hints',
 200: 'OK',
 201: 'Created',
 202: 'Accepted',
...
def _is_extended_parameter(key: str) -> bool: (source)

Per RFC 5987/8187, "extended" values may *not* be quoted. This is in keeping with browser implementations. So we test using this function to see if the key indicates this parameter follows the `ext-parameter` syntax (using a trailing '*').

_TAnyAccept = (source)

Undocumented

Value
t.TypeVar('_TAnyAccept',
          bound='ds.Accept')

Undocumented

Value
t.TypeVar('_TAnyCC',
          bound='ds._CacheControl')
_TAnyCSP = (source)

Undocumented

Value
t.TypeVar('_TAnyCSP',
          bound='ds.ContentSecurityPolicy')
_t_cc_update = (source)

Undocumented

Value
t.Optional[t.Callable[[_TAnyCC], None]]
_t_csp_update = (source)

Undocumented

Value
t.Optional[t.Callable[[_TAnyCSP], None]]
_accept_re = (source)

Undocumented

_entity_headers = (source)

Undocumented

_etag_re = (source)

Undocumented

_hop_by_hop_headers = (source)

Undocumented

_option_header_piece_re = (source)

Undocumented

_option_header_start_mime_type = (source)

Undocumented

_token_chars = (source)

Undocumented