<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
B
    ÍAÒ[¸w ã               @   s  d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
mZmZmZmZ ddlmZmZm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mZ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.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4 ddl5Z5ddl6Z6ddl7Z7ddl8Z8ddl9Z9ddl:Z:ddl;Z;ddl<Z<ddl=Z=ddl>Z>ddl?Z?yddl@Z@ddl@mAZA W n eBk
rŠ   dZCY nX dZCdddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-gZDe:jEdd.… ZFdaGde9jHfd/d$„ZId0d%„ ZJg ZKdd1d*„ZLd2d+„ ZMer(e8 Nd3e8jO¡ZPn
e8 Nd3¡ZPd4d5„ ZQG d6d„ deRƒZSG d7d„ deRƒZTd8d&„ ZUG d9d„ deRƒZVG d:d#„ d#eVƒZWG d;d„ deVƒZXG d<d„ deVƒZYd=d>„ ZZG d?d„ deVƒZ[G d@d„ deRƒZ\G dAd„ de\ƒZ]G dBd„ deRƒZ^G dCd„ de^eVƒZ_G dDd„ de^eVƒZ`e6jaZbG dEd„ deRƒZcG dFd„ deVecƒZdG dGd„ deVecƒZeG dHdI„ dIeVƒZfG dJd„ defƒZgehedKƒrŽG dLdM„ dMefƒZieD jdM¡ G dNd„ deVƒZkG dOd"„ d"eVƒZldPdQ„ ZmdRdS„ ZnG dTd„ deVƒZodUdV„ ZpG dWd „ d eVƒZqG dXd!„ d!eqƒZrdYZse6jtdZkrdd[lumvZvmwZw nd\d(„ Zvd]d'„ Zwi ZxG d^d,„ d,eRƒZyG d_d-„ d-eyƒZzda{d`da„ Z|da}dbdc„ Z~daddde„ Z€dadfdg„ Z‚G dhdi„ dieRƒZƒdjdk„ Z„dldm„ Z…dndo„ Z†e:j‡dpkrâddqlˆm‰Z‰mŠZŠ drds„ Z‹dtdu„ ZŒdvdw„ Zdxd)„ ZŽn6e6jtdZkrdydz„ Zd{d)„ ZŽd|d}„ Zd~dw„ Zne„ZŽe…ZdS )€aÝ
  
Ported using Python-Future from the Python 3.3 standard library.

An extensible library for opening URLs using a variety of protocols

The simplest way to use this module is to call the urlopen function,
which accepts a string containing a URL or a Request object (described
below).  It opens the URL and returns the results as file-like
object; the returned object has some extra methods described below.

The OpenerDirector manages a collection of Handler objects that do
all the actual work.  Each Handler implements a particular protocol or
option.  The OpenerDirector is a composite object that invokes the
Handlers needed to open the requested URL.  For example, the
HTTPHandler performs HTTP GET and POST requests and deals with
non-error returns.  The HTTPRedirectHandler automatically deals with
HTTP 301, 302, 303 and 307 redirect errors, and the HTTPDigestAuthHandler
deals with digest authentication.

urlopen(url, data=None) -- Basic usage is the same as original
urllib.  pass the url and optionally data to post to an HTTP URL, and
get a file-like object back.  One difference is that you can also pass
a Request instance instead of URL.  Raises a URLError (subclass of
IOError); for HTTP errors, raises an HTTPError, which can also be
treated as a valid response.

build_opener -- Function that creates a new OpenerDirector instance.
Will install the default handlers.  Accepts one or more Handlers as
arguments, either instances or Handler classes that it will
instantiate.  If one of the argument is a subclass of the default
handler, the argument will be installed instead of the default.

install_opener -- Installs a new opener as the default opener.

objects of interest:

OpenerDirector -- Sets up the User Agent as the Python-urllib client and manages
the Handler classes, while dealing with requests and responses.

Request -- An object that encapsulates the state of a request.  The
state can be as simple as the URL.  It can also include extra HTTP
headers, e.g. a User-Agent.

BaseHandler --

internals:
BaseHandler and parent
_call_chain conventions

Example usage:

import urllib.request

# set up authentication info
authinfo = urllib.request.HTTPBasicAuthHandler()
authinfo.add_password(realm='PDQ Application',
                      uri='https://mahler:8092/site-updates.py',
                      user='klem',
                      passwd='geheim$parole')

proxy_support = urllib.request.ProxyHandler({"http" : "http://ahad-haam:3128"})

# build a new opener that adds authentication and caching FTP handlers
opener = urllib.request.build_opener(proxy_support, authinfo,
                                     urllib.request.CacheFTPHandler)

# install it
urllib.request.install_opener(opener)

f = urllib.request.urlopen('http://www.python.org/')
é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literals)ÚbytesÚdictÚfilterÚinputÚintÚmapÚopenÚstr)ÚPY2ÚPY3Úraise_with_tracebackN)Úemail)Úclienté   )ÚURLErrorÚ	HTTPErrorÚContentTooShortError)ÚurlparseÚurlsplitÚurljoinÚunwrapÚquoteÚunquoteÚ	splittypeÚ	splithostÚ	splitportÚ	splituserÚsplitpasswdÚ	splitattrÚ
splitqueryÚ
splitvalueÚsplittagÚto_bytesÚ
urlunparse)Ú
addinfourlÚaddclosehook)Ú
SSLContextFTÚRequestÚOpenerDirectorÚBaseHandlerÚHTTPDefaultErrorHandlerÚHTTPRedirectHandlerÚHTTPCookieProcessorÚProxyHandlerÚHTTPPasswordMgrÚHTTPPasswordMgrWithDefaultRealmÚAbstractBasicAuthHandlerÚHTTPBasicAuthHandlerÚProxyBasicAuthHandlerÚAbstractDigestAuthHandlerÚHTTPDigestAuthHandlerÚProxyDigestAuthHandlerÚHTTPHandlerÚFileHandlerÚ
FTPHandlerÚCacheFTPHandlerÚUnknownHandlerÚHTTPErrorProcessorÚurlopenÚinstall_openerÚbuild_openerÚpathname2urlÚurl2pathnameÚ
getproxiesÚurlretrieveÚ
urlcleanupÚ	URLopenerÚFancyURLopeneré   c       
      K   sê   d|kr|d }|d= nd}d|kr4|d }|d= nd }d|krP|d }|d= nd }|s`|s`|rÄt sltdƒ‚t tj¡}| jtjO  _tj|_|s˜|r¦| 	||¡ n| 
¡  t|dd}t|ƒ}	ntd krØtƒ  a}	nt}	|	 | ||¡S )NÚ	cadefaultFÚcapathÚcafilezSSL support not availableT)ÚcontextÚcheck_hostname)Ú	_have_sslÚ
ValueErrorÚsslr*   ZPROTOCOL_SSLv23ÚoptionsZOP_NO_SSLv2ZCERT_REQUIREDZverify_modeZload_verify_locationsZset_default_verify_pathsÚHTTPSHandlerrB   Ú_openerr   )
ÚurlÚdataÚtimeoutZ_3to2kwargsrK   rL   rM   rN   Zhttps_handlerÚopener© rZ   úP/opt/alt/python37/lib/python3.7/site-packages/future/backports/urllib/request.pyr@   “   s6          
c             C   s   | a d S )N)rU   )rY   rZ   rZ   r[   rA   ­   s    c          
   C   s2  t | ƒ\}}t t| |ƒ¡æ}| ¡ }|dkrB|sBtj |¡|fS |rRt|dƒ}nt	j
dd}|j}t |¡ |† ||f}	d}
d}d}d}d|kr t|d	 ƒ}|r°|||
|ƒ xB| |
¡}|sÂP |t|ƒ7 }| |¡ |d
7 }|r²|||
|ƒ q²W W dQ R X W dQ R X |dkr.||k r.td||f |	ƒ‚|	S )aW  
    Retrieve a URL into a temporary location on disk.

    Requires a URL argument. If a filename is passed, it is used as
    the temporary file location. The reporthook argument should be
    a callable that accepts a block number, a read size, and the
    total file size of the URL target. The data argument should be
    valid URL encoded data.

    If a filename is passed and the URL points to a local resource,
    the result is a copy from local file to new file.

    Returns a tuple containing the path to the newly created
    data file as well as the resulting HTTPMessage object.
    ÚfileÚwbF)Údeletei    éÿÿÿÿr   zcontent-lengthzContent-Lengthr   Nz1retrieval incomplete: got only %i out of %i bytes)r   Ú
contextlibÚclosingr@   ÚinfoÚosÚpathÚnormpathr   ÚtempfileZNamedTemporaryFileÚnameÚ_url_tempfilesÚappendr
   ÚreadÚlenÚwriter   )rV   ÚfilenameÚ
reporthookrW   Zurl_typerd   ÚfpÚheadersÚtfpÚresultÚbsÚsizerj   ÚblocknumÚblockrZ   rZ   r[   rF   ²   sD    


$c           	   C   sH   x0t D ](} yt | ¡ W q tk
r,   Y qX qW t d d …= trDd ad S )N)rh   rc   ÚunlinkÚEnvironmentErrorrU   )Z	temp_filerZ   rZ   r[   rG   ñ   s    


z:\d+$c             C   s<   | j }t|ƒd }|dkr&|  dd¡}t d|d¡}| ¡ S )zˆReturn request-host, as defined by RFC 2965.

    Variation from RFC: returned value is lowercased, for convenient
    comparison.

    r   Ú ÚHost)Úfull_urlr   Ú
