
_U                 @   s2  d  Z  d d l Z 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 y d d l m Z Wn( e k
 r\d d l m Z d Z Yn Xe d  Z e j  d  Z! e" d d g  Z# d d d  Z$ d d d d d d d d d   Z% d! d"   Z& Gd# d$   d$ e'  Z( d% d&   Z) d' d(   Z* d) d*   Z+ d+ Z, d, Z- d- Z. d. Z/ Gd/ d0   d0 e'  Z0 d S)1z
    werkzeug.formparser
    ~~~~~~~~~~~~~~~~~~~

    This module implements the form parsing.  It supports url-encoded forms
    as well as non-nested multipart uploads.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
    N)update_wrapper)chain)repeat)tee   )
exceptions)BytesIO)	text_type)	to_native)FileStorage)Headers)	MultiDict)parse_options_header)url_decode_stream)get_content_length)get_input_stream)make_line_iter)SpooledTemporaryFile)TemporaryFile z^[ -~]{0,200}[!-~]$base64zquoted-printablec             C   sN   d } t  d k	 r% t  d | d d  S|  d k s= |  | k rG t d  St   S)z,The stream factory that is used per default.i   i  Nmax_sizemodezwb+i  )r   r   r   )total_content_lengthfilenamecontent_typecontent_lengthr    r   7/tmp/pip-build-5gj8f0j9/Werkzeug/werkzeug/formparser.pydefault_stream_factory5   s    
r   zutf-8replaceTc             C   s%   t  | | | | | | |  j |   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 :ref:`dealing-with-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:`~BaseResponse._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)environstream_factorycharseterrorsmax_form_memory_sizemax_content_lengthclssilentr   r   r   parse_form_dataA   s    2	r+   c                s     f d d   } t  |    S)z@Helper decorator for methods that exhausts the stream on return.c                sd   z   |  | | |  SWd  t  | d d   } | d  k	 rB |   n x | j d  } | sE PqE WXd  S)Nexhausti   @   i   )getattrread)selfstreamargskwargsr,   chunk)fr   r   wrapper   s    
zexhaust_stream.<locals>.wrapper)r   )r5   r6   r   )r5   r   exhaust_stream~   s    r7   c            	   @   s   e  Z d  Z d Z d d d d d d d d d  Z d d	   Z d
 d   Z d d d  Z e d d    Z	 e d d    Z
 d e	 d e
 d e
 i Z d S)r!   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:`~BaseResponse._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.
    Nzutf-8r    Tc             C   sg   | d  k r t  } | |  _ | |  _ | |  _ | |  _ | |  _ | d  k rQ t } | |  _ | |  _ d  S)N)	r   r$   r%   r&   r'   r(   r   r)   r*   )r0   r$   r%   r&   r'   r(   r)   r*   r   r   r   __init__   s    
						zFormDataParser.__init__c             C   s   |  j  j |  S)N)parse_functionsget)r0   mimetypeoptionsr   r   r   get_parse_func   s    zFormDataParser.get_parse_funcc             C   sL   | j  d d  } t |  } t |  \ } } |  j 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_TYPEr   )r:   r   r   parser   )r0   r#   r   r   r;   r<   r   r   r   r"      s    z!FormDataParser.parse_from_environc             C   s   |  j  d k	 r6 | d k	 r6 | |  j  k r6 t j    | d k rH i  } |  j | |  } | d k	 r y | |  | | | |  SWn t k
 r |  j s   Yn X| |  j   |  j   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(   r   RequestEntityTooLarger=   
ValueErrorr*   r)   )r0   r1   r;   r   r<   Z
parse_funcr   r   r   r?      s    	zFormDataParser.parsec       	      C   s   t  |  j |  j |  j d |  j d |  j } | j d  } | d  k rT t d   t | t	  rr | j
 d  } | j | | |  \ } } | | | f S)Nr'   r)   boundaryzMissing boundaryascii)MultiPartParserr$   r%   r&   r'   r)   r:   rA   
isinstancer	   encoder?   )	r0   r1   r;   r   r<   parserrB   formfilesr   r   r   _parse_multipart   s    		zFormDataParser._parse_multipartc             C   sm   |  j  d  k	 r6 | d  k	 r6 | |  j  k r6 t j    t | |  j d |  j d |  j } | | |  j   f S)Nr&   r)   )r'   r   r@   r   r%   r&   r)   )r0   r1   r;   r   r<   rH   r   r   r   _parse_urlencoded   s    $z FormDataParser._parse_urlencodedzmultipart/form-dataz!application/x-www-form-urlencodedzapplication/x-url-encoded)__name__
__module____qualname____doc__r8   r=   r"   r?   r7   rJ   rK   r9   r   r   r   r   r!      s    r!   c             C   s   t  j |   d k	 S)z9Checks if the string given is a valid multipart boundary.N)_multipart_boundary_rematch)rB   r   r   r   is_valid_multipart_boundary  s    rR   c             C   s^   |  d d  d k r* |  d d  d f S|  d d  d k rT |  d d  d f S|  d f S)z_Removes line ending characters and returns a tuple (`stripped_line`,
    `is_terminated`).
       N
   
Tr   
      
F)rT   rU   rZ   )rV   rW   rX   rY   r[   r   )liner   r   r   _line_parse  s
    r]   c             C   s   g  } x |  D] } t  |  } t |  \ } } | sC t d   | sM Pq | d d k r | r | d	 \ } } | | d | d d  f | d
 <q | j d d  } t |  d k r | j | d j   | d j   f  q Wt |  S)aB  Parses multipart headers from an iterable that yields lines (including
    the trailing newline symbol).  The iterable has to be newline terminated.

    The iterable will stop at the line where the headers ended so it can be
    further consumed.

    :param iterable: iterable of strings that are newline terminated
    z*unexpected end of line in multipart headerr   z 	r   z
 N:rS   r[   r[   )r
   r]   rA   splitlenappendstripr   )iterableresultr\   Zline_terminatedkeyvaluepartsr   r   r   parse_multipart_headers#  s    	%+rh   Z
begin_formZ
begin_fileZcontendc               @   s   e  Z d  Z d d d d d d d d  Z d d	   Z d
 d   Z d d   Z d d   Z d d   Z d d   Z	 d d   Z
 d d   Z d d d  Z d d   Z d d   Z d S) rD   Nzutf-8r    r-   i   c             C   s   | |  _  | |  _ | |  _ | d  k r- t n | |  _ | d  k rH t n | |  _ | d d k sm t d   | d k s t d   | |  _ d  S)N   r   z$buffer size has to be divisible by 4i   z"buffer size has to be at least 1KB)	r%   r&   r'   r   r$   r   r)   AssertionErrorbuffer_size)r0   r$   r%   r&   r'   r)   rl   r   r   r   r8   H  s    				zMultiPartParser.__init__c             C   sA   | d d  d k s, | d d  d k r= | j  d  d S| S)	zInternet Explorer 6 transmits the full file name if a file is
        uploaded.  This function strips the full path if it thinks the
        filename is Windows-like absolute.
        r      z:\NrS   z\\\r[   )r_   )r0   r   r   r   r   _fix_ie_filenameb  s    ,z MultiPartParser._fix_ie_filenamec             C   s2   x+ | D]# } | s P| j    } | r | Sq Wd S)zThe terminator might have some additional newlines before it.
        There is at least one application that sends additional newlines
        before headers (the python setuptools package).
            )rb   )r0   iteratorr\   r   r   r   _find_terminatork  s    z MultiPartParser._find_terminatorc             C   s   t  |   d  S)N)rA   )r0   messager   r   r   failx  s    zMultiPartParser.failc             C   s/   | j  d  } | d  k	 r+ | t k r+ | Sd  S)Nzcontent-transfer-encoding)r:   _supported_multipart_encodings)r0   headerstransfer_encodingr   r   r   get_part_encoding{  s    z!MultiPartParser.get_part_encodingc             C   sA   | j  d  } | r: t |  \ } } | j  d |  j  S|  j S)Nzcontent-typer%   )r:   r   r%   )r0   rv   r   r;   Z	ct_paramsr   r   r   get_part_charset  s
    z MultiPartParser.get_part_charsetc             C   s   t  | t  r' | j |  j |  j  } |  j |  } | j d  } y t | d  } Wn t t	 f k
 ry d } Yn X|  j
 d | d | d | d |  } | | f S)Nzcontent-typezcontent-lengthr   r   r   r   r   )rE   bytesdecoder%   r&   ro   r:   intKeyErrorrA   r$   )r0   r   rv   r   r   r   	containerr   r   r   start_file_streaming  s    		z$MultiPartParser.start_file_streamingc             C   s   t  j    d  S)N)r   r@   )r0   rz   r   r   r   in_memory_threshold_reached  s    z+MultiPartParser.in_memory_threshold_reachedc             C   sV   | s |  j  d  t |  s0 |  j  d |  t |  |  j k rR |  j  d  d  S)NzMissing boundaryzInvalid boundary: %sz Boundary longer than buffer size)rt   rR   r`   rl   )r0   rB   r   r   r   validate_boundary  s    z!MultiPartParser.validate_boundaryTc             c   s~  d | } | d } t  t | d | d |  j d | t  } |  j |  } | | k r] d S| | k rv |  j d  x| | k ryt |  }	 |	 j d  }
 |
 d k r |  j d  t |
  \ }
 } |  j	 |	  } | j d	  } | j d
  } | d k rt
 |	 | f f Vn t |	 | | f f Vd } x| D]} | sR|  j d  | d d  d k r| j   } | | | f k rP| d k	 r| d k rd } y t j | |  } Wn t k
 r|  j d  Yn X| rt | f Vd } | d d  d k rd } d } n | d d  } d } t | d |  f Vq9Wt d   | d k rkt | f Vt d f Vqy Wd S)a,  Generate parts of
        ``('begin_form', (headers, name))``
        ``('begin_file', (headers, name, filename))``
        ``('cont', bytestring)``
        ``('end', None)``

        Always obeys the grammar
        parts = ( begin_form cont* end |
                  begin_file cont* end )*
        s   --limitrl   cap_at_bufferNz,Expected boundary at start of multipart datazcontent-dispositionz"Missing Content-Disposition headernamer   rp   zunexpected end of streamrS   r   base64_codecz'could not decode transfer encoded chunk   
