B
    ^ Çc‹ ã            !   @   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ZddlZddlZddlZddl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%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+ yddl,Z,W n e-k
r   dZ.Y nX dZ.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%d&d'd(g!Z/d)ej0dd*…  Z1da2dej3fddddd+œd,d„Z4d-d „ Z5g Z6d~d.d%„Z7d/d&„ Z8e
 9d0e
j:¡Z;d1d2„ Z<G d3d„ dƒZ=G d4d	„ d	ƒZ>d5d!„ Z?G d6d
„ d
ƒZ@G d7d„ de@ƒZAG d8d„ de@ƒZBG d9d„ de@ƒZCd:d;„ ZDG d<d„ de@ƒZEG d=d„ dƒZFG d>d„ deFƒZGG d?d„ deGƒZHG d@d„ dƒZIG dAd„ deIe@ƒZJG dBd„ deIe@ƒZKejLZMG dCd„ dƒZNG dDd„ de@eNƒZOG dEd„ de@eNƒZPG dFdG„ dGe@ƒZQG dHd„ deQƒZReSejTdIƒr*G dJdK„ dKeQƒZUe/ VdK¡ G dLd„ de@ƒZWG dMd„ de@ƒZXdNdO„ ZYdPdQ„ ZZG dRd„ de@ƒZ[dSdT„ Z\G dUd„ de@ƒZ]G dVd„ de]ƒZ^G dWd„ de@ƒZ_dXZ`ejadYkrÄddZlbmcZcmdZd nd[d#„ Zcd\d"„ Zdi ZeG d]d'„ d'ƒZfG d^d(„ d(efƒZgdahd_d`„ Zidajdadb„ Zkdaldcdd„ Zmdandedf„ ZoG dgdh„ dhƒZpdidj„ Zqddkdl„Zrdmdn„ ZsejtdokrŒddplumvZvmwZw dqdr„ Zxdsdt„ Zydudv„ Zzdwd$„ Z{n6ejadYkrºdxdy„ Z|dzd$„ Z{d{d|„ Z}d}dv„ ZzneqZ{erZzdS )€aš
  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
OSError); 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/')
é    N)ÚURLErrorÚ	HTTPErrorÚContentTooShortError)ÚurlparseÚurlsplitÚurljoinÚunwrapÚquoteÚunquoteÚ	splittypeÚ	splithostÚ	splitportÚ	splituserÚsplitpasswdÚ	splitattrÚ
splitqueryÚ
splitvalueÚsplittagÚto_bytesÚunquote_to_bytesÚ
urlunparse)Ú
addinfourlÚaddclosehookFTÚRequestÚOpenerDirectorÚBaseHandlerÚHTTPDefaultErrorHandlerÚHTTPRedirectHandlerÚHTTPCookieProcessorÚProxyHandlerÚHTTPPasswordMgrÚHTTPPasswordMgrWithDefaultRealmÚHTTPPasswordMgrWithPriorAuthÚAbstractBasicAuthHandlerÚHTTPBasicAuthHandlerÚProxyBasicAuthHandlerÚAbstractDigestAuthHandlerÚHTTPDigestAuthHandlerÚProxyDigestAuthHandlerÚHTTPHandlerÚFileHandlerÚ
FTPHandlerÚCacheFTPHandlerÚDataHandlerÚUnknownHandlerÚHTTPErrorProcessorÚurlopenÚinstall_openerÚbuild_openerÚpathname2urlÚurl2pathnameÚ
getproxiesÚurlretrieveÚ
urlcleanupÚ	URLopenerÚFancyURLopenerz%d.%dé   )ÚcafileÚcapathÚ	cadefaultÚcontextc      
      C   s¤   |s|s|rfddl }| dtd¡ |dk	r2tdƒ‚ts>tdƒ‚tjtjj||d}t	|d}t
|ƒ}	n0|r~t	|d}t
|ƒ}	ntdkr’t
ƒ  a}	nt}	|	 | ||¡S )	a$
  Open the URL url, which can be either a string or a Request object.

    *data* must be an object specifying additional data to be sent to
    the server, or None if no such data is needed.  See Request for
    details.

    urllib.request module uses HTTP/1.1 and includes a "Connection:close"
    header in its HTTP requests.

    The optional *timeout* parameter specifies a timeout in seconds for
    blocking operations like the connection attempt (if not specified, the
    global default timeout setting will be used). This only works for HTTP,
    HTTPS and FTP connections.

    If *context* is specified, it must be a ssl.SSLContext instance describing
    the various SSL options. See HTTPSConnection for more details.

    The optional *cafile* and *capath* parameters specify a set of trusted CA
    certificates for HTTPS requests. cafile should point to a single file
    containing a bundle of CA certificates, whereas capath should point to a
    directory of hashed certificate files. More information can be found in
    ssl.SSLContext.load_verify_locations().

    The *cadefault* parameter is ignored.

    This function always returns an object which can work as a context
    manager and has methods such as

    * geturl() - return the URL of the resource retrieved, commonly used to
      determine if a redirect was followed

    * info() - return the meta-information of the page, such as headers, in the
      form of an email.message_from_string() instance (see Quick Reference to
      HTTP Headers)

    * getcode() - return the HTTP status code of the response.  Raises URLError
      on errors.

    For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse
    object slightly modified. In addition to the three new methods above, the
    msg attribute contains the same information as the reason attribute ---
    the reason phrase returned by the server --- instead of the response
    headers as it is specified in the documentation for HTTPResponse.

    For FTP, file, and data URLs and requests explicitly handled by legacy
    URLopener and FancyURLopener classes, this function returns a
    urllib.response.addinfourl object.

    Note that None may be returned if no handler handles the request (though
    the default installed global OpenerDirector uses UnknownHandler to ensure
    this never happens).

    In addition, if proxy settings are detected (for example, when a *_proxy
    environment variable like http_proxy is set), ProxyHandler is default
    installed and makes sure the requests are handled through the proxy.

    r   NzJcafile, capath and cadefault are deprecated, use a custom context instead.r:   zDYou can't pass both context and any of cafile, capath, and cadefaultzSSL support not available)r;   r<   )r>   )ÚwarningsÚwarnÚDeprecationWarningÚ
ValueErrorÚ	_have_sslÚsslZcreate_default_contextZPurposeZSERVER_AUTHÚHTTPSHandlerr2   Ú_openerÚopen)
ÚurlÚdataÚtimeoutr;   r<   r=   r>   r?   Zhttps_handlerÚopener© rL   úlib/python3.7/urllib/request.pyr0   ‹   s*    <




c             C   s   | a d S )N)rF   )rK   rL   rL   rM   r1   à   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-Lengthé   Nz1retrieval incomplete: got only %i out of %i bytes)r   Ú
contextlibÚclosingr0   ÚinfoÚosÚpathÚnormpathrG   ÚtempfileZNamedTemporaryFileÚnameÚ_url_tempfilesÚappendÚintÚreadÚlenÚwriter   )rH   ÚfilenameÚ
reporthookrI   Zurl_typerW   ÚfpÚheadersÚtfpÚresultÚbsÚsizer^   ÚblocknumÚblockrL   rL   rM   r6   å   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 )z0Clean up temporary files from urlretrieve calls.N)r[   rV   ÚunlinkÚOSErrorrF   )Z	temp_filerL   rL   rM   r7   $  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.

    rR   Ú ÚHost)Úfull_urlr   Ú
