ó
`žTc           @   sŤ  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 m	 Z	 m
 Z
 d d l m Z d d l m Z m Z d d l m Z d d	 l m Z m Z m Z d d
 l m Z m Z m Z d d l m Z e	 d  Z e j d  Z e d d g  Z  d d  Z" d d d d d d e# d  Z$ d   Z% d e& f d     YZ' d   Z( d   Z) d   Z* d Z+ d Z, d Z- d Z. d e& f d     YZ/ d d  l0 m1 Z1 d S(!   s*  
    werkzeug.formparser
    ~~~~~~~~~~~~~~~~~~~

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

    :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
i˙˙˙˙N(   t   BytesIO(   t   TemporaryFile(   t   chaint   repeatt   tee(   t   update_wrapper(   t	   to_nativet	   text_type(   t   url_decode_stream(   t   make_line_itert   get_input_streamt   get_content_length(   t   Headerst   FileStoraget	   MultiDict(   t   parse_options_headert    s   ^[ -~]{0,200}[!-~]$t   base64s   quoted-printablec         C   s   |  d k r t  d  St   S(   s,   The stream factory that is used per default.i   iô  s   wb+i Đ (   R   R    (   t   total_content_lengtht   filenamet   content_typet   content_length(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   default_stream_factory&   s    
s   utf-8t   replacec         C   s%   t  | | | | | | |  j |   S(   s  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)``.
    (   t   FormDataParsert   parse_from_environ(   t   environt   stream_factoryt   charsett   errorst   max_form_memory_sizet   max_content_lengtht   clst   silent(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   parse_form_data.   s    ,c            s     f d   } t  |    S(   s@   Helper decorator for methods that exhausts the stream on return.c            sf   z   |  | | |  SWd  t  | d d   } | d  k	 rB |   n x | j d  } | sE PqE qE Xd  S(   Nt   exhausti   i@   i   (   t   getattrt   Nonet   read(   t   selft   streamt   argst   kwargsR#   t   chunk(   t   f(    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   wrappera   s    
(   R   (   R,   R-   (    (   R,   sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   exhaust_stream_   s    R   c           B   s   e  Z d  Z d d d d d d e d  Z d   Z d   Z d d  Z e	 d    Z
 e	 d    Z i e
 d	 6e d
 6e d 6Z RS(   sÇ  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.
    s   utf-8R   c         C   sm   | d  k r t } n  | |  _ | |  _ | |  _ | |  _ | |  _ | d  k rW t } n  | |  _ | |  _	 d  S(   N(
   R%   R   R   R   R   R   R   R   R    R!   (   R'   R   R   R   R   R   R    R!   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   __init__   s    								c         C   s   |  j  j |  S(   N(   t   parse_functionst   get(   R'   t   mimetypet   options(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   get_parse_func   s    c         C   sL   | j  d d  } t |  } t |  \ } } |  j t |  | | |  S(   sČ   Parses 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)``.
        t   CONTENT_TYPER   (   R1   R   R   t   parseR
   (   R'   R   R   R   R2   R3   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyR      s
    c         C   sĹ   |  j  d k	 r9 | d k	 r9 | |  j  k r9 t j    n  | d k rN i  } n  |  j | |  } | d k	 rŹ y | |  | | | |  SWqŹ t k
 r¨ |  j sŠ   qŠ qŹ Xn  | |  j   |  j   f S(   sÍ  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%   t
   exceptionst   RequestEntityTooLargeR4   t
   ValueErrorR!   R    (   R'   R(   R2   R   R3   t
   parse_func(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyR6   Ť   s    		c   	      C   s    t  |  j |  j |  j d |  j d |  j } | j d  } | d  k rW t d   n  t	 | t
  rx | j d  } n  | j | | |  \ } } | | | f S(   NR   R    t   boundarys   Missing boundaryt   ascii(   t   MultiPartParserR   R   R   R   R    R1   R%   R9   t
   isinstanceR   t   encodeR6   (	   R'   R(   R2   R   R3   t   parserR;   t   formt   files(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   _parse_multipartČ   s    	c      	   C   sp   |  j  d  k	 r9 | d  k	 r9 | |  j  k r9 t j    n  t | |  j d |  j d |  j } | | |  j   f S(   NR   R    (   R   R%   R7   R8   R   R   R   R    (   R'   R(   R2   R   R3   RA   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   _parse_urlencodedŐ   s    s   multipart/form-datas!   application/x-www-form-urlencodeds   application/x-url-encodedN(   t   __name__t
   __module__t   __doc__R%   t   TrueR/   R4   R   R6   R.   RC   RD   R0   (    (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyR   p   s   		c         C   s   t  j |   d k	 S(   s9   Checks if the string given is a valid multipart boundary.N(   t   _multipart_boundary_ret   matchR%   (   R;   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   is_valid_multipart_boundaryç   s    c         C   sF   |  d d k r |  d  t  f S|  d d k r< |  d  t  f S|  t f S(   s_   Removes line ending characters and returns a tuple (`stripped_line`,
    `is_terminated`).
    iţ˙˙˙s   
i˙˙˙˙s   s   
(   s   
s   
(   s   s   
s   s   
(   RH   t   False(   t   line(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   _line_parseě   s
    c         C   sń   g  } xŢ |  D]Ö } t  |  } t |  \ } } | sF t d   n  | sP Pq | d d k r | r | d \ } } | | d | d f | d <q | j d d  } t |  d k r | j | d j   | d j   f  q q Wt |  S(	   sB  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
    s*   unexpected end of line in multipart headeri    s    	i˙˙˙˙s   
 i   t   :i   (   R   RN   R9   t   splitt   lent   appendt   stripR   (   t   iterablet   resultRM   t   line_terminatedt   keyt   valuet   parts(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   parse_multipart_headers÷   s    	.t
   begin_formt
   begin_filet   contt   endR=   c           B   s   e  Z d d  d d d d d  Z d   Z d   Z d   Z d   Z d	   Z d
   Z	 d   Z
 d   Z d   Z d   Z d   Z RS(   s   utf-8R   i@   i   c         C   s   | |  _  | |  _ | |  _ | |  _ | d  k r9 t } n  | d  k rN t } n  | |  _ | d d k ss t d   | d k s t d   | |  _	 d  S(   Ni   i    s$   buffer size has to be divisible by 4i   s"   buffer size has to be at least 1KB(
   R   R   R   R   R%   R   R   R    t   AssertionErrort   buffer_size(   R'   R   R   R   R   R    R`   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyR/     s    							c         C   s8   | d d !d k s# | d  d k r4 | j  d  d S| S(   s¸   Internet 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.
        i   i   s   :\i   s   \\s   \i˙˙˙˙(   RP   (   R'   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   _fix_ie_filename2  s    #c         C   s5   x. | D]& } | s Pn  | j    } | r | Sq Wd S(   sÇ   The terminator might have some additional newlines before it.
        There is at least one application that sends additional newlines
        before headers (the python setuptools package).
        R   (   RS   (   R'   t   iteratorRM   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   _find_terminator;  s    c         C   s   t  |   d  S(   N(   R9   (   R'   t   message(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   failH  s    c         C   s/   | j  d  } | d  k	 r+ | t k r+ | Sd  S(   Ns   content-transfer-encoding(   R1   R%   t   _supported_multipart_encodings(   R'   t   headerst   transfer_encoding(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   get_part_encodingK  s    c         C   sA   | j  d  } | r: t |  \ } } | j  d |  j  S|  j S(   Ns   content-typeR   (   R1   R   R   (   R'   Rg   R   R2   t	   ct_params(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   get_part_charsetQ  s
    c         C   s   t  | t  r* | j |  j |  j  } n  |  j |  } | j d  } y t | d  } Wn t t	 f k
 r{ d } n X|  j
 | | | |  } | | f S(   Ns   content-types   content-lengthi    (   R>   t   bytest   decodeR   R   Ra   R1   t   intt   KeyErrorR9   R   (   R'   R   Rg   R   R   R   t	   container(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   start_file_streamingY  s    
c         C   s   t  j    d  S(   N(   R7   R8   (   R'   Rl   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   in_memory_threshold_reachedf  s    c         C   s_   | s |  j  d  n  t |  s6 |  j  d |  n  t |  |  j k r[ |  j  d  n  d  S(   Ns   Missing boundarys   Invalid boundary: %ss    Boundary longer than buffer size(   Re   RK   RQ   R`   (   R'   R;   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   validate_boundaryi  s    c         c   sz  d | } | d } t  t | d | d |  j t  } |  j |  } | | k rW d S| | k rs |  j d  n  x | | k rut |  } | j d  }	 |	 d k rš |  j d  n  t	 |	  \ }	 }
 |  j
 |  } |
 j d  } |
 j d	  } | d k rt | | f f Vn t | | | f f Vd
 } x| D]} | sU|  j d  n  | d  d k r| j   } | | | f k rPqn  | d k	 rĺ| d k rŤd } n  y t j | |  } Wqĺt k
 rá|  j d  qĺXn  | r˙t | f Vd
 } n  | d d k rd } d } n | d } d } t | |  f Vq9Wt d   | d k rgt | f Vn  t d f Vqv Wd S(   s,  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   --t   limitR`   Ns,   Expected boundary at start of multipart datas   content-dispositions"   Missing Content-Disposition headert   nameR   R   s   unexpected end of streami   R   t   base64_codecs'   could not decode transfer encoded chunkiţ˙˙˙s   
i˙˙˙˙s   unexpected end of parts   s   
(   R   s   s   
s   
(   R   R	   R`   t   _empty_string_iterRc   Re   RZ   R1   R%   R   Ri   t   _begin_formt   _begin_filet   rstript   codecsRm   t	   Exceptiont   _contR9   t   _end(   R'   t   fileR;   R   t	   next_partt	   last_partRb   t
   terminatorRg   t   dispositiont   extraRh   Ru   R   t   bufRM   t   cutoff(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   parse_linesu  sd    

					
c      	   c   s  d } x|  j  | | |  D]j\ } } | t k rv | \ } } }	 t }
 t } |  j |	 | |  \ }	 } | j } q | t k rľ | \ } } t }
 g  } | j } |  j d k	 } q | t
 k r| |  | r| t |  7} | |  j k r|  j |  qqq | t k r |
 rK| j d  d | t | |	 | d | f f Vq|  j |  } d | d j |  j | |  j  f f Vq q Wd S(   sX   Generate ``('file', (name, val))`` and
        ``('form', (name, val))`` parts.
        i    R   Rg   RA   R   N(   R   Ry   RH   RL   Rq   t   writeRx   RR   R   R%   R}   RQ   Rr   R~   t   seekR   Rk   t   joinRm   R   (   R'   R   R;   R   t	   in_memoryt   elltt   ellRg   Ru   R   t   is_filet   guard_memoryRp   t   _writet   part_charset(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   parse_parts×  s>    "	
c         C   s`   t  |  j | | |  d  \ } } d   | D } d   | D } |  j |  |  j |  f S(   Ni   c         s   s)   |  ] } | d  d k r | d Vq d S(   i    RA   i   N(    (   t   .0t   p(    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pys	   <genexpr>  s    c         s   s)   |  ] } | d  d k r | d Vq d S(   i    R   i   N(    (   R   R   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pys	   <genexpr>  s    (   R   R   R    (   R'   R   R;   R   t
   formstreamt
   filestreamRA   RB   (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyR6     s
    !Ni   (   RE   RF   R%   R/   Ra   Rc   Re   Ri   Rk   Rq   Rr   Rs   R   R   R6   (    (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyR=     s   											b	,(   R7   (2   RG   t   reR{   t   ioR    t   tempfileR   t	   itertoolsR   R   R   t	   functoolsR   t   werkzeug._compatR   R   t   werkzeug.urlsR   t   werkzeug.wsgiR	   R
   R   t   werkzeug.datastructuresR   R   R   t   werkzeug.httpR   Rw   t   compileRI   t	   frozensetRf   R%   R   RH   R"   R.   t   objectR   RK   RN   RZ   Rx   Ry   R}   R~   R=   t   werkzeugR7   (    (    (    sY   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/formparser.pyt   <module>   s<   .	w			đ