U
    pdҌ                 !   @   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+ z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rd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rddplumvZvmwZw dqdr Zxdsdt Zydudv Zzdwd$ Z{n6ejadYkrdxdy 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rt
  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   $/usr/lib/python3.8/urllib/request.pyr0      s2    < 




c                 C   s   | a d S N)rF   )rK   rL   rL   rM   r1      s    c              
   C   s:  t | \}}tt| |}| }|dkrN|sNtj||fW  5 Q R  S |r^t|d}nt	j
dd}|j}t| | ||f}	d}
d}d}d}d|krt|d	 }|r|||
| ||
}|sq|t|7 }|| |d
7 }|r|||
| qW 5 Q R X W 5 Q R X |dkr6||k r6t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)delete    r   content-lengthContent-Length   1retrieval 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_typer\   fpheaderstfpresultbssizerc   blocknumblockrL   rL   rM   r6      sH    


"c               	   C   sD   t D ](} zt|  W q tk
r*   Y qX qt dd= tr@dadS )z0Clean up temporary files from urlretrieve calls.N)r`   r[   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 )zReturn request-host, as defined by RFC 2965.

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

    rV    Host)full_urlr   
get_header_cut_port_resublower)requestrH   hostrL   rL   rM   request_host3  s    r{   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   sl   || _ i | _i | _d | _|| _d | _| D ]\}}| || q,|d krRt| }|| _	|| _
|rh|| _d S rN   )rt   ri   unredirected_hdrs_datarI   _tunnel_hostitems
add_headerr{   origin_req_hostunverifiablemethod)	selfrH   rI   ri   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   rt   W  s    zRequest.full_urlc                 C   s(   t || _t| j\| _| _|   d S rN   )r   r   r   r   _parser   rH   rL   rL   rM   rt   ]  s    
c                 C   s   d | _ d | _d| _d S )Nrr   )r   r   selectorr   rL   rL   rM   rt   d  s    c                 C   s   | j S rN   )r}   r   rL   rL   rM   rI   j  s    zRequest.datac                 C   s(   || j kr$|| _ | dr$| d d S )NContent-length)r}   
has_headerremove_header)r   rI   rL   rL   rM   rI   n  s    

c                 C   s
   d | _ d S rN   )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   rt   r   rz   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 rN   )rt   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   r~   rz   rt   r   )r   rz   r   rL   rL   rM   	set_proxy  s
    
zRequest.set_proxyc                 C   s   | j | jkS rN   )r   rt   r   rL   rL   rM   	has_proxy  s    zRequest.has_proxyc                 C   s   || j | < d S rN   )ri   
capitalizer   r   valrL   rL   rM   r     s    zRequest.add_headerc                 C   s   || j | < d S rN   )r|   r   r   rL   rL   rM   add_unredirected_header  s    zRequest.add_unredirected_headerc                 C   s   || j kp|| jkS rN   )ri   r|   r   header_namerL   rL   rM   r     s    
zRequest.has_headerc                 C   s   | j || j||S rN   )ri   getr|   )r   r   defaultrL   rL   rM   ru     s    zRequest.get_headerc                 C   s    | j |d  | j|d  d S rN   )ri   popr|   r   rL   rL   rM   r     s    zRequest.remove_headerc                 C   s   | j | j}t| S rN   )r|   ri   listr   )r   hdrsrL   rL   rM   header_items  s    zRequest.header_items)N)__name__
__module____qualname__r   propertyrt   setterdeleterrI   r   r   r   r   r   r   r   r   ru   r   r   rL   rL   rL   rM   r   C  s8    





	

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 )N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   sT  t |dstdt| d}t|D ]}|dkr6q&|d}|d | }||d d  }|dr|d| d }||d d  }z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&|rPt| j| ||  d S )N
add_parentz%expected BaseHandler instance, got %rF)redirect_requestdo_open
proxy_open_rV   errorrG   responsery   T)hasattr	TypeErrorr   dirfind
startswithrb   rB   r   r   r   r   r   
setdefaultbisectZinsortra   r   r   )r   handlerZaddedmethiprotocolZ	conditionjkindlookupr   rL   rL   rM   add_handler  sL    



zOpenerDirector.add_handlerc                 C   s   d S rN   rL   r   rL   rL   rM   close  s    zOpenerDirector.closec           	      G   s<   | |d}|D ]&}t||}|| }|d k	r|  S qd S )NrL   )r   r   )	r   chainr   	meth_nameargsr   r   funcrk   rL   rL   rM   _call_chain  s    
zOpenerDirector._call_chainNc           
      C   s   t |trt||}n|}|d k	r(||_||_|j}|d }| j|g D ]}t||}||}qJt	
d|j|j|j|  | ||}	|d }| j|g D ]}t||}|||	}	q|	S )NZ_requestzurllib.RequestZ	_response)
isinstancestrr   rI   rJ   r   r   r   r   sysauditrt   ri   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   rk   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_%srV   Z_errorr   r   http_error_default)r   r   )r   protor   dictr   Zhttp_errZ	orig_argsrk   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 }|D ]B}| D ]8}t|trht||r||| qDt||rD|| qDq<|D ]}|| q|D ]}||  q| D ]}t|tr| }|| q|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   clientra   rE   setr   r   
issubclassaddremover   )r   rK   Zdefault_classesskipklassZcheckhrL   rL   rM   r2   ?  s8    	   




c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r     c                 C   s
   || _ d S rN   )parent)r   r   rL   rL   rM   r   f  s    zBaseHandler.add_parentc                 C   s   d S rN   rL   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   ,  r   )codemsgrZ   r   r   )r   ry   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 rN   )r   rt   )r   r   rh   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.
        )-  .  /  i3  )r   ZHEAD)r   r   r   r    z%20)rT   zcontent-typec                    s"   i | ]\}}|   kr||qS rL   )rx   .0kvZCONTENT_HEADERSrL   rM   
<dictcomp>  s     z8HTTPRedirectHandler.redirect_request.<locals>.<dictcomp>T)ri   r   r   )r   r   rt   replaceri   r   r   r   )	r   r   rh   r   r   ri   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rd S t|d	r|j }	|_|	|d
| jkst|	| jkrt|j|| j| ||ni  }	 |_|_|	|d
d |	|< |  |  | jj||jdS )Nlocationurir   r   ftprr   z+%s - Redirection to url '%s' is not allowed/r:   z
iso-8859-1)encodingsaferedirect_dictr   rV   rJ   )r   schemer   r\   Znetlocr   r   r	   stringZpunctuationr   rt   r   r   r  r   max_repeatsrd   max_redirectionsinf_msgrc   r   r   rG   rJ   )
r   r   rh   r   r   ri   r  urlpartsnewZvisitedrL   rL   rM   http_error_302  sT    


 
     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}| }nZ|ds6td|  d|krV|d}|d|}n|dd}|dkrnd}|d| }t|\}}|dk	rt|\}}	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
  N//zproxy URL with no authority: %r@r:   rS   )r   r   rB   r   r   r   )
proxyr  Zr_scheme	authorityZhost_separator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   sb   |d krt  }t|ds td|| _| D ].\}}| }t| d| ||| jfdd q.d S )Nkeysproxies must be a mappingz%s_openc                 S   s   || ||S rN   rL   )rr  r   r   rL   rL   rM   <lambda>)  s    z'ProxyHandler.__init__.<locals>.<lambda>)r5   r   AssertionErrorproxiesr   rx   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rd S | jj||jdS d S )N%s:%sasciiProxy-authorizationBasic r   r  )r   r"  rz   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 rN   )passwdr   rL   rL   rM   r   J  s    zHTTPPasswordMgr.__init__c                    s\   t |tr|g}|jkr$i j|< dD ]. t fdd|D }||fj| |< q(d S )NTFc                 3   s   | ]} | V  qd S rN   )
reduce_uri)r   udefault_portr   rL   rM   	<genexpr>T  s    z/HTTPPasswordMgr.add_password.<locals>.<genexpr>)r   r   r4  tuple)r   realmr  r   r4  reduced_urirL   r8  rM   add_passwordM  s    


zHTTPPasswordMgr.add_passwordc           	      C   s`   | j |i }dD ]H}| ||}| D ].\}}|D ] }| ||r6|      S q6q*qdS )Nr5  NN)r4  r   r6  r   	is_suburi)	r   r<  authuriZdomainsr9  reduced_authuriZurisZauthinfor  rL   rL   rM   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.rV   r   r:   r
  NP   i  r   z%s:%d)r   r   r   )