get_headerÚ_cut_port_reÚsubÚlower)ÚrequestrH   ÚhostrL   rL   rM   Úrequest_host3  s    rv   c               @   sÊ   e Zd Zdi dddfdd„Zedd„ ƒZejdd„ ƒZejdd„ ƒZed	d
„ ƒZejdd
„ ƒZejdd
„ ƒZdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd#dd„Zdd „ Zd!d"„ ZdS )$r   NFc       	      C   sp   || _ i | _i | _d | _|| _d | _x | ¡ D ]\}}|  ||¡ q.W |d krVt| ƒ}|| _	|| _
|rl|| _d S )N)ro   rd   Úunredirected_hdrsÚ_datarI   Ú_tunnel_hostÚitemsÚ
add_headerrv   Úorigin_req_hostÚunverifiableÚmethod)	ÚselfrH   rI   rd   r|   r}   r~   ÚkeyÚvaluerL   rL   rM   Ú__init__E  s    zRequest.__init__c             C   s   | j rd | j| j ¡S | jS )Nz{}#{})ÚfragmentÚformatÚ	_full_url)r   rL   rL   rM   ro   W  s    zRequest.full_urlc             C   s(   t |ƒ| _t| jƒ\| _| _|  ¡  d S )N)r   r…   r   rƒ   Ú_parse)r   rH   rL   rL   rM   ro   ]  s    
c             C   s   d | _ d | _d| _d S )Nrm   )r…   rƒ   Úselector)r   rL   rL   rM   ro   d  s    c             C   s   | j S )N)rx   )r   rL   rL   rM   rI   j  s    zRequest.datac             C   s(   || j kr$|| _ |  d¡r$|  d¡ d S )NzContent-length)rx   Ú
has_headerÚremove_header)r   rI   rL   rL   rM   rI   n  s    

c             C   s
   d | _ d S )N)rI   )r   rL   rL   rM   rI   x  s    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…   ÚtyperB   ro   r   ru   r‡   r
   )r   ÚrestrL   rL   rM   r†   |  s    
zRequest._parsec             C   s   | j dk	rdnd}t| d|ƒS )z3Return a string indicating the HTTP request method.NÚPOSTÚGETr~   )rI   Úgetattr)r   Zdefault_methodrL   rL   rM   Ú
get_method„  s    zRequest.get_methodc             C   s   | j S )N)ro   )r   rL   rL   rM   Úget_full_url‰  s    zRequest.get_full_urlc             C   s2   | j dkr| js| j| _n|| _ | j| _|| _d S )NÚhttps)rŠ   ry   ru   ro   r‡   )r   ru   rŠ   rL   rL   rM   Ú	set_proxyŒ  s
    
zRequest.set_proxyc             C   s   | j | jkS )N)r‡   ro   )r   rL   rL   rM   Ú	has_proxy”  s    zRequest.has_proxyc             C   s   || j | ¡ < d S )N)rd   Ú
capitalize)r   r€   ÚvalrL   rL   rM   r{   —  s    zRequest.add_headerc             C   s   || j | ¡ < d S )N)rw   r”   )r   r€   r•   rL   rL   rM   Úadd_unredirected_header›  s    zRequest.add_unredirected_headerc             C   s   || j kp|| jkS )N)rd   rw   )r   Úheader_namerL   rL   rM   rˆ   Ÿ  s    
zRequest.has_headerc             C   s   | j  || j ||¡¡S )N)rd   Úgetrw   )r   r—   ÚdefaultrL   rL   rM   rp   £  s    zRequest.get_headerc             C   s    | j  |d ¡ | j |d ¡ d S )N)rd   Úpoprw   )r   r—   rL   rL   rM   r‰   ¨  s    zRequest.remove_headerc             C   s"   | j  ¡ }| | j¡ t| ¡ ƒS )N)rw   ÚcopyÚupdaterd   Úlistrz   )r   ÚhdrsrL   rL   rM   Úheader_items¬  s    
zRequest.header_items)N)Ú__name__Ú
__module__Ú__qualname__r‚   Úpropertyro   ÚsetterÚdeleterrI   r†   r   r   r’   r“   r{   r–   rˆ   rp   r‰   rŸ   rL   rL   rL   rM   r   C  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_versionrL   rL   rM   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Ú_rR   ÚerrorrG   Úresponsert   T)ÚhasattrÚ	TypeErrorrŠ   ÚdirÚfindÚ
startswithr]   rB   rª   r˜   r©   r«   r¬   Ú
setdefaultÚbisectZinsortr\   r¨   r­   )r   ÚhandlerZaddedÚmethÚiÚprotocolZ	conditionÚjÚkindÚlookupr¨   rL   rL   rM   Úadd_handler½  sJ    



zOpenerDirector.add_handlerc             C   s   d S )NrL   )r   rL   rL   rM   Úcloseì  s    zOpenerDirector.closec       	      G   s<   |  |d¡}x*|D ]"}t||ƒ}||Ž }|d k	r|S qW d S )NrL   )r˜   rŽ   )	r   ÚchainrÀ   Ú	meth_nameÚargsr¨   r»   Úfuncrf   rL   rL   rM   Ú_call_chainð  s    

zOpenerDirector._call_chainNc       
      C   sª   t |tƒrt||ƒ}n|}|d k	r(||_||_|j}|d }x(| j |g ¡D ]}t||ƒ}||ƒ}qLW |  	||¡}	|d }x*| j
 |g ¡D ]}t||ƒ}|||	ƒ}	qŠW |	S )NZ_requestZ	_response)Ú
isinstanceÚstrr   rI   rJ   rŠ   r¬   r˜   rŽ   Ú_openr«   )
r   ÚfullurlrI   rJ   Úreqr¾   rÅ   Z	processorr¼   r³   rL   rL   rM   rG   û  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Í   rI   rf   r¾   rL   rL   rM   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Ð   r:   zhttp_error_%srR   Z_errorr   r™   Úhttp_error_default)rª   rÈ   )r   ÚprotorÆ   ÚdictrÅ   Zhttp_errZ	orig_argsrf   rL   rL   rM   r²   &  s     

zOpenerDirector.error)N)r    r¡   r¢   r‚   rÂ   rÃ   rÈ   ÚsocketÚ_GLOBAL_DEFAULT_TIMEOUTrG   rË   r²   rL   rL   rL   rM   r   ±  s   /
c           	   G   sæ   t ƒ }ttttttttt	g	}t
tjdƒr2| t¡ tƒ }xN|D ]F}x@| D ]8}t|tƒrlt||ƒr€| |¡ qHt||ƒrH| |¡ qHW q>W x|D ]}| |¡ qŽW x|D ]}| |ƒ ¡ q¦W x&| D ]}t|tƒ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.
    ÚHTTPSConnection)r   r   r.   r)   r   r   r+   r*   r/   r-   r´   rÐ   Úclientr\   rE   ÚsetrÉ   rŠ   Ú
