module documentation

Undocumented

Class ClosingIterator The WSGI specification requires that all middlewares and gateways respect the `close` callback of the iterable returned by the application. Because it is useful to add another close action to a returned iterable and adding a custom iterable is a boring task this class can be used for that::...
Class FileWrapper This class can be used to convert a :class:`file`-like object into an iterable. It yields `buffer_size` blocks until the file is fully read.
Class LimitedStream Wraps a stream so that it doesn't read more than n bytes. If the stream is exhausted and the caller tries to get more bytes from it :func:`on_exhausted` is called which by default returns an empty string...
Function extract_path_info Extracts the path info from the given URL (or WSGI environment) and path. The path info returned is a string. The URLs might also be IRIs.
Function get_content_length Returns the content length from the WSGI environment as integer. If it's not available or chunked transfer encoding is used, ``None`` is returned.
Function get_current_url Recreate the URL for a request from the parts in a WSGI environment.
Function get_host Return the host for the given WSGI environment.
Function get_input_stream Returns the input stream from the WSGI environment and wraps it in the most sensible way possible. The stream returned is not the raw WSGI stream in most cases but one that is safe to read from without taking into account the content length.
Function get_path_info Return the ``PATH_INFO`` from the WSGI environment and decode it unless ``charset`` is ``None``.
Function get_query_string Returns the ``QUERY_STRING`` from the WSGI environment. This also takes care of the WSGI decoding dance. The string returned will be restricted to ASCII characters.
Function get_script_name Return the ``SCRIPT_NAME`` from the WSGI environment and decode it unless `charset` is set to ``None``.
Function make_chunk_iter Works like :func:`make_line_iter` but accepts a separator which divides chunks. If you want newline based processing you should use :func:`make_line_iter` instead as it supports arbitrary newline markers.
Function make_line_iter Safely iterates line-based over an input stream. If the input stream is not a :class:`LimitedStream` the `limit` parameter is mandatory.
Function peek_path_info Returns the next segment on the `PATH_INFO` or `None` if there is none. Works like :func:`pop_path_info` without modifying the environment:
Function pop_path_info Removes and returns the next segment of `PATH_INFO`, pushing it onto `SCRIPT_NAME`. Returns `None` if there is nothing left on `PATH_INFO`.
Function responder Marks a function as responder. Decorate a function with it and it will automatically call the return value as WSGI application.
Function wrap_file Wraps a file. This uses the WSGI server's file wrapper if available or otherwise the generic :class:`FileWrapper`.
Class _RangeWrapper This class can be used to convert an iterable object into an iterable that will only yield a piece of the underlying content. It yields blocks until the underlying stream range is fully read. The yielded blocks will have a size that can't exceed the original iterator defined block size, but that can be smaller.
Function _get_server Undocumented
Function _make_chunk_iter Helper for the line and chunk iter functions.
def extract_path_info(environ_or_baseurl: t.Union[str, WSGIEnvironment], path_or_url: t.Union[str, _URLTuple], charset: str = 'utf-8', errors: str = 'werkzeug.url_quote', collapse_http_schemes: bool = True) -> t.Optional[str]: (source)