get_headerÚ_cut_port_reÚsubÚlower)ÚrequestrV   ÚhostrZ   rZ   r[   Úrequest_host  s    r‚   c               @   s²   e Zd Zdi dddf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„ 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S )*r+   NFc       	      C   s„   t |ƒ| _t| jƒ\| _| _|| _i | _d | _x | ¡ D ]\}}|  ||¡ q8W i | _	|d krft
| ƒ}|| _|| _|| _|  ¡  d S )N)r   r{   r%   ÚfragmentrW   rp   Ú_tunnel_hostÚitemsÚ
add_headerÚunredirected_hdrsr‚   Úorigin_req_hostÚunverifiableÚmethodÚ_parse)	ÚselfrV   rW   rp   rˆ   r‰   rŠ   ÚkeyÚvaluerZ   rZ   r[   Ú__init__  s    
zRequest.__init__c             C   sN   t | jƒ\| _}| jd kr(td| j ƒ‚t|ƒ\| _| _| jrJt| jƒ| _d S )Nzunknown url type: %r)r   r{   ÚtyperQ   r   r   Úselectorr   )rŒ   ÚrestrZ   rZ   r[   r‹   (  s    
zRequest._parsec             C   s&   | j dk	r| j S | jdk	rdS dS dS )z3Return a string indicating the HTTP request method.NÚPOSTÚGET)rŠ   rW   )rŒ   rZ   rZ   r[   Ú
get_method0  s
    

zRequest.get_methodc             C   s    | j rd| j| j f S | jS d S )Nz%s#%s)rƒ   r{   )rŒ   rZ   rZ   r[   Úget_full_url9  s    zRequest.get_full_urlc             C   s   d}t j|tdd || _d S )Nz&Request.add_data method is deprecated.r   )Ú
stacklevel)ÚwarningsÚwarnÚDeprecationWarningrW   )rŒ   rW   ÚmsgrZ   rZ   r[   Úadd_dataA  s    zRequest.add_datac             C   s   d}t j|tdd | jd k	S )Nz&Request.has_data method is deprecated.r   )r—   )r˜   r™   rš   rW   )rŒ   r›   rZ   rZ   r[   Úhas_dataF  s    zRequest.has_datac             C   s   d}t j|tdd | jS )Nz&Request.get_data method is deprecated.r   )r—   )r˜   r™   rš   rW   )rŒ   r›   rZ   rZ   r[   Úget_dataK  s    zRequest.get_datac             C   s   d}t j|tdd | jS )Nz&Request.get_type method is deprecated.r   )r—   )r˜   r™   rš   r   )rŒ   r›   rZ   rZ   r[   Úget_typeP  s    zRequest.get_typec             C   s   d}t j|tdd | jS )Nz&Request.get_host method is deprecated.r   )r—   )r˜   r™   rš   r   )rŒ   r›   rZ   rZ   r[   Úget_hostU  s    zRequest.get_hostc             C   s   d}t j|tdd | jS )Nz*Request.get_selector method is deprecated.r   )r—   )r˜   r™   rš   r‘   )rŒ   r›   rZ   rZ   r[   Úget_selectorZ  s    zRequest.get_selectorc             C   s   d}t j|tdd | jS )Nz-Request.is_unverifiable method is deprecated.r   )r—   )r˜   r™   rš   r‰   )rŒ   r›   rZ   rZ   r[   Úis_unverifiable_  s    zRequest.is_unverifiablec             C   s   d}t j|tdd | jS )Nz1Request.get_origin_req_host method is deprecated.r   )r—   )r˜   r™   rš   rˆ   )rŒ   r›   rZ   rZ   r[   Úget_origin_req_hostd  s    zRequest.get_origin_req_hostc             C   s2   | j dkr| js| j| _n|| _ | j| _|| _d S )NÚhttps)r   r„   r   r{   r‘   )rŒ   r   r   rZ   rZ   r[   Ú	set_proxyk  s
    
zRequest.set_proxyc             C   s   | j | jkS )N)r‘   r{   )rŒ   rZ   rZ   r[   Ú	has_proxys  s    zRequest.has_proxyc             C   s   || j | ¡ < d S )N)rp   Ú
capitalize)rŒ   r   ÚvalrZ   rZ   r[   r†   v  s    zRequest.add_headerc             C   s   || j | ¡ < d S )N)r‡   r§   )rŒ   r   r¨   rZ   rZ   r[   Úadd_unredirected_headerz  s    zRequest.add_unredirected_headerc             C   s   || j kp|| jkS )N)rp   r‡   )rŒ   Úheader_namerZ   rZ   r[   Ú
has_header~  s    
zRequest.has_headerc             C   s   | j  || j ||¡¡S )N)rp   Úgetr‡   )rŒ   rª   ÚdefaultrZ   rZ   r[   r|   ‚  s    zRequest.get_headerc             C   s"   | j  ¡ }| | j¡ t| ¡ ƒS )N)r‡   ÚcopyÚupdaterp   Úlistr…   )rŒ   ÚhdrsrZ   rZ   r[   Úheader_items‡  s    
zRequest.header_items)N)Ú__name__Ú
__module__Ú__qualname__r   r‹   r•   r–   rœ   r   rž   rŸ   r    r¡   r¢   r£   r¥   r¦   r†   r©   r«   r|   r²   rZ   rZ   rZ   r[   r+     s*   	
c               @   sN   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	ejfd
d„Z	ddd„Z
dd„ Zd	S )r,   c             C   s6   dt  }d|fg| _g | _i | _i | _i | _i | _d S )NzPython-urllib/%sz
User-agent)Ú__version__Ú
addheadersÚhandlersÚhandle_openÚhandle_errorÚprocess_responseÚprocess_request)rŒ   Zclient_versionrZ   rZ   r[   r     s    zOpenerDirector.__init__c          	   C   sZ  t |dƒstdt|ƒ ƒ‚d}xt|ƒD ]}|dkr:q*| d¡}|d |… }||d d … }| d¡rÈ| d¡| d }||d d … }yt|ƒ}W n tk
r¬   Y nX | j 	|i ¡}	|	| j|< n>|dkrÜ|}| j
}	n*|d	krð|}| j}	n|d
kr*|}| j}	nq*|	 |g ¡}
|
r&t |
|¡ n
|
 |¡ d}q*W |rVt | j|¡ | | ¡ d S )NÚ
add_parentz%expected BaseHandler instance, got %rF)Úredirect_requestÚdo_openÚ
proxy_openÚ_r   Úerrorr   Úresponser€   T)ÚhasattrÚ	TypeErrorr   ÚdirÚfindÚ
startswithr
   rQ   rº   r¬   r¹   r»   r¼   Ú
setdefaultÚbisectZinsortri   r¸   r½   )rŒ   ÚhandlerZaddedÚmethÚiÚprotocolZ	conditionÚjÚkindÚlookupr¸   rZ   rZ   r[   Úadd_handler˜  sJ    



zOpenerDirector.add_handlerc             C   s   d S )NrZ   )rŒ   rZ   rZ   r[   ÚcloseÇ  s    zOpenerDirector.closec       	      G   s<   |  |d¡}x*|D ]"}t||ƒ}||Ž }|d k	r|S qW d S )NrZ   )r¬   Úgetattr)	rŒ   ÚchainrÐ   Ú	meth_nameÚargsr¸   rË   Úfuncrr   rZ   rZ   r[   Ú_call_chainË  s    

zOpenerDirector._call_chainNc       
      C   s¼   t |tƒr| ¡ }t |tƒr(t||ƒ}n|}|dk	r:||_||_|j}|d }x(| j 	|g ¡D ]}t
||ƒ}||ƒ}q^W |  ||¡}	|d }x*| j 	|g ¡D ]}t
||ƒ}|||	ƒ}	qœW |	S )z‚
        Accept a URL or a Request object

        Python-Future: if the URL is passed as a byte-string, decode it first.
        NZ_requestZ	_response)Ú
isinstancer   Údecoder   r+   rW   rX   r   r¼   r¬   rÔ   Ú_openr»   )
rŒ   ÚfullurlrW   rX   ÚreqrÎ   rÖ   Z	processorrÌ   rÃ   rZ   rZ   r[   r   Ö  s&    



zOpenerDirector.openc             C   sP   |   | jdd|¡}|r|S |j}|   | j||d |¡}|r>|S |   | jdd|¡S )Nr­   Zdefault_openrÜ   ÚunknownÚunknown_open)rÙ   r¹   r   )rŒ   rÞ   rW   rr   rÎ   rZ   rZ   r[   rÜ   ø  s    


zOpenerDirector._openc             G   s~   |dkr,| j d }|d }d| }d}|}n| j }|d }d}|||f| }| j|Ž }|r^|S |rz|dd	f| }| j|Ž S d S )
N)Úhttpr¤   rá   é   zhttp_error_%sr   Z_errorr   r­   Úhttp_error_default)rº   rÙ   )rŒ   Úprotor×   r   rÖ   Zhttp_errZ	orig_argsrr   rZ   rZ   r[   rÂ     s     

zOpenerDirector.error)N)r³   r´   rµ   r   rÒ   rÓ   rÙ   ÚsocketÚ_GLOBAL_DEFAULT_TIMEOUTr   rÜ   rÂ   rZ   rZ   rZ   r[   r,   Œ  s   /"
c              G   sæ   dd„ }t ƒ }ttttttttg}t	t
dƒr6| t¡ tƒ }xL|D ]D}x>| D ]6}||ƒrnt||ƒr‚| |¡ qLt||ƒrL| |¡ qLW qBW x|D ]}| |¡ qW x|D ]}| |ƒ ¡ q¨W x$| D ]}||ƒrÔ|ƒ }| |¡ qÂW |S )a*  Create an opener object from a list of handlers.

    The opener will use several default handlers, including support
    for HTTP, FTP and when applicable HTTPS.

    If any of the handlers passed as arguments are subclasses of the
    default handlers, the default handlers will not be used.
    c             S   s   t | tƒpt| dƒS )NÚ	__bases__)rÚ   r   rÄ   )ÚobjrZ   rZ   r[   Úisclass)  s    zbuild_opener.<locals>.isclassÚHTTPSConnection)r,   r1   r>   r:   r.   r/   r<   r;   r?   rÄ   Úhttp_clientri   rT   ÚsetÚ
issubclassÚaddrÚ   ÚremoverÒ   )r¸   ré   rY   Zdefault_classesÚskipÚklassÚcheckÚhrZ   rZ   r[   rB      s0    	









c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r-   iô  c             C   s
   || _ d S )N)Úparent)rŒ   rô   rZ   rZ   r[   r½   I  s    zBaseHandler.add_parentc             C   s   d S )NrZ   )rŒ   rZ   rZ   r[   rÓ   L  s    zBaseHandler.closec             C   s   t |dƒsdS | j|jk S )NÚhandler_orderT)rÄ   rõ   )rŒ   ÚotherrZ   rZ   r[   Ú__lt__P  s    
zBaseHandler.__lt__N)r³   r´   rµ   rõ   r½   rÓ   r÷   rZ   rZ   rZ   r[   r-   F  s   c               @   s    e Zd ZdZdZdd„ ZeZdS )r?   zProcess HTTP error responses.iè  c             C   sH   |j |j| ¡   }}}d|  kr,dk sDn | j d|||||¡}|S )NéÈ   i,  rá   )Úcoder›   rb   rô   rÂ   )rŒ   r€   rÃ   rù   r›   r±   rZ   rZ   r[   Úhttp_response]  s
    z HTTPErrorProcessor.http_responseN)r³   r´   rµ   Ú__doc__rõ   rú   Úhttps_responserZ   rZ   rZ   r[   r?   Y  s   c               @   s   e Zd Zdd„ ZdS )r.   c             C   s   t |j||||ƒ‚d S )N)r   r{   )rŒ   rÞ   ro   rù   r›   r±   rZ   rZ   r[   rã   k  s    z*HTTPDefaultErrorHandler.http_error_defaultN)r³   r´   rµ   rã   rZ   rZ   rZ   r[   r.   j  s   c               @   s4   e Zd ZdZdZdd„ Zdd„ Ze Z ZZ	dZ
dS )	r/   é   é
   c       	         sx   |  ¡ }|dkr|dks:|dkr(|dks:t|j||||ƒ‚| dd¡}d‰ t‡ fdd	„|j ¡ D ƒƒ}t|||jd
