a
    -=ic>                     @   sT  d dl Zd dlmZ d dlmZ d dlmZ d dl mZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ zd dlmZ W n$ e y    d dlm!Z! dZY n0 ej"rfd dl Z#d dl$m%Z% ej&ej'e( eef Z)G dd de#j*Z+ej,dej-dej.f dZ/ej'e( dddd Z0d6ej1e2 ej1e3 ej1e3 ej1e2 ej'e( d!d"d#Z4d7d'ej1d e3e3ej1e2 ej1e2 ej1ej5e  e6d(d)	d*d+Z7e/e/d,d-d.Z8G d/d0 d0Z9e3ej&e3e6f d1d2d3Z:G d4d5 d5Z;dS )8    N)update_wrapper)BytesIO)chain)Union   )
exceptions)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                   @   sB   e Zd Zdeje eje eje eje eje dddZ	dS )TStreamFactoryNtotal_content_lengthcontent_typefilenamecontent_lengthreturnc                 C   s   d S N )selfr   r   r   r   r!   r!   d/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/werkzeug/formparser.py__call__'   s    zTStreamFactory.__call__)N)
__name__
__module____qualname__tOptionalintstrIObytesr$   r!   r!   r!   r#   r   &   s    r   F.)bound)streamr   c                 C   s   |  d}|r|  d}q
d S )N   )read)r0   Zbtsr!   r!   r#   _exhaust4   s    
r3   r   c                 C   sR   d}t d ur&ttjt t |ddS | d u s6| |krLttjt tdS t S )Ni  zrb+)max_sizemode)r   r(   castr,   r-   r   r   )r   r   r   r   r4   r!   r!   r#   default_stream_factory:   s    r7   utf-8replaceTr   t_parse_result)	environstream_factorycharseterrorsmax_form_memory_sizemax_content_lengthclssilentr   c                 C   s   t |||||||| S )a  Parse 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)``.
    )FormDataParserparse_from_environ)r;   r<   r=   r>   r?   r@   rA   rB   r!   r!   r#   parse_form_dataJ   s    2rE   )fr   c                    s    fdd}t tt| S )z@Helper decorator for methods that exhausts the stream on return.c              
      s   z\ | |g|R i |W t |dd }|d ur6|  S |d}|s6S q6S |d}|sJqqJn0t |dd }|d urz|  n|d}|szqqz0 d S )Nexhaustr1   )getattrr2   )r"   r0   argskwargsrG   chunkrF   r!   r#   wrapper   s*    
   

zexhaust_stream.<locals>.wrapper)r   r(   r6   r.   )rF   rM   r!   rL   r#   exhaust_stream   s    rN   c                   @   s  e Zd ZU dZdejd eeeje eje ejeje	  e
dddd	Zeejeef ejejd eje eeje ejeef gd
f  dddZdd
dddZdeje eeje ejejeef  d
dddZeeje eeje ejeef d
dddZeeje eeje ejeef d
dddZeeedZejeejd eje eeje ejeef gd
f f ed< dS )rC   a  This 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.
    Nr8   r9   Tr   )r<   r=   r>   r?   r@   rA   rB   r   c                 C   sF   |d u rt }|| _|| _|| _|| _|| _|d u r6t}|| _|| _d S r    )	r7   r<   r=   r>   r?   r@   r
   rA   rB   )r"   r<   r=   r>   r?   r@   rA   rB   r!   r!   r#   __init__   s    
zFormDataParser.__init__r:   )mimetypeoptionsr   c                 C   s   | j |S r    )parse_functionsget)r"   rP   rQ   r!   r!   r#   get_parse_func   s    zFormDataParser.get_parse_funcr   )r;   r   c                 C   s4   | dd}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)``.
        ZCONTENT_TYPE )rS   r   r   parser   )r"   r;   r   r   rP   rQ   r!   r!   r#   rD      s    z!FormDataParser.parse_from_environ)r0   rP   r   rQ   r   c                 C   s   | j dur,|dur,|| j kr,t| t |du r8i }| ||}|durzz|| ||||W S  tyx   | jst Y n0 ||  |  fS )a  Parses 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@   r3   r   RequestEntityTooLargerT   