Extracts the path info from the given URL (or WSGI environment) and path. The path info returned is a string. The URLs might also be IRIs. If the path info could not be determined, `None` is returned. Some examples: >>> extract_path_info('http://example.com/app', '/app/hello') '/hello' >>> extract_path_info('http://example.com/app', ... 'https://example.com/app/hello') '/hello' >>> extract_path_info('http://example.com/app', ... 'https://example.com/app/hello', ... collapse_http_schemes=False) is None True Instead of providing a base URL you can also pass a WSGI environment. :param environ_or_baseurl: a WSGI environment dict, a base URL or base IRI. This is the root of the application. :param path_or_url: an absolute path from the server root, a relative path (in which case it's the path info) or a full URL. :param charset: the charset for byte data in URLs :param errors: the error handling on decode :param collapse_http_schemes: if set to `False` the algorithm does not assume that http and https on the same server point to the same resource. .. deprecated:: 2.2 Will be removed in Werkzeug 2.3. .. versionchanged:: 0.15 The ``errors`` parameter defaults to leaving invalid bytes quoted instead of replacing them. .. versionadded:: 0.6

def get_content_length(environ: WSGIEnvironment) -> t.Optional[int]: (source)

Returns the content length from the WSGI environment as integer. If it's not available or chunked transfer encoding is used, ``None`` is returned. .. versionadded:: 0.9 :param environ: the WSGI environ to fetch the content length from.

def get_current_url(environ: WSGIEnvironment, root_only: bool = False, strip_querystring: bool = False, host_only: bool = False, trusted_hosts: t.Optional[t.Iterable[str]] = None) -> str: (source)

Recreate the URL for a request from the parts in a WSGI environment. The URL is an IRI, not a URI, so it may contain Unicode characters. Use :func:`~werkzeug.urls.iri_to_uri` to convert it to ASCII. :param environ: The WSGI environment to get the URL parts from. :param root_only: Only build the root path, don't include the remaining path or query string. :param strip_querystring: Don't include the query string. :param host_only: Only build the scheme and host. :param trusted_hosts: A list of trusted host names to validate the host against.

def get_host(environ: WSGIEnvironment, trusted_hosts: t.Optional[t.Iterable[str]] = None) -> str: (source)

Return the host for the given WSGI environment. The ``Host`` header is preferred, then ``SERVER_NAME`` if it's not set. The returned host will only contain the port if it is different than the standard port for the protocol. Optionally, verify that the host is trusted using :func:`host_is_trusted` and raise a :exc:`~werkzeug.exceptions.SecurityError` if it is not. :param environ: A WSGI environment dict. :param trusted_hosts: A list of trusted host names. :return: Host, with port if necessary. :raise ~werkzeug.exceptions.SecurityError: If the host is not trusted.

def get_input_stream(environ: WSGIEnvironment, safe_fallback: bool = True) -> t.IO[bytes]: (source)

Returns the input stream from the WSGI environment and wraps it in the most sensible way possible. The stream returned is not the raw WSGI stream in most cases but one that is safe to read from without taking into account the content length. If content length is not set, the stream will be empty for safety reasons. If the WSGI server supports chunked or infinite streams, it should set the ``wsgi.input_terminated`` value in the WSGI environ to indicate that. .. versionadded:: 0.9 :param environ: the WSGI environ to fetch the stream from. :param safe_fallback: use an empty stream as a safe fallback when the content length is not set. Disabling this allows infinite streams, which can be a denial-of-service risk.

def get_path_info(environ: WSGIEnvironment, charset: str = 'utf-8', errors: str = 'replace') -> str: (source)

Return the ``PATH_INFO`` from the WSGI environment and decode it unless ``charset`` is ``None``. :param environ: WSGI environment to get the path from. :param charset: The charset for the path info, or ``None`` if no decoding should be performed. :param errors: The decoding error handling. .. versionadded:: 0.9

def get_query_string(environ: WSGIEnvironment) -> str: (source)

Returns the ``QUERY_STRING`` from the WSGI environment. This also takes care of the WSGI decoding dance. The string returned will be restricted to ASCII characters. :param environ: WSGI environment to get the query string from. .. deprecated:: 2.2 Will be removed in Werkzeug 2.3. .. versionadded:: 0.9

def get_script_name(environ: WSGIEnvironment, charset: str = 'utf-8', errors: str = 'replace') -> str: (source)

Return the ``SCRIPT_NAME`` from the WSGI environment and decode it unless `charset` is set to ``None``. :param environ: WSGI environment to get the path from. :param charset: The charset for the path, or ``None`` if no decoding should be performed. :param errors: The decoding error handling. .. deprecated:: 2.2 Will be removed in Werkzeug 2.3. .. versionadded:: 0.9

def make_chunk_iter(stream: t.Union[t.Iterable[bytes], t.IO[bytes]], separator: bytes, limit: t.Optional[int] = None, buffer_size: int = 10*1024, cap_at_buffer: bool = False) -> t.Iterator[bytes]: (source)

Works like :func:`make_line_iter` but accepts a separator which divides chunks. If you want newline based processing you should use :func:`make_line_iter` instead as it supports arbitrary newline markers. .. versionadded:: 0.8 .. versionadded:: 0.9 added support for iterators as input stream. .. versionadded:: 0.11.10 added support for the `cap_at_buffer` parameter. :param stream: the stream or iterate to iterate over. :param separator: the separator that divides chunks. :param limit: the limit in bytes for the stream. (Usually content length. Not necessary if the `stream` is otherwise already limited). :param buffer_size: The optional buffer size. :param cap_at_buffer: if this is set chunks are split if they are longer than the buffer size. Internally this is implemented that the buffer size might be exhausted by a factor of two however.

def make_line_iter(stream: t.Union[t.Iterable[bytes], t.IO[bytes]], limit: t.Optional[int] = None, buffer_size: int = 10*1024, cap_at_buffer: bool = False) -> t.Iterator[bytes]: (source)

Safely iterates line-based over an input stream. If the input stream is not a :class:`LimitedStream` the `limit` parameter is mandatory. This uses the stream's :meth:`~file.read` method internally as opposite to the :meth:`~file.readline` method that is unsafe and can only be used in violation of the WSGI specification. The same problem applies to the `__iter__` function of the input stream which calls :meth:`~file.readline` without arguments. If you need line-by-line processing it's strongly recommended to iterate over the input stream using this helper function. .. versionchanged:: 0.8 This function now ensures that the limit was reached. .. versionadded:: 0.9 added support for iterators as input stream. .. versionadded:: 0.11.10 added support for the `cap_at_buffer` parameter. :param stream: the stream or iterate to iterate over. :param limit: the limit in bytes for the stream. (Usually content length. Not necessary if the `stream` is a :class:`LimitedStream`. :param buffer_size: The optional buffer size. :param cap_at_buffer: if this is set chunks are split if they are longer than the buffer size. Internally this is implemented that the buffer size might be exhausted by a factor of two however.

def peek_path_info(environ: WSGIEnvironment, charset: str = 'utf-8', errors: str = 'replace') -> t.Optional[str]: (source)

Returns the next segment on the `PATH_INFO` or `None` if there is none. Works like :func:`pop_path_info` without modifying the environment: >>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'} >>> peek_path_info(env) 'a' >>> peek_path_info(env) 'a' If the `charset` is set to `None` bytes are returned. .. deprecated:: 2.2 Will be removed in Werkzeug 2.3. .. versionadded:: 0.5 .. versionchanged:: 0.9 The path is now decoded and a charset and encoding parameter can be provided. :param environ: the WSGI environment that is checked.

def pop_path_info(environ: WSGIEnvironment, charset: str = 'utf-8', errors: str = 'replace') -> t.Optional[str]: (source)

Removes and returns the next segment of `PATH_INFO`, pushing it onto `SCRIPT_NAME`. Returns `None` if there is nothing left on `PATH_INFO`. If the `charset` is set to `None` bytes are returned. If there are empty segments (``'/foo//bar``) these are ignored but properly pushed to the `SCRIPT_NAME`: >>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'} >>> pop_path_info(env) 'a' >>> env['SCRIPT_NAME'] '/foo/a' >>> pop_path_info(env) 'b' >>> env['SCRIPT_NAME'] '/foo/a/b' .. deprecated:: 2.2 Will be removed in Werkzeug 2.3. .. versionadded:: 0.5 .. versionchanged:: 0.9 The path is now decoded and a charset and encoding parameter can be provided. :param environ: the WSGI environment that is modified. :param charset: The ``encoding`` parameter passed to :func:`bytes.decode`. :param errors: The ``errors`` paramater passed to :func:`bytes.decode`.

def responder(f: t.Callable[..., WSGIApplication]) -> WSGIApplication: (source)

Marks a function as responder. Decorate a function with it and it will automatically call the return value as WSGI application. Example:: @responder def application(environ, start_response): return Response('Hello World!')

def wrap_file(environ: WSGIEnvironment, file: t.IO[bytes], buffer_size: int = 8192) -> t.Iterable[bytes]: (source)

Wraps a file. This uses the WSGI server's file wrapper if available or otherwise the generic :class:`FileWrapper`. .. versionadded:: 0.5 If the file wrapper from the WSGI server is used it's important to not iterate over it from inside the application but to pass it through unchanged. If you want to pass out a file wrapper inside a response object you have to set :attr:`Response.direct_passthrough` to `True`. More information about file wrappers are available in :pep:`333`. :param file: a :class:`file`-like object with a :meth:`~file.read` method. :param buffer_size: number of bytes for one iteration.

def _get_server(environ: WSGIEnvironment) -> t.Optional[t.Tuple[str, t.Optional[int]]]: (source)

Undocumented

def _make_chunk_iter(stream: t.Union[t.Iterable[bytes], t.IO[bytes]], limit: t.Optional[int], buffer_size: int) -> t.Iterator[bytes]: (source)

Helper for the line and chunk iter functions.