r   r  r9  partsr  r  r\   rz   portZdportrL   rL   rM   r6  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   FrV   )	posixpathcommonprefixrd   )r   basetestcommonrL   rL   rM   r@  y  s    zHTTPPasswordMgr.is_suburiN)T)r   r   r   r   r>  rC  r6  r@  rL   rL   rL   rM   r    H  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 rN   )r    rC  )r   r<  rA  r   r!  rL   rL   rM   rC    s    z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r   r   r   rC  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 rN   )authenticatedsuperr   r   r   kwargs	__class__rL   rM   r     s    z%HTTPPasswordMgrWithPriorAuth.__init__Fc                    s<   |  || |d k	r&t d ||| t |||| d S rN   )update_authenticatedrM  r>  )r   r<  r  r   r4  is_authenticatedrP  rL   rM   r>    s    z)HTTPPasswordMgrWithPriorAuth.add_passwordc                 C   s>   t |tr|g}dD ]$}|D ]}| ||}|| j|< qqd S Nr5  )r   r   r6  rL  )r   r  rS  r9  r7  r=  rL   rL   rM   rR    s    
z1HTTPPasswordMgrWithPriorAuth.update_authenticatedc                 C   sD   dD ]:}|  ||}| jD ]"}| ||r| j|     S qqd S rT  )r6  rL  r@  )r   rA  r9  rB  r  rL   rL   rM   rS    s
    
