
º|jAw  ã               @   së  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 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 m Z m Z m Z m Z m Z m Z m Z m Z m Z m  Z  m! Z! m" Z" m# Z# d d
 l$ m% Z% d d l& m' Z' d d l( m) Z) m* Z* m+ Z+ m, Z, dl Z- e j. ƒ  Z/ d d d d i Z0 e j1 d k rÅd d „  Z2 d d „  Z d d „  Z3 d d „  Z4 d d d „ Z5 d d  „  Z6 d! d" „  Z7 d# d$ „  Z8 d% d& „  Z9 d' d( „  Z: d) d* „  Z; d d+ d, „ Z< d- d. „  Z= d/ d0 „  Z> d1 d2 „  Z? d3 d4 „  Z@ d5 d6 „  ZA d7 d8 „  ZB d9 d: „  ZC d; d< „  ZD eE d= d> ƒ ZF d? d@ „  ZG dA dB „  ZH dC dD „  ZI dE dF „  ZJ dG dH „  ZK dI dJ „  ZL e jM dK dL „  ƒ ZN dM dN „  ZO d dO dP „ ZP dQ dR „  ZQ dS dT dU „ ZR dV dW „  ZS dX dY „  ZT dZ jU d[ ƒ ZV eV d\ ZW eV d] ZX d^ d_ „  ZY d` da „  ZZ db dc „  Z[ e j\ dd ƒ Z] e j\ de ƒ Z^ df dg „  Z_ dh di „  Z` dj dk „  Za d S)mz”
requests.utils
~~~~~~~~~~~~~~

This module provides utility functions that are used within Requests
that are also useful for external consumption.
é    N)ÚOrderedDicté   )Ú__version__)Úcerts)Úto_native_string)Úparse_http_list)ÚquoteÚurlparseÚbytesÚstrÚunquoteÚ
getproxiesÚproxy_bypassÚ
urlunparseÚ
basestringÚinteger_typesÚis_py3Úproxy_bypass_environmentÚgetproxies_environmentÚMapping)Úcookiejar_from_dict)ÚCaseInsensitiveDict)Ú
InvalidURLÚInvalidHeaderÚFileModeWarningÚUnrewindableBodyErrorú.netrcÚ_netrcÚhttpéP   Úhttpsi»  Úwin32c             C   sF  y% t  r d d  l } n d d  l } Wn t k
 r= d SYn XyK | j | j d ƒ } t | j | d ƒ d ƒ } | j | d ƒ d } Wn t k
 r¡ d SYn X| s° | r´ d S| j	 d ƒ } x| | D]t } | d k rì d |  k rì d	 S| j
 d d
 ƒ } | j
 d d ƒ } | j
 d d ƒ } t j | |  t j ƒ rÊ d	 SqÊ Wd S)Nr   Fz;Software\Microsoft\Windows\CurrentVersion\Internet SettingsZProxyEnableZProxyOverrideú;z<local>Ú.Tz\.Ú*z.*ú?)r   ÚwinregÚ_winregÚImportErrorÚOpenKeyÚHKEY_CURRENT_USERÚintÚQueryValueExÚOSErrorÚsplitÚreplaceÚreÚmatchÚI)Úhostr&   ZinternetSettingsZproxyEnableZproxyOverrideÚtest© r5   ú2/tmp/pip-build-1hwfuv5s/requests/requests/utils.pyÚproxy_bypass_registry0   s8    				r7   c             C   s!   t  ƒ  r t |  ƒ St |  ƒ Sd S)zšReturn True, if the host should be bypassed.

        Checks proxy settings gathered from the environment, if specified,
        or the registry.
        N)r   r   r7   )r3   r5   r5   r6   r   W   s    	
r   c             C   s   t  |  d ƒ r |  j ƒ  }  |  S)z/Returns an internal sequence dictionary update.Úitems)Úhasattrr8   )Údr5   r5   r6   Údict_to_sequencec   s    r;   c             C   s‹  d  } d } t  |  d ƒ r* t |  ƒ } nƒ t  |  d ƒ rE |  j } nh t  |  d ƒ r­ y |  j ƒ  } Wn t j k
 r{ Yn2 Xt j | ƒ j } d |  j k r­ t	 j
 d t ƒ t  |  d ƒ rhy |  j ƒ  } Wn* t t f k
 rø | d  k	 rô | } Ynp Xt  |  d ƒ rh| d  k rhy3 |  j d d	 ƒ |  j ƒ  } |  j | pBd ƒ Wn t t f k
 rgd } Yn X| d  k rzd } t d | | ƒ S)
