U aC @stddlZddlZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z dd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z zddl!m"Z"Wn&e#k r6ddl!m$Z$dZ"YnXej%rxddlZ&ddl'm(Z(ej)ej*eefZ+Gddde&j,Z-ej.dej/dej0fdZ1ej*ddd d!Z2d:ej3e4ej3e5ej3e5ej3e4ej*d"d#d$Z6d;d(ej3de5e5ej3e4ej3e4ej3ej7ee8d)d* d+d,Z9e1e1d-d.d/Z:Gd0d1d1Z;e5ej)e5e8fd2d3d4Zed5d6d7Z?Gd8d9d9Z@dS)<N)update_wrapper)BytesIO)chain)Union) exceptions)_to_str) FileStorage)Headers) MultiDict)parse_options_header)Data)Epilogue)Field)File)MultipartDecoder)NeedData)url_decode_stream)_make_chunk_iter)get_content_length)get_input_stream)SpooledTemporaryFile) TemporaryFile)WSGIEnvironmentc@s>eZdZdejeejeejeejeejdddZdS)TStreamFactoryNtotal_content_length content_typefilenamecontent_lengthreturncCsdSN)selfrrrrr"r"TC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-1tps7o9u\werkzeug\formparser.py__call__)szTStreamFactory.__call__)N) __name__ __module__ __qualname__tOptionalintstrBinaryIOr%r"r"r"r$r(srF.)bound)streamr cCs|d}|r|d}q dS)N)read)r0Zbtsr"r"r$_exhaust6s r3rcCsJd}tdk r"ttjt|ddS|dks2||krDttjtdStS)Nizrb+)max_sizemode)rr)castr-rr)rrrrr4r"r"r$default_stream_factory<s r7utf-8replaceTrt_parse_result) environstream_factorycharseterrorsmax_form_memory_sizemax_content_lengthclssilentr cCst||||||||S)aParse the form data in the environ and return it as tuple in the form ``(stream, form, files)``. You should only call this method if the transport method is `POST`, `PUT`, or `PATCH`. If the mimetype of the data transmitted is `multipart/form-data` the files multidict will be filled with `FileStorage` objects. If the mimetype is unknown the input stream is wrapped and returned as first argument, else the stream is empty. This is a shortcut for the common usage of :class:`FormDataParser`. Have a look at :doc:`/request_data` for more details. .. versionadded:: 0.5 The `max_form_memory_size`, `max_content_length` and `cls` parameters were added. .. versionadded:: 0.5.1 The optional `silent` flag was added. :param environ: the WSGI environment to be used for parsing. :param stream_factory: An optional callable that returns a new read and writeable file descriptor. This callable works the same as :meth:`Response._get_file_stream`. :param charset: The character set for URL and url encoded form data. :param errors: The encoding error behavior. :param max_form_memory_size: the maximum number of bytes to be accepted for in-memory stored form data. If the data exceeds the value specified an :exc:`~exceptions.RequestEntityTooLarge` exception is raised. :param max_content_length: If this is provided and the transmitted data is longer than this value an :exc:`~exceptions.RequestEntityTooLarge` exception is raised. :param cls: an optional dict class to use. If this is not specified or `None` the default :class:`MultiDict` is used. :param silent: If set to False parsing errors will not be caught. :return: A tuple in the form ``(stream, form, files)``. )FormDataParserparse_from_environ)r;r<r=r>r?r@rArBr"r"r$parse_form_dataLs2rE)fr csfdd}ttt|S)z@Helper decorator for methods that exhausts the stream on return.c sLz||f||WSt|dd}|dk r4|n|d}|s4qFq4XdS)Nexhaustr1)getattrr2)r#r0argskwargsrGchunkrFr"r$wrappers  zexhaust_stream..wrapper)rr)r6r.)rFrMr"rLr$exhaust_streams rNc @sreZdZUdZdejdeeejeejeejeje e dddd Z eej eefejej dejeejeej eefgd fd d d Zdd dddZdejeejeejej eefd dddZeejeejeej eefd dddZeejeejeej eefd dddZeeedZej eej dejeejeej eefgd ffed<dS)rCaThis class implements parsing of form data for Werkzeug. By itself it can parse multipart and url encoded form data. It can be subclassed and extended but for most mimetypes it is a better idea to use the untouched stream and expose it as separate attributes on a request object. .. versionadded:: 0.8 :param stream_factory: An optional callable that returns a new read and writeable file descriptor. This callable works the same as :meth:`Response._get_file_stream`. :param charset: The character set for URL and url encoded form data. :param errors: The encoding error behavior. :param max_form_memory_size: the maximum number of bytes to be accepted for in-memory stored form data. If the data exceeds the value specified an :exc:`~exceptions.RequestEntityTooLarge` exception is raised. :param max_content_length: If this is provided and the transmitted data is longer than this value an :exc:`~exceptions.RequestEntityTooLarge` exception is raised. :param cls: an optional dict class to use. If this is not specified or `None` the default :class:`MultiDict` is used. :param silent: If set to False parsing errors will not be caught. Nr8r9Tr)r<r=r>r?r@rArBr cCsF|dkr t}||_||_||_||_||_|dkr6t}||_||_dSr!) r7r<r=r>r?r@r rArB)r#r<r=r>r?r@rArBr"r"r$__init__s zFormDataParser.__init__r:)mimetypeoptionsr cCs |j|Sr!)parse_functionsget)r#rPrQr"r"r$get_parse_funcszFormDataParser.get_parse_funcr)r;r cCs4|dd}t|}t|\}}|t||||S)zParses the information from the environment as form data. :param environ: the WSGI environment to be used for parsing. :return: A tuple in the form ``(stream, form, files)``. CONTENT_TYPE)rSrr parser)r#r;rrrPrQr"r"r$rDs  z!FormDataParser.parse_from_environ)r0rPrrQr cCs|jdk r,|dk r,||jkr,t|t|dkr8i}|||}|dk r|z||||||WStk rz|jsvYnX|||fS)aParses the information from the given stream, mimetype, content length and mimetype parameters. :param stream: an input stream :param mimetype: the mimetype of the data :param content_length: the content length of the incoming data :param options: optional mimetype parameters (used for the multipart boundary for instance) :return: A tuple in the form ``(stream, form, files)``. N)r@r3rRequestEntityTooLargerT ValueErrorrBrA)r#r0rPrrQZ parse_funcr"r"r$rWs$ zFormDataParser.parsec CsVt|j|j|j|j|jd}|ddd}|s:td| |||\}}|||fS)N)r?rAboundaryrVasciizMissing boundary) MultiPartParserr<r=r>r?rArSencoderYrW) r#r0rPrrQparserrZformfilesr"r"r$_parse_multipartszFormDataParser._parse_multipartcCsP|jdk r,|dk r,||jkr,t|tt||j|j|jd}|||fS)N)r>rA)r?r3rrXrr=r>rA)r#r0rPrrQr_r"r"r$_parse_urlencoded's z FormDataParser._parse_urlencoded)zmultipart/form-dataz!application/x-www-form-urlencodedzapplication/x-url-encodedrR)Nr8r9NNNT)N)r&r'r(__doc__r)r*r,r+Typer boolrODictCallabler-rTrDrWrNrarbrR__annotations__r"r"r"r$rCs     (  rC)liner cCsH|dddkr |dddfS|dddkr@|dddfS|dfS)z_Removes line ending characters and returns a tuple (`stripped_line`, `is_terminated`). Nz T>  Fr")rir"r"r$ _line_parseIs rn)iterabler cCstjdtddg}|D]}t|}t|\}}|sr?rA buffer_sizer cCs@||_||_||_|dkrt}||_|dkr0t}||_||_dSr!)r=r>r?r7r<r rAr)r#r<r=r>r?rArr"r"r$rO}s zMultiPartParser.__init__z te.NoReturn)messager cCs t|dSr!)rY)r#rr"r"r$failszMultiPartParser.fail)headersr cCs.|d}|r(t|\}}|d|jS|jS)N content-typer=)rSr r=)r#rrrPZ ct_paramsr"r"r$get_part_charsets   z MultiPartParser.get_part_charset)eventrr c CsT|jd}zt|jd}Wnttfk r:d}YnX|j||j||d}|S)Nrzcontent-lengthr)rrrr)rrSr+KeyErrorrYr<r)r#rrrr containerr"r"r$start_file_streamings  z$MultiPartParser.start_file_streaming)r0rZrr c Cs@tt|||jddg}t||j}g}g}|D]}|||} t| tt fs0t| t rn| } g} | j } nt| t r| } | | |} | j} nt| tr | | j| js t| t rd| || j|j} | | j| fn:ttj| } | d| | jt| | j| j| jdf|} qFq0||||fS)N)limitrr)r)rrrrr?Z receive_dataZ next_event isinstancerrrrxrrwriter dataZ more_datajoindecoderrr>namer)r6r-seekr rrA)r#r0rZriteratorr^fieldsr`rrZ current_partr_writer|r"r"r$rWs\            zMultiPartParser.parse)Nr8r9NNr1)r&r'r(r)r*r,r+rdr rOrr rrr-rbytesTuplerWr"r"r"r$r\|s4    r\)N)Nr8r9NNNT)Atypingr)rs functoolsrior itertoolsrrrVr _internalrZdatastructuresr r r httpr Zsansio.multipartr rrrrrurlsrZwsgirrrtempfiler ImportErrorr TYPE_CHECKINGteZ_typeshed.wsgirrr-r:ProtocolrTypeVarrgAnyr.r3r*r+r,r7rdrerErNrCrnIterablerr~r\r"r"r"r$s                           =, &