z-HTTPPasswordMgrWithPriorAuth.is_authenticated)F)F)r   r   r   r   r>  rR  rS  __classcell__rL   rL   rP  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 rN   )r    r4  r>  )r   Zpassword_mgrrL   rL   rM   r     s    z!AbstractBasicAuthHandler.__init__c                 c   sp   d}t j|D ]6}| \}}}|dkr8tdtd ||fV  d}q|sl|r^| d }nd}|d fV  d S )NF)"'zBasic Auth Realm was unquoted   Tr   rr   )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 }|D ]H}| |D ]8\}}| dkrB|}q(|d k	r(| |||    S q(q|d k	rztd|f d S )NbasiczBAbstractBasicAuthHandler does not support the following scheme: %r)Zget_allr_  rx   retry_http_basic_authrB   )	r   authreqrz   r   ri   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 )Nr+  r.  r,  r  )r4  rC  r0  r1  r2  r3  ru   auth_headerr   r   rG   rJ   )r   rz   r   r<  r   pwrawauthrL   rL   rM   ra    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 )NrS  Authorizationz{0}:{1}zBasic {})r   r4  rS  rt   r   rC  r   r2  r0  Zstandard_b64encoder3  r   strip)r   r   r   r4  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 )NrS  r   r   TF)r   r4  r   rR  rt   )r   r   r   rL   rL   rM   r     s
    z&AbstractBasicAuthHandler.http_response)N)r   r   r   recompileIrY  r   r_  rc  ra  rj  r   https_requestr   rL   rL   rL   rM   r#     s   
c                   @   s   e Zd ZdZdd ZdS )r$   rh  c                 C   s   |j }| d|||}|S )Nwww-authenticate)rt   rc  )r   r   rh   r   r   ri   rH   r   rL   rL   rM   http_error_401$  s      z#HTTPBasicAuthHandler.http_error_401N)r   r   r   rd  rp  rL   rL   rL   rM   r$      s   c                   @   s   e Zd ZdZdd ZdS )r%   r-  c                 C   s   |j }| d|||}|S Nproxy-authenticate)rz   rc  )r   r   rh   r   r   ri   r  r   rL   rL   rM   http_error_407/  s      z$ProxyBasicAuthHandler.http_error_407N)r   r   r   rd  rs  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    r4  r>  retriednonce_count
last_nonce)r   r4  rL   rL   rM   r   I  s    
z"AbstractDigestAuthHandler.__init__c                 C   s
   d| _ d S rt  )ru  r   rL   rL   rM   reset_retry_countR  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 failedrV   r   Zdigestr`  zEAbstractDigestAuthHandler does not support the following scheme: '%s')r   ru  r   rt   r]  rx   retry_http_digest_authrB   )r   rd  rz   r   ri   rb  r  rL   rL   rM   rc  U  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   rV   z	Digest %sr  )r]  parse_keqv_listfilterparse_http_listget_authorizationri   r   rd  r   r   rG   rJ   )r   r   rg  tokenZ	challengechalZauth_valZresprL   rL   rM   rz  i  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ctimer2  _randombyteshashlibsha1	hexdigest)r   noncesbdigrL   rL   rM   
get_cnonceu  s    z$AbstractDigestAuthHandler.get_cnoncec                 C   s  z6|d }|d }| d}| dd}| dd }W n tk
rL   Y d S X | |\}}	|d krhd S | j||j\}
}|
d krd S |jd k	r| |j|}nd }d|
||f }d| |j	f }|d kr|	||d|||f }n~d	|
d
kr\|| 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<  r  qop	algorithmMD5opaquez%s:%s:%sr+  rg  ,rV   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_implsr4  rC  rt   rI   get_entity_digestr   r   r]  rw  rv  r  r   )r   r   r  r<  r  r  r  r  HKDr   re  ZentdigZA1ZA2ZrespdigZncvalueZcnonceZnoncebitrI  rL   rL   rM   r~    s\    




z+AbstractDigestAuthHandler.get_authorizationc                    sD   |dkrdd  n|dkr$dd  nt d|  fdd} |fS )Nr  c                 S   s   t | d S Nr,  )r  Zmd5r2  r  xrL   rL   rM   r'        z?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAc                 S   s   t | d S r  )r  r  r2  r  r  rL   rL   rM   r'    r  z.Unsupported digest authentication algorithm %rc                    s    d| |f S )Nr+  rL   )r  dr  rL   rM   r'    r  )rB   )r   r  r  rL   r  rM   r    s    

z-AbstractDigestAuthHandler.get_algorithm_implsc                 C   s   d S rN   rL   )r   rI   r  rL   rL   rM   r    s    z+AbstractDigestAuthHandler.get_entity_digest)N)r   r   r   r   rx  rc  rz  r  r~  r  r  rL   rL   rL   rM   r&   >  s   
	>c                   @   s    e Zd ZdZdZdZdd ZdS )r'   zAn authentication protocol defined by RFC 2069

    Digest authentication improves on basic authentication because it
    does not transmit passwords in the clear.
    rh    c                 C   s*   t |jd }| d|||}|   |S )NrV   ro  )r   rt   rc  rx  r   r   rh   r   r   ri   rz   retryrL   rL   rM   rp    s      z$HTTPDigestAuthHandler.http_error_401N)r   r   r   r   rd  r   rp  rL   rL   rL   rM   r'     s   c                   @   s   e Zd ZdZdZdd ZdS )r(   Proxy-Authorizationr  c                 C   s"   |j }| d|||}|   |S rq  )rz   rc  rx  r  rL   rL   rM   rs    s      z%ProxyDigestAuthHandler.http_error_407N)r   r   r   rd  r   rs  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 rN   _debuglevel)r   
debuglevelrL   rL   rM   r     s    zAbstractHTTPHandler.__init__c                 C   s
   || _ d S rN   r  )r   levelrL   rL   rM   set_http_debuglevel  s    z'AbstractHTTPHandler.set_http_debuglevelc                 C   s   t jj|j| S rN   )r   r   HTTPConnection_get_content_lengthrI   r   r   ry   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 |}|	 rt
|j\}}t|\}}	|ds|d| | jjD ]&\}
}|
 }
||
s||
| q|S )	Nno host givenz\POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.zContent-type!application/x-www-form-urlencodedr   Transfer-encodingZchunkedrs   )rz   r   rI   r   r   r   r   r   r  r   r   r   r   r   r   r   )r   ry   rz   rI   r   Zcontent_lengthZsel_hostr  ZselZsel_pathr_   r   rL   rL   rM   do_request_  sJ    




  

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
ri }d}| kr | ||<  |= |j|j
|d	 z`z&|j| |j|j |d
d W n, tk
r } zt|W 5 d}~X Y nX | }	W n   |   Y nX |jr>|j  d|_| |	_|	j|	_|	S )zReturn an HTTPResponse object for the request, using http_class.

        http_class must implement the HTTPConnection API from http.client.
        r  rJ   c                    s   i | ]\}}| kr||qS rL   rL   r   ri   rL   rM   r  /  s     z/AbstractHTTPHandler.do_open.<locals>.<dictcomp>r   