Nr   Ú__len__ÚlenÚfilenoÚba%  Requests has determined the content-length for this request using the binary size of the file: however, the file has been opened in text mode (i.e. without the 'b' flag in the mode). This may lead to an incorrect content-length. In Requests 3.0, support will be removed for files in text mode.ÚtellÚseeké   )r9   r=   r>   ÚioÚUnsupportedOperationÚosÚfstatÚst_sizeÚmodeÚwarningsÚwarnr   r@   r-   ÚIOErrorrA   Úmax)ÚoÚtotal_lengthÚcurrent_positionr>   r5   r5   r6   Ú	super_lenl   s@    rP   Fc             C   s–  t  j j d ƒ } | d k	 r* | f } n d d „  t Dƒ } y:d d l m } m } d } xV | D]N } y t  j j | ƒ } Wn t k
 r— d SYn Xt  j j	 | ƒ rc | } Pqc W| d k rÅ d St
 |  ƒ }	 d }
 t |  t ƒ rõ |
 j d ƒ }
 |	 j j |
 ƒ d } yG | | ƒ j | ƒ } | rQ| d r9d n d	 } | | | d
 f SWn! | t f k
 ru| rq‚  Yn XWn t t f k
 r‘Yn Xd S)z;Returns the Requests tuple auth for a given url from netrc.ZNETRCNc             s   s   |  ] } d  j  | ƒ Vq d S)z~/{}N)Úformat)Ú.0Úfr5   r5   r6   ú	<genexpr>°   s    z!get_netrc_auth.<locals>.<genexpr>r   )ÚnetrcÚNetrcParseErroró   :Úasciir   rB   )rE   ÚenvironÚgetÚNETRC_FILESrU   rV   ÚpathÚ
expanduserÚKeyErrorÚexistsr	   Ú
isinstancer   ÚdecodeÚnetlocr.   ÚauthenticatorsrK   r(   ÚAttributeError)ÚurlÚraise_errorsZ
netrc_fileZnetrc_locationsrU   rV   Ú
netrc_pathrS   ÚlocÚriÚsplitstrr3   r   Úlogin_ir5   r5   r6   Úget_netrc_auth©   s@    	rl   c             C   s[   t  |  d d ƒ } | rW t | t ƒ rW | d d k rW | d d k rW t j j | ƒ Sd S)z0Tries to guess the filename of the given object.ÚnameNr   ú<r   ú>éÿÿÿÿ)Úgetattrr`   r   rE   r\   Úbasename)Úobjrm   r5   r5   r6   Úguess_filenameâ   s    %rt   c             C   s  t  j j |  ƒ r |  St  j j |  ƒ \ } } xJ | rz t  j j | ƒ rz t  j j | ƒ \ } } d j | | g ƒ } q1 Wt j | ƒ sŽ |  St j | ƒ } | | j ƒ  k r³ |  St	 j
 ƒ  } t  j j | | j d ƒ Œ } t  j j | ƒ s| j | d | ƒ} | S)zÄReplace nonexistent paths that look like they refer to a member of a zip
    archive with the location of an extracted copy of the target, or else
    just return the provided path unchanged.
    ú/r\   )rE   r\   r_   r.   ÚjoinÚzipfileÚ
is_zipfileÚZipFileÚnamelistÚtempfileÚ
gettempdirÚextract)r\   ÚarchiveÚmemberÚprefixZzip_fileÚtmpZextracted_pathr5   r5   r6   Úextract_zipped_pathsê   s     r‚   c             C   sA   |  d k r d St  |  t t t t f ƒ r7 t d ƒ ‚ t |  ƒ S)a  Take an object and test to see if it can be represented as a
    dictionary. Unless it can not be represented as such, return an
    OrderedDict, e.g.,

    ::

        >>> from_key_val_list([('key', 'val')])
        OrderedDict([('key', 'val')])
        >>> from_key_val_list('string')
        Traceback (most recent call last):
        ...
        ValueError: cannot encode objects that are not 2-tuples
        >>> from_key_val_list({'key': 'val'})
        OrderedDict([('key', 'val')])

    :rtype: OrderedDict
    Nz+cannot encode objects that are not 2-tuples)r`   r   r
   Úboolr+   Ú
