
    %hQ                       S SK Jr  S SKrS SKrS SKJr  S SKJr  SSKJ	r	  SSKJ
r
  SSKJr  SS	KJr  \R                   (       a  S S
KJr  S SKJr  SS jr    S           SS jjr    SS jr S      S!S jjrS"S jr  S#       S$S jjrS%S jr " S S5      r S&       S'S jjr " S S5      r " S S5      r " S S\R>                  5      r g)(    )annotationsN)partialupdate_wrapper   )ClientDisconnected)RequestEntityTooLarge)utils)host_is_trusted)WSGIApplication)WSGIEnvironmentc                $   ^  [        U 4S jT 5      $ )zMarks 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!')
c                    > T" U 6 " U SS  6 $ )N )afs    E/var/www/auris/envauris/lib/python3.13/site-packages/werkzeug/wsgi.py<lambda>responder.<locals>.<lambda>   s    QUAbcF^    r   )r   s   `r   	responderr      s     3Q77r   c                   U S   [        X5      S.nU(       d\  U R                  SS5      US'   U(       d@  U R                  SS5      US'   U(       d$  U R                  SS5      R                  S	5      US
'   [        R                  " S0 UD6$ )a,  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.
wsgi.url_scheme)schemehostSCRIPT_NAME 	root_path	PATH_INFOpathQUERY_STRINGlatin1query_stringr   )get_hostgetencode_sansio_utilsget_current_url)environ	root_onlystrip_querystring	host_onlytrusted_hostspartss         r   r)   r)      s    , +,0E
 $[[;k#KKR8E&M$(/NB(G(N(Nx(Xn%((1511r   c                    U R                  S5      nUc  g  [        U R                  SS 5      5      nX4$ ! [        [        4 a    S n X4$ f = f)NSERVER_NAMESERVER_PORT)r&   int	TypeError
ValueError)r*   nameports      r   _get_serverr8   E   sa     ;;}%D|w{{=$?@
 :	 z" :	s   5 AAc                j    [         R                  " U S   U R                  S5      [        U 5      U5      $ )aB  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.
r   	HTTP_HOST)r(   r%   r&   r8   )r*   r.   s     r   r%   r%   V   s7    ( !!!"K G	 r   c                h    [         R                  " U R                  S5      U R                  S5      S9$ )aP  Return the ``Content-Length`` header value as an int. If the header is not given
or the ``Transfer-Encoding`` header is ``chunked``, ``None`` is returned to indicate
a streaming request. If the value is not an integer, or negative, 0 is returned.

:param environ: The WSGI environ to get the content length from.

.. versionadded:: 0.9
CONTENT_LENGTHHTTP_TRANSFER_ENCODING)http_content_lengthhttp_transfer_encoding)r(   get_content_lengthr&   )r*   s    r   r@   r@   r   s1     ++#KK(89&{{+CD r   c           	        [         R                  " [         R                  [           U S   5      n[	        U 5      nUb  Ub  XB:  a
  [        5       eSU ;   a9  Ub4  [         R                  " [         R                  [           [        X2SS95      $ U$ Uc  U(       a  [        R                  " 5       $ U$ [         R                  " [         R                  [           [        X45      5      $ )aT  Return the WSGI input stream, wrapped so that it may be read safely without going
past the ``Content-Length`` header value or ``max_content_length``.

If ``Content-Length`` exceeds ``max_content_length``, a
:exc:`RequestEntityTooLarge`` ``413 Content Too Large`` error is raised.

If the WSGI server sets ``environ["wsgi.input_terminated"]``, it indicates that the
server handles terminating the stream, so it is safe to read directly. For example,
a server that knows how to handle chunked requests safely would set this.

If ``max_content_length`` is set, it can be enforced on streams if
``wsgi.input_terminated`` is set. Otherwise, an empty stream is returned unless the
user explicitly disables this safe fallback.

If the limit is reached before the underlying stream is exhausted (such as a file
that is too large, or an infinite stream), the remaining contents of the stream
cannot be read safely. Depending on how the server handles this, clients may show a
"connection reset" failure instead of seeing the 413 response.

:param environ: The WSGI environ containing the stream.
:param safe_fallback: Return an empty stream when ``Content-Length`` is not set.
    Disabling this allows infinite streams, which can be a denial-of-service risk.
:param max_content_length: The maximum length that content-length or streaming
    requests may not exceed.

.. versionchanged:: 2.3.2
    ``max_content_length`` is only applied to streaming requests if the server sets
    ``wsgi.input_terminated``.

.. versionchanged:: 2.3
    Check ``max_content_length`` and raise an error if it is exceeded.