dS )a­  Return a Request or None in response to a redirect.

        This is called by the http_error_30x methods when a
        redirection response is received.  If a redirection should
        take place, return a new Request to allow http_error_30x to
        perform the redirect.  Otherwise, raise HTTPError if no-one
        else should try to handle this url.  Return None if you can't
        but another Handler might.
        )i-  i.  i/  i3  )r”   ZHEAD)i-  i.  i/  r“   ú z%20)zcontent-lengthzcontent-typec             3   s&   | ]\}}|  ¡ ˆ kr||fV  qd S )N)r   )Ú.0ÚkÚv)ÚCONTENT_HEADERSrZ   r[   ú	<genexpr>  s    z7HTTPRedirectHandler.redirect_request.<locals>.<genexpr>T)rp   rˆ   r‰   )	r•   r   r{   Úreplacer   rp   r…   r+   rˆ   )	rŒ   rÞ   ro   rù   r›   rp   ÚnewurlÚmZ
newheadersrZ   )r  r[   r¾   v  s    
z$HTTPRedirectHandler.redirect_requestc       
      C   s2  d|kr|d }nd|kr$|d }nd S t |ƒ}|jdkrRt||d||f ||ƒ‚|jsht|ƒ}d|d< t|ƒ}t|j|ƒ}|  ||||||¡}|d krœd S t	|dƒrì|j
 }	|_
|	 |d¡| jksÒt|	ƒ| jkrüt|j|| j| ||ƒ‚ni  }	 |_
|_
|	 |d¡d	 |	|< | ¡  | ¡  | jj||jd
S )NÚlocationÚuri)rá   r¤   Úftpry   z+%s - Redirection to url '%s' is not allowedú/râ   Úredirect_dictr   r   )rX   )r   Úschemer   rd   r°   r'   r   r{   r¾   rÄ   r  r¬   Úmax_repeatsrk   Úmax_redirectionsÚinf_msgrj   rÓ   rô   r   rX   )
rŒ   rÞ   ro   rù   r›   rp   r  ÚurlpartsÚnewZvisitedrZ   rZ   r[   Úhttp_error_302˜  s<    




z"HTTPRedirectHandler.http_error_302zoThe HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
N)r³   r´   rµ   r  r  r¾   r  Úhttp_error_301Úhttp_error_303Úhttp_error_307r  rZ   rZ   rZ   r[   r/   n  s   "7c       	      C   s   t | ƒ\}}| d¡s d}| }n:| d¡s6td|  ƒ‚| dd¡}|dkrNd}|d|… }t|ƒ\}}|dk	r|t|ƒ\}}nd }}||||fS )a3  Return (scheme, user, password, host/port) given a URL or an authority.

    If a URL is supplied, it must have an authority (host:port) component.
    According to RFC 3986, having an authority component means the URL must
    have two slashes after the scheme:

    >>> _parse_proxy('file:/ftp.example.com/')
    Traceback (most recent call last):
    ValueError: proxy URL with no authority: 'file:/ftp.example.com/'

    The first three items of the returned tuple may be None.

    Examples of authority parsing:

    >>> _parse_proxy('proxy.example.com')
    (None, None, None, 'proxy.example.com')
    >>> _parse_proxy('proxy.example.com:3128')
    (None, None, None, 'proxy.example.com:3128')

    The authority component may optionally include userinfo (assumed to be
    username:password):

    >>> _parse_proxy('joe:password@proxy.example.com')
    (None, 'joe', 'password', 'proxy.example.com')
    >>> _parse_proxy('joe:password@proxy.example.com:3128')
    (None, 'joe', 'password', 'proxy.example.com:3128')

    Same examples, but with URLs instead:

    >>> _parse_proxy('http://proxy.example.com/')
    ('http', None, None, 'proxy.example.com')
    >>> _parse_proxy('http://proxy.example.com:3128/')
    ('http', None, None, 'proxy.example.com:3128')
    >>> _parse_proxy('http://joe:password@proxy.example.com/')
    ('http', 'joe', 'password', 'proxy.example.com')
    >>> _parse_proxy('http://joe:password@proxy.example.com:3128')
    ('http', 'joe', 'password', 'proxy.example.com:3128')

    Everything after the authority is ignored:

    >>> _parse_proxy('ftp://joe:password@proxy.example.com/rubbish:3128')
    ('ftp', 'joe', 'password', 'proxy.example.com')

    Test for no trailing '/' case:

    >>> _parse_proxy('http://joe:password@proxy.example.com')
    ('http', 'joe', 'password', 'proxy.example.com')

    r  Nz//zproxy URL with no authority: %rrâ   r_   )r   rÈ   rQ   rÇ   r    r!   )	Úproxyr  Zr_schemeÚ	authorityÚendZuserinfoÚhostportÚuserÚpasswordrZ   rZ   r[   Ú_parse_proxyÖ  s    2

r  c               @   s"   e Zd ZdZddd„Zdd„ ZdS )r1   éd   Nc             C   s^   |d krt ƒ }t|dƒs tdƒ‚|| _x2| ¡ D ]&\}}t| d| ||| jfdd„ƒ q0W d S )NÚkeyszproxies must be a mappingz%s_openc             S   s   || ||ƒS )NrZ   )Úrr  r   rÌ   rZ   rZ   r[   Ú<lambda>)  s    z'ProxyHandler.__init__.<locals>.<lambda>)rE   rÄ   ÚAssertionErrorÚproxiesr…   ÚsetattrrÀ   )rŒ   r#  r   rV   rZ   rZ   r[   r   "  s    
zProxyHandler.__init__c             C   s´   |j }t|ƒ\}}}}|d kr"|}|jr6t|jƒr6d S |rv|rvdt|ƒt|ƒf }	t |	 ¡ ¡ d¡}
| 	dd|
 ¡ t|ƒ}| 
||¡ ||ksš|dkržd S | jj||jdS d S )Nz%s:%sÚasciizProxy-authorizationzBasic r¤   )rX   )r   r  r   Úproxy_bypassr   Úbase64Ú	b64encodeÚencoderÛ   r†   r¥   rô   r   rX   )rŒ   rÞ   r  r   Z	orig_typeZ
proxy_typer  r  r  Z	user_passZcredsrZ   rZ   r[   rÀ   ,  s     zProxyHandler.proxy_open)N)r³   r´   rµ   rõ   r   rÀ   rZ   rZ   rZ   r[   r1     s   

c               @   s6   e Zd Zdd„ Zdd„ Zdd„ Zddd	„Zd
d„ ZdS )r2   c             C   s
   i | _ d S )N)Úpasswd)rŒ   rZ   rZ   r[   r   J  s    zHTTPPasswordMgr.__init__c                s`   t |tƒr|g}|ˆjkr$i ˆj|< x6dD ].‰ t‡ ‡fdd„|D ƒƒ}||fˆj| |< q*W d S )N)TFc                s   g | ]}ˆ  |ˆ ¡‘qS rZ   )Ú
reduce_uri)r   Úu)Údefault_portrŒ   rZ   r[   ú
<listcomp>U  s    z0HTTPPasswordMgr.add_password.<locals>.<listcomp>)rÚ   r   r*  Útuple)rŒ   Úrealmr	  r  r*  Zreduced_urirZ   )r-  rŒ   r[   Úadd_passwordM  s    



zHTTPPasswordMgr.add_passwordc       	      C   s`   | j  |i ¡}xLdD ]D}|  ||¡}x2| ¡ D ]&\}}x|D ]}|  ||¡r<|S q<W q.W qW dS )N)TF)NN)r*  r¬   r+  r…   Ú	is_suburi)	rŒ   r0  ÚauthuriZdomainsr-  Zreduced_authuriZurisZauthinfor	  rZ   rZ   r[   Úfind_user_passwordX  s    

z"HTTPPasswordMgr.find_user_passwordTc       
      C   s†   t |ƒ}|d r.|d }|d }|d p*d}nd}|}d}t|ƒ\}}|r~|dkr~|dk	r~dddœ |¡}	|	dk	r~d	||	f }||fS )
z@Accept authority or URI and extract only the authority and path.r   r   râ   r  NéP   i»  )rá   r¤   z%s:%d)r   r   r¬   )
rŒ   r	  r-  Úpartsr  r  rd   r   ÚportZdportrZ   rZ   r[   r+  b  s     zHTTPPasswordMgr.reduce_uric             C   sR   ||krdS |d |d kr dS t  |d |d f¡}t|ƒt|d ƒkrNdS dS )zcCheck if test is below base in a URI tree

        Both args must be URIs in reduced form.
        Tr   Fr   )Ú	posixpathÚcommonprefixrk   )rŒ   ÚbaseÚtestÚcommonrZ   rZ   r[   r2  y  s    zHTTPPasswordMgr.is_suburiN)T)r³   r´   rµ   r   r1  r4  r+  r2  rZ   rZ   rZ   r[   r2   H  s
   

c               @   s   e Zd Zdd„ ZdS )r3   c             C   s0   t  | ||¡\}}|d k	r"||fS t  | d |¡S )N)r2   r4  )rŒ   r0  r3  r  r  rZ   rZ   r[   r4  Š  s
    
z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r³   r´   rµ   r4  rZ   rZ   rZ   r[   r3   ˆ  s   c               @   s<   e Zd Ze dej¡Zddd„Zdd„ Zdd„ Z	d	d
„ Z
dS )r4   z1(?:.*,)*[ 	]*([^ 	]+)[ 	]+realm=(["']?)([^"']*)\2Nc             C   s(   |d krt ƒ }|| _| jj| _d| _d S )Nr   )r2   r*  r1  Úretried)rŒ   Zpassword_mgrrZ   rZ   r[   r      s
    
z!AbstractBasicAuthHandler.__init__c             C   s
   d| _ d S )Nr   )r=  )rŒ   rZ   rZ   r[   Úreset_retry_count§  s    z*AbstractBasicAuthHandler.reset_retry_countc       
      C   sÎ   |  |d ¡}| jdkr,t| ¡ dd|d ƒ‚n|  jd7  _|rÊ| ¡ d }| ¡ dkrdtd| ƒ‚nftj 	|¡}|rÊ| 