issubclassÚaddÚremoverÂ   )r¨   rK   Zdefault_classesÚskipÚklassZcheckÚhrL   rL   rM   r2   ?  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ß   rL   rL   rM   r­   f  s    zBaseHandler.add_parentc             C   s   d S )NrL   )r   rL   rL   rM   rÃ   i  s    zBaseHandler.closec             C   s   t |dƒsdS | j|jk S )NÚhandler_orderT)r´   rà   )r   ÚotherrL   rL   rM   Ú__lt__m  s    
zBaseHandler.__lt__N)r    r¡   r¢   rà   r­   rÃ   râ   rL   rL   rL   rM   r   c  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Ð   )ÚcodeÚmsgrU   rß   r²   )r   rt   r³   rä   rå   rž   rL   rL   rM   Úhttp_responsez  s
    z HTTPErrorProcessor.http_responseN)r    r¡   r¢   Ú__doc__rà   ræ   Úhttps_responserL   rL   rL   rM   r/   v  s   c               @   s   e Zd Zdd„ ZdS )r   c             C   s   t |j||||ƒ‚d S )N)r   ro   )r   rÍ   rc   rä   rå   rž   rL   rL   rM   rÑ   ˆ  s    z*HTTPDefaultErrorHandler.http_error_defaultN)r    r¡   r¢   rÑ   rL   rL   rL   rM   r   ‡  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       	         st   |  ¡ }|dkr|dks:|dkr(|dks:t|j||||ƒ‚| dd¡}d‰ ‡ 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                s"   i | ]\}}|  ¡ ˆ kr||“qS rL   )rs   )Ú.0ÚkÚv)ÚCONTENT_HEADERSrL   rM   ú
<dictcomp>®  s    z8HTTPRedirectHandler.redirect_request.<locals>.<dictcomp>T)rd   r|   r}   )r   r   ro   Úreplacerd   rz   r   r|   )	r   rÍ   rc   rä   rå   rd   ÚnewurlÚmZ
newheadersrL   )rï   rM   r®   “  s    
z$HTTPRedirectHandler.redirect_requestc       
      C   sL  d|kr|d }nd|kr$|d }nd S t |ƒ}|jdkrRt||d||f ||ƒ‚|jsn|jrnt|ƒ}d|d< t|ƒ}t|dtj	d}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‘   Úftprm   z+%s - Redirection to url '%s' is not allowedú/r:   z
iso-8859-1)ÚencodingÚsafeÚredirect_dictr   rR   )rJ   )r   Úschemer   rW   Znetlocr   r   r	   ÚstringZpunctuationr   ro   r®   r´   rú   r˜   Úmax_repeatsr_   Úmax_redirectionsÚinf_msgr^   rÃ   rß   rG   rJ   )
r   rÍ   rc   rä   rå   rd   rò   ÚurlpartsÚnewZvisitedrL   rL   rM   Ú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ÿ   rL   rL   rL   rM   r   ‹  s   &<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 )a  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.
    r÷   Nz//zproxy URL with no authority: %rr:   rQ   )r   r¸   rB   r·   r   r   )	Úproxyrû   Zr_schemeÚ	authorityÚendZuserinfoÚhostportÚuserÚpasswordrL   rL   rM   Ú_parse_proxyü  s    

r  c               @   s"   e Zd ZdZddd„Zdd„ ZdS )r   é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 )NrL   )Úrr  rŠ   r¼   rL   rL   rM   Ú<lambda>$  s    z'ProxyHandler.__init__.<locals>.<lambda>)r5   r´   ÚAssertionErrorÚproxiesrz   Úsetattrr°   )r   r  rŠ   rH   rL   rL   rM   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‘   )rJ   )rŠ   r  ru   Úproxy_bypassr
   Úbase64Ú	b64encodeÚencodeÚdecoder{   r’   rß   rG   rJ   )r   rÍ   r  rŠ   Z	orig_typeZ
proxy_typer
  r  r	  Z	user_passZcredsrL   rL   rM   r°   '  s     zProxyHandler.proxy_open)N)r    r¡   r¢   rà   r‚   r°   rL   rL   rL   rM   r     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 )r    c             C   s
   i | _ d S )N)Úpasswd)r   rL   rL   rM   r‚   E  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             3   s   | ]}ˆ  |ˆ ¡V  qd S )N)Ú
reduce_uri)rì   Úu)Údefault_portr   rL   rM   ú	<genexpr>P  s    z/HTTPPasswordMgr.add_password.<locals>.<genexpr>)rÉ   rÊ   r  Útuple)r   Úrealmrõ   r
  r  Úreduced_urirL   )r  r   rM   Úadd_passwordH  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  rz   Ú	is_suburi)	r   r   ÚauthuriZdomainsr  Úreduced_authuriZurisZauthinforõ   rL   rL   rM   Úfind_user_passwordS  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.rR   r   r:   r÷   NéP   i»  )rÐ   r‘   z%s:%d)r   r   r˜   )
r   rõ   r  Úpartsrû   r  rW   ru   ÚportZdportrL   rL   rM   r  ]  s     zHTTPPasswordMgr.reduce_uric             C   sN   ||krdS |d |d kr dS |d }|dd… dkr@|d7 }|d   |¡S )zcCheck if test is below base in a URI tree

        Both args must be URIs in reduced form.
        Tr   FrR   rQ   Nr÷   )r¸   )r   ÚbaseÚtestÚprefixrL   rL   rM   r#  t  s    zHTTPPasswordMgr.is_suburiN)T)r    r¡   r¢   r‚   r"  r&  r  r#  rL   rL   rL   rM   r    C  s
   

c               @   s   e Zd Zdd„ ZdS )r!   c             C   s0   t  | ||¡\}}|d k	r"||fS t  | d |¡S )N)r    r&  )r   r   r$  r
  r  rL   rL   rM   r&  …  s
    
z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r    r¡   r¢   r&  rL   rL   rL   rM   r!   ƒ  s   c                   s<   e Zd Z‡ fdd„Zd
‡ fdd„	Zddd„Zdd	„ Z‡  ZS )r"   c                s   i | _ tƒ j||Ž d S )N)ÚauthenticatedÚsuperr‚   )r   rÆ   Úkwargs)Ú	__class__rL   rM   r‚     s    z%HTTPPasswordMgrWithPriorAuth.__init__Fc                s<   |   ||¡ |d k	r&tƒ  d |||¡ tƒ  ||||¡ d S )N)Úupdate_authenticatedr.  r"  )r   r   rõ   r
  r  Úis_authenticated)r0  rL   rM   r"  “  s    z)HTTPPasswordMgrWithPriorAuth.add_passwordc             C   sF   t |tƒr|g}x0dD ](}x"|D ]}|  ||¡}|| j|< q W qW d S )N)TF)rÉ   rÊ   r  r-  )r   rõ   r2  r  r  r!  rL   rL   rM   r1  š  s    


z1HTTPPasswordMgrWithPriorAuth.update_authenticatedc             C   sD   x>dD ]6}|   ||¡}x$| jD ]}|  ||¡r| j| S qW qW d S )N)TF)r  r-  r#  )r   r$  r  r%  rõ   rL   rL   rM   r2  ¤  s
    
z-HTTPPasswordMgrWithPriorAuth.is_authenticated)F)F)r    r¡   r¢   r‚   r"  r1  r2  Ú__classcell__rL   rL   )r0  rM   r"     s   

