U a@s&ddlZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl m Z dd l mZdd lmZdd lmZdd lmZdd lmZddlmZddlmZejrddlmZddlmZejdddddZdOdeeeejej e!e!dddZ"dejej#e!eje$fdddZ%dPdejej e!e!dd d!Z&deje$dd"d#Z'dQdeej(d%d&d'Z)de!dd(d)Z*dRde!e!e!d,d-d.Z+dSde!e!e!d,d/d0Z,dTde!e!eje!d,d1d2Z-dUde!e!eje!d,d3d4Z.dVej/e!dfej/e!efe!e!eeje!d6d7d8Z0Gd9d:d:Z1dWdej(e$ej e2d<d=d>Z3Gd?d@d@Z4GdAdBdBZ5ej/ej e2ej(feje$e$ej6e2dCdDdEZ7dXej/ej e2ej(feje$e$eej6e2dGdHdIZ8dYej/ej e2ej(fe2eje$e$eej6e2dJdKdLZ9GdMdNdNej:Z;dS)ZN)partialupdate_wrapper)chain)_make_encode_wrapper) _to_bytes)_to_str)utils)host_is_trusted) _URLTuple) uri_to_iri)url_join) url_parse) url_quote)WSGIApplication)WSGIEnvironment).rr)freturncstfddS)a 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!') cs||ddS)N)arrNC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-1tps7o9u\werkzeug\wsgi.py"zresponder..rrrrr responders rFr)environ root_onlystrip_querystring host_only trusted_hostsrcCsb|dt||d}|sV|dd|d<|sV|dd|d<|sV|ddd |d <tjf|S) aXRecreate 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)schemehost SCRIPT_NAME root_path PATH_INFOpath QUERY_STRINGlatin1Z query_string)get_hostgetencode _sansio_utilsget_current_url)rrrr r!partsrrrr0%sr0)rrc CsP|d}|dkrdSzt|dd}Wnttfk rFd}YnX||fS)NZ SERVER_NAMEZ SERVER_PORT)r-int TypeError ValueError)rnameportrrr _get_serverKs  r7)rr!rcCst|d|dt||S)arReturn 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"Z HTTP_HOST)r/r,r-r7)rr!rrrr,\s r,c CsT|dddkrdS|d}|dk rPztdt|WSttfk rNYnXdS)aReturns 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. ZHTTP_TRANSFER_ENCODINGr&chunkedNCONTENT_LENGTHr)r-maxr2r4r3)rcontent_lengthrrrget_content_lengthxs  r<T)r safe_fallbackrcCsTttj|d}t|}|dr(|S|dkr@|r>> 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. :param charset: The ``encoding`` parameter passed to :func:`bytes.decode`. :param errors: The ``errors`` paramater passed to :func:`bytes.decode`. r(Nr%r&/r+rTrM)r-lstriplenr.splitr )rrKrLr) script_nameold_pathrvsegmentrrr pop_path_infos"!       rZcCs<|ddddd}|r8t|dd||ddSd S) a'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. .. 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. r(r&rRrrr+TrMN)r-rSrUr r.)rrKrLsegmentsrrrpeek_path_infos r\werkzeug.url_quote)environ_or_baseurl path_or_urlrKrLcollapse_http_schemesrcCstttddd}t|||}t|tr4t|dd}t|||}t|dd\}} } tt||dd\} } } ||| } || | } |r|| fD]}|dkrdSqn|dkr|| ksdS| | krdS| d } | | sdSd | t | d d S) a%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. .. versionchanged:: 0.15 The ``errors`` parameter defaults to leaving invalid bytes quoted instead of replacing them. .. versionadded:: 0.6 )r#netlocrcSsx|ddddd}t|dkrR|\}}|dkr<|dksL|dkr^|d kr^d}n |d }d}|dk rt|d|7}|S) N@r:http80https443r)rUrT)r#rar1r6rrr_normalize_netlocls z,extract_path_info.._normalize_netlocT)rN)rfrhrR) strr isinstancedictr0rrrstrip startswithrTrS)r^r_rKrLr`rjr)Zbase_iriZ base_schemeZ base_netloc base_pathZ cur_schemeZ cur_netlocZcur_pathr#rrrextract_path_info>s*.          rrc @s~eZdZdZd ejeejejej gdfejej gdffddddZ ddddZ edd d Z ddd d Z dS)ClosingIteratoraAThe 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() N)iterable callbacksrcCsrt|}ttjgtftt||_|dkr4g}nt|rD|g}nt |}t |dd}|rh| d|||_ dS)Ncloser) iterr>r?Callablebytesrnext_nextcallablelistgetattrinsert _callbacks)selfrtruiteratorZiterable_closerrr__init__s  zClosingIterator.__init__rcCs|SNrrrrr__iter__szClosingIterator.__iter__cCs|Sr)r{rrrr__next__szClosingIterator.__next__cCs|jD] }|qdSr)r)rcallbackrrrrvs zClosingIterator.close)N)__name__ __module__ __qualname____doc__r>IterableryOptionalUnionrxrrrrvrrrrrss& rs )rfile buffer_sizercCs|dt||S)aWraps 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. zwsgi.file_wrapper)r- FileWrapper)rrrrrr wrap_files rc@seZdZdZdejeddddZdddd Ze dd d Z ej dd d dZ ej edddZddddZedddZdS)ra\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. rN)rrrcCs||_||_dSr)rr)rrrrrrrszFileWrapper.__init__rcCst|jdr|jdSNrv)hasattrrrvrrrrrvs zFileWrapper.closecCs*t|jdr|jSt|jdr&dSdS)NseekableseekTF)rrrrrrrrs    zFileWrapper.seekable)argsrcGst|jdr|jj|dS)Nr)rrr)rrrrrr s zFileWrapper.seekcCst|jdr|jSdS)Ntell)rrrrrrrrs  zFileWrapper.tellcCs|SrrrrrrrszFileWrapper.__iter__cCs |j|j}|r|StdSr)rreadr StopIteration)rdatarrrrszFileWrapper.__next__)r)rrrrr>r@r2rrvboolrAnyrrrrryrrrrrrsrc@seZdZdZdejejeejfe ej e dddZ dddd Z edd d Z ejej ee fdd d ZedddZedddZddddZdS) _RangeWrapperasThis 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. rN)rt start_byte byte_rangecCsRt||_||_||_d|_|dk r.|||_d|_t|doD||_d|_dS)NrrF) rwrtrrend_byte read_lengthrr end_reached)rrtrrrrrr1s  z_RangeWrapper.__init__rcCs|SrrrrrrrEsz_RangeWrapper.__iter__cCsDz"t|j}|jt|7_|WStk r>d|_YnXdSNT)rzrtrrTrrrchunkrrr _next_chunkHs z_RangeWrapper._next_chunkcCsld}|jr,|j|j|j|_|j}n8|j|jkrB|}q,|dk r^||j|jd}|j}||fSr)rrtrrrrrrrZcontextual_read_lengthrrr_first_iterationQs   z_RangeWrapper._first_iterationcCsn|jr td}|j}|jdkr,|\}}|dkr<|}|jdk rj|j|jkrjd|_|d|j|S|S)NrT)rrrrrrrrrrr{_s  z_RangeWrapper._nextcCs |}|r|Sd|_tdSr)r{rrrrrrrms z_RangeWrapper.__next__cCst|jdr|jdSr)rrtrvrrrrrvts z_RangeWrapper.close)rN)rrrrr>rrryr@r2rrrrTuplerr{rrvrrrrr s  r)rDlimitrrccst|tttfrtdt|ds:|D]}|r&|Vq&dSttj|}t|t sn|dk rnttjt ||}|j }||}|sq|VqtdS)z-Helper for the line and chunk iter functions.zBPassed a string or byte object instead of true iterator or stream.rN) rmry bytearrayrlr3rr>r?r@rCr)rDrritem_readrrr_make_chunk_iterys" r()rDrr cap_at_bufferrc #st||td}|sdSt|}tt|dtt|d}tt|d}tt|dttjtt|ftjtdfdd }} |D]6} | |kr| d d|kr| | 7} } | r| V| } q| r| VdS) aSafely 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. r&N  z rc3sj}g}td}|sqg}d}ttjtt||dD]}|||t |7}|r~|ddkr~||Vg}q@r@|kr@||}t |kr|dV|d}q|g}q@|}q |r||VdS)Nr&rTrc) joinrzr>r?Iteratorryr splitlinesappendrT)_joinbuffernew_datanew_bufbuf_sizerrX_iterrrZcrlfemptyrr_iter_basic_liness2      z)make_line_iter.._iter_basic_linesrc)rrzrr>r?ryrr) rDrrr first_itemsZcrlfrpreviousrrrrmake_line_iters*%    r)rD separatorrrrrccsdt|||}t|d}|sdSttjtt|f|}t|trlt |}t dt |dj }dj}n(t|}t dt |dj }dj}g} t|d} | sqP|| } g} d} t| | D]}||kr|| Vg} d} q| t|7} | ||r| |kr|| }t||kr:|d|V||d}q|g} t|} q| } q| r`|| VdS) a 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. rN()r&()r)rrzr>r?rryrrmrlr recompileescaperUrrrTr)rDrrrrrr_splitrrrchunksrrrrXrrrmake_chunk_itersH         rc@seZdZdZejeddddZddddZe e dd d Z e dd d Z e dd dZd eddddZd!ejee dddZd"ejee dddZd#ejeeje dddZedddZe dddZe dddZdS)$rCaWraps 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. The return value of that function is forwarded to the reader function. So if it returns an empty string :meth:`read` will return an empty string as well. The limit however must never be higher than what the stream can output. Otherwise :meth:`readlines` will try to read past the limit. .. admonition:: Note on WSGI compliance calls to :meth:`readline` and :meth:`readlines` are not WSGI compliant because it passes a size argument to the readline methods. Unfortunately the WSGI PEP is not safely implementable without a size argument to :meth:`readline` because there is no EOF marker in the stream. As a result of that the use of :meth:`readline` is discouraged. For the same reason iterating over the :class:`LimitedStream` is not portable. It internally calls :meth:`readline`. We strongly suggest using :meth:`read` only or using the :func:`make_line_iter` which safely iterates line-based over a WSGI input stream. :param stream: the stream to wrap. :param limit: the limit for the stream, must not be longer than what the string can provide if the stream does not end with `EOF` (like `wsgi.input`) N)rDrrcCs |j|_|j|_d|_||_dS)Nr)rrreadline _readline_posr)rrDrrrrr^szLimitedStream.__init__rcCs|SrrrrrrrdszLimitedStream.__iter__cCs |j|jkS)z4If the stream is exhausted this attribute is `True`.)rrrrrr is_exhaustedgszLimitedStream.is_exhaustedcCs |dS)zThis is called when the stream tries to read past the limit. The return value of this function is returned from the reading function. r)rrrrr on_exhaustedlszLimitedStream.on_exhaustedcCsddlm}|dS)aWhat should happen if a disconnect is detected? The return value of this function is returned from read functions in case the client went away. By default a :exc:`~werkzeug.exceptions.ClientDisconnected` exception is raised. r)ClientDisconnectedN) exceptionsr)rrrrr on_disconnectus zLimitedStream.on_disconnect) chunk_sizercCs:|j|j}|}|dkr6t||}||||8}qdS)aExhaust the stream. This consumes all the data left until the limit is reached. :param chunk_size: the size for a chunk. It will read the chunk until the stream is exhausted and throw away the results. rN)rrminr)rrto_readrrrrexhausts    zLimitedStream.exhaust)sizerc Cs|j|jkr|S|dks$|dkr*|j}t|j|j|}z||}Wn ttfk rj|YSX|rt||kr|S|jt|7_|S)zyRead `size` bytes or if size is not provided everything is read. :param size: the number of bytes read. Nrc) rrrrrOSErrorr4rrT)rrrrrrrrs zLimitedStream.readc Cs|j|jkr|S|dkr*|j|j}nt||j|j}z||}Wn ttfk rj|YSX|r||s||S|jt|7_|S)zReads one line from the stream.N) rrrrrr4rrrT)rrlinerrrrs zLimitedStream.readlinecCsp|j}g}|dk r$t|j||}n|j}|dk r@|||j8}|j|krLql||||dk r*|j}q*|S)zReads a file into a list of strings. It calls :meth:`readline` until the file is read to the end. It does support the optional `size` argument if the underlying stream supports it for `readline`. N)rrrrr)rrZlast_posresultendrrr readliness zLimitedStream.readlinescCs|jS)zKReturns the position of the stream. .. versionadded:: 0.9 )rrrrrrszLimitedStream.tellcCs|}|st|Sr)rr)rrrrrrszLimitedStream.__next__cCsdSrrrrrrreadableszLimitedStream.readable)r)N)N)N)rrrrr>r@r2rrpropertyrrryrrrrrrListrrrrrrrrrC<s!  rC)FFFN)N)T)rIrJ)rIrJ)rIrJ)rIrJ)rIr]T)r)NrF)NrF) functoolsrr itertoolsr _internalrrr Zsansior r/Z sansio.utilsr urlsr r rrr TYPE_CHECKINGZ_typeshed.wsgirrrxrrrrrlr0rr2r7r,r<r@rErHrPrQrZr\rrrrsryrrrrrrrIOBaserCrrrrs                '   &   ; "   b6 4Z  _ M