¡ \}}}|dkr˜t d	td
¡ | ¡ dkrÊ|  |||¡}	|	rÆ|	jdkrÆd| _|	S d S )Né   i‘  zbasic auth failedr   r   ÚbasiczDAbstractBasicAuthHandler does not support the following scheme: '%s')ú"ú'zBasic Auth Realm was unquotedrâ   )r¬   r=  r   r–   Úsplitr   rQ   r4   ÚrxÚsearchÚgroupsr˜   r™   ÚUserWarningÚretry_http_basic_authrù   )
rŒ   Úauthreqr   rÞ   rp   r  Úmor   r0  rÃ   rZ   rZ   r[   Úhttp_error_auth_reqedª  s*    


z.AbstractBasicAuthHandler.http_error_auth_reqedc             C   s~   | j  ||¡\}}|d k	rvd||f }dt | ¡ ¡ d¡ }|j | jd ¡|krVd S | 	| j|¡ | j
j||jdS d S d S )Nz%s:%szBasic r%  )rX   )r*  r4  r'  r(  r)  rÛ   rp   r¬   Úauth_headerr©   rô   r   rX   )rŒ   r   rÞ   r0  r  ÚpwÚrawÚauthrZ   rZ   r[   rH  Ê  s    z.AbstractBasicAuthHandler.retry_http_basic_auth)N)r³   r´   rµ   ÚreÚcompileÚIrD  r   r>  rK  rH  rZ   rZ   rZ   r[   r4   ’  s   
 c               @   s   e Zd ZdZdd„ ZdS )r5   ÚAuthorizationc             C   s"   |j }|  d|||¡}|  ¡  |S )Nzwww-authenticate)r{   rK  r>  )rŒ   rÞ   ro   rù   r›   rp   rV   rÃ   rZ   rZ   r[   Úhttp_error_401Û  s
    
z#HTTPBasicAuthHandler.http_error_401N)r³   r´   rµ   rL  rT  rZ   rZ   rZ   r[   r5   ×  s   c               @   s   e Zd ZdZdd„ ZdS )r6   zProxy-authorizationc             C   s"   |j }|  d|||¡}|  ¡  |S )Nzproxy-authenticate)r   rK  r>  )rŒ   rÞ   ro   rù   r›   rp   r  rÃ   rZ   rZ   r[   Úhttp_error_407ç  s
    
z$ProxyBasicAuthHandler.http_error_407N)r³   r´   rµ   rL  rU  rZ   rZ   rZ   r[   r6   ã  s   c               @   sN   e Zd Z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S )r7   Nc             C   s4   |d krt ƒ }|| _| jj| _d| _d| _d | _d S )Nr   )r2   r*  r1  r=  Únonce_countÚ
last_nonce)rŒ   r*  rZ   rZ   r[   r     s    
z"AbstractDigestAuthHandler.__init__c             C   s
   d| _ d S )Nr   )r=  )rŒ   rZ   rZ   r[   r>    s    z+AbstractDigestAuthHandler.reset_retry_countc             C   s|   |  |d ¡}| jdkr*t|jdd|d ƒ‚n|  jd7  _|rx| ¡ d }| ¡ dkr`|  ||¡S | ¡ dkrxtd| ƒ‚d S )	Nr?  i‘  zdigest auth failedr   r   Zdigestr@  zEAbstractDigestAuthHandler does not support the following scheme: '%s')r¬   r=  r   r{   rC  r   Úretry_http_digest_authrQ   )rŒ   rL  r   rÞ   rp   rI  r  rZ   rZ   r[   rK    s    


z/AbstractDigestAuthHandler.http_error_auth_reqedc             C   sz   |  dd¡\}}ttd t|ƒƒƒ}|  ||¡}|rvd| }|j | jd ¡|krRd S | | j|¡ | j	j
||jd}|S d S )Nrÿ   r   z	Digest %s)rX   )rC  Úparse_keqv_listr   Úparse_http_listÚget_authorizationrp   r¬   rL  r©   rô   r   rX   )rŒ   rÞ   rO  ÚtokenZ	challengeÚchalZauth_valZresprZ   rZ   r[   rX  "  s    z0AbstractDigestAuthHandler.retry_http_digest_authc             C   s@   d| j |t ¡ f }| d¡tdƒ }t |¡ ¡ }|d d… S )Nz	%s:%s:%s:r%  é   é   )rV  ÚtimeÚctimer)  Ú_randombytesÚhashlibÚsha1Ú	hexdigest)rŒ   ÚnonceÚsÚbÚdigrZ   rZ   r[   Ú
get_cnonce.  s    z$AbstractDigestAuthHandler.get_cnoncec             C   s¾  y6|d }|d }|  d¡}|  dd¡}|  dd ¡}W n tk
rJ   d S X |  |¡\}}	|d krfd S | j ||j¡\}
}|
d kr†d S |jd k	r |  |j|¡}nd }d|
||f }d| ¡ |j	f }|d	kr.|| j
krè|  jd
7  _nd
| _|| _
d| j }|  |¡}d||||||ƒf }|	||ƒ|ƒ}n2|d krT|	||ƒd|||ƒf ƒ}ntd| ƒ‚d|
|||j	|f }|r†|d| 7 }|r˜|d| 7 }|d| 7 }|rº|d||f 7 }|S )Nr0  rf  ÚqopÚ	algorithmÚMD5Úopaquez%s:%s:%sz%s:%srO  r   z%08xz%s:%s:%s:%s:%szqop '%s' is not supported.z>username="%s", realm="%s", nonce="%s", uri="%s", response="%s"z, opaque="%s"z, digest="%s"z, algorithm="%s"z, qop=auth, nc=%s, cnonce="%s")r¬   ÚKeyErrorÚget_algorithm_implsr*  r4  r{   rW   Úget_entity_digestr•   r‘   rW  rV  rj  r   )rŒ   rÞ   r]  r0  rf  rk  rl  rn  ÚHÚKDr  rM  ZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr:  rZ   rZ   r[   r[  9  sV    








z+AbstractDigestAuthHandler.get_authorizationc                s6   |dkrdd„ ‰ n|dkr"dd„ ‰ ‡ fdd„}ˆ |fS )Nrm  c             S   s   t  |  d¡¡ ¡ S )Nr%  )rc  Zmd5r)  re  )ÚxrZ   rZ   r[   r!  x  ó    z?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAc             S   s   t  |  d¡¡ ¡ S )Nr%  )rc  rd  r)  re  )rt  rZ   rZ   r[   r!  z  ru  c                s   ˆ d| |f ƒS )Nz%s:%srZ   )rg  Úd)rr  rZ   r[   r!  |  ru  rZ   )rŒ   rl  rs  rZ   )rr  r[   rp  u  s    
z-AbstractDigestAuthHandler.get_algorithm_implsc             C   s   d S )NrZ   )rŒ   rW   r]  rZ   rZ   r[   rq    s    z+AbstractDigestAuthHandler.get_entity_digest)N)r³   r´   rµ   r   r>  rK  rX  rj  r[  rp  rq  rZ   rZ   rZ   r[   r7   ÷  s   
	<
c               @   s    e Zd ZdZdZdZdd„ ZdS )r8   z¨An authentication protocol defined by RFC 2069

    Digest authentication improves on basic authentication because it
    does not transmit passwords in the clear.
    rS  iê  c             C   s*   t |jƒd }|  d|||¡}|  ¡  |S )Nr   zwww-authenticate)r   r{   rK  r>  )rŒ   rÞ   ro   rù   r›   rp   r   ÚretryrZ   rZ   r[   rT  Ž  s
    
z$HTTPDigestAuthHandler.http_error_401N)r³   r´   rµ   rû   rL  rõ   rT  rZ   rZ   rZ   r[   r8   „  s   c               @   s   e Zd ZdZdZdd„ ZdS )r9   zProxy-Authorizationiê  c             C   s"   |j }|  d|||¡}|  ¡  |S )Nzproxy-authenticate)r   rK  r>  )rŒ   rÞ   ro   rù   r›   rp   r   rw  rZ   rZ   r[   rU  ›  s
    
z%ProxyDigestAuthHandler.http_error_407N)r³   r´   rµ   rL  rõ   rU  rZ   rZ   rZ   r[   r9   –  s   c               @   s.   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
S )ÚAbstractHTTPHandlerr   c             C   s
   || _ d S )N)Ú_debuglevel)rŒ   Ú
debuglevelrZ   rZ   r[   r   ¤  s    zAbstractHTTPHandler.__init__c             C   s
   || _ d S )N)ry  )rŒ   ÚlevelrZ   rZ   r[   Úset_http_debuglevel§  s    z'AbstractHTTPHandler.set_http_debuglevelc             C   sX  |j }|stdƒ‚|jd k	rÜ|j}t|tƒr8d}t|ƒ‚| d¡sN| dd¡ | d¡sÜd }y:tr~t|t	j	ƒr~t
|ƒ|j }nt|ƒ}t
|ƒ|j }W n4 tk
rÊ   t|tjƒrÆtdt|ƒ|f ƒ‚Y nX | dd| ¡ |}| ¡ rt|jƒ\}}	t|	ƒ\}}
| d¡s| d|¡ x6| jjD ]*\}}| ¡ }| |¡s&| ||¡ q&W |S )	Nzno host givenzLPOST data should be bytes or an iterable of bytes. It cannot be of type str.zContent-typez!application/x-www-form-urlencodedzContent-lengthzBContent-Length should be specified for iterable data of type %r %rz%drz   )r   r   rW   rÚ   r   rÅ   r«   r©   r   Úarrayrk   ÚitemsizeÚ
memoryviewÚcollectionsÚIterablerQ   r   r¦   r   r‘   r   rô   r·   r§   )rŒ   r€   r   rW   r›   rt   ZmvZsel_hostr  ZselZsel_pathrg   rŽ   rZ   rZ   r[   Údo_request_ª  sJ    




zAbstractHTTPHandler.do_request_c       
   
      s2  |j }|stdƒ‚||fd|ji|—Ž}t|jƒ‰ ˆ  t‡ fdd„|j ¡ D ƒƒ¡ dˆ d< tdd„ ˆ  ¡ D ƒƒ‰ |jrªi }d}|ˆ kršˆ | ||< ˆ |= |j	|j|d	 y| 
| ¡ |j|jˆ ¡ W n4 tjk
rú } z| ¡  t|ƒ‚W d
d
}~X Y n"X | ¡ }	|jr|j ¡  d
|_| ¡ |	_|	j|	_|	S )z•Return an HTTPResponse object for the request, using http_class.

        http_class must implement the HTTPConnection API from http.client.
        zno host givenrX   c             3   s"   | ]\}}|ˆ kr||fV  qd S )NrZ   )r   r  r  )rp   rZ   r[   r  ê  s    z.AbstractHTTPHandler.do_open.<locals>.<genexpr>rÓ   Ú
Connectionc             s   s   | ]\}}|  ¡ |fV  qd S )N)Útitle)r   rg   r¨   rZ   rZ   r[   r  ÷  s    zProxy-Authorization)rp   N)r   r   rX   r   r‡   r¯   rp   r…   r„   Z
set_tunnelr€   r•   r‘   rW   rå   rÂ   rÓ   ÚgetresponseZsockr–   rV   Úreasonr›   )
rŒ   Z
http_classrÞ   Zhttp_conn_argsr   ró   Ztunnel_headersZproxy_auth_hdrÚerrr   rZ   )rp   r[   r¿   Ý  s6    
"

zAbstractHTTPHandler.do_openN)r   )r³   r´   rµ   r   r|  r‚  r¿   rZ   rZ   rZ   r[   rx  ¢  s   
3rx  c               @   s   e Zd Zdd„ ZejZdS )r:   c             C   s   |   tj|¡S )N)r¿   rë   ÚHTTPConnection)rŒ   rÞ   rZ   rZ   r[   Ú	http_open  s    zHTTPHandler.http_openN)r³   r´   rµ   r‰  rx  r‚  Úhttp_requestrZ   rZ   rZ   r[   r:     s   rê   c               @   s$   e Zd Zddd„Zdd„ ZejZdS )rT   r   Nc             C   s   t  | |¡ || _|| _d S )N)rx  r   Ú_contextÚ_check_hostname)rŒ   rz  rN   rO   rZ   rZ   r[   r   '  s    zHTTPSHandler.__init__c             C   s   | j tj|| j| jdS )N)rN   rO   )r¿   rë   rê   r‹  rŒ  )rŒ   rÞ   rZ   rZ   r[   Ú
https_open,  s    
zHTTPSHandler.https_open)r   NN)r³   r´   rµ   r   r  rx  r‚  Úhttps_requestrZ   rZ   rZ   r[   rT   %  s   
rT   c               @   s.   e Zd Zddd„Zdd„ Zdd„ ZeZeZdS )	r0   Nc             C   s2   dd l m  m  m} |d kr(| ¡ }|| _d S )Nr   )Zfuture.backports.http.cookiejarÚ	backportsrá   Ú	cookiejarZ	CookieJar)rŒ   r  Zhttp_cookiejarrZ   rZ   r[   r   5  s    zHTTPCookieProcessor.__init__c             C   s   | j  |¡ |S )N)r  Zadd_cookie_header)rŒ   r€   rZ   rZ   r[   rŠ  ;  s    z HTTPCookieProcessor.http_requestc             C   s   | j  ||¡ |S )N)r  Zextract_cookies)rŒ   r€   rÃ   rZ   rZ   r[   rú   ?  s    z!HTTPCookieProcessor.http_response)N)r³   r´   rµ   r   rŠ  rú   rŽ  rü   rZ   rZ   rZ   r[   r0   4  s
   
c               @   s   e Zd Zdd„ ZdS )r>   c             C   s   |j }td| ƒ‚d S )Nzunknown url type: %s)r   r   )rŒ   rÞ   r   rZ   rZ   r[   rà   G  s    zUnknownHandler.unknown_openN)r³   r´   rµ   rà   rZ   rZ   rZ   r[   r>   F  s   c             C   sR   i }xH| D ]@}|  dd¡\}}|d dkrB|d dkrB|dd… }|||< q
W |S )z>Parse list of key=value strings where keys are not duplicated.ú=r   r   rA  r_   )rC  )ÚlZparsedZeltr  r  rZ   rZ   r[   rY  K  s    
rY  c             C   s¢   g }d}d }}xt| D ]l}|r,||7 }d}q|rV|dkr@d}qn|dkrLd}||7 }q|dkrn|  |¡ d}q|dkrzd}||7 }qW |r”|  |¡ dd„ |D ƒS )	ap  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.  Neither commas nor quotes count if they are escaped.
    Only double-quotes count, not single-quotes.
    ry   Fú\TrA  ú,c             S   s   g | ]}|  ¡ ‘qS rZ   )Ústrip)r   ÚpartrZ   rZ   r[   r.  ~  s    z#parse_http_list.<locals>.<listcomp>)ri   )rg  Úresr–  Úescaper   ZcurrZ   rZ   r[   rZ  U  s4    	


rZ  c               @   s(   e Zd Zdd„ ZdZdd„ Zdd„ ZdS )r;   c             C   s\   |j }|d d… dkrN|dd… dkrN|jrN|jdkrN|j|  ¡ k	rXtdƒ‚n
|  |¡S d S )Nrâ   z//rJ   r  Ú	localhostz-file:// scheme is supported only on localhost)r‘   r   Ú	get_namesr   Úopen_local_file)rŒ   rÞ   rV   rZ   rZ   r[   Ú	file_open‚  s    &

zFileHandler.file_openNc             C   s`   t jd krZy*tt d¡d t t ¡ ¡d  ƒt _W n$ tjk
rX   t d¡ft _Y nX t jS )Nr™  râ   )r;   Únamesr/  rå   Úgethostbyname_exÚgethostnameÚgaierrorÚgethostbyname)rŒ   rZ   rZ   r[   rš    s    
zFileHandler.get_namesc          
   C   s
  dd l m  m  m} dd l}|j}|j}t|ƒ}yžt 	|¡}|j
}|j|jdd}	| |¡d }
t d|
ppd||	f ¡}|rŒt|ƒ\}}|r¤|sÐt|ƒ|  ¡ krÐ|r¶d| | }nd| }tt|dƒ||ƒS W n* tk
rü } zt|ƒ‚W d d }~X Y nX tdƒ‚d S )	Nr   T)Úusegmtz6Content-type: %s
Content-length: %d
Last-modified: %s
z
text/plainzfile://Úrbzfile not on local host)Úfuture.backports.email.utilsr  r   ÚutilsÚ	mimetypesr   r‘   rD   rc   ÚstatÚst_sizeÚ
formatdateÚst_mtimeÚ
guess_typeÚmessage_from_stringr   Ú_safe_gethostbynamerš  r(   r   ÚOSErrorr   )rŒ   rÞ   Úemail_utilsr¦  r   rm   Z	localfileÚstatsrt   ÚmodifiedÚmtyperp   r7  ZorigurlÚexprZ   rZ   r[   r›  ˜  s0    
zFileHandler.open_local_file)r³   r´   rµ   rœ  r  rš  r›  rZ   rZ   rZ   r[   r;   €  s   
c             C   s&   y
t  | ¡S  t jk
r    d S X d S )N)rå   r¡  r   )r   rZ   rZ   r[   r­  ´  s    
r­  c               @   s   e Zd Zdd„ Zdd„ ZdS )r<   c          
   C   s$  dd l }dd l}|j}|s"tdƒ‚t|ƒ\}}|d kr>|j}nt|ƒ}t|ƒ\}}|rdt|ƒ\}}nd }t	|ƒ}|pvd}|p~d}yt
 |¡}W n, t