ValueErrorr   )Úvaluer5   r5   r6   Úfrom_key_val_list
  s
    r†   c             C   s\   |  d k r d St  |  t t t t f ƒ r7 t d ƒ ‚ t  |  t ƒ rR |  j ƒ  }  t |  ƒ S)aÂ  Take an object and test to see if it can be represented as a
    dictionary. If it can be, return a list of tuples, e.g.,

    ::

        >>> to_key_val_list([('key', 'val')])
        [('key', 'val')]
        >>> to_key_val_list({'key': 'val'})
        [('key', 'val')]
        >>> to_key_val_list('string')
        Traceback (most recent call last):
        ...
        ValueError: cannot encode objects that are not 2-tuples

    :rtype: list
    Nz+cannot encode objects that are not 2-tuples)	r`   r   r
   rƒ   r+   r„   r   r8   Úlist)r…   r5   r5   r6   Úto_key_val_list%  s    rˆ   c             C   st   g  } xg t  |  ƒ D]Y } | d d … | d d … k oD d k n r_ t | d d … ƒ } | j | ƒ q W| S)a  Parse lists as described by RFC 2068 Section 2.

    In particular, parse comma-separated lists where the elements of
    the list may include quoted-strings.  A quoted-string could
    contain a comma.  A non-quoted string could have quotes in the
    middle.  Quotes are removed automatically after parsing.

    It basically works like :func:`parse_set_header` just that items
    may appear multiple times and case sensitivity is preserved.

    The return value is a standard :class:`list`:

    >>> parse_list_header('token, "quoted value"')
    ['token', 'quoted value']

    To create a header from the :class:`list` again, use the
    :func:`dump_header` function.

    :param value: a string with a list header.
    :return: :class:`list`
    :rtype: list
    Nr   ú"rp   rp   )Ú_parse_list_headerÚunquote_header_valueÚappend)r…   ÚresultÚitemr5   r5   r6   Úparse_list_headerC  s    0r�   c             C   s¢   i  } x• t  |  ƒ D]‡ } d | k r2 d | | <q | j d d ƒ \ } }  |  d d … |  d d … k ou d k n r� t |  d d … ƒ }  |  | | <q W| S)a^  Parse lists of key, value pairs as described by RFC 2068 Section 2 and
    convert them into a python dict:

    >>> d = parse_dict_header('foo="is a fish", bar="as well"')
    >>> type(d) is dict
    True
    >>> sorted(d.items())
    [('bar', 'as well'), ('foo', 'is a fish')]

    If there is no value for a key it will be `None`:

    >>> parse_dict_header('key_without_value')
    {'key_without_value': None}

    To create a header from the :class:`dict` again, use the
    :func:`dump_header` function.

    :param value: a string with a dict header.
    :return: :class:`dict`
    :rtype: dict
    ú=Nr   r‰   rp   rp   )rŠ   r.   r‹   )r…   r�   rŽ   rm   r5   r5   r6   Úparse_dict_headerc  s    
0r‘   c             C   sw   |  rs |  d |  d	 k o% d k n rs |  d d
 … }  | sW |  d d … d k rs |  j  d d ƒ j  d d ƒ S|  S)zçUnquotes a header value.  (Reversal of :func:`quote_header_value`).
    This does not use the real unquoting but what browsers are actually
    using for quoting.

    :param value: the header value to unquote.
    :rtype: str
    r   r   r‰   NrB   z\\ú\z\"rp   rp   )r/   )r…   Úis_filenamer5   r5   r6   r‹   †  s
    *r‹   c             C   s+   i  } x |  D] } | j  | | j <q W| S)z€Returns a key/value dictionary from a CookieJar.

    :param cj: CookieJar object to extract cookies from.
    :rtype: dict
    )r…   rm   )ÚcjÚcookie_dictÚcookier5   r5   r6   Údict_from_cookiejarŸ  s    r—   c             C   s   t  | |  ƒ S)zÂReturns a CookieJar from a key/value dictionary.

    :param cj: CookieJar to insert cookies into.
    :param cookie_dict: Dict of key/values to insert into CookieJar.
    :rtype: CookieJar
    )r   )r”   r•   r5   r5   r6   Úadd_dict_to_cookiejar®  s    r˜   c             C   sv   t  j d t ƒ t j d d t j ƒ} t j d d t j ƒ} t j d ƒ } | j |  ƒ | j |  ƒ | j |  ƒ S)zlReturns encodings from given content string.

    :param content: bytestring to extract encodings from.
    z¤In requests 3.0, get_encodings_from_content will be removed. For more information, please see the discussion on issue #2266. (This warning should only appear once.)z!<meta.*?charset=["\']*(.+?)["\'>]Úflagsz+<meta.*?content=["\']*;?charset=(.+?)["\'>]z$^<\?xml.*?encoding=["\']*(.+?)["\'>])rI   rJ   ÚDeprecationWarningr0   Úcompiler2   Úfindall)ÚcontentÚ
charset_reÚ	pragma_reÚxml_rer5   r5   r6   Úget_encodings_from_content¹  s    r¡   c       
      C   s×   |  j  d ƒ } | d j ƒ  | d d … } } i  } d } xŽ | D]† } | j ƒ  } | rC | d } } | j d ƒ }	 |	 d k r¹ | d |	 … j | ƒ } | |	 d d … j | ƒ } | | | j ƒ  <qC W| | f S)	z¦Returns content type and parameters from given header

    :param header: string
    :return: tuple containing content type and dictionary of
         parameters
    r"   r   r   Nz"' Tr�   rp   )r.   ÚstripÚfindÚlower)
ÚheaderÚtokensÚcontent_typeÚparamsZparams_dictZitems_to_stripÚparamÚkeyr…   Zindex_of_equalsr5   r5   r6   Ú_parse_content_type_headerÍ  s    !r«   c             C   sl   |  j  d ƒ } | s d St | ƒ \ } } d | k rH | d j d ƒ Sd | k rX d Sd | k rh d Sd S)	z}Returns encodings from given HTTP Header Dict.

    :param headers: dictionary to extract encoding from.
    :rtype: str
    zcontent-typeNÚcharsetz'"Útextz
ISO-8859-1zapplication/jsonzutf-8)rZ   r«   r¢   )Úheadersr§   r¨   r5   r5   r6   Úget_encoding_from_headersæ  s    r¯   c             c   s“   | j  d k r) x |  D] } | Vq Wd St j | j  ƒ d d ƒ } x( |  D]  } | j | ƒ } | rK | VqK W| j d d d ƒ} | r� | Vd S)zStream decodes a iterator.NÚerrorsr/   ó    ÚfinalT)ÚencodingÚcodecsÚgetincrementaldecoderra   )ÚiteratorÚrrŽ   ÚdecoderÚchunkÚrvr5   r5   r6   Ústream_decode_response_unicodeÿ  s    		r»   c             c   sd   d } | d k s | d k r* t  |  ƒ } x3 | t  |  ƒ k  r_ |  | | | … V| | 7} q- Wd S)z Iterate over slices of a string.r   N)r=   )ÚstringÚslice_lengthÚposr5   r5   r6   Úiter_slices  s    r¿   c             C   s›   t  j d t ƒ g  } t |  j ƒ } | ra y t |  j | ƒ SWn t k
 r` | j | ƒ Yn Xy t |  j | d d ƒSWn t	 k
 r– |  j SYn Xd S)zâReturns the requested content back in unicode.

    :param r: Response object to get unicode content from.

    Tried:

    1. charset from content-type
    2. fall back and replace all unicode characters

    :rtype: str
    z£In requests 3.0, get_unicode_from_response will be removed. For more information, please see the discussion on issue #2266. (This warning should only appear once.)r°   r/   N)