.. versionadded:: 0.9
z
wsgi.inputzwsgi.input_terminatedT)is_max)	tcastIObytesr@   r	   LimitedStreamioBytesIO)r*   safe_fallbackmax_content_lengthstreamcontent_lengths        r   get_input_streamrN      s    N VVADDK!67F'0N!&8&D.')) ')) 66U]6dS  
 ,rzz|8&866!$$u+}VDEEr   c                b    U R                  SS5      R                  S5      nUR                  SS9$ )zReturn ``PATH_INFO`` from  the WSGI environment.

:param environ: WSGI environment to get the path from.

.. versionchanged:: 3.0
    The ``charset`` and ``errors`` parameters were removed.

.. versionadded:: 0.9
r    r   r#   replace)errors)r&   r'   decode)r*   r!   s     r   get_path_inforS      s0     ++k2.55h?D;;i;((r   c                  P    \ rS rSrSr S	     S
S jjrSS jrSS jrSS jrSr	g)ClosingIterator   a  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::

    return ClosingIterator(app(environ, start_response), [cleanup_session,
                                                          cleanup_locals])

If there is just one close function it can be passed instead of the list.

A closing iterator is not needed if the application uses response objects
and finishes the processing if the response is started::

    try:
        return response(environ, start_response)
    finally:
        cleanup_session()
        cleanup_locals()
Nc                >   [        U5      n[        R                  " [        R                  / [        4   [        [        U5      5      U l        Uc  / nO[        U5      (       a  U/nO[        U5      n[        USS 5      nU(       a  UR                  SU5        X l        g )Ncloser   )iterrC   rD   CallablerF   r   next_nextcallablelistgetattrinsert
_callbacks)selfiterable	callbacksiteratoriterable_closes        r   __init__ClosingIterator.__init__   s     >VVAJJr5y1743JK
Ii  "IYI 7D9Q/#r   c                    U $ Nr   rb   s    r   __iter__ClosingIterator.__iter__       r   c                "    U R                  5       $ rj   )r\   rk   s    r   __next__ClosingIterator.__next__   s    zz|r   c                8    U R                    H
  nU" 5         M     g rj   )ra   )rb   callbacks     r   rX   ClosingIterator.close  s    HJ (r   )ra   r\   rj   )rc   t.Iterable[bytes]rd   z@None | (t.Callable[[], None] | t.Iterable[t.Callable[[], None]])returnNone)rv   rU   rv   rF   rv   rw   )
__name__
__module____qualname____firstlineno____doc__rg   rl   rp   rX   __static_attributes__r   r   r   rU   rU      s?    2 GK	$#$D$
 
$&r   rU   c                :    U R                  S[        5      " X5      $ )ag  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.
zwsgi.file_wrapper)r&   FileWrapper)r*   filebuffer_sizes      r   	wrap_filer     s     $ ;;*K8 r   c                  b    \ rS rSrSrSSS jjrSS jrSS jrSS jrSS jr	SS jr
SS	 jrS
rg)r   i  a0  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.

You should not use this class directly but rather use the
:func:`wrap_file` function that uses the WSGI server's file wrapper
support if it's available.

.. versionadded:: 0.5

If you're using this object together with a :class:`Response` you have
to use the `direct_passthrough` mode.

:param file: a :class:`file`-like object with a :meth:`~file.read` method.
:param buffer_size: number of bytes for one iteration.
c                    Xl         X l        g rj   )r   r   )rb   r   r   s      r   rg   FileWrapper.__init__0  s    	&r   c                p    [        U R                  S5      (       a  U R                  R                  5         g g NrX   )hasattrr   rX   rk   s    r   rX   FileWrapper.close4  s&    499g&&IIOO 'r   c                    [        U R                  S5      (       a  U R                  R                  5       $ [        U R                  S5      (       a  gg)NseekableseekTF)r   r   r   rk   s    r   r   FileWrapper.seekable8  s=    499j))99%%''499f%%r   c                n    [        U R                  S5      (       a  U R                  R                  " U6   g g )Nr   )r   r   r   )rb   argss     r   r   FileWrapper.seek?  s(    499f%%IINND! &r   c                n    [        U R                  S5      (       a  U R                  R                  5       $ g )Ntell)r   r   r   rk   s    r   r   FileWrapper.tellC  s'    499f%%99>>##r   c                    U $ rj   r   rk   s    r   rl   FileWrapper.__iter__H  rn   r   c                r    U R                   R                  U R                  5      nU(       a  U$ [        5       erj   )r   readr   StopIteration)rb   datas     r   rp   FileWrapper.__next__K  s*    yy~~d../Kor   )r   r   Ni    )r   t.IO[bytes]r   r3   rv   rw   ry   rv   bool)r   zt.Anyrv   rw   )rv   
int | None)rv   r   rx   )rz   r{   r|   r}   r~   rg   rX   r   r   r   rl   rp   r   r   r   r   r   r     s*    "'"
r   r   c                  p    \ rS rSrSr  S     SS jjrSS jrSS jrSS jrSS jr	SS	 jr
SS
 jrSrg)_RangeWrapperiR  aK  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.