jk
rº } zt|ƒ‚W d d }~X Y nX t|jƒ\}	}
|	 d¡}ttt	|ƒƒ}|d d… |d  }}|r|d s|dd … }yÐ|  ||||||j¡}|r8dp:d}x:|
D ]2}t|ƒ\}}| ¡ d	krB|d
krB| ¡ }qBW | ||¡\}}d}| |j¡d }|r®|d| 7 }|d k	rÎ|dkrÎ|d| 7 }t |¡}t|||jƒS  |jk
r } ztd| ƒ}t|ƒ W d d }~X Y nX d S )Nr   zftp error: no host givenry   r  r_   r   rR  ÚDr   )ÚaÚArÍ   rR  rv  r´  zContent-type: %s
zContent-length: %d
zftp error: %r)Úftplibr¦  r   r   r   ÚFTP_PORTr
   r    r!   r   rå   r¡  rÂ   r"   r‘   rC  r°   r   Úconnect_ftprX   r$   r   ÚupperÚretrfiler«  r{   r   r¬  r(   Ú
all_errorsr   )rŒ   rÞ   r·  r¦  r   r7  r  r*  r›   rd   ÚattrsÚdirsr\   Úfwr   ÚattrrŽ   ro   Úretrlenrp   r²  r³  ÚexcrZ   rZ   r[   Úftp_open»  s\    



zFTPHandler.ftp_openc          	   C   s   t ||||||ddS )NF)Ú
persistent)Ú
ftpwrapper)rŒ   r  r*  r   r7  r¾  rX   rZ   rZ   r[   r¹  ð  s    zFTPHandler.connect_ftpN)r³   r´   rµ   rÃ  r¹  rZ   rZ   rZ   r[   r<   º  s   5c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )r=   c             C   s"   i | _ i | _d| _d| _d| _d S )Nr   é<   r_  )ÚcacherX   ÚsoonestÚdelayÚ	max_conns)rŒ   rZ   rZ   r[   r   ÷  s
    zCacheFTPHandler.__init__c             C   s
   || _ d S )N)rÉ  )rŒ   ÚtrZ   rZ   r[   Ú
setTimeoutþ  s    zCacheFTPHandler.setTimeoutc             C   s
   || _ d S )N)rÊ  )rŒ   r  rZ   rZ   r[   ÚsetMaxConns  s    zCacheFTPHandler.setMaxConnsc             C   sr   |||d  |¡|f}|| jkr4t ¡ | j | j|< n,t||||||ƒ| j|< t ¡ | j | j|< |  ¡  | j| S )Nr  )ÚjoinrÇ  r`  rÉ  rX   rÅ  Úcheck_cache)rŒ   r  r*  r   r7  r¾  rX   r   rZ   rZ   r[   r¹    s    

zCacheFTPHandler.connect_ftpc             C   sÈ   t   ¡ }| j|krTx@t| j ¡ ƒD ].\}}||k r"| j|  ¡  | j|= | j|= q"W tt| j ¡ ƒƒ| _t	| jƒ| j
krÄx6t| j ¡ ƒD ]$\}}|| jkrˆ| j|= | j|= P qˆW tt| j ¡ ƒƒ| _d S )N)r`  rÈ  r°   rX   r…   rÇ  rÓ   ÚminÚvaluesrk   rÊ  )rŒ   rË  r  r  rZ   rZ   r[   rÏ    s    

zCacheFTPHandler.check_cachec             C   s4   x| j  ¡ D ]}| ¡  qW | j  ¡  | j ¡  d S )N)rÇ  rÑ  rÓ   ÚclearrX   )rŒ   ÚconnrZ   rZ   r[   Úclear_cache#  s    
zCacheFTPHandler.clear_cacheN)	r³   r´   rµ   r   rÌ  rÍ  r¹  rÏ  rÔ  rZ   rZ   rZ   r[   r=   ô  s   rþ   Únt)rD   rC   c             C   s   t | ƒS )zOS-specific conversion from a relative URL of the 'file' scheme
        to a file system path; not recommended for general use.)r   )ÚpathnamerZ   rZ   r[   rD   2  s    c             C   s   t | ƒS )zOS-specific conversion from a file system path to a relative URL
        of the 'file' scheme; not recommended for general use.)r   )rÖ  rZ   rZ   r[   rC   7  s    c               @   sÊ   e Zd ZdZdZde Zd*dd„Zdd„ Zdd	„ Z	d
d„ Z
dd„ Zd+dd„Zd,dd„Zd-dd„Zd.dd„Zdd„ Zd/dd„Zd0dd„Zdd„ Zer¤dd„ Zd1d d!„Zd"d#„ Zd$d%„ Zd&d'„ Zd2d(d)„ZdS )3rH   a,  Class to open URLs.
    This is a class rather than just a subroutine because we may need
    more than one set of global protocol-specific options.
    Note -- this is a base class for those who don't want the
    automatic handling of errors type 302 (relocated) and 401
    (authorization needed).NzPython-urllib/%sc             K   sŠ   dd| j ji }tj|tdd |d kr.tƒ }t|dƒs@tdƒ‚|| _| 	d¡| _