Connectionc                 S   s   i | ]\}}|  |qS rL   )title)r   r_   r   rL   rL   rM   r  <  s      r  r  r  )Zencode_chunkedN)rz   r   rJ   Zset_debuglevelr  r   r|   updateri   r   r~   Z
set_tunnelry   r   r   rI   r   rq   getresponser   Zsockr   rH   reasonr   )
r   Z
http_classr   Zhttp_conn_argsrz   r   Ztunnel_headersZproxy_auth_hdrerrr&  rL   r  rM   r   !  sB    



zAbstractHTTPHandler.do_openN)r   )r   r   r   r   r  r  r  r   rL   rL   rL   rM   r    s
   
&r  c                   @   s   e Zd Zdd ZejZdS )r)   c                 C   s   |  tjj|S rN   )r   r   r   r  r   r   rL   rL   rM   	http_openf  s    zHTTPHandler.http_openN)r   r   r   r  r  r  rj  rL   rL   rL   rM   r)   d  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 rN   )r  r   _context_check_hostname)r   r  r>   check_hostnamerL   rL   rM   r   o  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  rL   rL   rM   
https_opent  s     zHTTPSHandler.https_open)r   NN)r   r   r   r   r  r  r  rn  rL   rL   rL   rM   rE   m  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 rt  )Zhttp.cookiejar	cookiejarZ	CookieJar)r   r  r   rL   rL   rM   r   }  s    
zHTTPCookieProcessor.__init__c                 C   s   | j | |S rN   )r  Zadd_cookie_headerr  rL   rL   rM   rj    s    z HTTPCookieProcessor.http_requestc                 C   s   | j || |S rN   )r  Zextract_cookies)r   ry   r   rL   rL   rM   r     s    z!HTTPCookieProcessor.http_response)N)r   r   r   r   rj  r   rn  r   rL   rL   rL   rM   r   |  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   sN   i }| D ]@}| dd\}}|d dkr@|d dkr@|dd }|||< q|S )z>Parse list of key=value strings where keys are not duplicated.=rV   r   rV  rS   )r]  )lZparsedZeltr   r   rL   rL   rM   r{    s    
r{  c                 C   s   g }d}d }}| D ]l}|r*||7 }d}q|rT|dkr>d}qn|dkrJd}||7 }q|dkrl| | d}q|dkrxd}||7 }q|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.
    rr   F\TrV  r  c                 S   s   g | ]}|  qS rL   )ri  )r   partrL   rL   rM   
<listcomp>  s     z#parse_http_list.<locals>.<listcomp>)ra   )r  resr  escaper	   ZcurrL   rL   rM   r}    s4    	


r}  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:   r  rX  r
  	localhost-file:// scheme is supported only on localhost)r   rz   	get_namesr   open_local_file)r   r   rH   rL   rL   rM   	file_open  s    &