If you're using this object together with a :class:`Response` you have
to use the `direct_passthrough` mode.

:param iterable: an iterable object with a :meth:`__next__` method.
:param start_byte: byte from which read will start.
:param byte_range: how many bytes to read.
Nc                    [        U5      U l        X0l        X l        S U l        Ub	  X#-   U l        SU l        [        US5      =(       a    UR                  5       U l        SU l        g )Nr   r   F)	rY   rc   
byte_range
start_byteend_byteread_lengthr   r   end_reached)rb   rc   r   r   s       r   rg   _RangeWrapper.__init__c  s\     X$$!&3DM*5M(:K:K:M r   c                    U $ rj   r   rk   s    r   rl   _RangeWrapper.__iter__u  rn   r   c                     [        U R                  5      nU =R                  [        U5      -  sl        U$ ! [         a	    SU l        e f = fNT)r[   rc   r   lenr   r   rb   chunks     r   _next_chunk_RangeWrapper._next_chunkx  sI    	'EE
*L 	#D	s	   47 A
c                   S nU R                   (       aS  U R                  R                  U R                  5        U R                  R	                  5       U l        U R
                  nX4$ U R
                  U R                  ::  a,  U R                  5       nU R
                  U R                  ::  a  M,  Ub  XR                  U R
                  -
  S  nU R                  nX4$ rj   )r   rc   r   r   r   r   r   rb   r   contextual_read_lengths      r   _first_iteration_RangeWrapper._first_iteration  s    ==MMt/#}}113D%)%5%5" ,, ""doo5((* ""doo5 oo0@0@@BC%)__",,r   c                B   U R                   (       a
  [        5       eS nU R                  nU R                  S:X  a  U R                  5       u  pUc  U R	                  5       nU R
                  b3  U R                  U R
                  :  a  SU l         US U R
                  U-
   $ U$ )Nr   T)r   r   r   r   r   r   r   s      r   r\   _RangeWrapper._next  s    /!!%!1!1q ,0,A,A,C)E=$$&E==$)9)9T]])J#DA4==+AABBr   c                V    U R                  5       nU(       a  U$ SU l        [        5       er   )r\   r   r   r   s     r   rp   _RangeWrapper.__next__  s%    

Lor   c                p    [        U R                  S5      (       a  U R                  R                  5         g g r   )r   rc   rX   rk   s    r   rX   _RangeWrapper.close  s(    4=='**MM! +r   )r   r   r   rc   r   r   r   )r   N)rc   zt.Iterable[bytes] | t.IO[bytes]r   r3   r   r   )rv   r   rx   )rv   ztuple[bytes | None, int]ry   )rz   r{   r|   r}   r~   rg   rl   r   r   r\   rp   rX   r   r   r   r   r   r   R  sN    " !%	!1! ! 	!$-"r   r   c                      \ rS rSrSrSSS jjr\SS j5       rSS jrSSS jjr	SS jr
SS	 jrSS
 jrSS jrSS jrSrg)rG   i  a  Wrap a stream so that it doesn't read more than a given limit. This is used to
limit ``wsgi.input`` to the ``Content-Length`` header value or
:attr:`.Request.max_content_length`.

When attempting to read after the limit has been reached, :meth:`on_exhausted` is
called. When the limit is a maximum, this raises :exc:`.RequestEntityTooLarge`.

If reading from the stream returns zero bytes or raises an error,
:meth:`on_disconnect` is called, which raises :exc:`.ClientDisconnected`. When the
limit is a maximum and zero bytes were read, no error is raised, since it may be the
end of the stream.

If the limit is reached before the underlying stream is exhausted (such as a file
that is too large, or an infinite stream), the remaining contents of the stream
cannot be read safely. Depending on how the server handles this, clients may show a
"connection reset" failure instead of seeing the 413 response.

:param stream: The stream to read from. Must be a readable binary IO object.
:param limit: The limit in bytes to not read past. Should be either the
    ``Content-Length`` header value or ``request.max_content_length``.
:param is_max: Whether the given ``limit`` is ``request.max_content_length`` instead
    of the ``Content-Length`` header value. This changes how exhausted and
    disconnect events are handled.

.. versionchanged:: 2.3
    Handle ``max_content_length`` differently than ``Content-Length``.

.. versionchanged:: 2.3
    Implements ``io.RawIOBase`` rather than ``io.IOBase``.
c                6    Xl         SU l        X l        X0l        g )Nr   )_stream_poslimit_limit_is_max)rb   rL   r   rB   s       r   rg   LimitedStream.__init__  s    	
#r   c                4    U R                   U R                  :  $ )z:Whether the current stream position has reached the limit.)r   r   rk   s    r   is_exhaustedLimitedStream.is_exhausted  s     yyDJJ&&r   c                :    U R                   (       a
  [        5       eg)aR  Called when attempting to read after the limit has been reached.