r   zunexpected end of part      
rZ   rZ   r[   r[   )rp   r   r   r   )r   r   rl   _empty_string_iterrr   rt   rh   r:   r   rx   _begin_form_begin_filerstripcodecsr{   	Exception_contrA   _end)r0   filerB   r   r   Z	next_partZ	last_partrq   
terminatorrv   dispositionextrarw   r   r   bufr\   cutoffr   r   r   parse_lines  sl    

			zMultiPartParser.parse_linesc          	   c   s  d } xx|  j  | | |  D]a\ } } | t k rv | \ } } }	 d }
 d } |  j |	 | |  \ }	 } | j } q | t k r | \ } } d }
 g  } | j } |  j d k	 } q | t k r | |  | r}| t |  7} | |  j k r}|  j	 |  q | t
 k r |
 rE| j d  d | t | |	 | d | f f Vq |  j |  } d | d j |  j | |  j  f f Vq Wd S)	zXGenerate ``('file', (name, val))`` and
        ``('form', (name, val))`` parts.
        r   TFNr   rv   rH   rp   )r   r   r   writer   ra   r'   r   r`   r   r   seekr   ry   joinr{   r&   )r0   r   rB   r   Z	in_memoryZelltZellrv   r   r   is_fileZguard_memoryr~   _writeZpart_charsetr   r   r   parse_parts  s:    "	
#zMultiPartParser.parse_partsc             C   sf   t  |  j | | |  d  \ } } d d   | D } d d   | D } |  j |  |  j |  f S)NrS   c             s   s)   |  ] } | d  d k r | d Vq d S)r   rH   r   Nr   ).0pr   r   r   	<genexpr>F  s    z(MultiPartParser.parse.<locals>.<genexpr>c             s   s)   |  ] } | d  d k r | d Vq d S)r   r   r   Nr   )r   r   r   r   r   r   G  s    )r   r   r)   )r0   r   rB   r   Z
formstreamZ
filestreamrH   rI   r   r   r   r?   B  s
    !zMultiPartParser.parsei   )rL   rM   rN   r8   ro   rr   rt   rx   ry   r   r   r   r   r   r?   r   r   r   r   rD   G  s"   	h/rD   )1rO   r   re	functoolsr   	itertoolsr   r   r   r   r   _compatr   r	   r
   Zdatastructuresr   r   r   httpr   urlsr   Zwsgir   r   r   tempfiler   ImportErrorr   r   compilerP   	frozensetru   r   r+   r7   objectr!   rR   r]   rh   r   r   r   r   rD   r   r   r   r   <module>   sX   5