| 	d¡| _d	| jfg| _g | _tj| _d | _t| _d S )
NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methodsÚclassrJ   )r—   r  zproxies must be a mappingÚkey_fileÚ	cert_filez
User-Agent)Ú	__class__r³   r˜   r™   rš   rE   rÄ   r"  r#  r¬   rØ  rÙ  Úversionr·   Ú_URLopener__tempfilesrc   rw   Ú_URLopener__unlinkÚ	tempcacheÚftpcache)rŒ   r#  Zx509r›   rZ   rZ   r[   r   Q  s    zURLopener.__init__c             C   s   |   ¡  d S )N)rÓ   )rŒ   rZ   rZ   r[   Ú__del__k  s    zURLopener.__del__c             C   s   |   ¡  d S )N)Úcleanup)rŒ   rZ   rZ   r[   rÓ   n  s    zURLopener.closec          	   C   sZ   | j rFx2| j D ](}y|  |¡ W q tk
r4   Y qX qW | j d d …= | jrV| j ¡  d S )N)rÜ  rÝ  r®  rÞ  rÒ  )rŒ   r\   rZ   rZ   r[   rá  q  s    
zURLopener.cleanupc             G   s   | j  |¡ dS )zdAdd a header to be used by the HTTP interface only
        e.g. u.addheader('Accept', 'sound/basic')N)r·   ri   )rŒ   r×   rZ   rZ   r[   Ú	addheader  s    zURLopener.addheaderc          
   C   sV  t t|ƒƒ}t|dd}| jrL|| jkrL| j| \}}t|dƒ}t|||ƒS t|ƒ\}}|s`d}|| jkr–| j| }t|ƒ\}}	t|	ƒ\}
}|
|f}nd}d| }|| _	| 
dd¡}t| |ƒsÜ|rÐ|  |||¡S |  ||¡S y*|dkrôt| |ƒ|ƒS t| |ƒ||ƒS W nJ tk
r   ‚ Y n4 tjk
rP } zttd	|ƒƒ W dd}~X Y nX dS )
z6Use URLopener().open(file) instead of open(file, 'r').z%/:=&?~#+!$,;'@()*[]|)Úsafer£  r\   NZopen_ú-rÁ   zsocket error)r   r&   r   rÞ  r   r(   r   r#  r   r   r  rÄ   Úopen_unknown_proxyÚopen_unknownrÔ   r   rå   rÂ   r   ÚIOError)rŒ   rÝ   rW   rm   rp   ro   ÚurltyperV   r  Ú	proxyhostr   r‘   rg   r›   rZ   rZ   r[   r   …  s<    




zURLopener.openc             C   s   t |ƒ\}}tdd|ƒ‚dS )z/Overridable interface to open unknown URL type.z	url errorzunknown url typeN)r   rç  )rŒ   rÝ   rW   r   rV   rZ   rZ   r[   ræ  ©  s    zURLopener.open_unknownc             C   s    t |ƒ\}}tdd| |ƒ‚dS )z/Overridable interface to open unknown URL type.z	url errorzinvalid proxy for %sN)r   rç  )rŒ   r  rÝ   rW   r   rV   rZ   rZ   r[   rå  ®  s    zURLopener.open_unknown_proxyc          
   C   s$  t t|ƒƒ}| jr&|| jkr&| j| S t|ƒ\}}|dkr˜|rF|dkr˜y.|  |¡}| ¡ }| ¡  tt|ƒd ƒ|fS  t	k
r– }	 zW dd}	~	X Y nX |  
||¡}zH| ¡ }
|rÀt
|dƒ}n|ddl}t|ƒ\}}t|pÜdƒ\}}t|pìdƒ\}}t| pþdƒ\}}tj |¡d }| |¡\}}| j |¡ t |d¡}z¤||
f}| jdk	r\|| j|< d}d}d}d}d	|
kr‚t|
d
 ƒ}|r”||||ƒ xH| |¡}|s¨P |t|ƒ7 }| |¡ |d7 }|r–||||ƒ q–W W d| ¡  X W d| ¡  X |dkr ||k r td||f |ƒ‚|S )ztretrieve(url) returns (filename, headers) for a local object
        or (tempfilename, headers) for a remote object.Nr\   r   r]   r   ry   i    r_   zcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytes)r   r&   rÞ  r   r›  rb   rÓ   rD   r   rç  r   rf   r#   r"   rc   rd   ÚsplitextÚmkstemprÜ  ri   Úfdopenr
   rj   rk   rl   r   )rŒ   rV   rm   rn   rW   r   Zurl1ro   r±   r›   rp   rq   rf   Zgarbagerd   ÚsuffixÚfdrr   rs   rt   rj   ru   rv   rZ   rZ   r[   Úretrieve´  sl    






zURLopener.retrievec             C   s&  d}d}t |tƒr<t|ƒ\}}|r6t|ƒ\}}t|ƒ}|}nt|\}}t|ƒ\}}t|ƒ\}	}
|
}d}|	 ¡ dkrvd}n:t|
ƒ\}}
|r’t|ƒ\}}|r¤d|	||
f }t|ƒr°|}|s¾tddƒ‚|ràt|ƒ}t	 
| ¡ ¡ d¡}nd}|rt|ƒ}t	 
| ¡ ¡ d¡}nd}||ƒ}i }|r*d| |d< |r<d| |d	< |rJ||d
< d|d< x| jD ]\}}|||< qZW |dk	r”d|d< | d|||¡ n|jd||d y| ¡ }W n  tjk
rÐ   tdƒ‚Y nX d|j  krìdk rn nt||jd| |jƒS |  ||j|j|j|j|¡S dS )a‘  Make an HTTP connection using connection_class.

        This is an internal method that should be called from
        open_http() or open_https().

        Arguments:
        - connection_factory should take a host name and return an
          HTTPConnection instance.
        - url is the url to retrieval or a host, relative-path pair.
        - data is payload for a POST request or None.
        Nrá   z	%s://%s%sz
http errorzno host givenr%  zBasic %szProxy-AuthorizationrS  rz   rÓ   rƒ  z!application/x-www-form-urlencodedzContent-Typer“   r”   )rp   z$http protocol error: bad status linerø   i,  zhttp:)rÚ   r   r   r    r   r   r   r&  rç  r'  r(  r)  rÛ   r·   r€   r…  rë   ZBadStatusLiner   Ústatusr(   r›   Ú
http_errorro   r†  )rŒ   Zconnection_factoryrV   rW   Zuser_passwdZproxy_passwdr   r‘   Zrealhostrè  r’   Z
proxy_authrO  Z	http_connrp   ÚheaderrŽ   rÃ   rZ   rZ   r[   Ú_open_generic_httpö  sr    
 

zURLopener._open_generic_httpc             C   s   |   tj||¡S )zUse HTTP protocol.)ró  rë   rˆ  )rŒ   rV   rW   rZ   rZ   r[   Ú	open_httpR  s    zURLopener.open_httpc       
      C   sb   d| }t | |ƒrPt| |ƒ}|dkr6||||||ƒ}	n|||||||ƒ}	|	rP|	S |  |||||¡S )zœHandle http errors.

        Derived class can override this, or provide specific handlers
        named http_error_DDD where DDD is the 3-digit error code.zhttp_error_%dN)rÄ   rÔ   rã   )
rŒ   rV   ro   ÚerrcodeÚerrmsgrp   rW   rg   rŠ   rr   rZ   rZ   r[   rñ  V  s    

 zURLopener.http_errorc             C   s   |  ¡  t||||dƒ‚dS )z>Default error handler: close the connection and raise IOError.N)rÓ   r   )rŒ   rV   ro   rõ  rö  rp   rZ   rZ   r[   rã   f  s    zURLopener.http_error_defaultc             C   s   t j|| j| jdS )N)rØ  rÙ  )rë   rê   rØ  rÙ  )rŒ   r   rZ   rZ   r[   Ú_https_connectionl  s    zURLopener._https_connectionc             C   s   |   | j||¡S )zUse HTTPS protocol.)ró  r÷  )rŒ   rV   rW   rZ   rZ   r[   Ú
open_httpsq  s    zURLopener.open_httpsc             C   s^   t |tƒstdƒ‚|dd… dkrP|dd… dkrP|dd…  ¡ dkrPtd	ƒ‚n
|  |¡S dS )
z/Use local file or FTP depending on form of URL.zEfile error: proxy support for file protocol currently not implementedNrâ   z//rJ   r  é   z
localhost/z-file:// scheme is supported only on localhost)rÚ   r   r   r   rQ   r›  )rŒ   rV   rZ   rZ   r[   Ú	open_fileu  s
    
4
zURLopener.open_filec          
   C   sj  ddl m  m  m} ddl}t|ƒ\}}t|ƒ}yt |¡}W n0 t	k
rr } zt
|j|jƒ‚W dd}~X Y nX |j}	|j|jdd}
| |¡d }t d|p¤d|	|
f ¡}|sâ|}|dd… dkrÐd	| }tt|d
ƒ||ƒS t|ƒ\}}|s^t |¡tƒ ftƒ  kr^|}|dd… dkr.d	| }n|dd… dkrLtd| ƒ‚tt|d
ƒ||ƒS t
dƒ‚dS )zUse local file.r   NT)r¢  z6Content-Type: %s
Content-Length: %d
Last-modified: %s
z
text/plainr   r  zfile://r£  râ   z./zAlocal file url may start with / or file:. Unknown url of type: %sz#local file error: not on local host)r¤  r  r   r¥  r¦  r   rD   rc   r§  r®  r   Ústrerrorrm   r¨  r©  rª  r«  r¬  r(   r   r   rå   r¡  r™  ÚthishostrQ   )rŒ   rV   r¯  r¦  r   r\   Z	localnamer°  Úert   r±  r²  rp   Zurlfiler7  rZ   rZ   r[   r›  ~  s:     
zURLopener.open_local_filec          
   C   sˆ  t |tƒstdƒ‚ddl}t|ƒ\}}|s2tdƒ‚t|ƒ\}}t|ƒ\}}|r\t|ƒ\}}nd}t|ƒ}t|ppdƒ}t|p|dƒ}t	 
|¡}|sžddl}|j}nt|ƒ}t|ƒ\}}	t|ƒ}| d¡}
|
dd… |
d  }
}|
rò|
d sò|
dd… }
|
r
|
d s
d|
d< |||d |
¡f}t| jƒtkrfx8| j ¡ D ]*}||kr8| j| }| j|= | ¡  q8W yæ|| jkrŠt|||||
ƒ| j|< |s–d	}nd
}x:|	D ]2}t|ƒ\}}| ¡ dkr |dkr | ¡ }q W | j|  ||¡\}}| d| ¡d }d}|r|d| 7 }|dk	r4|dkr4|d| 7 }t |¡}t||d| ƒS  tƒ k
r‚ } zt td| ƒƒ W dd}~X Y nX dS )zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedr   Nzftp error: no host givenry   r  r_   r   r´  rR  r   )rµ  r¶  rÍ   rR  rv  r´  zftp:zContent-Type: %s
zContent-Length: %d
zftp error %r)!rÚ   r   r   r¦  r   r   r    r!   r   rå   r¡  r·  r¸  r
   r"   rC  rÎ  rk   rß  ÚMAXFTPCACHEr  rÓ   rÅ  r$   r   rº  r»  r«  r   r¬  r(   Ú	ftperrorsr   )rŒ   rV   r¦  r   rd   r7  r  r*  r·  r½  r¾  r\   r   r  r  r   rÀ  rŽ   ro   rÁ  r²  rp   r³  rZ   rZ   r[   Úopen_ftpž  sp    
  

  

 


zURLopener.open_ftpc       	   
   C   s<  t |tƒstdƒ‚y| dd¡\}}W n tk
rD   tddƒ‚Y nX |sNd}| d¡}|dkrŽd	||d
… krŽ||d d
… }|d
|… }nd}g }| dt 	dt 
t ¡ ¡¡ ¡ | d| ¡ |dkrät | d¡¡ d¡}nt|ƒ}| dt|ƒ ¡ | d¡ | |¡ d |¡}t |¡}t |¡}t|||ƒS )zUse "data" URL.zEdata error: proxy support for data protocol currently not implementedr”  r   z
data errorzbad data URLztext/plain;charset=US-ASCIIú;r   r‘  Nry   zDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %sr'  r%  zlatin-1zContent-Length: %dÚ
)rÚ   r   r   rC  rQ   rç  Úrfindri   r`  ÚstrftimeÚgmtimer'  Zdecodebytesr)  rÛ   r   rk   rÎ  r   r¬  ÚioÚStringIOr(   )	rŒ   rV   rW   r   ZsemiÚencodingr›   rp   ÚfrZ   rZ   r[   Ú	open_dataØ  s6    