The default behavior is to do nothing, unless the limit is a maximum, in which
case it raises :exc:`.RequestEntityTooLarge`.

.. versionchanged:: 2.3
    Raises ``RequestEntityTooLarge`` if the limit is a maximum.

.. versionchanged:: 2.3
    Any return value is ignored.
N)r   r	   rk   s    r   on_exhaustedLimitedStream.on_exhausted  s     ')) r   Nc                @    U R                   (       a  Ub
  [        5       eg)a  Called when an attempted read receives zero bytes before the limit was
reached. This indicates that the client disconnected before sending the full
request body.

The default behavior is to raise :exc:`.ClientDisconnected`, unless the limit is
a maximum and no error was raised.

.. versionchanged:: 2.3
    Added the ``error`` parameter. Do nothing if the limit is a maximum and no
    error was raised.

.. versionchanged:: 2.3
    Any return value is ignored.
N)r   r   )rb   errors     r   on_disconnectLimitedStream.on_disconnect  s      !!U%6$&& &7r   c                F    U R                   (       d  U R                  5       $ g)a  Exhaust the stream by reading until the limit is reached or the client
disconnects, returning the remaining data.

.. versionchanged:: 2.3
    Return the remaining data.

.. versionchanged:: 2.2.3
    Handle case where wrapped stream returns fewer bytes than requested.
r   )r   readallrk   s    r   exhaustLimitedStream.exhaust  s       <<>!r   c                &   [        U5      nU R                  U R                  -
  nUS::  a  U R                  5         g[	        U R
                  S5      (       aU  X#::  a   U R
                  R                  U5      nOg[        U5      n U R
                  R                  U5      nU(       a  XaS U& O4 U R
                  R                  [        X#5      5      n[        U5      nXqS U& U(       d  U R                  5         gU =R                  U-  sl        U$ ! [        [        4 a  nU R                  US9   S nAgS nAff = f! [        [        4 a  nU R                  US9   S nAgS nAff = f! [        [        4 a  nU R                  US9   S nAgS nAff = f)Nr   readinto)r   )r   r   r   r   r   r   r   OSErrorr5   r   	bytearrayr   min)rb   bsize	remainingout_sizeetemp_br   s           r   r   LimitedStream.readinto  sn   1vJJ*	>4<<,, +/<<+@+@+CH #9-#||44V<H
 #)ixL||((T)=>
 4yHixL 		X	A  , &&Q&/  , &&Q&/ Z( """+sH   D D; -$E' D8D33D8;E$EE$'F7FFc                   U R                   (       a  U R                  5         g[        5       nU R                   (       d=  U R                  S5      nU(       d  O$UR	                  U5        U R                   (       d  M=  [        U5      $ )Nr   i   )r   r   r   r   extendrF   )rb   outr   s      r   r   LimitedStream.readall8  sh    k ##99Y'D JJt ### Szr   c                    U R                   $ )z;Return the current stream position.

.. versionadded:: 0.9
)r   rk   s    r   r   LimitedStream.tellK  s    
 yyr   c                    gr   r   rk   s    r   readableLimitedStream.readableR  s    r   )r   r   r   r   )F)rL   r   r   r3   rB   r   rv   rw   r   ry   rj   )r   zException | Nonerv   rw   rx   )r   r   rv   r   )rv   r3   )rz   r{   r|   r}   r~   rg   propertyr   r   r   r   r   r   r   r   r   r   r   r   rG   rG     sD    >$ ' '*',.`&r   rG   )r   z t.Callable[..., WSGIApplication]rv   r   )FFFN)r*   r   r+   r   r,   r   r-   r   r.   t.Iterable[str] | Nonerv   str)r*   r   rv   ztuple[str, int | None] | Nonerj   )r*   r   r.   r   rv   r   )r*   r   rv   r   )TN)r*   r   rJ   r   rK   r   rv   r   )r*   r   rv   r   r   )r*   r   r   r   r   r3   rv   ru   )!
__future__r   rH   typingrC   	functoolsr   r   
exceptionsr   r	   sansior
   r(   sansio.utilsr   TYPE_CHECKING_typeshed.wsgir   r   r   r)   r8   r%   r@   rN   rS   rU   r   r   r   	RawIOBaserG   r   r   r   <module>r      s^   " 	   $ * - * )??..
8 #,0#2#2#2 #2 	#2
 *#2 	#2L"$ GK-C8" %)AFAFAF #AF 	AFH)1 1j EI$/>A.1 1hT" T"njBLL jr   