rI   rJ   rš   r¯   r®   r   r�   ÚUnicodeErrorrŒ   Ú	TypeError)r·   Útried_encodingsr³   r5   r5   r6   Úget_unicode_from_response  s    rÃ   Z4ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzz0123456789-._~c             C   sþ   |  j  d ƒ } xß t d t | ƒ ƒ D]È } | | d d … } t | ƒ d k rÛ | j ƒ  rÛ y t t | d ƒ ƒ } Wn" t k
 rš t d | ƒ ‚ Yn X| t k rÆ | | | d d … | | <qí d | | | | <q% d | | | | <q% Wd j	 | ƒ S)	z¨Un-escape any percent-escape sequences in a URI that are unreserved
    characters. This leaves all reserved, illegal and non-ASCII bytes encoded.

    :rtype: str
    ú%r   r   rB   é   z%Invalid percent-escape sequence: '%s'NÚ )
r.   Úranger=   ÚisalnumÚchrr+   r„   r   ÚUNRESERVED_SETrv   )ÚuriÚpartsÚiÚhÚcr5   r5   r6   Úunquote_unreservedD  s    rÐ   c             C   sO   d } d } y t  t |  ƒ d | ƒSWn" t k
 rJ t  |  d | ƒSYn Xd S)z¬Re-quote the given URI.

    This function passes the given URI through an unquote/quote cycle to
    ensure that it is fully and consistently quoted.

    :rtype: str
    z!#$%&'()*+,/:;=?@[]~z!#$&'()*+,/:;=?@[]~ÚsafeN)r   rÐ   r   )rË   Úsafe_with_percentÚsafe_without_percentr5   r5   r6   Úrequote_uri\  s    rÔ   c             C   s”   t  j d t j |  ƒ ƒ d } | j d ƒ \ } } t  j d t j t t | ƒ ƒ ƒ ƒ d } t  j d t j | ƒ ƒ d | @} | | @| | @k S)zïThis function allows you to check if an IP belongs to a network subnet

    Example: returns True if ip = 192.168.1.1 and net = 192.168.1.0/24
             returns False if ip = 192.168.1.1 and net = 192.168.100.0/24

    :rtype: bool
    z=Lr   ru   )ÚstructÚunpackÚsocketÚ	inet_atonr.   Údotted_netmaskr+   )ÚipÚnetÚipaddrÚnetaddrÚbitsÚnetmaskÚnetworkr5   r5   r6   Úaddress_in_networkr  s
    +#rá   c             C   s/   d d d |  >d A} t  j t j d | ƒ ƒ S)z�Converts mask from /xx format to xxx.xxx.xxx.xxx

    Example: if mask is 24 function returns 255.255.255.0

    :rtype: str
    l   ÿÿ r   é    z>I)r×   Ú	inet_ntoarÕ   Úpack)ÚmaskrÞ   r5   r5   r6   rÙ   �  s    rÙ   c             C   s1   y t  j |  ƒ Wn t  j k
 r, d SYn Xd S)z
    :rtype: bool
    FT)r×   rØ   Úerror)Ú	string_ipr5   r5   r6   Úis_ipv4_addressŒ  s
    	rè   c             C   s¬   |  j  d ƒ d k r¤ y t |  j d ƒ d ƒ } Wn t k
 rJ d SYn X| d k  sc | d k rg d Sy t j |  j d ƒ d ƒ Wq¨ t j k
 r  d SYq¨ Xn d Sd S)zV
    Very simple check of the cidr format in no_proxy variable.

    :rtype: bool
    ru   r   Frâ   r   T)Úcountr+   r.   r„   r×   rØ   ræ   )Ústring_networkrå   r5   r5   r6   Úis_valid_cidr—  s    	rë   c             c   sn   | d k	 } | r1 t  j j |  ƒ } | t  j |  <z	 d VWd | ri | d k r\ t  j |  =n | t  j |  <Xd S)zÎSet the environment variable 'env_name' to 'value'

    Save previous value, yield, and then restore the previous value stored in
    the environment variable 'env_name'.

    If 'value' is None, do nothingN)rE   rY   rZ   )Zenv_namer…   Zvalue_changedÚ	old_valuer5   r5   r6   Úset_environ¯  s    	rí   c       	      C   sŒ  d d „  } | } | d k r* | d ƒ } t  |  ƒ } | j d k rI d S| r0d d „  | j d d	 ƒ j d
 ƒ Dƒ } t | j ƒ rÒ x§ | D]> } t | ƒ r¸ t | j | ƒ rË d Sq� | j | k r� d Sq� Wn^ | j } | j rú | d j | j ƒ 7} x3 | D]+ } | j j	 | ƒ s(| j	 | ƒ rd SqWt
 d | ƒ �< y t | j ƒ } Wn! t t j f k
 rvd } Yn XWd QRX| rˆd Sd S)zL
    Returns whether we should bypass proxies or not.

    :rtype: bool
    c             S   s(   t  j j |  ƒ p' t  j j |  j ƒ  ƒ S)N)rE   rY   rZ   Úupper)Úkr5   r5   r6   Ú<lambda>Í  s    z'should_bypass_proxies.<locals>.<lambda>NÚno_proxyTc             s   s   |  ] } | r | Vq d  S)Nr5   )rR   r3   r5   r5   r6   rT   Þ  s    z(should_bypass_proxies.<locals>.<genexpr>ú rÆ   ú,z:{}F)r	   Úhostnamer/   r.   rè   rë   rá   ÚportrQ   Úendswithrí   r   rÁ   r×   Úgaierror)	re   rñ   Ú	get_proxyZno_proxy_argÚparsedÚproxy_ipZhost_with_portr3   Úbypassr5   r5   r6   Úshould_bypass_proxiesÅ  s<    (		!rü   c             C   s!   t  |  d | ƒr i  St ƒ  Sd S)zA
    Return a dict of environment proxies.

    :rtype: dict
    rñ   N)rü   r   )re   rñ   r5   r5   r6   Úget_environ_proxies  s    rý   c             C   sŸ   | p	 i  } t  |  ƒ } | j d k rC | j | j | j d ƒ ƒ S| j d | j | j d | j d g } d } x% | D] } | | k rz | | } Pqz W| S)z­Select a proxy for the url, if applicable.

    :param url: The url being for the request
    :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs
    NÚallz://zall://)r	   rô   rZ   Úscheme)re   ÚproxiesÚurlpartsÚ