c               @   sT   e Zd Ze dej¡Zddd„Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ ZeZeZdS )r#   z1(?:^|,)[ 	]*([^ 	,]+)[ 	]+realm=(["']?)([^"']*)\2Nc             C   s"   |d krt ƒ }|| _| jj| _d S )N)r    r  r"  )r   Zpassword_mgrrL   rL   rM   r‚   Á  s    z!AbstractBasicAuthHandler.__init__c             c   st   d}xFt j |¡D ]6}| ¡ \}}}|dkr:t dtd¡ ||fV  d}qW |sp|rb| ¡ d }nd}|d fV  d S )NF)ú"ú'zBasic Auth Realm was unquotedé   Tr   rm   )r#   ÚrxÚfinditerÚgroupsr?   r@   ÚUserWarningÚsplit)r   ÚheaderZfound_challengeZmorû   r	   r   rL   rL   rM   Ú_parse_realmÇ  s    
z%AbstractBasicAuthHandler._parse_realmc       	      C   s~   |  |¡}|sd S d }xL|D ]D}x>|  |¡D ]0\}}| ¡ dkrF|}q,|d k	r,|  |||¡S q,W qW |d k	rztd|f ƒ‚d S )NÚbasiczBAbstractBasicAuthHandler does not support the following scheme: %r)Zget_allr=  rs   Úretry_http_basic_authrB   )	r   Úauthreqru   rÍ   rd   Zunsupportedr<  rû   r   rL   rL   rM   Úhttp_error_auth_reqedÛ  s    

z.AbstractBasicAuthHandler.http_error_auth_reqedc             C   s|   | j  ||¡\}}|d k	rtd||f }dt | ¡ ¡ d¡ }| | jd ¡|krTd S | | j|¡ | j	j
||jdS d S d S )Nz%s:%szBasic r  )rJ   )r  r&  r  r  r  r  rp   Úauth_headerr–   rß   rG   rJ   )r   ru   rÍ   r   r
  ÚpwÚrawÚauthrL   rL   rM   r?  õ  s    z.AbstractBasicAuthHandler.retry_http_basic_authc             C   st   t | jdƒr| j |j¡s|S | d¡sp| j d |j¡\}}d ||¡ ¡ }t 	|¡ 
¡ }| dd | ¡ ¡¡ |S )Nr2  ÚAuthorizationz{0}:{1}zBasic {})r´   r  r2  ro   rˆ   r&  r„   r  r  Zstandard_b64encoder  r–   Ústrip)r   rÍ   r
  r  ZcredentialsZauth_strrL   rL   rM   Úhttp_request  s    
z%AbstractBasicAuthHandler.http_requestc             C   sL   t | jdƒrHd|j  kr"dk r8n n| j |jd¡ n| j |jd¡ |S )Nr2  rã   i,  TF)r´   r  rä   r1  ro   )r   rÍ   r³   rL   rL   rM   ræ     s
    z&AbstractBasicAuthHandler.http_response)N)r    r¡   r¢   ÚreÚcompileÚIr7  r‚   r=  rA  r?  rH  ræ   Úhttps_requestrè   rL   rL   rL   rM   r#   ¬  s   
c               @   s   e Zd ZdZdd„ ZdS )r$   rF  c             C   s   |j }|  d|||¡}|S )Nzwww-authenticate)ro   rA  )r   rÍ   rc   rä   rå   rd   rH   r³   rL   rL   rM   Úhttp_error_401  s    
z#HTTPBasicAuthHandler.http_error_401N)r    r¡   r¢   rB  rM  rL   rL   rL   rM   r$     s   c               @   s   e Zd ZdZdd„ ZdS )r%   zProxy-authorizationc             C   s   |j }|  d|||¡}|S )Nzproxy-authenticate)ru   rA  )r   rÍ   rc   rä   rå   rd   r  r³   rL   rL   rM   Úhttp_error_407*  s    
z$ProxyBasicAuthHandler.http_error_407N)r    r¡   r¢   rB  rN  rL   rL   rL   rM   r%   &  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 )r&   Nc             C   s4   |d krt ƒ }|| _| jj| _d| _d| _d | _d S )Nr   )r    r  r"  ÚretriedÚnonce_countÚ
last_nonce)r   r  rL   rL   rM   r‚   D  s    
z"AbstractDigestAuthHandler.__init__c             C   s
   d| _ d S )Nr   )rO  )r   rL   rL   rM   Úreset_retry_countM  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 )	Né   i‘  zdigest auth failedrR   r   Zdigestr>  zEAbstractDigestAuthHandler does not support the following scheme: '%s')r˜   rO  r   ro   r;  rs   Úretry_http_digest_authrB   )r   rB  ru   rÍ   rd   r@  rû   rL   rL   rM   rA  P  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ë   rR   z	Digest %s)rJ   )r;  Úparse_keqv_listÚfilterÚparse_http_listÚget_authorizationrd   r˜   rB  r–   rß   rG   rJ   )r   rÍ   rE  ÚtokenZ	challengeÚchalZauth_valZresprL   rL   rM   rT  d  s    z0AbstractDigestAuthHandler.retry_http_digest_authc             C   s@   d| j |t ¡ f }| d¡tdƒ }t |¡ ¡ }|d d… S )Nz	%s:%s:%s:r  é   é   )rP  ÚtimeZctimer  Ú_randombytesÚhashlibÚsha1Ú	hexdigest)r   ÚnonceÚsÚbÚdigrL   rL   rM   Ú
