
Ã|jƒ!  ã               @   sÌ   d  d l  m Z d  d l Z d  d l Z d  d l Z d d l m Z d d d „ Z d d	 „  Z	 d
 d d d i Z
 e
 j d d „  e d  d ƒ Dƒ ƒ d d „  Z d d „  Z e Z Gd d „  d e ƒ Z d S)é    )Úabsolute_importNé   )Úsixzapplication/octet-streamc             C   s!   |  r t  j |  ƒ d p | S| S)zæ
    Guess the "Content-Type" of a file.

    :param filename:
        The filename to guess the "Content-Type" of using :mod:`mimetypes`.
    :param default:
        If no "Content-Type" can be guessed, default to `default`.
    r   )Ú	mimetypesÚ
guess_type)ÚfilenameÚdefault© r	   ú1/tmp/pip-build-1hwfuv5s/urllib3/urllib3/fields.pyÚguess_content_type
   s    	r   c                sÙ   t  ˆ  t j ƒ r! ˆ  j d ƒ ‰  t ‡  f d d †  d Dƒ ƒ s€ d |  ˆ  f } y | j d ƒ Wn t t f k
 r{ Yn X| St j r˜ ˆ  j d ƒ ‰  t	 j
 j ˆ  d ƒ ‰  d |  ˆ  f ‰  t j rÕ ˆ  j d ƒ ‰  ˆ  S)a!  
    Helper function to format and quote a single header parameter using the
    strategy defined in RFC 2231.

    Particularly useful for header parameters which might contain
    non-ASCII values, like file names. This follows
    `RFC 2388 Section 4.4 <https://tools.ietf.org/html/rfc2388#section-4.4>`_.

    :param name:
        The name of the parameter, a string expected to be ASCII only.
    :param value:
        The value of the parameter, provided as ``bytes`` or `str``.
    :ret:
        An RFC-2231-formatted unicode string.
    zutf-8c             3   s   |  ] } | ˆ  k Vq d  S)Nr	   )Ú.0Úch)Úvaluer	   r
   ú	<genexpr>+   s    z.format_header_param_rfc2231.<locals>.<genexpr>z"\
z%s="%s"Úasciiz%s*=%s)Ú
isinstancer   Úbinary_typeÚdecodeÚanyÚencodeÚUnicodeEncodeErrorÚUnicodeDecodeErrorÚPY2ÚemailÚutilsÚencode_rfc2231)Únamer   Úresultr	   )r   r
   Úformat_header_param_rfc2231   s     		r   ú"z%22ú\z\\c             C   s7   i  |  ]- } | d k r d j  | ƒ t j | ƒ “ q S)é   z%{:02X})r!   )Úformatr   Úunichr)r   Úccr	   r	   r
   ú
<dictcomp>J   s   	r%   é   c                sS   ‡  f d d †  } t  j d j d d „  ˆ  j ƒ  Dƒ ƒ ƒ } | j | |  ƒ } | S)Nc                s   ˆ  |  j  d ƒ S)Nr   )Úgroup)Úmatch)Úneedles_and_replacementsr	   r
   ÚreplacerS   s    z#_replace_multiple.<locals>.replacerú|c             S   s   g  |  ] } t  j | ƒ ‘ q Sr	   )ÚreÚescape)r   Zneedler	   r	   r
   ú
<listcomp>W   s   	 z%_replace_multiple.<locals>.<listcomp>)r,   ÚcompileÚjoinÚkeysÚsub)r   r)   r*   Úpatternr   r	   )r)   r
   Ú_replace_multipleR   s
    %r4   c             C   s>   t  | t j ƒ r! | j d ƒ } t | t ƒ } d |  | f S)a±  
    Helper function to format and quote a single header parameter using the
    HTML5 strategy.

    Particularly useful for header parameters which might contain
    non-ASCII values, like file names. This follows the `HTML5 Working Draft
    Section 4.10.22.7`_ and matches the behavior of curl and modern browsers.

    .. _HTML5 Working Draft Section 4.10.22.7:
        https://w3c.github.io/html/sec-forms.html#multipart-form-data

    :param name:
        The name of the parameter, a string expected to be ASCII only.
    :param value:
        The value of the parameter, provided as ``bytes`` or `str``.
    :ret:
        A unicode string, stripped of troublesome characters.
    zutf-8z%s="%s")r   r   r   r   r4   Ú_HTML5_REPLACEMENTS)r   r   r	   r	   r
   Úformat_header_param_html5_   s    r6   c               @   sy   e  Z d  Z d Z d d e d d „ Z e e d d „ ƒ Z d d „  Z d	 d
 „  Z	 d d „  Z
 d d d d d „ Z d S)ÚRequestFielda  
    A data container for request body parameters.

    :param name:
        The name of this request field. Must be unicode.
    :param data:
        The data/value body.
    :param filename:
        An optional filename of the request field. Must be unicode.
    :param headers:
        An optional dict-like object of headers to initially use for the field.
    :param header_formatter:
        An optional callable that is used to encode and format the headers. By
        default, this is :func:`format_header_param_html5`.
    Nc             C   sF   | |  _  | |  _ | |  _ i  |  _ | r9 t | ƒ |  _ | |  _ d  S)N)Ú_nameÚ	_filenameÚdataÚheadersÚdictÚheader_formatter)Úselfr   r:   r   r;   r=   r	   r	   r
   Ú__init__�   s    				zRequestField.__init__c             C   s�   t  | t ƒ rN t | ƒ d k r3 | \ } } } q` | \ } } t | ƒ } n d } d } | } |  | | d | d | ƒ} | j d | ƒ | S)a³  
        A :class:`~urllib3.fields.RequestField` factory from old-style tuple parameters.

        Supports constructing :class:`~urllib3.fields.RequestField` from
        parameter of key/value strings AND key/filetuple. A filetuple is a
        (filename, data, MIME type) tuple where the MIME type is optional.
        For example::

            'foo': 'bar',
            'fakefile': ('foofile.txt', 'contents of foofile'),
            'realfile': ('barfile.txt', open('realfile').read()),
            'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'),
            'nonamefile': 'contents of nonamefile field',

        Field names and filenames must be unicode.
        é   Nr   r=   Úcontent_type)r   ÚtupleÚlenr   Úmake_multipart)ÚclsÚ	fieldnamer   r=   r   r:   rA   Úrequest_paramr	   r	   r
   Úfrom_tuplesŸ   s    zRequestField.from_tuplesc             C   s   |  j  | | ƒ S)aI  
        Overridable helper function to format a single header parameter. By
        default, this calls ``self.header_formatter``.

        :param name:
            The name of the parameter, a string expected to be ASCII only.
        :param value:
            The value of the parameter, provided as a unicode string.
        )r=   )r>   r   r   r	   r	   r
   Ú_render_partÃ   s    zRequestField._render_partc             C   sp   g  } | } t  | t ƒ r' | j ƒ  } x9 | D]1 \ } } | d k	 r. | j |  j | | ƒ ƒ q. Wd j | ƒ S)aO  
        Helper function to format and quote a single header.

        Useful for single headers that are composed of multiple items. E.g.,
        'Content-Disposition' fields.

        :param header_parts:
            A sequence of (k, v) tuples or a :class:`dict` of (k, v) to format
            as `k1="v1"; k2="v2"; ...`.
        Nz; )r   r<   ÚitemsÚappendrI   r0   )r>   Úheader_partsÚpartsÚiterabler   r   r	   r	   r
   Ú_render_partsÐ   s    zRequestField._render_partsc             C   s¼   g  } d d d g } xA | D]9 } |  j  j | d ƒ r | j d | |  j  | f ƒ q WxF |  j  j ƒ  D]5 \ } } | | k ri | ri | j d | | f ƒ qi W| j d ƒ d j | ƒ S)z=
        Renders the headers for this request field.
        zContent-DispositionzContent-TypezContent-LocationFz%s: %sz
)r;   ÚgetrK   rJ   r0   )r>   ÚlinesÚ	sort_keysÚsort_keyÚheader_nameÚheader_valuer	   r	   r
   Úrender_headersæ   s    "zRequestField.render_headersc          	   C   sr   | p	 d |  j  d <|  j  d d j d |  j d |  j f d |  j f f ƒ g ƒ 7<| |  j  d <| |  j  d <d	 S)
a|  
        Makes this request field into a multipart request field.

        This method overrides "Content-Disposition", "Content-Type" and
        "Content-Location" headers to the request parameter.

        :param content_type:
            The 'Content-Type' of the request body.
        :param content_location:
            The 'Content-Location' of the request body.

        z	form-datazContent-Dispositionz; Ú r   r   zContent-TypezContent-LocationN)r;   r0   rO   r8   r9   )r>   Úcontent_dispositionrA   Úcontent_locationr	   r	   r
   rD   ù   s    'zRequestField.make_multipart)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r6   r?   ÚclassmethodrH   rI   rO   rV   rD   r	   r	   r	   r
   r7   ~   s   
#r7   é    )Ú
__future__r   Úemail.utilsr   r   r,   Úpackagesr   r   r   r5   ÚupdateÚranger4   r6   Úformat_header_paramÚobjectr7   r	   r	   r	   r
   Ú<module>   s   +	