zURLopener.open_data)N)N)N)N)NNN)N)N)N)N)r³   r´   rµ   rû   rÜ  r¶   rÛ  r   rà  rÓ   rá  râ  r   ræ  rå  rï  ró  rô  rñ  rã   rP   r÷  rø  rú  r›  r   r
  rZ   rZ   rZ   r[   rH   D  s.   

$


B\


	 :c               @   sž   e Zd ZdZdd„ Zdd„ Zd#dd„Zd	d
„ Zd$dd„Zd%dd„Z	d&dd„Z
d'dd„Zd(dd„Zd)dd„Zd*dd„Zd+dd„Zd,dd„Zd-dd „Zd!d"„ ZdS ).rI   z?Derived class with handlers for errors we can handle (perhaps).c             O   s(   t j| f|ž|Ž i | _d| _d| _d S )Nr   rþ   )rH   r   Ú
auth_cacheÚtriesÚmaxtries)rŒ   r×   ÚkwargsrZ   rZ   r[   r     s    zFancyURLopener.__init__c             C   s   t ||d| |ƒS )z3Default error handling -- don't raise an exception.zhttp:)r(   )rŒ   rV   ro   rõ  rö  rp   rZ   rZ   r[   rã     s    z!FancyURLopener.http_error_defaultNc       	      C   sl   |  j d7  _ | jrN| j | jkrNt| dƒr2| j}n| j}d| _ |||dd|ƒS |  ||||||¡}d| _ |S )z%Error 302 -- relocated (temporarily).r   Úhttp_error_500r   iô  z)Internal Server Error: Redirect Recursion)r  r  rÄ   r  rã   Úredirect_internal)	rŒ   rV   ro   rõ  rö  rp   rW   rÌ   rr   rZ   rZ   r[   r    s    
zFancyURLopener.http_error_302c       	      C   sx   d|kr|d }nd|kr$|d }nd S |  ¡  t| jd | |ƒ}t|ƒ}|jdkrnt|||d|  ||ƒ‚|  |¡S )Nr  r	  ú:)rá   r¤   r
  ry   z( Redirection to url '%s' is not allowed.)rÓ   r   r   r   r  r   r   )	rŒ   rV   ro   rõ  rö  rp   rW   r  r  rZ   rZ   r[   r    s    


z FancyURLopener.redirect_internalc             C   s   |   ||||||¡S )z*Error 301 -- also relocated (permanently).)r  )rŒ   rV   ro   rõ  rö  rp   rW   rZ   rZ   r[   r  ;  s    zFancyURLopener.http_error_301c             C   s   |   ||||||¡S )z;Error 303 -- also relocated (essentially identical to 302).)r  )rŒ   rV   ro   rõ  rö  rp   rW   rZ   rZ   r[   r  ?  s    zFancyURLopener.http_error_303c             C   s2   |dkr|   ||||||¡S |  |||||¡S dS )z1Error 307 -- relocated, but turn POST into error.N)r  rã   )rŒ   rV   ro   rõ  rö  rp   rW   rZ   rZ   r[   r  C  s    zFancyURLopener.http_error_307Fc             C   sÈ   d|krt  | |||||¡ |d }t d|¡}	|	sHt  | |||||¡ |	 ¡ \}
}|
 ¡ dkrtt  | |||||¡ |sŒt  | |||||¡ d| j d }|dkr²t| |ƒ||ƒS t| |ƒ|||ƒS dS )z_Error 401 -- authentication required.
        This function supports Basic authentication only.zwww-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r@  Zretry_Ú_basic_authN)rH   rã   rP  ÚmatchrF  r   r   rÔ   )rŒ   rV   ro   rõ  rö  rp   rW   rw  Ústuffr  r  r0  rg   rZ   rZ   r[   rT  J  s&    





zFancyURLopener.http_error_401c             C   sÈ   d|krt  | |||||¡ |d }t d|¡}	|	sHt  | |||||¡ |	 ¡ \}
}|
 ¡ dkrtt  | |||||¡ |sŒt  | |||||¡ d| j d }|dkr²t| |ƒ||ƒS t| |ƒ|||ƒS dS )zeError 407 -- proxy authentication required.
        This function supports Basic authentication only.zproxy-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r@  Zretry_proxy_r  N)rH   rã   rP  r  rF  r   r   rÔ   )rŒ   rV   ro   rõ  rö  rp   rW   rw  r  r  r  r0  rg   rZ   rZ   r[   rU  c  s&    





zFancyURLopener.http_error_407c             C   sÄ   t |ƒ\}}d| | }| jd }t|ƒ\}}	t |	ƒ\}	}
|	 d¡d }|	|d … }	|  |	||¡\}}|sr|srd S dt|ddt|dd|	f }	d|	 |
 | jd< |d kr´|  |¡S |  ||¡S d S )Nzhttp://rá   ú@r   z%s:%s@%sry   )rã  )r   r#  r   rÇ   Úget_user_passwdr   r   )rŒ   rV   r0  rW   r   r‘   r  r  rè  ré  ÚproxyselectorrÍ   r  r*  rZ   rZ   r[   Úretry_proxy_http_basic_auth|  s     
 
z*FancyURLopener.retry_proxy_http_basic_authc             C   sÄ   t |ƒ\}}d| | }| jd }t|ƒ\}}	t |	ƒ\}	}
|	 d¡d }|	|d … }	|  |	||¡\}}|sr|srd S dt|ddt|dd|	f }	d|	 |
 | jd< |d kr´|  |¡S |  ||¡S d S )Nzhttps://r¤   r  r   z%s:%s@%sry   )rã  )r   r#  r   rÇ   r  r   r   )rŒ   rV   r0  rW   r   r‘   r  r  rè  ré  r  rÍ   r  r*  rZ   rZ   r[   Úretry_proxy_https_basic_authŽ  s     
 
z+FancyURLopener.retry_proxy_https_basic_authc       
      C   s   t |ƒ\}}| d¡d }||d … }|  |||¡\}}|sD|sDd S dt|ddt|dd|f }d| | }	|d kr€|  |	¡S |  |	|¡S d S )Nr  r   z%s:%s@%sry   )rã  zhttp://)r   rÇ   r  r   r   )
rŒ   rV   r0  rW   r   r‘   rÍ   r  r*  r  rZ   rZ   r[   rH     s     
z$FancyURLopener.retry_http_basic_authc       
      C   s   t |ƒ\}}| d¡d }||d … }|  |||¡\}}|sD|sDd S dt|ddt|dd|f }d| | }	|d kr€|  |	¡S |  |	|¡S d S )Nr  r   z%s:%s@%sry   )rã  zhttps://)r   rÇ   r  r   r   )