get_cnoncep  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è|	||ƒd|||ƒf ƒ}n~d	| 
d
¡krZ|| jkr|  jd7  _nd| _|| _d| j }|  |¡}d|||d	||ƒf }|	||ƒ|ƒ}ntd| ƒ‚d|
|||j	|f }|rŒ|d| 7 }|rž|d| 7 }|d| 7 }|rÀ|d||f 7 }|S )Nr   rb  ÚqopÚ	algorithmÚMD5Úopaquez%s:%s:%sz%s:%srE  ú,rR   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  r&  ro   rI   Úget_entity_digestr   r‡   r;  rQ  rP  rf  r   )r   rÍ   rZ  r   rb  rg  rh  rj  ÚHÚKDr
  rC  ZentdigZA1ZA2ZrespdigZncvalueZcnonceZnoncebitr*  rL   rL   rM   rX  {  sV    





z+AbstractDigestAuthHandler.get_authorizationc                sD   |dkrdd„ ‰ n|dkr$dd„ ‰ nt d| ƒ‚‡ fdd„}ˆ |fS )Nri  c             S   s   t  |  d¡¡ ¡ S )Nr  )r_  Zmd5r  ra  )ÚxrL   rL   rM   r  ¼  ó    z?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAc             S   s   t  |  d¡¡ ¡ S )Nr  )r_  r`  r  ra  )rq  rL   rL   rM   r  ¾  rr  z.Unsupported digest authentication algorithm %rc                s   ˆ d| |f ƒS )Nz%s:%srL   )rc  Úd)ro  rL   rM   r  Ã  rr  )rB   )r   rh  rp  rL   )ro  rM   rm  ¹  s    

z-AbstractDigestAuthHandler.get_algorithm_implsc             C   s   d S )NrL   )r   rI   rZ  rL   rL   rM   rn  Æ  s    z+AbstractDigestAuthHandler.get_entity_digest)N)r    r¡   r¢   r‚   rR  rA  rT  rf  rX  rm  rn  rL   rL   rL   rM   r&   9  s   
	>c               @   s    e Zd ZdZdZdZdd„ ZdS )r'   z¨An authentication protocol defined by RFC 2069

    Digest authentication improves on basic authentication because it
    does not transmit passwords in the clear.
    rF  iê  c             C   s*   t |jƒd }|  d|||¡}|  ¡  |S )NrR   zwww-authenticate)r   ro   rA  rR  )r   rÍ   rc   rä   rå   rd   ru   ÚretryrL   rL   rM   rM  Õ  s
    
z$HTTPDigestAuthHandler.http_error_401N)r    r¡   r¢   rç   rB  rà   rM  rL   rL   rL   rM   r'   Ë  s   c               @   s   e Zd ZdZdZdd„ ZdS )r(   zProxy-Authorizationiê  c             C   s"   |j }|  d|||¡}|  ¡  |S )Nzproxy-authenticate)ru   rA  rR  )r   rÍ   rc   rä   rå   rd   ru   rt  rL   rL   rM   rN  â  s
    
z%ProxyDigestAuthHandler.http_error_407N)r    r¡   r¢   rB  rà   rN  rL   rL   rL   rM   r(   Ý  s   c               @   s6   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚAbstractHTTPHandlerr   c             C   s
   || _ d S )N)Ú_debuglevel)r   Ú
debuglevelrL   rL   rM   r‚   ë  s    zAbstractHTTPHandler.__init__c             C   s
   || _ d S )N)rv  )r   ÚlevelrL   rL   rM   Úset_http_debuglevelî  s    z'AbstractHTTPHandler.set_http_debuglevelc             C   s   t jj |j| ¡ ¡S )N)rÐ   r×   ÚHTTPConnectionÚ_get_content_lengthrI   r   )r   rt   rL   rL   rM   r{  ñ  s    z'AbstractHTTPHandler._get_content_lengthc             C   s  |j }|stdƒ‚|jd k	r’|j}t|tƒr8d}t|ƒ‚| d¡sN| dd¡ | d¡s’| d¡s’|  |¡}|d k	r†| dt|ƒ¡ n| dd¡ |}| 	¡ r¸t
|jƒ\}}t|ƒ\}}	| d¡sÎ| d|¡ x2| jjD ]&\}
}|
 ¡ }
| |
¡sØ| |
|¡ qØW |S )	Nzno host givenz\POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.zContent-typez!application/x-www-form-urlencodedzContent-lengthzTransfer-encodingZchunkedrn   )ru   r   rI   rÉ   rÊ   rµ   rˆ   r–   r{  r“   r   r‡   r   rß   r§   r”   )r   rt   ru   rI   rå   Zcontent_lengthZsel_hostrû   ZselZsel_pathrZ   r   rL   rL   rM   Údo_request_ö  s>    







zAbstractHTTPHandler.do_request_c       
   
      sT  |j }|stdƒ‚||fd|ji|—Ž}| | j¡ t|jƒ‰ ˆ  ‡ fdd„|j 	¡ D ƒ¡ dˆ d< dd„ ˆ  	¡ D ƒ‰ |j
r®i }d}|ˆ kržˆ | ||< ˆ |= |j|j
|d	 y`y&|j| ¡ |j|jˆ | d
¡d W n, tk
r } zt|ƒ‚W dd}~X Y nX | ¡ }	W n   | ¡  ‚ 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 givenrJ   c                s   i | ]\}}|ˆ kr||“qS rL   rL   )rì   rí   rî   )rd   rL   rM   rð   *  s    z/AbstractHTTPHandler.do_open.<locals>.<dictcomp>rÃ   Ú
Connectionc             S   s   i | ]\}}||  ¡ “qS rL   )Útitle)rì   rZ   r•   rL   rL   rM   rð   7  s    zProxy-Authorization)rd   zTransfer-encoding)Zencode_chunkedN)ru   r   rJ   Zset_debuglevelrv  rÓ   rw   rœ   rd   rz   ry   Z
set_tunnelrt   r   r‡   rI   rˆ   rl   ÚgetresponserÃ   Zsockr   rH   Úreasonrå   )
r   Z
http_classrÍ   Zhttp_conn_argsru   rÞ   Ztunnel_headersZproxy_auth_hdrÚerrr  rL   )rd   rM   r¯     s@    


zAbstractHTTPHandler.do_openN)r   )r    r¡   r¢   r‚   ry  r{  r|  r¯   rL   rL   rL   rM   ru  é  s
   
&ru  c               @   s   e Zd Zdd„ ZejZdS )r)   c             C   s   |   tjj|¡S )N)r¯   rÐ   r×   rz  )r   rÍ   rL   rL   rM   Ú	http_opena  s    zHTTPHandler.http_openN)r    r¡   r¢   r‚  ru  r|  rH  rL   rL   rL   rM   r)   _  s   rÖ   c               @   s$   e Zd Zddd„Zdd„ ZejZdS )rE   r   Nc             C   s   t  | |¡ || _|| _d S )N)ru  r‚   Ú_contextÚ_check_hostname)r   rw  r>   Úcheck_hostnamerL   rL   rM   r‚   j  s    zHTTPSHandler.__init__c             C   s   | j tjj|| j| jdS )N)r>   r…  )r¯   rÐ   r×   rÖ   rƒ  r„  )r   rÍ   rL   rL   rM   Ú
https_openo  s    zHTTPSHandler.https_open)r   NN)r    r¡   r¢   r‚   r†  ru  r|  rL  rL   rL   rL   rM   rE   h  s   
rE   c               @   s.   e Zd Zddd„Zdd„ Zdd„ ZeZeZdS )	r   Nc             C   s$   dd l }|d kr|j ¡ }|| _d S )Nr   )Zhttp.cookiejarÚ	cookiejarZ	CookieJar)r   r‡  rÐ   rL   rL   rM   r‚   x  s    
zHTTPCookieProcessor.__init__c             C   s   | j  |¡ |S )N)r‡  Zadd_cookie_header)r   rt   rL   rL   rM   rH  ~  s    z HTTPCookieProcessor.http_requestc             C   s   | j  ||¡ |S )N)r‡  Zextract_cookies)r   rt   r³   rL   rL   rM   ræ   ‚  s    z!HTTPCookieProcessor.http_response)N)r    r¡   r¢   r‚   rH  ræ   rL  rè   rL   rL   rL   rM   r   w  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Š   rL   rL   rM   rÏ   Š  s    zUnknownHandler.unknown_openN)r    r¡   r¢   rÏ   rL   rL   rL   rM   r.   ‰  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.ú=rR   r   r4  rQ   )r;  )ÚlZparsedZeltrí   rî   rL   rL   rM   rU  Ž  s    
rU  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.
    rm   Fú\Tr4  rk  c             S   s   g | ]}|  ¡ ‘qS rL   )rG  )rì   ÚpartrL   rL   rM   ú
<listcomp>Á  s    z#parse_http_list.<locals>.<listcomp>)r\   )rc  Zresr‹  Úescaper	   ZcurrL   rL   rM   rW  ˜  s4    	


rW  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//r6  r÷   Ú	localhostz-file:// scheme is supported only on localhost)r‡   ru   Ú	get_namesr   Úopen_local_file)r   rÍ   rH   rL   rL   rM   Ú	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   rL   rL   rM   r  Ð  s    
zFileHandler.get_namesc          
   C   sü   dd l }dd l}|j}|j}t|ƒ}y t |¡}|j}|jj	|j