zFileHandler.file_openNc                 C   s`   t jd krZz*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gethostbynamer   rL   rL   rM   r    s    

zFileHandler.get_namesc              
   C   s   dd l }dd l}|j}|j}t|}zt|}|j}|jj	|j
dd}	||d }
|d|
pbd||	f }|r~t|\}}|r|st||  kr|rd| | }nd| }tt|d||W S W n* tk
r } zt|W 5 d }~X Y nX tdd S )	Nr   TZusegmtz6Content-type: %s
Content-length: %d
Last-modified: %s

text/plainfile://rbzfile not on local host)email.utils	mimetypesrz   r   r4   r[   statst_sizeutils
formatdatest_mtime
guess_typemessage_from_stringr   _safe_gethostbynamer  r   rG   rq   r   )r   r   emailr  rz   rf   Z	localfilestatsrm   modifiedmtyperi   rF  ZorigurlexprL   rL   rM   r    s:    
zFileHandler.open_local_file)r   r   r   r  r  r  r  rL   rL   rL   rM   r*     s   
c                 C   s*   zt | W S  t jk
r$   Y d S X d S rN   )r   r  r  )rz   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}zt
|}W n* tk
r } zt|W 5 d }~X Y nX t|j\}	}
|	d}ttt	|}|d d |d  }}|r|d s|dd  }z| ||||||j}|r6dp8d}|
D ]2}t|\}}| d	kr>|d
kr>| }q>|||\}}d}||jd }|r|d| 7 }|d k	r|dkr|d| 7 }t|}t|||jW S  |jk
r$ } z"td| }|t  d W 5 d }~X Y nX d S )Nr   ftp error: no host givenrr   r
  rS   rV   rm  Dr   aAr   rm  r  r  zContent-type: %s
zContent-length: %d
ftp error: %rr:   )!ftplibr  rz   r   r   FTP_PORTrb   r   r   r
   r   r  rq   r   r   r]  r   mapconnect_ftprJ   r   rx   upperretrfiler  rt   r  r  r   
all_errorswith_tracebackr   exc_info)r   r   r  r  rz   rF  r   r4  r   r\   attrsdirsrO   fwr   attrr   rh   retrlenri   r  r  excrL   rL   rM   ftp_open  s^    

zFTPHandler.ftp_openc              	   C   s   t ||||||ddS )NF)
persistent)
ftpwrapper)r   r   r4  rz   rF  r  rJ   rL   rL   rM   r  7  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_connsr   rL   rL   rM   r   >  s
    zCacheFTPHandler.__init__c                 C   s
   || _ d S rN   )r  )r   trL   rL   rM   
setTimeoutE  s    zCacheFTPHandler.setTimeoutc                 C   s
   || _ d S rN   )r  )r   r  rL   rL   rM   setMaxConnsH  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   r4  rz   rF  r  rJ   r   rL   rL   rM   r  K  s    

 
zCacheFTPHandler.connect_ftpc                 C   s   t   }| j|krPt| j D ].\}}||k r | j|   | j|= | j|= q tt| j | _t	| j| j
krt| j D ]&\}}|| jkr| j|= | j|=  qqtt| j | _d S rN   )r  r  r   rJ   r   r  r   minvaluesrd   r  )r   r  r   r   rL   rL   rM   r  V  s    


zCacheFTPHandler.check_cachec                 C   s0   | j  D ]}|  q
| j   | j  d S rN   )r  r  r   clearrJ   )r   connrL   rL   rM   clear_cachej  s    

zCacheFTPHandler.clear_cacheN)	r   r   r   r   r  r   r  r  r  rL   rL   rL   rM   r,   ;  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:rV   r  z;base64itext/plain;charset=US-ASCIIz$Content-type: %s
Content-length: %d
)rt   r]  r   endswithr0  decodebytesr  r  rd   r   ioBytesIO)r   r   rH   r  rI   Z	mediatyperi   rL   rL   rM   	data_openq  s    



zDataHandler.data_openN)r   r   r   r  rL   rL   rL   rM   r-   p  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rd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).Nr   c                 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classrX  )
stacklevelr$  r%  key_file	cert_filez
User-Agent)ZAcceptz*/*)rQ  r   r?   r@   rA   r5   r   r(  r)  r   r  r  versionr   _URLopener__tempfilesr[   rp   _URLopener__unlink	tempcacheftpcache)r   r)  Zx509r   rL   rL   rM   r     s    
zURLopener.__init__c                 C   s   |    d S rN   )r   r   rL   rL   rM   __del__  s    zURLopener.__del__c                 C   s   |    d S rN   )cleanupr   rL   rL   rM   r     s    zURLopener.closec              	   C   sV   | j rB| j D ](}z| | W q tk
r2   Y qX q| j d d = | jrR| j  d S rN   )r  r  rq   r  r  )r   rO   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   ra   )r   r   rL   rL   rM   	addheader  s    zURLopener.addheaderc              
   C   sp  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 z0|dkr t| ||W S t| |||W S W nV ttfk
r0    Y n< tk
rj } ztd
|t d W 5 d}~X Y nX dS )z6Use URLopener().open(file) instead of open(file, 'r').z%/:=&?~#+!$,;'@()*[]|r  r  rO   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   rq   r  r   r  )r   r   rI   rf   ri   rh   urltyperH   r  	proxyhostrz   r   r_   r   rL   rL   rM   rG     s<    




zURLopener.openc                 C   s   t |\}}tdd|dS )/Overridable interface to open unknown URL type.	url errorzunknown url typeNr   rq   )r   r   rI   r   rH   rL   rL   rM   r!  
  s    zURLopener.open_unknownc                 C   s    t |\}}tdd| |dS )r$  r%  zinvalid proxy for %sNr&  )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rz0| |}| }|  tt|d |fW S  t	k
r }	 zW 5 d}	~	X Y nX | 
||}z<| }
|rt
|d}nrt|\}}t|pd\}}t|pd\}}t|pd\}}tj|d }t|\}}| j| t|d}z||
f}| jdk	rT|| j|< d}d}d}d}d	|
krzt|
d
 }|r|||| ||}|sq|t|7 }|| |d7 }|r|||| qW 5 |  X W 5 |  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.NrO   rV   rP   rr   rR   rS   r   rT   rU   rW   )r   r   r  r   r  rZ   r   r4   r   rq   rG   r   r   r[   r\   splitextr^   Zmkstempr  ra   fdopenrb   rc   rd   re   r   )r   rH   rf   rg   rI   r   Zurl1rh   r   r   ri   rj   Zgarbager\   suffixfdrk   rl   rm   rc   rn   ro   rL   rL   rM   retrieve  sn    






zURLopener.retrievec                 C   s$  d}d}t |tr<t|\}}|r6t|\}}t|}|}nt|\}}t|\}}t|\}	}
|
}d}|	 dkrvd}n:t|
\}}
|rt|\}}|rd|	||
f }t|r|}|stdd|rt|}t	
| d}nd}|rt|}t	
| d}nd}||}i }|r*d| |d< |r<d| |d	< |rJ||d
< d|d< | jD ]\}}|||< qX|dk	rd|d< |d||| n|jd||d z| }W n" tjjk
r   tdY nX d|j  krd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 errorr  r,  zBasic %sr  rh  rs   r   r  r  zContent-Typer   r   r  z$http protocol error: bad status liner   r   http:)r   r   r   r   r
   r   rx   r/  rq   r0  r1  r2  r3  r   ry   r  r   r   ZBadStatusLiner   Zstatusr   r   
http_errorrh   r  )r   Zconnection_factoryrH   rI   Zuser_passwdZproxy_passwdrz   r   Zrealhostr"  r   Z
proxy_authrg  Z	http_connri   r^  r   r   rL   rL   rM   _open_generic_httpV  s~    
 

    zURLopener._open_generic_httpc                 C   s   |  tjj||S )zUse HTTP protocol.)r.  r   r   r  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 )zHandle 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   rh   errcodeerrmsgri   rI   r_   r   rk   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   rh   r1  r2  ri   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   rz   rL   rL   rM   _https_connection  s    zURLopener._https_connectionc                 C   s   |  | j||S )zUse HTTPS protocol.)r.  r4  r/  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:   r  rX  r
     z
localhost/r  )r   r   r   rx   rB   r  r   rL   rL   rM   	open_file  s
    
4
zURLopener.open_filec              
   C   s\  ddl }ddl}t|\}}t|}zt|}W n0 tk
rb } zt|j|j	W 5 d}~X Y nX |j
}	|jj|jdd}
||d }|d|pd|	|
f }|s|}|dd dkrd	| }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   NTr  z6Content-Type: %s
Content-Length: %d
Last-modified: %s
r  rV   r
  r  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   r[   r  rq   r   strerrorrf   r  r  r  r  r  r  r   rG   r   r   r  r  thishostrB   )r   rH   r  r  rz   rO   Z	localnamer  erm   r  r  ri   ZurlfilerF  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	
|}|sd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rbt| jD ]*}||kr6| j| }| j|= |  q6z|| jkrt|||||
| j|< |sd	}nd
}|	D ]2}t|\}}| dkr|dkr| }q| j| ||\}}|d| d }d}|r|d| 7 }|dk	r,|dkr,|d| 7 }t|}t||d| W S  t k
r } ztd|  t!" d W 5 d}~X Y nX dS )zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedr   Nr  rr   r
  rS   rV   r  rm  r   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  rb   r   r]  r  rd   r  MAXFTPCACHEr   r   r  r   rx   r  r  r  r  r  r   	ftperrorsr  r   r  )r   rH   r  rz   r\   rF  r   r4  r  r  r  rO   r   r   r   r   r  r   rh   r  r  ri   r  rL   rL   rM   open_ftp  st    
  

  

 
zURLopener.open_ftpc           	   
   C   s<  t |tstdz|dd\}}W n tk
rD   tddY nX |sNd}|d}|dkrd	||d
 kr||d d
 }|d
| }nd}g }|dt	dt
t   |d|  |dkrt|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  rV   z
data errorzbad data URLr	  ;r   r  Nrr   zDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %sr0  r,  zlatin-1zContent-Length: %d
)r   r   r   r]  rB   rq   rfindra   r  strftimegmtimer0  r  r2  r3  r
   rd   r  r  r  r  StringIOr   )	r   rH   rI   r   Zsemir  r   ri   frL   rL   rM   	open_data8  s8    






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.  r0  r-  r   rC   r4  r5  r7  r  r=  rE  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maxtriesrN  rL   rL   rM   r   e  s    zFancyURLopener.__init__c                 C   s   t ||d| |S )z3Default error handling -- don't raise an exception.r,  )r   r3  rL   rL   rM   r   k  s    z!FancyURLopener.http_error_defaultNc           	      C   sv   |  j d7  _ zZ| jrN| j | jkrNt| dr4| j}n| j}|||dd|W S | ||||||}|W S d| _ X dS )z%Error 302 -- relocated (temporarily).rV   r   http_error_500r   z)Internal Server Error: Redirect RecursionN)rG  rH  r   rI  r   redirect_internal)	r   rH   rh   r1  r2  ri   rI   r   rk   rL   rL   rM   r  o  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  z( Redirection to url '%s' is not allowed.)r   r   r   r   r  r   rG   )	r   rH   rh   r1  r2  ri   rI   r  r  rL   rL   rM   rJ    s"    


 z FancyURLopener.redirect_internalc                 C   s   |  ||||||S )z*Error 301 -- also relocated (permanently).r  r   rH   rh   r1  r2  ri   rI   rL   rL   rM   r    s    zFancyURLopener.http_error_301c                 C   s   |  ||||||S )z;Error 303 -- also relocated (essentially identical to 302).rK  rL  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   rL  rL   rL   rM   r    s    zFancyURLopener.http_error_307Fc                 C   s   d|krt | ||||| |d }td|}	|	sHt | ||||| |	 \}
}|
 dkrtt | ||||| |st | ||||| d| j d }|dkrt| |||S t| ||||S dS )z_Error 401 -- authentication required.
        This function supports Basic authentication only.ro  ![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r`  Zretry__basic_authNr8   r   rk  matchr[  rx   r   r   r   rH   rh   r1  r2  ri   rI   r  ZstuffrP  r  r<  r_   rL   rL   rM   rp    s:    
  
  
  zFancyURLopener.http_error_401c                 C   s   d|krt | ||||| |d }td|}	|	sHt | ||||| |	 \}
}|
 dkrtt | ||||| |st | ||||| d| j d }|dkrt| |||S t| ||||S dS )zeError 407 -- proxy authentication required.
        This function supports Basic authentication only.rr  rM  r`  Zretry_proxy_rN  NrO  rQ  rL   rL   rM   rs    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 )Nhttp://r   r  rV   %s:%s@%srr   r  r   r)  r   r   get_user_passwdr	   rG   r   rH   r<  rI   rz   r   r  r  r"  r#  Zproxyselectorr   r   r4  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 )Nhttps://r   r  rV   rS  rr   r  rT  rV  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  rV   rS  rr   r  rR  r   r   rU  r	   rG   
r   rH   r<  rI   rz   r   r   r   r4  r  rL   rL   rM   ra  	  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  rV   rS  rr   r  rX  rZ  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  )rx   rF  prompt_user_passwd)r   rz   r<  r  r   r   r4  rL   rL   rM   rU  	  s    


 zFancyURLopener.get_user_passwdc                 C   sX   ddl }z.td||f }| d|||f }||fW S  tk
rR   t  Y 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: r?  )getpassinputKeyboardInterruptprint)r   rz   r<  r^  r   r4  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  rJ  r  r  r  rp  rs  rW  rY  ra  r\  rU  r]  rL   rL   rL   rM   r9   b  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  9	  s    
r  c                   C   sP   t dkrLz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   r9  A	  s    r9  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<  L	  s    r<  c                   C   s   t dkrtda t S )z%Return an empty email Message object.Nrr   )
_noheadersr  r  rL   rL   rL   rM   	noheadersU	  s    
rf  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| _|| _z|   W n   | 	   Y nX d S rt  )
r   r4  rz   rF  r  rJ   refcount	keepaliveinitr   )r   r   r4  rz   rF  r  rJ   r  rL   rL   rM   r   b	  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connectrz   rF  rJ   Zloginr   r4  r  r  cwd)r   r  Z_targetrL   rL   rM   ri  r	  s    
zftpwrapper.initc              
   C   s  dd l }|   |dkr"d}d}nd| }d}z| j| W n* |jk
rh   |   | j| Y nX d }|r|szd| }| j|\}}W nR |jk
r } z2t|d d dkrt	d	| 
t d
 W 5 d }~X Y nX |s| jd |rl| j }	zJz| j| W n4 |jk
rN } zt	d	| |W 5 d }~X Y nX W 5 | j|	 X d| }nd}| j|\}}d| _t|d| j}
|  jd7  _|  |
|fS )Nr   )r  r  zTYPE ArV   zTYPE zRETR rX  Z550r  r:   zLIST ZLISTr  )r  endtransferr	  Zvoidcmdr  ri  ZntransfercmdZ
error_permr   r   r  r   r  pwdrk  rj  r   Zmakefile
file_closerg  r   )r   rO   r   r  cmdisdirr  r  r  rm  ZftpobjrL   rL   rM   r  {	  sP       

$
zftpwrapper.retrfilec                 C   s
   d| _ d S rt  )rj  r   rL   rL   rM   rl  	  s    zftpwrapper.endtransferc                 C   s   d| _ | jdkr|   d S )NFr   )rh  rg  
real_closer   rL   rL   rM   r   	  s    
zftpwrapper.closec                 C   s2   |    |  jd8  _| jdkr.| js.|   d S )NrV   r   )rl  rg  rh  rq  r   rL   rL   rM   rn  	  s    zftpwrapper.file_closec                 C   s2   |    z| j  W n t k
r,   Y nX d S rN   )rl  r	  r   r<  r   rL   rL   rM   rq  	  s
    zftpwrapper.real_close)NT)r   r   r   r   r   ri  r  rl  r   rn  rq  rL   rL   rL   rM   r  _	  s     
	-r  c                  C   s   i } t j D ]4\}}| }|r|dd dkr|| |dd < qdt jkrZ| dd t j D ]J\}}|dd dkrd| }|r|| |dd < qd| |dd d qd| 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.

    iN_proxyZREQUEST_METHODr   )r[   environr   rx   r   )r)  r_   r   rL   rL   rM   getproxies_environment	  s    	
rt  c                 C   s   |dkrt  }z|d }W n tk
r0   Y dS X |dkr>dS |  } t| \}}|dD ]Z}| }|r\|d}| }||ks| |kr dS d| }||s| |r\ dS q\dS )zTest 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*Tr  .)rt  r  rx   r   r]  ri  lstripr
  )rz   r)  Zno_proxyhostonlyrF  r_   rL   rL   rM   proxy_bypass_environment	  s*    
ry  c              	   C   s0  ddl m } t| \}}dd }d| kr4|d r4dS d}|d	d
D ]}|sNqDtd|}|dk	r|dkrzt|}||}W n tk
r   Y qDY nX ||d}	|d}
|
dkrd|d	dd  }
nt
|
dd }
|
dk sD|
dkrqDd|
 }
||
? |	|
? kr* dS qD|| |rD dS qDd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 )
Nrv  r   r      rV   r  r:   r  rX  )r]  r   r  rb   rd   )ZipAddrrE  rL   rL   rM   ip2num
  s
    
z,_proxy_bypass_macosx_sysconf.<locals>.ip2numrv  Zexclude_simpleTN
exceptionsrL   z(\d+(?:\.\d+)*)(/\d+)?rV   r:   r      F)rz  r   r   rk  rP  r   r  rq   groupcountrb   )rz   proxy_settingsrz  rx  rF  r|  ZhostIPr   r  rI  maskrL   rL   rM   _proxy_bypass_macosx_sysconf
  s>     




r  darwin)_get_proxy_settings_get_proxiesc                 C   s   t  }t| |S rN   )r  r  )rz   r  rL   rL   rM   proxy_bypass_macosx_sysconfF
  s    r  c                   C   s   t  S )zReturn a dictionary of scheme -> proxy server URL mappings.

        This function uses the MacOSX framework SystemConfiguration
        to fetch the proxy information.
        )r  rL   rL   rL   rM   getproxies_macosx_sysconfJ
  s    r  c                 C   s    t  }|rt| |S t| S dS )zReturn True, if host should be bypassed.

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

        N)rt  ry  r  rz   r)  rL   rL   rM   r/  T
  s    
r/  c                   C   s   t  p
t S rN   )rt  r  rL   rL   rL   rM   r5   a
  s    c               
   C   s  i } zddl }W n tk
r(   |  Y S X z||jd}||dd }|rt||dd }d|kr|dD ]4}|dd\}}td	|sd
||f }|| |< qtn>|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   N;Software\Microsoft\Windows\CurrentVersion\Internet SettingsProxyEnableZProxyServerr  r>  rV   z(?:[^/:]+)://z%s://%sry  r,  r   z	http://%sz
https://%sr   zftp://%sr	  )winregImportErrorOpenKeyHKEY_CURRENT_USERQueryValueExr   r]  rk  rP  ZCloserq   rB   r   )r)  r  internetSettingsproxyEnableZproxyServerpr   ZaddressrL   rL   rM   getproxies_registryf
  sF    

r  c                   C   s   t  p
t S )zReturn a dictionary of scheme -> proxy server URL mappings.

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

        )rt  r  rL   rL   rL   rM   r5   
  s    c                 C   s|  zdd l }W n tk
r"   Y dS X z6||jd}||dd }t||dd }W n tk
rp   Y dS X |rz|s~dS t| \}}|g} z t	|}||kr| 
| W n tk
r   Y nX z t|}||kr| 
| W n tk
 r   Y nX |d}|D ]j}	|	dkr*d|kr* dS |	dd	}	|	d
d}	|	dd}	| D ] }
t|	|
tjrR  dS qRqdS )Nr   r  r  ZProxyOverrider>  z<local>rv  rV   z\.ru  z.*?)r  r  r  r  r  r   rq   r   r   r  ra   Zgetfqdnr]  r  rk  rP  rm  )rz   r  r  r  ZproxyOverrideZrawHostrF  ZaddrZfqdnrJ  r   rL   rL   rM   proxy_bypass_registry
  s`    





r  c                 C   s    t  }|rt| |S t| S dS )zReturn True, if host should be bypassed.

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

        N)rt  ry  r  r  rL   rL   rM   r/  
  s    
)NNN)N)~r   r0  r   r  r  Zhttp.clientr   r  r[   rG  rk  r   r  r   r  r^   rX   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   rl  ASCIIrv   r{   r   r   r2   r   r/   r   r   r"  r   r    r!   r"   r#   r$   r%   urandomr  r&   r'   r(   r  r)   r   r   rE   ra   r   r.   r{  r}  r*   r  r+   r,   r-   r;  r_   Z
nturl2pathr4   r3   r  r8   r9   rb  r  rc  r9  rd  r<  re  rf  r  rt  ry  r  platformZ_scproxyr  r  r  r  r/  r5   r  r  rL   rL   rL   rM   <module>   s   SP
                         U
?m $q!+@
o v

+3:5!   @ W

_
%A

-	2