rŒ   rV   r0  rW   r   r‘   rÍ   r  r*  r  rZ   rZ   r[   Úretry_https_basic_auth®  s     
z%FancyURLopener.retry_https_basic_authr   c             C   s`   |d |  ¡  }|| jkr2|r(| j|= n
| j| S |  ||¡\}}|sJ|rX||f| j|< ||fS )Nr  )r   r  Úprompt_user_passwd)rŒ   r   r0  rÔ  r   r  r*  rZ   rZ   r[   r  ¼  s    


 zFancyURLopener.get_user_passwdc             C   sT   ddl }y,td||f ƒ}|  d|||f ¡}||fS  tk
rN   tƒ  dS X dS )z#Override this in a GUI environment!r   NzEnter username for %s at %s: z#Enter password for %s in %s at %s: )NN)Úgetpassr	   ÚKeyboardInterruptÚprint)rŒ   r   r0  r  r  r*  rZ   rZ   r[   r  Ç  s    z!FancyURLopener.prompt_user_passwd)N)N)N)N)NF)NF)N)N)N)N)r   )r³   r´   rµ   rû   r   rã   r  r  r  r  r  rT  rU  r  r  rH  r  r  r  rZ   rZ   rZ   r[   rI     s$   



 
 





c               C   s   t dkrt d¡a t S )z8Return the IP address of the magic hostname 'localhost'.Nr™  )Ú
_localhostrå   r¡  rZ   rZ   rZ   r[   r™  ×  s    
r™  c               C   sP   t dkrLytt t ¡ ¡d ƒa W n( tjk
rJ   tt d¡d ƒa Y nX t S )z,Return the IP addresses of the current host.Nrâ   r™  )Ú	_thishostr/  rå   rž  rŸ  r   rZ   rZ   rZ   r[   rü  ß  s    rü  c              C   s   t dkrddl} | ja t S )z1Return the set of errors raised by the FTP class.Nr   )Ú
_ftperrorsr·  r¼  )r·  rZ   rZ   r[   rÿ  ê  s    rÿ  c               C   s   t dkrt d¡a t S )z%Return an empty email Message object.Nry   )Ú
_noheadersr   r¬  rZ   rZ   rZ   r[   Ú	noheadersó  s    
r#  c               @   sJ   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dS )rÅ  z;Class used by open_ftp() for cache of open FTP connections.NTc             C   s<   || _ || _|| _|| _|| _|| _d| _|| _|  ¡  d S )Nr   )	r  r*  r   r7  r¾  rX   ÚrefcountÚ	keepaliveÚinit)rŒ   r  r*  r   r7  r¾  rX   rÄ  rZ   rZ   r[   r    	  s    zftpwrapper.__init__c             C   s\   dd l }d| _| ¡ | _| j | j| j| j¡ | j | j	| j
¡ d | j¡}| j |¡ d S )Nr   r  )r·  ÚbusyZFTPr
  Zconnectr   r7  rX   Zloginr  r*  rÎ  r¾  Úcwd)rŒ   r·  Ú_targetrZ   rZ   r[   r&  	  s    
zftpwrapper.initc          
   C   s´  dd l }|  ¡  |dkr"d}d}nd| }d}y| j |¡ W n* |jk
rh   |  ¡  | j |¡ Y nX d }|rÜ|sÜyd| }| j |¡\}}W nH |jk
rÚ } z(t|ƒd d… dkrÊt	t
d	| ƒƒ W d d }~X Y nX |s~| j d¡ |rj| j ¡ }	zRy| j |¡ W n< |jk
rL } zt
d	| ƒ}
||
_|
‚W d d }~X Y nX W d | j |	¡ X d
| }nd}| j |¡\}}d| _t| d¡| jƒ}|  jd7  _| ¡  ||fS )Nr   )rv  r´  zTYPE Ar   zTYPE zRETR rJ   Z550zftp error: %rzLIST ZLISTr£  )r·  Úendtransferr
  Zvoidcmdr¼  r&  ZntransfercmdZ
error_permr   r   r   Úpwdr(  Ú	__cause__r'  r)   ZmakefileÚ
file_closer$  rÓ   )rŒ   r\   r   r·  ÚcmdÚisdirrÓ  rÁ  r†  r+  rÂ  ZftpobjrZ   rZ   r[   r»  	  sP       "

zftpwrapper.retrfilec             C   s
   d| _ d S )Nr   )r'  )rŒ   rZ   rZ   r[   r*  E	  s    zftpwrapper.endtransferc             C   s   d| _ | jdkr|  ¡  d S )NFr   )r%  r$  Ú
real_close)rŒ   rZ   rZ   r[   rÓ   H	  s    
zftpwrapper.closec             C   s2   |   ¡  |  jd8  _| jdkr.| js.|  ¡  d S )Nr   r   )r*  r$  r%  r0  )rŒ   rZ   rZ   r[   r-  M	  s    zftpwrapper.file_closec             C   s2   |   ¡  y| j ¡  W n tƒ k
r,   Y nX d S )N)r*  r
  rÓ   rÿ  )rŒ   rZ   rZ   r[   r0  S	  s
    zftpwrapper.real_close)NT)r³   r´   rµ   rû   r   r&  r»  r*  rÓ   r-  r0  rZ   rZ   rZ   r[   rÅ  ý  s    
	0rÅ  c              C   sL   i } xBt j ¡ D ]4\}}| ¡ }|r|dd… dkr|| |dd… < qW | S )a  Return a dictionary of scheme -> proxy server URL mappings.

    Scan the environment for variables named <scheme>_proxy;
    this seems to be the standard convention.  If you need a
    different way, you can pass a proxies dictionary to the
    [Fancy]URLopener constructor.

    iúÿÿÿNÚ_proxy)rc   Úenvironr…   r   )r#  rg   rŽ   rZ   rZ   r[   Úgetproxies_environment[	  s    	r3  c             C   sv   t j dd¡pt j dd¡}|dkr(dS t| ƒ\}}dd„ | d¡D ƒ}x(|D ] }|rN| |¡sj|  |¡rNdS qNW d	S )
zÏTest if proxies should not be used for a particular host.

    Checks the environment for a variable named no_proxy, which should
    be a list of DNS suffixes separated by commas, or '*' for all hosts.
    Úno_proxyry   ZNO_PROXYÚ*r   c             S   s   g | ]}|  ¡ ‘qS rZ   )r•  )r   r  rZ   rZ   r[   r.  x	  s    z,proxy_bypass_environment.<locals>.<listcomp>r”  r   )rc   r2  r¬   r   rC  Úendswith)r   r4  Úhostonlyr7  Zno_proxy_listrg   rZ   rZ   r[   Úproxy_bypass_environmentk	  s    
r8  c          	   C   s  ddl m } t| ƒ\}}dd„ }d| kr4|d r4dS d}xà| d	d
¡D ]Ð}|sPqFt d|¡}|dk	r|dkržyt |¡}||ƒ}W n tjk
rœ   wFY nX || d¡ƒ}	| d¡}
|
dkrØd| d¡ 	d¡d  }
nt
|
dd… ƒ}
d|
 }
||
? |	|
? krdS qF|| |ƒrFdS qFW dS )aj  
    Return True iff this host shouldn't be accessed using a proxy

    This function uses the MacOSX framework SystemConfiguration
    to fetch the proxy information.

    proxy_settings come from _scproxy._get_proxy_settings or get mocked ie:
    { 'exclude_simple': bool,
      'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.1', '10.0/16']
    }
    r   )Úfnmatchc             S   sh   |   d¡}ttt|ƒƒ}t|ƒdkr<|ddddg d d… }|d d> |d d> B |d d> B |d	 B S )
NÚ.rý   r   é   r   r_  râ   r^  rJ   )rC  r°   r   r
   rk   )ZipAddrr6  rZ   rZ   r[   Úip2num’	  s
    
z,_proxy_bypass_macosx_sysconf.<locals>.ip2numr:  Zexclude_simpleTNÚ
exceptionsrZ   z(\d+(?:\.\d+)*)(/\d+)?r   râ   r^  é    F)r9  r   r¬   rP  r  rå   r¡  rÂ   ÚgroupÚcountr
   )r   Úproxy_settingsr9  r7  r7  r<  ZhostIPrŽ   r  r:  ÚmaskrZ   rZ   r[   Ú_proxy_bypass_macosx_sysconf‚	  s:     



rC  Údarwin)Ú_get_proxy_settingsÚ_get_proxiesc             C   s   t ƒ }t| |ƒS )N)rE  rC  )r   rA  rZ   rZ   r[   Úproxy_bypass_macosx_sysconfÁ	  s    rG  c               C   s   t ƒ S )z±Return a dictionary of scheme -> proxy server URL mappings.

        This function uses the MacOSX framework SystemConfiguration
        to fetch the proxy information.
        )rF  rZ   rZ   rZ   r[   Úgetproxies_macosx_sysconfÅ	  s    rH  c             C   s   t ƒ rt| ƒS t| ƒS d S )N)r3  r8  rG  )r   rZ   rZ   r[   r&  Ï	  s    r&  c               C   s   t ƒ p
tƒ S )N)r3  rH  rZ   rZ   rZ   r[   rE   Õ	  s    c           
   C   s  i } yddl }W n tk
r$   | S X yÎ| |jd¡}| |d¡d }|rêt| |d¡d ƒ}d|kr¬x‚| d¡D ]4}| dd¡\}}t d	|¡sžd
||f }|| |< qrW n>|dd… dkrÆ|| d< n$d| | d< d| | d< d| | d< | 	¡  W n t
ttfk
r   Y nX | S )zxReturn a dictionary of scheme -> proxy server URL mappings.

        Win32 uses the registry to store proxies.

        r   Nz;Software\Microsoft\Windows\CurrentVersion\Internet SettingsÚProxyEnableZProxyServerr‘  r  r   z^([^/:]+)://z%s://%sr?  zhttp:rá   z	http://%sz
https://%sr¤   zftp://%sr
  )ÚwinregÚImportErrorÚOpenKeyÚHKEY_CURRENT_USERÚQueryValueExr   rC  rP  r  ÚCloseÚWindowsErrorrQ   rÅ   )r#  rJ  ÚinternetSettingsÚproxyEnableZproxyServerÚprÎ   ZaddressrZ   rZ   r[   Úgetproxies_registryÚ	  s8    

rT  c               C   s   t ƒ p
tƒ S )z¥Return a dictionary of scheme -> proxy server URL mappings.

        Returns settings gathered from the environment, if specified,
        or the registry.

        )r3  rT  rZ   rZ   rZ   r[   rE   
  s    c             C   s~  ydd l }W n tk
r    dS X y6| |jd¡}| |d¡d }t| |d¡d ƒ}W n tk
rl   dS X |rv|szdS t| ƒ\}}|g} y t 	|¡}||krª|  
|¡ W n tjk
rÂ   Y nX y t |¡}||krâ|  
|¡ W n tjk
 rü   Y nX | d¡}xp|D ]h}	|	dkr*d|kr*dS |	 dd	¡}	|	 d
d¡}	|	 dd¡}	x$| D ]}
t |	|
tj¡rTdS qTW qW dS )Nr   z;Software\Microsoft\Windows\CurrentVersion\Internet SettingsrI  ZProxyOverrider  z<local>r:  r   z\.r5  z.*ú?)rJ  rK  rL  rM  rN  r   rP  r   rå   r¡  ri   rÂ   ZgetfqdnrC  r  rP  r  rR  )r   rJ  rQ  rR  ZproxyOverrideZrawHostr7  ZaddrZfqdnr;  r¨   rZ   rZ   r[   Úproxy_bypass_registry
  sR    







rV  c             C   s   t ƒ rt| ƒS t| ƒS dS )z¥Return a dictionary of scheme -> proxy server URL mappings.

        Returns settings gathered from the environment, if specified,
        or the registry.

        N)r3  r8  rV  )r   rZ   rZ   r[   r&  B
  s    )NNN)‘rû   Z
__future__r   r   r   r   Zfuture.builtinsr   r   r   r	   r
   r   r   r   Zfuture.utilsr   r   r   r'  rÊ   rc  r}  Zfuture.backportsr   Zfuture.backports.httpr   rë   rÂ   r   r   r   Úparser   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   rÃ   r(   r)   r  rc   r8  rP  rå   Úsysr`  r€  rf   r`   r˜   rR   r*   rK  rP   Ú__all__rÛ  r¶   rU   ræ   r@   rA   rh   rF   rG   rQ  ÚASCIIr}   r‚   Úobjectr+   r,   rB   r-   r?   r.   r/   r  r1   r2   r3   r4   r5   r6   Úurandomrb  r7   r8   r9   rx  r:   rÄ   rT   ri   r0   r>   rY  rZ  r;   r­  r<   r=   rþ  rg   Z
nturl2pathrD   rC   rß  rH   rI   r  r™  r   rü  r!  rÿ  r"  r#  rÅ  r3  r8  rC  ÚplatformZ_scproxyrE  rF  rG  rH  r&  rE   rT  rV  rZ   rZ   rZ   r[   Ú<module>G   sð   (L

?
y &hH*@
E z

+4:8   A U

^<

-	2