dd}	| |¡d }
| d|
pbd||	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)Úemail.utilsÚ	mimetypesru   r‡   r4   rV   ÚstatÚst_sizeÚutilsÚ
formatdateÚst_mtimeÚ
guess_typeÚmessage_from_stringr   Ú_safe_gethostbynamer  r   rG   rl   r   )r   rÍ   Úemailrš  ru   ra   Z	localfileÚstatsrh   ÚmodifiedÚmtyperd   r)  ZorigurlÚexprL   rL   rM   r  Û  s0    
zFileHandler.open_local_file)r    r¡   r¢   r‘  r’  r  r  rL   rL   rL   rM   r*   Ã  s   
c             C   s&   y
t  | ¡S  t jk
r    d S X d S )N)rÔ   r–  r•  )ru   rL   rL   rM   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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¡}|r6dp8d}x:|
D ]2}t|ƒ\}}| ¡ d	kr@|d
kr@| ¡ }q@W | ||¡\}}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  ¡ d ¡‚W d d }~X Y nX d S )Nr   zftp error: no host givenrm   r÷   rQ   rR   rK  ÚDrŠ   )ÚaÚAr½   rK  rs  r¨  zContent-type: %s
zContent-length: %d
zftp error: %rr:   )!Úftplibrš  ru   r   r   ÚFTP_PORTr]   r   r   r
   rÔ   r–  rl   r   r‡   r;  r   ÚmapÚconnect_ftprJ   r   rs   ÚupperÚretrfiler   ro   r£  r¡  r   Ú
all_errorsÚwith_tracebackÚsysÚexc_info)r   rÍ   r«  rš  ru   r)  r
  r  rå   rW   ÚattrsÚdirsrN   ÚfwrŠ   Úattrr   rc   Úretrlenrd   r¦  r§  ÚexcrL   rL   rM   Úftp_opený  s\    



zFTPHandler.ftp_openc          	   C   s   t ||||||ddS )NF)Ú
persistent)Ú
ftpwrapper)r   r
  r  ru   r)  r¶  rJ   rL   rL   rM   r®  2  s    zFTPHandler.connect_ftpN)r    r¡   r¢   r»  r®  rL   rL   rL   rM   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\  )ÚcacherJ   ÚsoonestÚdelayÚ	max_conns)r   rL   rL   rM   r‚   9  s
    zCacheFTPHandler.__init__c             C   s
   || _ d S )N)rÁ  )r   ÚtrL   rL   rM   Ú
setTimeout@  s    zCacheFTPHandler.setTimeoutc             C   s
   || _ d S )N)rÂ  )r   ró   rL   rL   rM   ÚsetMaxConnsC  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Á  rJ   r½  Úcheck_cache)r   r
  r  ru   r)  r¶  rJ   r€   rL   rL   rM   r®  F  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   rJ   rz   r¿  rÃ   ÚminÚvaluesr_   rÂ  )r   rÃ  rí   rî   rL   rL   rM   rÇ  Q  s    

zCacheFTPHandler.check_cachec             C   s4   x| j  ¡ D ]}| ¡  qW | j  ¡  | j ¡  d S )N)r¿  rÉ  rÃ   ÚclearrJ   )r   ÚconnrL   rL   rM   Úclear_cachee  s    
zCacheFTPHandler.clear_cacheN)	r    r¡   r¢   r‚   rÄ  rÅ  r®  rÇ  rÌ  rL   rL   rL   rM   r,   6  s   c               @   s   e Zd Zdd„ ZdS )r-   c             C   s~   |j }| dd¡\}}| dd¡\}}t|ƒ}| d¡rNt |¡}|d d… }|sVd}t d|t|ƒf ¡}t	t
 |¡||ƒS )Nú:rR   rk  z;base64iùÿÿÿztext/plain;charset=US-ASCIIz$Content-type: %s
Content-length: %d
)ro   r;  r   Úendswithr  Údecodebytesr£  r¡  r_   r   ÚioÚBytesIO)r   rÍ   rH   rû   rI   Z	mediatyperd   rL   rL   rM   Ú	data_openl  s    


zDataHandler.data_openN)r    r¡   r¢   rÒ  rL   rL   rL   rM   r-   k  s   rê   Únt)r4   r3   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
   )ÚpathnamerL   rL   rM   r4   ’  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Ô  rL   rL   rM   r3   —  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 )3r8   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d
