OpenQuizz
Une application de gestion des contenus pédagogiques
werkzeug.wsgi Namespace Reference

Data Structures

class  _RangeWrapper
 
class  ClosingIterator
 
class  FileWrapper
 
class  LimitedStream
 

Functions

def responder (f)
 
def get_current_url (environ, root_only=False, strip_querystring=False, host_only=False, trusted_hosts=None)
 
def host_is_trusted (hostname, trusted_list)
 
def get_host (environ, trusted_hosts=None)
 
def get_content_length (environ)
 
def get_input_stream (environ, safe_fallback=True)
 
def get_query_string (environ)
 
def get_path_info (environ, charset="utf-8", errors="replace")
 
def get_script_name (environ, charset="utf-8", errors="replace")
 
def pop_path_info (environ, charset="utf-8", errors="replace")
 
def peek_path_info (environ, charset="utf-8", errors="replace")
 
def extract_path_info (environ_or_baseurl, path_or_url, charset="utf-8", errors="werkzeug.url_quote", collapse_http_schemes=True)
 
def wrap_file (environ, file, buffer_size=8192)
 
def make_line_iter (stream, limit=None, buffer_size=10 *1024, cap_at_buffer=False)
 
def make_chunk_iter (stream, separator, limit=None, buffer_size=10 *1024, cap_at_buffer=False)
 

Function Documentation

◆ extract_path_info()

def werkzeug.wsgi.extract_path_info (   environ_or_baseurl,
  path_or_url,
  charset = "utf-8",
  errors = "werkzeug.url_quote",
  collapse_http_schemes = True 
)
Extracts the path info from the given URL (or WSGI environment) and
path.  The path info returned is a unicode string, not a bytestring
suitable for a WSGI environment.  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')
u'/hello'
>>> extract_path_info('http://example.com/app',
...                   'https://example.com/app/hello')
u'/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.  Also accepts IRIs and unicode
                    parameters.
: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.

.. versionchanged:: 0.15
    The ``errors`` parameter defaults to leaving invalid bytes
    quoted instead of replacing them.

.. versionadded:: 0.6

◆ get_content_length()

def werkzeug.wsgi.get_content_length (   environ)
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.

◆ get_current_url()

def werkzeug.wsgi.get_current_url (   environ,
  root_only = False,
  strip_querystring = False,
  host_only = False,
  trusted_hosts = None 
)
A handy helper function that recreates the full URL as IRI for the
current request or parts of it.  Here's an example:

>>> from werkzeug.test import create_environ
>>> env = create_environ("/?param=foo", "http://localhost/script")
>>> get_current_url(env)
'http://localhost/script/?param=foo'
>>> get_current_url(env, root_only=True)
'http://localhost/script/'
>>> get_current_url(env, host_only=True)
'http://localhost/'
>>> get_current_url(env, strip_querystring=True)
'http://localhost/script/'

This optionally it verifies that the host is in a list of trusted hosts.
If the host is not in there it will raise a
:exc:`~werkzeug.exceptions.SecurityError`.

Note that the string returned might contain unicode characters as the
representation is an IRI not an URI.  If you need an ASCII only
representation you can use the :func:`~werkzeug.urls.iri_to_uri`
function:

>>> from werkzeug.urls import iri_to_uri
>>> iri_to_uri(get_current_url(env))
'http://localhost/script/?param=foo'

:param environ: the WSGI environment to get the current URL from.
:param root_only: set `True` if you only want the root URL.
:param strip_querystring: set to `True` if you don't want the querystring.
:param host_only: set to `True` if the host URL should be returned.
:param trusted_hosts: a list of trusted hosts, see :func:`host_is_trusted`
                      for more information.

◆ get_host()

def werkzeug.wsgi.get_host (   environ,
  trusted_hosts = None 
)
Return the host for the given WSGI environment. This first checks
the ``Host`` header. If it's not present, then ``SERVER_NAME`` and
``SERVER_PORT`` are used. The 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: The WSGI environment to get the host from.
:param trusted_hosts: A list of trusted hosts.
:return: Host, with port if necessary.
:raise ~werkzeug.exceptions.SecurityError: If the host is not
    trusted.

◆ get_input_stream()

def werkzeug.wsgi.get_input_stream (   environ,
  safe_fallback = True 
)
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.

◆ get_path_info()

def werkzeug.wsgi.get_path_info (   environ,
  charset = "utf-8",
  errors = "replace" 
)
Returns the `PATH_INFO` from the WSGI environment and properly
decodes it.  This also takes care about the WSGI decoding dance
on Python 3 environments.  if the `charset` is set to `None` a
bytestring is returned.

.. versionadded:: 0.9

:param environ: the WSGI environment object 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.

◆ get_query_string()

def werkzeug.wsgi.get_query_string (   environ)
Returns the `QUERY_STRING` from the WSGI environment.  This also takes
care about the WSGI decoding dance on Python 3 environments as a
native string.  The string returned will be restricted to ASCII
characters.

.. versionadded:: 0.9

:param environ: the WSGI environment object to get the query string from.

◆ get_script_name()

def werkzeug.wsgi.get_script_name (   environ,
  charset = "utf-8",
  errors = "replace" 
)
Returns the `SCRIPT_NAME` from the WSGI environment and properly
decodes it.  This also takes care about the WSGI decoding dance
on Python 3 environments.  if the `charset` is set to `None` a
bytestring is returned.

.. versionadded:: 0.9

:param environ: the WSGI environment object 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.

◆ host_is_trusted()

def werkzeug.wsgi.host_is_trusted (   hostname,
  trusted_list 
)
Checks if a host is trusted against a list.  This also takes care
of port normalization.

.. versionadded:: 0.9

:param hostname: the hostname to check
:param trusted_list: a list of hostnames to check against.  If a
                     hostname starts with a dot it will match against
                     all subdomains as well.

◆ make_chunk_iter()

def werkzeug.wsgi.make_chunk_iter (   stream,
  separator,
  limit = None,
  buffer_size = 10 * 1024,
  cap_at_buffer = False 
)
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.

◆ make_line_iter()

def werkzeug.wsgi.make_line_iter (   stream,
  limit = None,
  buffer_size = 10 * 1024,
  cap_at_buffer = False 
)
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.

◆ peek_path_info()

def werkzeug.wsgi.peek_path_info (   environ,
  charset = "utf-8",
  errors = "replace" 
)
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` a bytestring is returned.

.. 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.

◆ pop_path_info()

def werkzeug.wsgi.pop_path_info (   environ,
  charset = "utf-8",
  errors = "replace" 
)
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` a bytestring is 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'

.. 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.

◆ responder()

def werkzeug.wsgi.responder (   f)
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!')

◆ wrap_file()

def werkzeug.wsgi.wrap_file (   environ,
  file,
  buffer_size = 8192 
)
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:`~BaseResponse.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.