proxy_keysÚproxyÚ	proxy_keyr5   r5   r6   Úselect_proxy  s    
	
r  zpython-requestsc             C   s   d |  t  f S)zO
    Return a string representing the default user agent.

    :rtype: str
    z%s/%s)r   )rm   r5   r5   r6   Údefault_user_agent(  s    r  c            	   C   s.   t  d t ƒ  d d j d
 ƒ d d d d	 i ƒ S)z9
    :rtype: requests.structures.CaseInsensitiveDict
    z
User-AgentzAccept-Encodingz, ÚgzipÚdeflateÚAcceptz*/*Ú
Connectionz
keep-alive)r  r  )r   r  rv   r5   r5   r5   r6   Údefault_headers1  s
    	r  c       	      C   s  g  } d } |  j  | ƒ }  |  s% | Sxß t j d |  ƒ D]Ë } y | j d d ƒ \ } } Wn t k
 r{ | d } } Yn Xd | j  d ƒ i } xb | j d ƒ D]Q } y | j d ƒ \ } }  Wn t k
 rÕ PYn X|  j  | ƒ | | j  | ƒ <q¡ W| j | ƒ q8 W| S)	z¼Return a list of parsed link headers proxies.

    i.e. Link: <http:/.../front.jpeg>; rel=front; type="image/jpeg",<http://.../back.jpeg>; rel=back;type="image/jpeg"

    :rtype: list
    z '"z, *<r"   r   rÆ   re   z<> '"r�   )r¢   r0   r.   r„   rŒ   )	r…   ÚlinksÚreplace_charsÚvalre   r¨   Úlinkr©   rª   r5   r5   r6   Úparse_header_links=  s&     r  ú rX   rB   é   c             C   s  |  d d … } | t  j t  j f k r, d S| d d … t  j k rI d S| d d … t  j t  j f k ro d S| j t ƒ } | d k rŽ d	 S| d k rÔ | d d d … t k r· d
 S| d d d … t k rÔ d S| d k r| d d … t	 k rú d S| d d … t	 k rd Sd S)z
    :rtype: str
    Né   zutf-32r  z	utf-8-sigrB   zutf-16r   zutf-8z	utf-16-ber   z	utf-16-lez	utf-32-bez	utf-32-le)
r´   ÚBOM_UTF32_LEÚBOM_UTF32_BEÚBOM_UTF8ÚBOM_UTF16_LEÚBOM_UTF16_BEré   Ú_nullÚ_null2Ú_null3)ÚdataÚsampleÚ	nullcountr5   r5   r6   Úguess_json_utfh  s*    "r  c             C   sP   t  |  | ƒ \ } } } } } } | s4 | | } } t | | | | | | f ƒ S)zªGiven a URL that may or may not have a scheme, prepend the given scheme.
    Does not replace a present scheme with the one provided as an argument.

    :rtype: str
    )r	   r   )re   Ú
new_schemerÿ   rb   r\   r¨   ÚqueryÚfragmentr5   r5   r6   Úprepend_scheme_if_neededˆ  s    !r#  c             C   sS   t  |  ƒ } y" t | j ƒ t | j ƒ f } Wn t t f k
 rN d } Yn X| S)z{Given a url with authentication components, extract them into a tuple of
    username,password.

    :rtype: (str,str)
    rÆ   )rÆ   rÆ   )r	   r   ÚusernameÚpasswordrd   rÁ   )re   rù   Úauthr5   r5   r6   Úget_auth_from_url™  s    "r'  s   ^\S[^\r\n]*$|^$z^\S[^\r\n]*$|^$c             C   s…   |  \ } } t  | t ƒ r$ t } n t } y# | j | ƒ sL t d | ƒ ‚ Wn1 t k
 r€ t d | | t | ƒ f ƒ ‚ Yn Xd S)zÕVerifies that header value is a string which doesn't contain
    leading whitespace or return characters. This prevents unintended
    header injection.

    :param header: tuple, in the format (name, value).
    z7Invalid return character or leading space in header: %sz>Value for header {%s: %s} must be of type str or bytes, not %sN)r`   r
   Ú_CLEAN_HEADER_REGEX_BYTEÚ_CLEAN_HEADER_REGEX_STRr1   r   rÁ   Útype)r¥   rm   r…   Úpatr5   r5   r6   Úcheck_header_validity®  s    	r,  c             C   sc   t  |  ƒ \ } } } } } } | s1 | | } } | j d d ƒ d } t | | | | | d f ƒ S)zW
    Given a url remove the fragment and the authentication part.

    :rtype: str
    ú@r   rÆ   rp   )r	   Úrsplitr   )re   rÿ   rb   r\   r¨   r!  r"  r5   r5   r6   ÚurldefragauthÃ  s
    r/  c             C   s~   t  |  j d d ƒ } | d k	 rn t |  j t ƒ rn y | |  j ƒ Wqz t t f k
 rj t d ƒ ‚ Yqz Xn t d ƒ ‚ d S)zfMove file pointer back to its recorded starting position
    so it can be read again on redirect.
    rA   Nz;An error occurred when rewinding request body for redirect.z+Unable to rewind request body for redirect.)rq   Úbodyr`   Ú_body_positionr   rK   r-   r   )Úprepared_requestÚ	body_seekr5   r5   r6   Úrewind_bodyÔ  s    r4  )r   r   )bÚ__doc__r´   Ú
contextlibrC   rE   r0   r×   rÕ   Úsysr{   rI   rw   Úcollectionsr   r   rÆ   r   Ú_internal_utilsr   Úcompatr   rŠ   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Úcookiesr   Ú
structuresr   Ú
exceptionsr   r   r   r   r[   ÚwhereÚDEFAULT_CA_BUNDLE_PATHZDEFAULT_PORTSÚplatformr7   r;   rP   rl   rt   r‚   r†   rˆ   r�   r‘   r‹   r—   r˜   r¡   r«   r¯   r»   r¿   rÃ   Ú	frozensetrÊ   rÐ   rÔ   rá   rÙ   rè   rë   Úcontextmanagerrí   rü   rý   r  r  r  r  Úencoder  r  r  r  r#  r'  r›   r(  r)  r,  r/  r4  r5   r5   r5   r6   Ú<module>	   sŒ   ^"'	=9  #
%=	&

 