g| _g | _tj| _d | _t| _d S )NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methodsÚclassr6  )Ú
stacklevelr  zproxies must be a mappingÚkey_fileÚ	cert_filez
User-Agent)ZAcceptz*/*)r0  r    r?   r@   rA   r5   r´   r  r  r˜   r×  rØ  Úversionr§   Ú_URLopener__tempfilesrV   rk   Ú_URLopener__unlinkÚ	tempcacheÚftpcache)r   r  Zx509rå   rL   rL   rM   r‚   ­  s    zURLopener.__init__c             C   s   |   ¡  d S )N)rÃ   )r   rL   rL   rM   Ú__del__Ç  s    zURLopener.__del__c             C   s   |   ¡  d S )N)Úcleanup)r   rL   rL   rM   rÃ   Ê  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Û  rl   rÜ  rÊ  )r   rN   rL   rL   rM   rß  Í  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§   r\   )r   rÆ   rL   rL   rM   Ú	addheaderÛ  s    zURLopener.addheaderc          
   C   sl  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| |ƒrÆ|d	krä|rØ|  |||¡S |  ||¡S y,|dk rþt| |ƒ|ƒS t| |ƒ||ƒS W nV ttfk
r,   ‚ Y n< tk
rf } ztd
|ƒ t ¡ d ¡‚W dd}~X Y nX dS )z6Use URLopener().open(file) instead of open(file, 'r').z%/:=&?~#+!$,;'@()*[]|)rù   r˜  rN   NZopen_ú-r±   r  zsocket errorr:   )r   r   r	   rÜ  rG   r   r   r  r   rŠ   rñ   r´   Úopen_unknown_proxyÚopen_unknownrŽ   r   r   rl   r²  r³  r´  )r   rÌ   rI   ra   rd   rc   ÚurltyperH   r  Ú	proxyhostru   r‡   rZ   rå   rL   rL   rM   rG   á  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   rl   )r   rÌ   rI   rŠ   rH   rL   rL   rM   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   rl   )r   r  rÌ   rI   rŠ   rH   rL   rL   rM   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 |  
||¡}z>| ¡ }
|rÀt
|dƒ}nrt|ƒ\}}t|pÔdƒ\}}t|pädƒ\}}t|pôdƒ\}}tj |¡d }t |¡\}}| j |¡ t |d¡}z¤||
f}| jdk	rR|| j|< d}d}d}d}d	|
krxt|
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.NrN   rR   rO   rm   i    rQ   r   zcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytes)r   r   rÜ  r   r  rU   rÃ   r4   r   rl   rG   r   r   rV   rW   ÚsplitextrY   ZmkstemprÚ  r\   Úfdopenr]   r^   r_   r`   r   )r   rH   ra   rb   rI   rŠ   Zurl1rc   rž   rå   rd   re   ZgarbagerW   ÚsuffixÚfdrf   rg   rh   r^   ri   rj   rL   rL   rM   Úretrieve  sj    






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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-AuthorizationrF  rn   rÃ   r}  z!application/x-www-form-urlencodedzContent-TyperŒ   r   )rd   z$http protocol error: bad status linerã   i,  zhttp:)rÉ   rÊ   r   r   r
   r   rs   r  rl   r  r  r  r  r§   rt   r  rÐ   r×   ZBadStatusLiner   Zstatusr   rå   Ú
http_errorrc   r€  )r   Zconnection_factoryrH   rI   Zuser_passwdZproxy_passwdru   r‡   Zrealhosträ  r‹   Z
proxy_authrE  Z	http_connrd   r<  r   r³   rL   rL   rM   Ú_open_generic_httpQ  sr    
 

zURLopener._open_generic_httpc             C   s   |   tjj||¡S )zUse HTTP protocol.)rì  rÐ   r×   rz  )r   rH   rI   rL   rL   rM   Ú	open_http­  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   rH   rc   ÚerrcodeÚerrmsgrd   rI   rZ   r~   rf   rL   rL   rM   rë  ±  s    

 zURLopener.http_errorc             C   s   |  ¡  t||||dƒ‚dS )z>Default error handler: close the connection and raise OSError.N)rÃ   r   )r   rH   rc   rî  rï  rd   rL   rL   rM   rÑ   Á  s    zURLopener.http_error_defaultc             C   s   t jj|| j| jdS )N)r×  rØ  )rÐ   r×   rÖ   r×  rØ  )r   ru   rL   rL   rM   Ú_https_connectionÇ  s    zURLopener._https_connectionc             C   s   |   | j||¡S )zUse HTTPS protocol.)rì  rð  )r   rH   rI   rL   rL   rM   Ú
open_httpsÌ  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//r6  r÷   é   z
localhost/z-file:// scheme is supported only on localhost)rÉ   rÊ   r   rs   rB   r  )r   rH   rL   rL   rM   Ú	open_fileÐ  s
    
4
zURLopener.open_filec          
   C   s\  ddl }ddl}t|ƒ\}}t|ƒ}yt |¡}W n0 tk
rb } zt|j|j	ƒ‚W dd}~X Y nX |j
}	|jj|jdd}
| |¡d }| d|p–d|	|
f ¡}|sÔ|}|dd… dkrÂd	| }tt|d
ƒ||ƒS t|ƒ\}}|sPt |¡tƒ ftƒ  krP|}|dd… dkr d	| }n|dd… dkr>t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/plainrR   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   r4   rV   r›  rl   r   Ústrerrorra   rœ  r  rž  rŸ  r   r¡  r   rG   r   rÔ   r–  rŽ  ÚthishostrB   )r   rH   r£  rš  ru   rN   Z	localnamer¤  Úerh   r¥  r¦  rd   Zurlfiler)  rL   rL   rM   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rfx8t| 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d| ƒ  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 givenrm   r÷   rQ   rR   r¨  rK  rŠ   )r©  rª  r½   rK  rs  r¨  zftp:zContent-Type: %s
zContent-Length: %d
zftp error %rr:   )#rÉ   rÊ   r   rš  r   r   r   r   r
   rÔ   r–  r«  r¬  r]   r   r;  rÆ  r_   rÝ  ÚMAXFTPCACHEr   rÃ   r½  r   rs   r¯  r°  r   r£  r¡  r   Ú	ftperrorsr²  r³  r´  )r   rH   rš  ru   rW   r)  r
  r  r«  rµ  r¶  rN   r€   rí   rî   rŠ   r¸  r   rc   r¹  r¦  rd   r§  rL   rL   rM   Ú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 implementedrk  rR   z
data errorzbad data URLztext/plain;charset=US-ASCIIú;r   rˆ  Nrm   zDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %sr  r  zlatin-1zContent-Length: %dÚ
)rÉ   rÊ   r   r;  rB   rl   Úrfindr\   r]  ZstrftimeZgmtimer  rÏ  r  r  r
   r_   rÆ  r£  r¡  rÐ  ÚStringIOr   )	r   rH   rI   rŠ   Zsemirø   rå   rd   ÚfrL   rL   rM   Ú	open_data3  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à  rG   rã  râ  rê  rì  rí  rë  rÑ   rC   rð  rñ  ró  r  rù  rÿ  rL   rL   rL   rM   r8      s.   

$


A\


	 :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 ).r9   z?Derived class with handlers for errors we can handle (perhaps).c             O   s(   t j| f|ž|Ž i | _d| _d| _d S )Nr   rê   )r8   r‚   Ú
auth_cacheÚtriesÚmaxtries)r   rÆ   r/  rL   rL   rM   r‚   `  s    zFancyURLopener.__init__c             C   s   t ||d| |ƒS )z3Default error handling -- don't raise an exception.zhttp:)r   )r   rH   rc   rî  rï  rd   rL   rL   rM   rÑ   f  s    z!FancyURLopener.http_error_defaultNc       	      C   sn   |  j d7  _ zR| jrJ| j | jkrJt| dƒr4| j}n| j}|||dd|ƒS |  ||||||¡}|S d| _ X dS )z%Error 302 -- relocated (temporarily).rR   Úhttp_error_500iô  z)Internal Server Error: Redirect RecursionNr   )r  r  r´   r  rÑ   Úredirect_internal)	r   rH   rc   rî  rï  rd   rI   r¼   rf   rL   rL   rM   r  j  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‘   rö   rm   z( Redirection to url '%s' is not allowed.)rÃ   r   rŠ   r   rû   r   rG   )	r   rH   rc   rî  rï  rd   rI   rò   r   rL   rL   rM   r  |  s    


z FancyURLopener.redirect_internalc             C   s   |   ||||||¡S )z*Error 301 -- also relocated (permanently).)r  )r   rH   rc   rî  rï  rd   rI   rL   rL   rM   r  ˜  s    zFancyURLopener.http_error_301c             C   s   |   ||||||¡S )z;Error 303 -- also relocated (essentially identical to 302).)r  )r   rH   rc   rî  rï  rd   rI   rL   rL   rM   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   rH   rc   rî  rï  rd   rI   rL   rL   rM   r     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)r8   rÑ   rI  Úmatchr9  rs   rŠ   rŽ   )r   rH   rc   rî  rï  rd   rI   rt  Ústuffr  rû   r   rZ   rL   rL   rM   rM  §  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)r8   rÑ   rI  r  r9  rs   rŠ   rŽ   )r   rH   rc   rî  rï  rd   rI   rt  r  r  rû   r   rZ   rL   rL   rM   rN  À  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Ð   ú@rR   z%s:%s@%srm   )rù   )r   r  r   r·   Úget_user_passwdr	   rG   )r   rH   r   rI   ru   r‡   rò   r  rä  rå  Úproxyselectorr½   r
  r  rL   rL   rM   Ú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  rR   z%s:%s@%srm   )rù   )r   r  r   r·   r	  r	   rG   )r   rH   r   rI   ru   r‡   rò   r  rä  rå  r
  r½   r
  r  rL   rL   rM   Ú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  rR   z%s:%s@%srm   )rù   zhttp://)r   r·   r	  r	   rG   )
r   rH   r   rI   ru   r‡   r½   r
  r  rò   rL   rL   rM   r?  ý  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  rR   z%s:%s@%srm   )rù   zhttps://)r   r·   r	  r	   rG   )
r   rH   r   rI   ru   r‡   r½   r
  r  rò   rL   rL   rM   Ú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  )rs   r   Úprompt_user_passwd)r   ru   r   rÌ  r€   r
  r  rL   rL   rM   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)ÚgetpassÚinputÚKeyboardInterruptÚprint)r   ru   r   r  r
  r  rL   rL   rM   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  rM  rN  r  r  r?  r  r	  r  rL   rL   rL   rM   r9   ]  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–  rL   rL   rL   rM   rŽ  4	  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•  rL   rL   rL   rM   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«  rL   rL   rM   rø  G	  s    rø  c               C   s   t dkrt d¡a t S )z%Return an empty email Message object.Nrm   )Ú
_noheadersr£  r¡  rL   rL   rL   rM   Ú	noheadersP	  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   sX   || _ || _|| _|| _|| _|| _d| _|| _y|  ¡  W n   |  	¡  ‚ Y nX d S )Nr   )
r
  r  ru   r)  r¶  rJ   ÚrefcountÚ	keepaliveÚinitrÃ   )r   r
  r  ru   r)  r¶  rJ   r¼  rL   rL   rM   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connectru   r)  rJ   Zloginr
  r  rÆ  r¶  Úcwd)r   r«  Z_targetrL   rL   rM   r  m	  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 nR |jk
rä } z2t|ƒd d… dkrÔt	d	| ƒ 
t ¡ d
 ¡‚W d d }~X Y nX |s€| j d¡ |rl| j ¡ }	zJy| j |¡ W n4 |jk
rN } 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   )rs  r¨  zTYPE ArR   zTYPE zRETR r6  Z550zftp error: %rr:   zLIST ZLISTr˜  )r«  Úendtransferrö   Zvoidcmdr±  r  ZntransfercmdZ
error_permrÊ   r   r²  r³  r´  Úpwdr  r  r   ZmakefileÚ
file_closer  rÃ   )r   rN   rŠ   r«  ÚcmdÚisdirrË  r¹  r€  r  ZftpobjrL   rL   rM   r°  v	  sN        
$
zftpwrapper.retrfilec             C   s
   d| _ d S )Nr   )r  )r   rL   rL   rM   r  £	  s    zftpwrapper.endtransferc             C   s   d| _ | jdkr|  ¡  d S )NFr   )r  r  Ú
real_close)r   rL   rL   rM   rÃ   ¦	  s    
zftpwrapper.closec             C   s2   |   ¡  |  jd8  _| jdkr.| js.|  ¡  d S )NrR   r   )r  r  r  r"  )r   rL   rL   rM   r  «	  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   rL   rL   rM   r"  ±	  s
    zftpwrapper.real_close)NT)r    r¡   r¢   rç   r‚   r  r°  r  rÃ   r  r"  rL   rL   rL   rM   r½  Z	  s    
	-r½  c              C   s¼   i } xBt j ¡ D ]4\}}| ¡ }|r|dd… dkr|| |dd… < qW dt jkr^|  dd¡ xXt j ¡ D ]J\}}|dd… dkrj| ¡ }|r || |dd… < qj|  |dd… d¡ qjW | 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Ú_proxyZREQUEST_METHODrÐ   )rV   Úenvironrz   rs   rš   )r  rZ   r   rL   rL   rM   Úgetproxies_environment¹	  s    	
r%  c             C   sº   |dkrt ƒ }y|d }W n tk
r.   dS X |dkr<dS |  ¡ } t| ƒ\}}xd| d¡D ]V}| ¡ }|r\| d¡}| ¡ }||ksŽ| |kr’dS d| }| |¡s®|  |¡r\dS q\W dS )zÇTest if proxies should not be used for a particular host.

    Checks the proxy dict for the value of no_proxy, which should
    be a list of comma separated DNS suffixes, or '*' for all hosts.

    NZnoFÚ*Trk  Ú.)r%  rl  rs   r   r;  rG  ÚlstriprÎ  )ru   r  Zno_proxyÚhostonlyr)  rZ   rL   rL   rM   Úproxy_bypass_environmentØ	  s*    
r*  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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 )
Nr'  ré   r   é   rR   r\  r:   r[  r6  )r;  r   r­  r]   r_   )ZipAddrr(  rL   rL   rM   Úip2num
  s
    
z,_proxy_bypass_macosx_sysconf.<locals>.ip2numr'  Zexclude_simpleTNÚ
exceptionsrL   z(\d+(?:\.\d+)*)(/\d+)?rR   r:   r[  é    F)r+  r   r˜   rI  r  rÔ   r–  rl   ÚgroupÚcountr]   )ru   Úproxy_settingsr+  r)  r)  r-  ZhostIPr   ró   r*  ÚmaskrL   rL   rM   Ú_proxy_bypass_macosx_sysconfý	  s:     



r4  Údarwin)Ú_get_proxy_settingsÚ_get_proxiesc             C   s   t ƒ }t| |ƒS )N)r6  r4  )ru   r2  rL   rL   rM   Úproxy_bypass_macosx_sysconf<
  s    r8  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.
        )r7  rL   rL   rL   rM   Úgetproxies_macosx_sysconf@
  s    r9  c             C   s    t ƒ }|rt| |ƒS t| ƒS dS )z¸Return True, if host should be bypassed.

        Checks proxy settings gathered from the environment, if specified,
        or from the MacOSX framework SystemConfiguration.

        N)r%  r*  r8  )ru   r  rL   rL   rM   r  J
  s    
r  c               C   s   t ƒ p
tƒ S )N)r%  r9  rL   rL   rL   rM   r5   W
  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ú  rR   z(?:[^/:]+)://z%s://%srS  zhttp:rÐ   z	http://%sz
https://%sr‘   zftp://%srö   )ÚwinregÚImportErrorÚOpenKeyÚHKEY_CURRENT_USERÚQueryValueExrÊ   r;  rI  r  ZCloserl   rB   rµ   )r  r;  ÚinternetSettingsÚproxyEnableZproxyServerÚpr¾   ZaddressrL   rL   rM   Úgetproxies_registry\
  s8    

rC  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.

        )r%  rC  rL   rL   rL   rM   r5   ‰
  s    c             C   sx  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k
rÀ   Y nX y t |¡}||krà|  
|¡ W n t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¡rNdS qNW qW dS )Nr   z;Software\Microsoft\Windows\CurrentVersion\Internet Settingsr:  ZProxyOverriderú  z<local>r'  rR   z\.r&  z.*ú?)r;  r<  r=  r>  r?  rÊ   rl   r   rÔ   r–  r\   Zgetfqdnr;  rñ   rI  r  rK  )ru   r;  r@  rA  ZproxyOverrideZrawHostr)  ZaddrZfqdnr+  r•   rL   rL   rM   Úproxy_bypass_registry’
  sR    







rE  c             C   s    t ƒ }|rt| |ƒS t| ƒS dS )z—Return True, if host should be bypassed.

        Checks proxy settings gathered from the environment, if specified,
        or the registry.

        N)r%  r*  rE  )ru   r  rL   rL   rM   r  Ä
  s    
)NNN)N)~rç   r  rº   r£  r_  Zhttp.clientrÐ   rÐ  rV   Ú	posixpathrI  rÔ   rü   r³  r]  rY   rS   r?   Zurllib.errorr   r   r   Zurllib.parser   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   Zurllib.responser   r   rD   r<  rC   Ú__all__Úversion_infor¦   rF   rÕ   r0   r1   r[   r6   r7   rJ  ÚASCIIrq   rv   r   r   r2   r   r/   r   r   r  r   r    r!   r"   r#   r$   r%   Úurandomr^  r&   r'   r(   ru  r)   r´   r×   rE   r\   r   r.   rU  rW  r*   r¢  r+   r,   r-   r÷  rZ   Z
nturl2pathr4   r3   rÝ  r8   r9   r  rŽ  r  rõ  r  rø  r  r  r½  r%  r*  r4  ÚplatformZ_scproxyr6  r7  r8  r9  r  r5   rC  rE  rL   rL   rL   rM   Ú<module>D   sê   P
T
?n $q*@
o v

+3:5!   @ W

_
%<

-	2