ValueErrorrB   rA   )r"   r0   rP   r   rQ   Z
parse_funcr!   r!   r#   rV      s$    zFormDataParser.parsec           	      C   sV   t | j| j| j| j| jd}|ddd}|s:td|	|||\}}|||fS )N)r?   rA   boundaryrU   asciizMissing boundary)
MultiPartParserr<   r=   r>   r?   rA   rS   encoderX   rV   )	r"   r0   rP   r   rQ   parserrY   formfilesr!   r!   r#   _parse_multipart  s    zFormDataParser._parse_multipartc                 C   sP   | j d ur,|d ur,|| j kr,t| t t|| j| j| jd}|||  fS )N)r>   rA   )r?   r3   r   rW   r   r=   r>   rA   )r"   r0   rP   r   rQ   r^   r!   r!   r#   _parse_urlencoded%  s    	z FormDataParser._parse_urlencoded)zmultipart/form-dataz!application/x-www-form-urlencodedzapplication/x-url-encodedrR   )Nr8   r9   NNNT)N)r%   r&   r'   __doc__r(   r)   r+   r*   Typer
   boolrO   DictCallabler,   r-   rT   rD   rV   rN   r`   ra   rR   __annotations__r!   r!   r!   r#   rC      s~   
       "
 ("rC   )liner   c                 C   sH   | dd dkr | dd dfS | dd dv r@| dd dfS | dfS )z_Removes line ending characters and returns a tuple (`stripped_line`,
    `is_terminated`).
    Nz
T>   
Fr!   )rh   r!   r!   r#   _line_parseG  s
    rm   c                	   @   s   e Zd Zdejd eeeje ejeje  eddddZ	ed	d
ddZ
eedddZeeje eje dddZeje eeje ejeef dddZdS )r[   Nr8   r9   r1   r   )r<   r=   r>   r?   rA   buffer_sizer   c                 C   s@   || _ || _|| _|d u rt}|| _|d u r0t}|| _|| _d S r    )r=   r>   r?   r7   r<   r
   rA   rn   )r"   r<   r=   r>   r?   rA   rn   r!   r!   r#   rO   U  s    	zMultiPartParser.__init__zte.NoReturn)messager   c                 C   s   t |d S r    )rX   )r"   ro   r!   r!   r#   failn  s    zMultiPartParser.fail)headersr   c                 C   s.   | d}|r(t|\}}| d| jS | jS )Ncontent-typer=   )rS   r   r=   )r"   rq   r   rP   Z	ct_paramsr!   r!   r#   get_part_charsetq  s
    
z MultiPartParser.get_part_charset)eventr   r   c              	   C   sR   |j d}zt|j d }W n ttfy8   d}Y n0 | j||j||d}|S )Nrr   zcontent-lengthr   )r   r   r   r   )rq   rS   r*   KeyErrorrX   r<   r   )r"   rt   r   r   r   	containerr!   r!   r#   start_file_streaming{  s    
z$MultiPartParser.start_file_streaming)r0   rY   r   r   c              
   C   sF  t t||| jdd g}t|| j}g }g }|D  ]}|| | }	t|	tt	fs0t|	t
rp|	}
g }|j}nt|	tr|	}
| |	|}|j}nt|	tr&||	j |	js&t|
t
rd|| |
j| j}||
j|f n>ttjt |}|d ||
jt||
j|
j|
jdf | }	qHq0| || |fS )N)limitrn       r   )rq   ) r   r   rn   r   r?   Zreceive_dataZ
next_event
isinstancer   r   r   appendr   rw   writer   dataZ	more_datajoindecoders   rq   r>   namer(   r6   r,   r-   seekr   r   rA   )r"   r0   rY   r   iteratorr]   fieldsr_   r}   rt   Zcurrent_partrv   _writevaluer!   r!   r#   rV     sZ    	







zMultiPartParser.parse)Nr8   r9   NNr1   )r%   r&   r'   r(   r)   r+   r*   rc   r
   rO   rp   r	   rs   r   r,   r-   rw   TuplerV   r!   r!   r!   r#   r[   T  s.         
r[   )N)Nr8   r9   NNNT)<typingr(   	functoolsr   ior   	itertoolsr   r   rU   r   Zdatastructuresr   r	   r
   httpr   Zsansio.multipartr   r   r   r   r   r   urlsr   Zwsgir   r   r   tempfiler   ImportErrorr   TYPE_CHECKINGteZ_typeshed.wsgir   r   r,   r-   r:   Protocolr   TypeVarrf   Anyr.   r3   r)   r*   r+   r7   rc   rd   rE   rN   rC   rm   r[   r!   r!   r!   r#   <module>   s|   

        = ,