AlkantarClanX12

Your IP : 18.220.112.210


Current Path : /proc/self/root/opt/alt/python39/lib64/python3.9/urllib/__pycache__/
Upload File :
Current File : //proc/self/root/opt/alt/python39/lib64/python3.9/urllib/__pycache__/request.cpython-39.opt-1.pyc

a

R�f���@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlmZmZmZddlmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(ddl)m*Z*m+Z+zddl,Z,Wne-�ydZ.Yn0dZ.gd�Z/d	e
j0dd
�Z1da2dej3fddddd�dd
�Z4dd�Z5gZ6d�dd�Z7dd�Z8e
�9de
j:�Z;dd�Z<Gdd�d�Z=Gdd�d�Z>dd�Z?Gdd�d�Z@Gdd �d e@�ZAGd!d"�d"e@�ZBGd#d$�d$e@�ZCd%d&�ZDGd'd(�d(e@�ZEGd)d*�d*�ZFGd+d,�d,eF�ZGGd-d.�d.eG�ZHGd/d0�d0�ZIGd1d2�d2eIe@�ZJGd3d4�d4eIe@�ZKejLZMGd5d6�d6�ZNGd7d8�d8e@eN�ZOGd9d:�d:e@eN�ZPGd;d<�d<e@�ZQGd=d>�d>eQ�ZReSejTd?��r�Gd@dA�dAeQ�ZUe/�VdA�GdBdC�dCe@�ZWGdDdE�dEe@�ZXdFdG�ZYdHdI�ZZGdJdK�dKe@�Z[dLdM�Z\GdNdO�dOe@�Z]GdPdQ�dQe]�Z^GdRdS�dSe@�Z_dTZ`ejadUk�r�ddVlbmcZcmdZdndWdX�ZcdYdZ�ZdiZeGd[d\�d\�ZfGd]d^�d^ef�Zgdahd_d`�Zidajdadb�Zkdaldcdd�Zmdandedf�ZoGdgdh�dh�Zpdidj�Zqd�dkdl�Zrdmdn�Zsdodp�Zte
judqk�rTddrlvmwZwmxZxdsdt�Zydudv�Zzdwdx�Z{dydz�Z|n6ejadUk�r�d{d|�Z}d}dz�Z|d~d�Z~d�dx�Z{neqZ|erZ{dS)�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('https://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
Cs�|s|s|rfddl}|�dtd�|dur2td��ts>td��tjtjj||d�}t	|d�}t
|�}	n0|r~t	|d�}t
|�}	ntdur�t
�a}	nt}	|	�|||�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 the properties url, headers, and status.
    See urllib.response.addinfourl for more detail on these properties.

    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.

    rNzJcafile, capath and cadefault are deprecated, use a custom context instead.r:zDYou can't pass both context and any of cafile, capath, and cadefaultzSSL support not available)r;r<)r>)
�warnings�warn�DeprecationWarning�
ValueError�	_have_ssl�sslZcreate_default_contextZPurposeZSERVER_AUTH�HTTPSHandlerr2�_opener�open)
�url�data�timeoutr;r<r=r>r?Z
https_handler�opener�rL�3/opt/alt/python39/lib64/python3.9/urllib/request.pyr0�s04��
�



r0cCs|adS�N)rF)rKrLrLrMr1�sr1c	Cslt|�\}}t�t||����}|��}|dkrR|sRtj�|�|fWd�S|rbt|d�}nt	j
dd�}|j}t�
|�|��||f}	d}
d}d}d}
d	|vr�t|d
�}|r�||
|
|�|�|
�}|sҐq|t|�7}|�|�|
d7}
|r�||
|
|�q�Wd�n1�s0YWd�n1�s80Y|dk�rh||k�rhtd||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.
    �fileN�wbF)�delete� ���r�content-length�Content-Length��1retrieval incomplete: got only %i out of %i bytes)r�
contextlib�closingr0�info�os�path�normpathrG�tempfileZNamedTemporaryFile�name�_url_tempfiles�append�int�read�len�writer)rH�filename�
reporthookrIZurl_typer\�fp�headers�tfp�result�bs�sizerc�blocknum�blockrLrLrMr6�sH


N��r6c	CsBtD]&}zt�|�Wqty(Yq0qtdd�=tr>dadS)z0Clean up temporary files from urlretrieve calls.N)r`r[�unlink�OSErrorrF)Z	temp_filerLrLrMr7s
r7z:\d+$cCs<|j}t|�d}|dkr&|�dd�}t�d|d�}|��S)z�Return 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�hostrLrLrM�request_host+sr{c@s�eZdZdidddfdd�Zedd��Zejdd��Zejdd��Zed	d
��Zejdd
��Zejdd
��Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zd#dd�Zdd �Zd!d"�ZdS)$rNFc	Csl||_i|_i|_d|_||_d|_|��D]\}}|�||�q,|durRt|�}||_	||_
|rh||_dSrN)rtri�unredirected_hdrs�_datarI�_tunnel_host�items�
add_headerr{�origin_req_host�unverifiable�method)	�selfrHrIrir�r�r��key�valuerLrLrM�__init__=szRequest.__init__cCs|jrd�|j|j�S|jS)Nz{}#{})�fragment�format�	_full_url�r�rLrLrMrtOszRequest.full_urlcCs(t|�|_t|j�\|_|_|��dSrN)rr�rr��_parse�r�rHrLrLrMrtUs
cCsd|_d|_d|_dS)Nrr)r�r��selectorr�rLrLrMrt\scCs|jSrN)r}r�rLrLrMrIbszRequest.datacCs(||jkr$||_|�d�r$|�d�dS)N�Content-length)r}�
has_header�
remove_header)r�rIrLrLrMrIfs

cCs
d|_dSrN)rIr�rLrLrMrIpscCsNt|j�\|_}|jdur(td|j��t|�\|_|_|jrJt|j�|_dS)Nzunknown url type: %r)	rr��typerBrtrrzr�r
)r��restrLrLrMr�ts
zRequest._parsecCs|jdurdnd}t|d|�S)z3Return a string indicating the HTTP request method.N�POST�GETr�)rI�getattr)r�Zdefault_methodrLrLrM�
get_method|szRequest.get_methodcCs|jSrN)rtr�rLrLrM�get_full_url�szRequest.get_full_urlcCs2|jdkr|js|j|_n||_|j|_||_dS)N�https)r�r~rzrtr�)r�rzr�rLrLrM�	set_proxy�s

zRequest.set_proxycCs|j|jkSrN)r�rtr�rLrLrM�	has_proxy�szRequest.has_proxycCs||j|��<dSrN)ri�
capitalize�r�r��valrLrLrMr��szRequest.add_headercCs||j|��<dSrN)r|r�r�rLrLrM�add_unredirected_header�szRequest.add_unredirected_headercCs||jvp||jvSrN)rir|�r��header_namerLrLrMr��s
�zRequest.has_headercCs|j�||j�||��SrN)ri�getr|)r�r��defaultrLrLrMru�s�zRequest.get_headercCs |j�|d�|j�|d�dSrN)ri�popr|r�rLrLrMr��szRequest.remove_headercCsi|j�|j�}t|���SrN)r|ri�listr)r��hdrsrLrLrM�header_items�szRequest.header_items)N)�__name__�
__module__�__qualname__r��propertyrt�setter�deleterrIr�r�r�r�r�r�r�r�rur�r�rLrLrLrMr;s6�





	

rc@sNeZdZdd�Zdd�Zdd�Zdd�Zd	ejfd
d�Z	ddd
�Z
dd�Zd	S)rcCs6dt}d|fg|_g|_i|_i|_i|_i|_dS)N�Python-urllib/%sz
User-agent)�__version__�
addheaders�handlers�handle_open�handle_error�process_response�process_request)r�Zclient_versionrLrLrMr��szOpenerDirector.__init__c	CsRt|d�stdt|���d}t|�D�]}|dvr6q&|�d�}|d|�}||dd�}|�d�r�|�d�|d}||dd�}zt|�}Wnty�Yn0|j�	|i�}	|	|j|<n>|dkr�|}|j
}	n*|d	kr�|}|j}	n|d
kr&|}|j}	nq&|	�
|g�}
|
�r t�|
|�n
|
�|�d}q&|�rNt�|j|�|�|�dS)N�
add_parentz%expected BaseHandler instance, got %rF)�redirect_request�do_open�
proxy_open�_rV�errorrG�responseryT)�hasattr�	TypeErrorr��dir�find�
startswithrbrBr�r�r�r�r��
setdefault�bisectZinsortrar�r�)r��handlerZadded�meth�i�protocolZ	condition�j�kind�lookupr�rLrLrM�add_handler�sL
�


zOpenerDirector.add_handlercCsdSrNrLr�rLrLrM�close�szOpenerDirector.closec	Gs<|�|d�}|D]&}t||�}||�}|dur|SqdS)NrL)r�r�)	r��chainr��	meth_name�argsr�r��funcrkrLrLrM�_call_chain�s
zOpenerDirector._call_chainNc
Cs�t|t�rt||�}n|}|dur(||_||_|j}|d}|j�|g�D]}t||�}||�}qJt	�
d|j|j|j|�
��|�||�}	|d}|j�|g�D]}t||�}|||	�}	q�|	S)NZ_requestzurllib.RequestZ	_response)�
isinstance�strrrIrJr�r�r�r��sys�auditrtrir��_openr�)
r��fullurlrIrJ�reqr�r�Z	processorr�r�rLrLrMrG�s$



zOpenerDirector.opencCsP|�|jdd|�}|r|S|j}|�|j||d|�}|r>|S|�|jdd|�S)Nr�Zdefault_openr��unknown�unknown_open)r�r�r�)r�r�rIrkr�rLrLrMr�s 
���
�zOpenerDirector._opencGs~|dvr,|jd}|d}d|}d}|}n|j}|d}d}|||f|}|j|�}|r^|S|rz|dd	f|}|j|�SdS)
N��httpr�r�r:z
http_error_%srVZ_errorrr��http_error_default)r�r�)r��protor��dictr�Zhttp_errZ	orig_argsrkrLrLrMr�s 

zOpenerDirector.error)N)r�r�r�r�r�r�r��socket�_GLOBAL_DEFAULT_TIMEOUTrGr�r�rLrLrLrMr�s/
rc	Gs�t�}ttttttttt	g	}t
tj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)rrr.r)rrr+r*r/r-r�r��clientrarE�setr�r��
issubclass�add�remover�)r�rKZdefault_classes�skip�klassZcheck�hrLrLrMr27s2	�




r2c@s(eZdZdZdd�Zdd�Zdd�ZdS)	r��cCs
||_dSrN)�parent)r�r�rLrLrMr�^szBaseHandler.add_parentcCsdSrNrLr�rLrLrMr�aszBaseHandler.closecCst|d�sdS|j|jkS)N�
handler_orderT)r�r�)r��otherrLrLrM�__lt__es
zBaseHandler.__lt__N)r�r�r�r�r�r�r�rLrLrLrMr[src@s eZdZdZdZdd�ZeZdS)r/zProcess HTTP error responses.i�cCsH|j|j|��}}}d|kr,dksDn|j�d|||||�}|S)N���,r�)�code�msgrZr�r�)r�ryr�r�r�r�rLrLrM�
http_responsers�z HTTPErrorProcessor.http_responseN)r�r�r��__doc__r�r��https_responserLrLrLrMr/nsr/c@seZdZdd�ZdS)rcCst|j||||��dSrN)rrt)r�r�rhr�r�r�rLrLrMr��sz*HTTPDefaultErrorHandler.http_error_defaultN)r�r�r�r�rLrLrLrMrsrc@s4eZdZdZdZdd�Zdd�ZeZZZ	dZ
dS)	r��
c	st|��}|dvr|dvs:|dvr(|dks:t|j||||��|�dd�}d��fdd	�|j��D�}t|||jd
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�ZHEAD)r�r�r�r�� z%20)rTzcontent-typecs"i|]\}}|���vr||�qSrL)rx��.0�k�v�ZCONTENT_HEADERSrLrM�
<dictcomp>�s�z8HTTPRedirectHandler.redirect_request.<locals>.<dictcomp>T)rir�r�)r�rrt�replacerirrr�)	r�r�rhr�r�ri�newurl�mZ
newheadersrLrrMr��s
���z$HTTPRedirectHandler.redirect_requestc
CsLd|vr|d}nd|vr$|d}ndSt|�}|jdvrRt||d||f||��|jsn|jrnt|�}d|d<t|�}t|dtj	d�}t
|j|�}|�||||||�}|dur�dSt
|d	��r|j}	|_|	�|d
�|jks�t|	�|jk�rt|j||j|||��ni}	|_|_|	�|d
�d|	|<|��|��|jj||jd�S)
N�location�uri�r�r��ftprrz+%s - Redirection to url '%s' is not allowed�/r:z
iso-8859-1)�encoding�safe�
redirect_dictrrV�rJ)r�schemerr\Znetlocr�rr	�stringZpunctuationrrtr�r�r
r��max_repeatsrd�max_redirections�inf_msgrcr�r�rGrJ)
r�r�rhr�r�rir�urlparts�newZvisitedrLrLrM�http_error_302�sH



����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�rrr�r�http_error_301�http_error_303�http_error_307rrLrLrLrMr�s&<rc
Cs�t|�\}}|�d�s d}|}nZ|�d�s6td|��d|vrV|�d�}|�d|�}n|�dd�}|dkrnd}|d|�}t|�\}}|dur�t|�\}}	nd}}	|||	|fS)aReturn (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�//zproxy URL with no authority: %r�@r:rS)rr�rBr�rr)
�proxyrZr_scheme�	authorityZhost_separator�endZuserinfo�hostport�user�passwordrLrLrM�_parse_proxy�s$


r"c@s"eZdZdZddd�Zdd�ZdS)r�dNcCsP|durt�}||_|��D].\}}|��}t|d||||jfdd��qdS)Nz%s_opencSs||||�SrNrL)�rrr�r�rLrLrM�<lambda>!sz'ProxyHandler.__init__.<locals>.<lambda>)r5�proxiesrrx�setattrr�)r�r&r�rHrLrLrMr�s
�zProxyHandler.__init__cCs�|j}t|�\}}}}|dur"|}|jr6t|j�r6dS|rv|rvdt|�t|�f}	t�|	����d�}
|�	dd|
�t|�}|�
||�||ks�|dkr�dS|jj||j
d�SdS)N�%s:%s�ascii�Proxy-authorization�Basic r�r)r�r"rz�proxy_bypassr
�base64�	b64encode�encode�decoder�r�r�rGrJ)r�r�rr�Z	orig_typeZ
proxy_typer r!rZ	user_passZcredsrLrLrMr�$s"�zProxyHandler.proxy_open)N)r�r�r�r�r�r�rLrLrLrMrs
rc@s6eZdZdd�Zdd�Zdd�Zd
dd	�Zd
d�ZdS)r cCs
i|_dSrN)�passwdr�rLrLrMr�BszHTTPPasswordMgr.__init__cs\t|t�r|g}|�jvr$i�j|<dD].�t��fdd�|D��}||f�j||<q(dS)N�TFc3s|]}��|��VqdSrN)�
reduce_uri)r��u��default_portr�rLrM�	<genexpr>Lsz/HTTPPasswordMgr.add_password.<locals>.<genexpr>)r�r�r1�tuple)r��realmrr r1�reduced_urirLr5rM�add_passwordEs


�zHTTPPasswordMgr.add_passwordc	Cs`|j�|i�}dD]H}|�||�}|��D].\}}|D] }|�||�r6|Sq6q*qdS)Nr2�NN)r1r�r3r�	is_suburi)	r�r9�authuriZdomainsr6�reduced_authuriZurisZauthinforrLrLrM�find_user_passwordPsz"HTTPPasswordMgr.find_user_passwordTc
Cs�t|�}|dr.|d}|d}|dp*d}nd}|}d}t|�\}}|r~|dur~|dur~ddd��|�}	|	dur~d	||	f}||fS)
z@Accept authority or URI and extract only the authority and path.rVrr:r
N�Pi�r�z%s:%d)rr
r�)
r�rr6�partsrrr\rz�portZdportrLrLrMr3Zs$��zHTTPPasswordMgr.reduce_uricCsN||krdS|d|dkr dS|d}|dd�dkr@|d7}|d�|�S)zcCheck if test is below base in a URI tree

        Both args must be URIs in reduced form.
        TrFrVrSNr
)r�)r��base�test�prefixrLrLrMr=qszHTTPPasswordMgr.is_suburiN)T)r�r�r�r�r;r@r3r=rLrLrLrMr @s


r c@seZdZdd�ZdS)r!cCs0t�|||�\}}|dur"||fSt�|d|�SrN)r r@)r�r9r>r r!rLrLrMr@�s�z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r�r�r�r@rLrLrLrMr!�sr!cs<eZdZ�fdd�Zd
�fdd�	Zddd�Zdd	�Z�ZS)r"csi|_t�j|i|��dSrN)�
authenticated�superr��r�r��kwargs��	__class__rLrMr��sz%HTTPPasswordMgrWithPriorAuth.__init__Fcs<|�||�|dur&t��d|||�t��||||�dSrN)�update_authenticatedrHr;)r�r9rr r1�is_authenticatedrKrLrMr;�sz)HTTPPasswordMgrWithPriorAuth.add_passwordcCs>t|t�r|g}dD]$}|D]}|�||�}||j|<qqdS�Nr2)r�r�r3rG)r�rrNr6r4r:rLrLrMrM�s
z1HTTPPasswordMgrWithPriorAuth.update_authenticatedcCsDdD]:}|�||�}|jD]"}|�||�r|j|SqqdSrO)r3rGr=)r�r>r6r?rrLrLrMrN�s

z-HTTPPasswordMgrWithPriorAuth.is_authenticated)F)F)r�r�r�r�r;rMrN�
__classcell__rLrLrKrMr"�s

r"c@sTeZdZe�dej�Zddd�Zdd�Zdd�Z	d	d
�Z
dd�Zd
d�ZeZ
eZdS)r#z1(?:^|,)[ 	]*([^ 	,]+)[ 	]+realm=(["']?)([^"']*)\2NcCs"|durt�}||_|jj|_dSrN)r r1r;)r�Zpassword_mgrrLrLrMr��sz!AbstractBasicAuthHandler.__init__ccspd}tj�|�D]6}|��\}}}|dvr8t�dtd�||fVd}q|sl|r^|��d}nd}|dfVdS)NF)�"�'zBasic Auth Realm was unquoted�Trrr)r#�rx�finditer�groupsr?r@�UserWarning�split)r��headerZfound_challengeZmorr	r9rLrLrM�_parse_realm�s�
z%AbstractBasicAuthHandler._parse_realmc	Cs~|�|�}|sdSd}|D]H}|�|�D]8\}}|��dkrB|}q(|dur(|�|||�Sq(q|durztd|f��dS)N�basiczBAbstractBasicAuthHandler does not support the following scheme: %r)Zget_allrZrx�retry_http_basic_authrB)	r��authreqrzr�riZunsupportedrYrr9rLrLrM�http_error_auth_reqed�s
�z.AbstractBasicAuthHandler.http_error_auth_reqedcCs||j�||�\}}|durtd||f}dt�|����d�}|�|jd�|krTdS|�|j|�|j	j
||jd�SdSdS)Nr(r+r)r)r1r@r-r.r/r0ru�auth_headerr�r�rGrJ)r�rzr�r9r �pw�raw�authrLrLrMr\�sz.AbstractBasicAuthHandler.retry_http_basic_authcCstt|jd�r|j�|j�s|S|�d�sp|j�d|j�\}}d�||���}t�	|��
�}|�dd�|����|S)NrN�
Authorizationz{0}:{1}zBasic {})
r�r1rNrtr�r@r�r/r-Zstandard_b64encoder0r��strip)r�r�r r1ZcredentialsZauth_strrLrLrM�http_request�s�
�z%AbstractBasicAuthHandler.http_requestcCsLt|jd�rHd|jkr"dkr8nn|j�|jd�n|j�|jd�|S)NrNr�r�TF)r�r1r�rMrt)r�r�r�rLrLrMr�s
z&AbstractBasicAuthHandler.http_response)N)r�r�r��re�compile�IrTr�rZr^r\rer��
https_requestr�rLrLrLrMr#�s�

r#c@seZdZdZdd�ZdS)r$rccCs|j}|�d|||�}|S)N�www-authenticate)rtr^)r�r�rhr�r�rirHr�rLrLrM�http_error_401s
�z#HTTPBasicAuthHandler.http_error_401N)r�r�r�r_rkrLrLrLrMr$sr$c@seZdZdZdd�ZdS)r%r*cCs|j}|�d|||�}|S�N�proxy-authenticate)rzr^)r�r�rhr�r�rirr�rLrLrM�http_error_407's
�z$ProxyBasicAuthHandler.http_error_407N)r�r�r�r_rnrLrLrLrMr%#sr%c@sNeZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dS)r&NcCs4|durt�}||_|jj|_d|_d|_d|_dS�Nr)r r1r;�retried�nonce_count�
last_nonce)r�r1rLrLrMr�As
z"AbstractDigestAuthHandler.__init__cCs
d|_dSro)rpr�rLrLrM�reset_retry_countJsz+AbstractDigestAuthHandler.reset_retry_countcCs||�|d�}|jdkr*t|jdd|d��n|jd7_|rx|��d}|��dkr`|�||�S|��dkrxtd|��dS)	N�i�zdigest auth failedrVrZdigestr[zEAbstractDigestAuthHandler does not support the following scheme: '%s')r�rprrtrXrx�retry_http_digest_authrB)r�r_rzr�rir]rrLrLrMr^Ms

��z/AbstractDigestAuthHandler.http_error_auth_reqedcCsz|�dd�\}}ttdt|���}|�||�}|rvd|}|j�|jd�|krRdS|�|j|�|j	j
||jd�}|SdS)Nr�rVz	Digest %sr)rX�parse_keqv_list�filter�parse_http_list�get_authorizationrir�r_r�r�rGrJ)r�r�rb�tokenZ	challenge�chalZauth_valZresprLrLrMruasz0AbstractDigestAuthHandler.retry_http_digest_authcCs@d|j|t��f}|�d�td�}t�|���}|dd�S)Nz	%s:%s:%s:r)��)rq�time�ctimer/�_randombytes�hashlib�sha1�	hexdigest)r��nonce�s�b�digrLrLrM�
get_cnoncemsz$AbstractDigestAuthHandler.get_cnoncecCs�z6|d}|d}|�d�}|�dd�}|�dd�}WntyJYdS0|�|�\}}	|durfdS|j�||j�\}
}|
dur�dS|jdur�|�|j|�}nd}d|
||f}
d|��|j	f}|dur�|	||
�d|||�f�}n~d	|�
d
�v�rZ||jk�r|jd7_nd|_||_d|j}|�
|�}d
|||d	||�f}|	||
�|�}ntd|��d|
|||j	|f}|�r�|d|7}|�r�|d|7}|d|7}|�r�|d||f7}|S)Nr9r��qop�	algorithm�MD5�opaquez%s:%s:%sr(rb�,rVz%08xz%s:%s:%s:%s:%szqop '%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_implsr1r@rtrI�get_entity_digestr�r�rXrrrqr�r)r�r�r{r9r�r�r�r��H�KDr r`ZentdigZA1ZA2ZrespdigZncvalueZcnonceZnoncebitrDrLrLrMryxs\

�


��z+AbstractDigestAuthHandler.get_authorizationcsD|dkrdd��n|dkr$dd��ntd|���fdd�}�|fS)Nr�cSst�|�d����S�Nr))r�Zmd5r/r���xrLrLrMr%��z?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAcSst�|�d����Sr�)r�r�r/r�r�rLrLrMr%�r�z.Unsupported digest authentication algorithm %rcs�d||f�S)Nr(rL)r��d�r�rLrMr%�r�)rB)r�r�r�rLr�rMr��s

�z-AbstractDigestAuthHandler.get_algorithm_implscCsdSrNrL)r�rIr{rLrLrMr��sz+AbstractDigestAuthHandler.get_entity_digest)N)r�r�r�r�rsr^rur�ryr�r�rLrLrLrMr&6s
	>
r&c@s eZdZdZdZdZdd�ZdS)r'z�An authentication protocol defined by RFC 2069

    Digest authentication improves on basic authentication because it
    does not transmit passwords in the clear.
    rc��cCs*t|j�d}|�d|||�}|��|S)NrVrj)rrtr^rs�r�r�rhr�r�rirz�retryrLrLrMrk�s�z$HTTPDigestAuthHandler.http_error_401N)r�r�r�r�r_r�rkrLrLrLrMr'�sr'c@seZdZdZdZdd�ZdS)r(�Proxy-Authorizationr�cCs"|j}|�d|||�}|��|Srl)rzr^rsr�rLrLrMrn�s�z%ProxyDigestAuthHandler.http_error_407N)r�r�r�r_r�rnrLrLrLrMr(�sr(c@s6eZdZd
dd�Zdd�Zdd�Zdd	�Zd
d�ZdS)�AbstractHTTPHandlerrcCs
||_dSrN��_debuglevel)r��
debuglevelrLrLrMr��szAbstractHTTPHandler.__init__cCs
||_dSrNr�)r��levelrLrLrM�set_http_debuglevel�sz'AbstractHTTPHandler.set_http_debuglevelcCstjj�|j|���SrN)r�r��HTTPConnection�_get_content_lengthrIr��r�ryrLrLrMr��s�z'AbstractHTTPHandler._get_content_lengthcCs|j}|std��|jdur�|j}t|t�r8d}t|��|�d�sN|�dd�|�d�s�|�d�s�|�|�}|dur�|�dt|��n|�dd�|}|�	�r�t
|j�\}}t|�\}}	|�d�s�|�d|�|j
jD]&\}
}|
��}
|�|
�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-encodingZchunkedrs)rzrrIr�r�r�r�r�r�r�rr�rr�r�r�)r�ryrzrIr�Zcontent_lengthZsel_hostrZselZsel_pathr_r�rLrLrM�do_request_�sF


�
�
��

zAbstractHTTPHandler.do_request_c

sV|j}|std��||fd|ji|��}|�|j�t|j�����fdd�|j�	�D��d�d<dd���	�D��|j
r�i}d}|�vr��|||<�|=|j|j
|d	�zbz&|j|�
�|j|j�|�d
�d�Wn.t�y}zt|��WYd}~n
d}~00|��}	Wn|���Yn0|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.
        r�rJcsi|]\}}|�vr||�qSrLrLr��rirLrMr's�z/AbstractHTTPHandler.do_open.<locals>.<dictcomp>r��
ConnectioncSsi|]\}}|��|�qSrL)�title)r�r_r�rLrLrMr4r�r�r�r�)Zencode_chunkedN)rzrrJZset_debuglevelr�r�r|�updaterirr~Z
set_tunnelryr�r�rIr�rq�getresponser�Zsockr�rH�reasonr�)
r�Z
http_classr�Zhttp_conn_argsrzr�Ztunnel_headersZproxy_auth_hdr�errr$rLr�rMr�sB
�


zAbstractHTTPHandler.do_openN)r)r�r�r�r�r�r�r�r�rLrLrLrMr��s

&r�c@seZdZdd�ZejZdS)r)cCs|�tjj|�SrN)r�r�r�r��r�r�rLrLrM�	http_open^szHTTPHandler.http_openN)r�r�r�r�r�r�rerLrLrLrMr)\sr)r�c@s$eZdZddd�Zdd�ZejZdS)rErNcCst�||�||_||_dSrN)r�r��_context�_check_hostname)r�r�r>�check_hostnamerLrLrMr�gszHTTPSHandler.__init__cCs|jtjj||j|jd�S)N)r>r�)r�r�r�r�r�r�r�rLrLrM�
https_openls�zHTTPSHandler.https_open)rNN)r�r�r�r�r�r�r�rirLrLrLrMrEes
rEc@s.eZdZddd�Zdd�Zdd�ZeZeZdS)	rNcCs$ddl}|dur|j��}||_dSro)Zhttp.cookiejar�	cookiejarZ	CookieJar)r�r�r�rLrLrMr�us
zHTTPCookieProcessor.__init__cCs|j�|�|SrN)r�Zadd_cookie_headerr�rLrLrMre{sz HTTPCookieProcessor.http_requestcCs|j�||�|SrN)r�Zextract_cookies)r�ryr�rLrLrMr�sz!HTTPCookieProcessor.http_response)N)r�r�r�r�rer�rir�rLrLrLrMrts

rc@seZdZdd�ZdS)r.cCs|j}td|��dS)Nzunknown url type: %s)r�r)r�r�r�rLrLrMr��szUnknownHandler.unknown_openN)r�r�r�r�rLrLrLrMr.�sr.cCsNi}|D]@}|�dd�\}}|ddkr@|ddkr@|dd�}|||<q|S)z>Parse list of key=value strings where keys are not duplicated.�=rVrrQrS)rX)�lZparsed�eltr�rrLrLrMrv�s
rvcCs�g}d}d}}|D]l}|r*||7}d}q|rT|dkr>d}qn|dkrJd}||7}q|dkrl|�|�d}q|dkrxd}||7}q|r�|�|�dd�|D�S)	apParse 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.
    rrF�\TrQr�cSsg|]}|���qSrL)rd)r��partrLrLrM�
<listcomp>�r�z#parse_http_list.<locals>.<listcomp>)ra)r��resr��escaper	ZcurrLrLrMrx�s4	


rxc@s(eZdZdd�ZdZdd�Zdd�ZdS)r*cCs\|j}|dd�dkrN|dd�dkrN|jrN|jdkrN|j|��vrXtd��n
|�|�SdS)Nr:rrSr
�	localhost�-file:// scheme is supported only on localhost)r�rz�	get_namesr�open_local_file)r�r�rHrLrLrM�	file_open�s&�
zFileHandler.file_openNcCs^tjdurXz*tt�d�dt�t���d�t_Wn"tjyVt�d�ft_Yn0tjS)Nr�r:)r*�namesr8r��gethostbyname_ex�gethostname�gaierror�
gethostbynamer�rLrLrMr��s
��
zFileHandler.get_namesc
Csddl}ddl}|j}|j}t|�}z�t�|�}|j}|jj	|j
dd�}	|�|�d}
|�d|
pbd||	f�}|r~t
|�\}}|r�|s�t|�|��vr�|r�d||}
nd|}
tt|d�||
�WSWn,ty�}zt|��WYd}~n
d}~00td��dS)	NrT�Zusegmtz6Content-type: %s
Content-length: %d
Last-modified: %s
�
text/plain�file://�rbzfile not on local host)�email.utils�	mimetypesrzr�r4r[�stat�st_size�utils�
formatdate�st_mtime�
guess_type�message_from_stringr
�_safe_gethostbynamer�rrGrqr)r�r��emailr�rzrfZ	localfile�statsrm�modified�mtyperirCZorigurl�exprLrLrMr��s:
����zFileHandler.open_local_file)r�r�r�r�r�r�r�rLrLrLrMr*�s
r*cCs(zt�|�WStjy"YdS0dSrN)r�r�r�)rzrLrLrMr��sr�c@seZdZdd�Zdd�ZdS)r+c
Cs.ddl}ddl}|j}|s"td��t|�\}}|dur>|j}nt|�}t|�\}}|rdt|�\}}nd}t	|�}|pvd}|p~d}zt
�|�}Wn,ty�}zt|��WYd}~n
d}~00t
|j�\}	}
|	�d�}ttt	|��}|dd�|d}}|�r|d�s|dd�}z�|�||||||j�}
|�r8d�p:d}|
D]2}t|�\}}|��d	k�r@|d
v�r@|��}�q@|
�||�\}}d}|�|j�d}|�r�|d|7}|du�r�|dk�r�|d|7}t�|�}t|||j�WS|j�y(}z*td
|�}|�t� �d��WYd}~n
d}~00dS)Nr�ftp error: no host givenrrr
rSrVrh�Dr���a�Ar�rhr�r�zContent-type: %s
zContent-length: %d
�
ftp error: %rr:)!�ftplibr�rzrr
�FTP_PORTrbrrr
r�r�rqrr�rXr��map�connect_ftprJrrx�upper�retrfiler�rtr�r�r�
all_errors�with_tracebackr��exc_info)r�r�r�r�rzrCr r1r�r\�attrs�dirsrO�fwr��attrr�rh�retrlenrir�r��excrLrLrM�ftp_open�s^
�
zFTPHandler.ftp_openc	Cst||||||dd�S)NF)�
persistent)�
ftpwrapper)r�r r1rzrCr�rJrLrLrMr�/s�zFTPHandler.connect_ftpN)r�r�r�r�r�rLrLrLrMr+�s5r+c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)r,cCs"i|_i|_d|_d|_d|_dS)Nr�<r})�cacherJ�soonest�delay�	max_connsr�rLrLrMr�6s
zCacheFTPHandler.__init__cCs
||_dSrN)r�)r��trLrLrM�
setTimeout=szCacheFTPHandler.setTimeoutcCs
||_dSrN)r�)r�rrLrLrM�setMaxConns@szCacheFTPHandler.setMaxConnscCsr|||d�|�|f}||jvr4t��|j|j|<n,t||||||�|j|<t��|j|j|<|��|j|S)Nr
)�joinr�r~r�rJr��check_cache)r�r r1rzrCr�rJr�rLrLrMr�Cs

�
zCacheFTPHandler.connect_ftpcCs�t��}|j|krPt|j���D].\}}||kr |j|��|j|=|j|=q tt|j����|_t	|j�|j
kr�t|j���D]&\}}||jkr�|j|=|j|=q�q�tt|j����|_dSrN)r~r�r�rJrr�r��min�valuesrdr�)r�r�r�rrLrLrMr�Ns


zCacheFTPHandler.check_cachecCs0|j��D]}|��q
|j��|j��dSrN)r�rr��clearrJ)r��connrLrLrM�clear_cachebs

zCacheFTPHandler.clear_cacheN)	r�r�r�r�r�r�r�r�rrLrLrLrMr,3sr,c@seZdZdd�ZdS)r-cCs~|j}|�dd�\}}|�dd�\}}t|�}|�d�rNt�|�}|dd�}|sVd}t�d|t|�f�}t	t
�|�||�S)N�:rVr�z;base64i�����text/plain;charset=US-ASCIIz$Content-type: %s
Content-length: %d
)rtrXr�endswithr-�decodebytesr�r�rdr�io�BytesIO)r�r�rHrrIZ	mediatyperirLrLrM�	data_openis



�zDataHandler.data_openN)r�r�r�r
rLrLrLrMr-hsr-r��nt)r4r3cCst|�S)zOS-specific conversion from a relative URL of the 'file' scheme
        to a file system path; not recommended for general use.)r
��pathnamerLrLrMr4�sr4cCst|�S)zOS-specific conversion from a file system path to a relative URL
        of the 'file' scheme; not recommended for general use.)r	rrLrLrMr3�sr3c@s�eZdZdZdZdeZd*dd�Zdd�Zdd	�Z	d
d�Z
dd
�Zd+dd�Zd,dd�Z
d-dd�Zd.dd�Zdd�Zd/dd�Zd0dd�Zdd�Zer�dd�Zd1d d!�Zd"d#�Zd$d%�Zd&d'�Zd2d(d)�ZdS)3r8a,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�cKszdd|jji}tj|tdd�|dur.t�}||_|�d�|_|�d�|_	d|j
fdg|_g|_t
j|_d|_t|_dS)	NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methods�classrS)�
stacklevel�key_file�	cert_filez
User-Agent)ZAcceptz*/*)rLr�r?r@rAr5r&r�rr�versionr��_URLopener__tempfilesr[rp�_URLopener__unlink�	tempcache�ftpcache)r�r&Zx509r�rLrLrMr��s
�zURLopener.__init__cCs|��dSrN)r�r�rLrLrM�__del__�szURLopener.__del__cCs|��dSrN)�cleanupr�rLrLrMr��szURLopener.closec	CsT|jr@|jD]&}z|�|�Wqty0Yq0q|jdd�=|jrP|j��dSrN)rrrqrr)r�rOrLrLrMr�s
zURLopener.cleanupcGs|j�|�dS)zdAdd a header to be used by the HTTP interface only
        e.g. u.addheader('Accept', 'sound/basic')N)r�ra)r�r�rLrLrM�	addheader�szURLopener.addheaderc
Csptt|��}t|dd�}|jrL||jvrL|j|\}}t|d�}t|||�St|�\}}|s`d}||jvr�|j|}t|�\}}	t|	�\}
}|
|f}nd}d|}||_	|�
dd�}t||�r�|d	kr�|r�|�|||�S|�
||�Sz0|du�rt||�|�WSt||�||�WSWnVttf�y.�Yn>t�yj}
z$td
|
��t��d��WYd}
~
n
d}
~
00dS)z6Use URLopener().open(file) instead of open(file, 'r').z%/:=&?~#+!$,;'@()*[]|�rr�rONZopen_�-r�r�zsocket errorr:)rrr	rrGrrr&rr�rr��open_unknown_proxy�open_unknownr�rrrqr�r�r�)r�r�rIrfrirh�urltyperHr�	proxyhostrzr�r_r�rLrLrMrG�s<




zURLopener.opencCst|�\}}tdd|��dS)�/Overridable interface to open unknown URL type.�	url errorzunknown url typeN�rrq)r�r�rIr�rHrLrLrMrszURLopener.open_unknowncCs t|�\}}tdd||��dS)r r!zinvalid proxy for %sNr")r�rr�rIr�rHrLrLrMrszURLopener.open_unknown_proxycCstt|��}|jr&||jvr&|j|St|�\}}|dur�|rF|dkr�z0|�|�}|��}|��tt|�d�|fWSt	y�Yn0|�
||�}�zL|��}	|r�t
|d�}
nrt|�\}}t|p�d�\}}t|p�d�\}}t|p�d�\}}t
j�|�d}
t�|
�\}}|j�|�t
�|d�}
z�||	f}|jdu�rD||j|<d}d}d}d}d	|	v�rjt|	d
�}|�r|||||�|�|�}|�s��q�|t|�7}|
�|�|d7}|�r|||||��q|W|
��n
|
��0W|��n
|��0|dk�r||k�rtd||f|��|S)ztretrieve(url) returns (filename, headers) for a local object
        or (tempfilename, headers) for a remote object.NrOrVrPrrrRrSrrTrUrW)rrrrr�rZr�r4rrqrGrrr[r\�splitextr^Zmkstemprra�fdopenrbrcrdrer)r�rHrfrgrIr�Zurl1rhr�rirjZgarbager\�suffix�fdrkrlrmrcrnrorLrLrM�retrieve
sn





��zURLopener.retrievecCs"d}d}t|t�r<t|�\}}|r6t|�\}}t|�}|}nt|\}}t|�\}}t|�\}	}
|
}d}|	��dkrvd}n:t|
�\}}
|r�t|�\}}|r�d|	||
f}t|�r�|}|s�tdd��|r�t|�}t	�
|����d�}nd}|�rt|�}t	�
|����d�}nd}||�}
i}|�r*d||d<|�r<d||d	<|�rJ||d
<d|d<|j
D]\}}|||<�qX|du�r�d
|d<|
�d|||�n|
jd||d�z|
��}Wn tjj�y�td��Yn0d|jk�r�dk�rnnt||jd||j�S|�||j|j|j|j|�SdS)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)zBasic %sr�rcrsr�r�r�zContent-Typer�r�r�z$http protocol error: bad status liner�r��http:)r�r�rrr
rrxr,rqr-r.r/r0r�ryr�r�r�Z
BadStatusLinerZstatusrr��
http_errorrhr�)r�Zconnection_factoryrHrIZuser_passwdZproxy_passwdrzr�Zrealhostrr�Z
proxy_authrbZ	http_connrirYr�r�rLrLrM�_open_generic_httpNst


��zURLopener._open_generic_httpcCs|�tjj||�S)zUse HTTP protocol.)r*r�r�r��r�rHrIrLrLrM�	open_http�szURLopener.open_httpc
Csbd|}t||�rPt||�}|dur6||||||�}	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�rHrh�errcode�errmsgrirIr_r�rkrLrLrMr)�s

zURLopener.http_errorcCs|��t||||d��dS)z>Default error handler: close the connection and raise OSError.N)r�r�r�rHrhr-r.rirLrLrMr��szURLopener.http_error_defaultcCstjj||j|jd�S)N)rr)r�r�r�rr)r�rzrLrLrM�_https_connection�s�zURLopener._https_connectioncCs|�|j||�S)zUse HTTPS protocol.)r*r0r+rLrLrM�
open_https�szURLopener.open_httpscCs^t|t�std��|dd�dkrP|dd�dkrP|dd���dkrPtd	��n
|�|�SdS)
z/Use local file or FTP depending on form of URL.zEfile error: proxy support for file protocol currently not implementedNr:rrSr
�z
localhost/r�)r�r�rrxrBr�r�rLrLrM�	open_file�s

4
zURLopener.open_filec
Cs^ddl}ddl}t|�\}}t|�}zt�|�}Wn2tyd}zt|j|j	��WYd}~n
d}~00|j
}	|jj|j
dd�}
|�|�d}|�d|p�d|	|
f�}|s�|}
|dd�dkr�d	|}
tt|d
�||
�St|�\}}|�sRt�|�t�ft�v�rR|}
|dd�dk�r"d	|}
n|dd�dk�r@td
|��tt|d
�||
�Std��dS)zUse local file.rNTr�z6Content-Type: %s
Content-Length: %d
Last-modified: %s
r�rVr
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r4r[r�rqr�strerrorrfr�r�r�r�r�r�rrGr
r�r�r��thishostrB)r�rHr�r�rzrOZ	localnamer��ermr�r�riZurlfilerCrLrLrMr��s@$���
zURLopener.open_local_filec
Cs�t|t�std��ddl}t|�\}}|s2td��t|�\}}t|�\}}|r\t|�\}}nd}t|�}t|ppd�}t|p|d�}t	�
|�}|s�ddl}|j}nt
|�}t|�\}}	t|�}|�d�}
|
dd�|
d}
}|
r�|
ds�|
dd�}
|
�r
|
d�s
d|
d<|||d�|
�f}t|j�tk�rbt|j�D]*}
|
|k�r6|j|
}|j|
=|���q6z�||jv�r�t|||||
�|j|<|�s�d	}nd
}|	D]2}t|�\}}|��dk�r�|dv�r�|��}�q�|j|�||�\}}|�d
|�d}d}|�r|d|7}|du�r,|dk�r,|d|7}t�|�}t||d
|�WSt��y�}z&td|�� t!�"�d��WYd}~n
d}~00dS)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedrNr�rrr
rSrVr�rhr�r�zftp:zContent-Type: %s
zContent-Length: %d
zftp error %rr:)#r�r�rr�rr
rrr
r�r�r�r�rbrrXr�rdr�MAXFTPCACHEr�r�r�rrxr�r�r�r�r�r�	ftperrorsr�r�r�)r�rHr�rzr\rCr r1r�r�r�rOr�r�rr�r�r�rhr�r�rir�rLrLrM�open_ftp�sj




��
zURLopener.open_ftpc	
Cs:t|t�std��z|�dd�\}}WntyBtdd��Yn0|sLd}|�d�}|dkr�d	||d
�vr�||dd
�}|d
|�}nd}g}|�dt�	d
t�
t�����|�d|�|dkr�t�|�
d���d�}nt|�}|�dt|��|�d�|�|�d�|�}t�|�}t�|�}t|||�S)zUse "data" URL.zEdata error: proxy support for data protocol currently not implementedr�rVz
data errorzbad data URLr�;rr�NrrzDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %sr-r)zlatin-1zContent-Length: %d�
)r�r�rrXrBrq�rfindrar~�strftime�gmtimer-rr/r0r
rdr�r�r�r�StringIOr)	r�rHrIr�Zsemirr�ri�frLrLrM�	open_data0s8

�




zURLopener.open_data)N)N)N)N)NNN)N)N)N)N)r�r�r�r�rr�rr�rr�rrrGrrr'r*r,r)r�rCr0r1r3r�r9rArLrLrLrMr8�s.

$


A\


	 :r8c@s�eZdZdZdd�Zdd�Zd#dd�Zd	d
�Zd$dd�Zd%d
d�Z	d&dd�Z
d'dd�Zd(dd�Zd)dd�Z
d*dd�Zd+dd�Zd,dd�Zd-dd �Zd!d"�ZdS).r9z?Derived class with handlers for errors we can handle (perhaps).cOs.tj|g|�Ri|��i|_d|_d|_dS)Nrr�)r8r��
auth_cache�tries�maxtriesrIrLrLrMr�]szFancyURLopener.__init__cCst||d||�S)z3Default error handling -- don't raise an exception.r()rr/rLrLrMr�csz!FancyURLopener.http_error_defaultNc	Cs~|jd7_zb|jrR|j|jkrRt|d�r4|j}n|j}|||dd|�Wd|_S|�||||||�}|Wd|_Sd|_0dS)z%Error 302 -- relocated (temporarily).rV�http_error_500r�z)Internal Server Error: Redirect RecursionrN)rCrDr�rEr��redirect_internal)	r�rHrhr-r.rirIr�rkrLrLrMrgs&
����zFancyURLopener.http_error_302c	Csxd|vr|d}nd|vr$|d}ndS|��t|jd||�}t|�}|jdvrnt|||d|||��|�|�S)Nrrrrz( Redirection to url '%s' is not allowed.)r�rr�rrrrG)	r�rHrhr-r.rirIrrrLrLrMrFys 


��z FancyURLopener.redirect_internalcCs|�||||||�S)z*Error 301 -- also relocated (permanently).�r�r�rHrhr-r.rirIrLrLrMr�szFancyURLopener.http_error_301cCs|�||||||�S)z;Error 303 -- also relocated (essentially identical to 302).rGrHrLrLrMr�szFancyURLopener.http_error_303cCs2|dur|�||||||�S|�|||||�SdS)z1Error 307 -- relocated, but turn POST into error.N)rr�rHrLrLrMr�szFancyURLopener.http_error_307Fc
Cs�d|vrt�||||||�|d}t�d|�}	|	sHt�||||||�|	��\}
}|
��dkrtt�||||||�|s�t�||||||�d|jd}|dur�t||�||�St||�|||�SdS)z_Error 401 -- authentication required.
        This function supports Basic authentication only.rj�![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r[Zretry_�_basic_authN�r8r�rf�matchrVrxr�r��
r�rHrhr-r.rirIr�ZstuffrLrr9r_rLrLrMrk�s.
�
�
��zFancyURLopener.http_error_401c
Cs�d|vrt�||||||�|d}t�d|�}	|	sHt�||||||�|	��\}
}|
��dkrtt�||||||�|s�t�||||||�d|jd}|dur�t||�||�St||�|||�SdS)zeError 407 -- proxy authentication required.
        This function supports Basic authentication only.rmrIr[Zretry_proxy_rJNrKrMrLrLrMrn�s.
�
�
��zFancyURLopener.http_error_407cCs�t|�\}}d||}|jd}t|�\}}	t|	�\}	}
|	�d�d}|	|d�}	|�|	||�\}}
|sr|
srdSdt|dd�t|
dd�|	f}	d|	|
|jd<|dur�|�|�S|�||�SdS)N�http://r�rrV�%s:%s@%srrr�rr&rr��get_user_passwdr	rG�r�rHr9rIrzr�rrrrZ
proxyselectorr�r r1rLrLrM�retry_proxy_http_basic_auth�s 
�
z*FancyURLopener.retry_proxy_http_basic_authcCs�t|�\}}d||}|jd}t|�\}}	t|	�\}	}
|	�d�d}|	|d�}	|�|	||�\}}
|sr|
srdSdt|dd�t|
dd�|	f}	d|	|
|jd<|dur�|�|�S|�||�SdS)N�https://r�rrVrOrrrrPrRrLrLrM�retry_proxy_https_basic_auth�s 
�
z+FancyURLopener.retry_proxy_https_basic_authc
Cs�t|�\}}|�d�d}||d�}|�|||�\}}|sD|sDdSdt|dd�t|dd�|f}d||}	|dur�|�|	�S|�|	|�SdS)NrrVrOrrrrN�rr�rQr	rG�
r�rHr9rIrzr�r�r r1rrLrLrMr\�s�
z$FancyURLopener.retry_http_basic_authc
Cs�t|�\}}|�d�d}||d�}|�|||�\}}|sD|sDdSdt|dd�t|dd�|f}d||}	|dur�|�|	�S|�|	|�SdS)NrrVrOrrrrTrVrWrLrLrM�retry_https_basic_auth	s�
z%FancyURLopener.retry_https_basic_authrcCs`|d|��}||jvr2|r(|j|=n
|j|S|�||�\}}|sJ|rX||f|j|<||fS)Nr)rxrB�prompt_user_passwd)r�rzr9rr�r r1rLrLrMrQ	s


zFancyURLopener.get_user_passwdcCsVddl}z.td||f�}|�d|||f�}||fWStyPt�YdS0dS)z#Override this in a GUI environment!rNzEnter username for %s at %s: z#Enter password for %s in %s at %s: r<)�getpass�input�KeyboardInterrupt�print)r�rzr9rZr r1rLrLrMrY!	s�
z!FancyURLopener.prompt_user_passwd)N)N)N)N)NF)NF)N)N)N)N)r)r�r�r�r�r�r�rrFrrrrkrnrSrUr\rXrQrYrLrLrLrMr9Zs(



�
�





r9cCstdurt�d�atS)z8Return the IP address of the magic hostname 'localhost'.Nr�)�
_localhostr�r�rLrLrLrMr�1	s
r�cCsNtdurJztt�t���d�aWn&tjyHtt�d�d�aYn0tS)z,Return the IP addresses of the current host.Nr:r�)�	_thishostr8r�r�r�r�rLrLrLrMr59	sr5cCstdurddl}|jatS)z1Return the set of errors raised by the FTP class.Nr)�
_ftperrorsr�r�)r�rLrLrMr8D	sr8cCstdurt�d�atS)z%Return an empty email Message object.Nrr)�
_noheadersr�r�rLrLrLrM�	noheadersM	s
rbc@sJeZdZdZddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dS)r�z;Class used by open_ftp() for cache of open FTP connections.NTcCsX||_||_||_||_||_||_d|_||_z|��Wn|�	��Yn0dSro)
r r1rzrCr�rJ�refcount�	keepalive�initr�)r�r r1rzrCr�rJr�rLrLrMr�Z	szftpwrapper.__init__cCs\ddl}d|_|��|_|j�|j|j|j�|j�|j	|j
�d�|j�}|j�
|�dS)Nrr
)r��busyZFTPr	ZconnectrzrCrJZloginr r1r�r��cwd)r�r�Z_targetrLrLrMrej	s
zftpwrapper.initc
Cs�ddl}|��|dvr"d}d}nd|}d}z|j�|�Wn(|jyf|��|j�|�Yn0d}|r�|s�zd|}|j�|�\}}WnT|jy�}z:t|�dd�dkr�t	d	|��
t��d
��WYd}~n
d}~00|�s�|j�d�|�rz|j�
�}	zXz|j�|�Wn6|j�yP}zt	d	|�|�WYd}~n
d}~00W|j�|	�n|j�|	�0d|}nd}|j�|�\}}d|_t|�d
�|j�}
|jd7_|��|
|fS)Nr)r�r�zTYPE ArVzTYPE zRETR rSZ550r�r:zLIST ZLISTr�)r��endtransferr	Zvoidcmdr�reZntransfercmdZ
error_permr�rr�r�r��pwdrgrfrZmakefile�
file_closercr�)r�rOr�r��cmd�isdirrr�r�riZftpobjrLrLrMr�s	sJ
�
&
zftpwrapper.retrfilecCs
d|_dSro)rfr�rLrLrMrh�	szftpwrapper.endtransfercCsd|_|jdkr|��dS)NFr)rdrc�
real_closer�rLrLrMr��	s
zftpwrapper.closecCs2|��|jd8_|jdkr.|js.|��dS)NrVr)rhrcrdrmr�rLrLrMrj�	szftpwrapper.file_closecCs0|��z|j��Wnt�y*Yn0dSrN)rhr	r�r8r�rLrLrMrm�	s
zftpwrapper.real_close)NT)r�r�r�r�r�rer�rhr�rjrmrLrLrLrMr�W	s�
	-r�cCs�i}tj��D]4\}}|��}|r|dd�dkr|||dd�<qdtjvrZ|�dd�tj��D]J\}}|dd�dkrd|��}|r�|||dd�<qd|�|dd�d�qd|S)aReturn 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�_proxyZREQUEST_METHODr�)r[�environrrxr�)r&r_r�rLrLrM�getproxies_environment�	s	
rpcCs�|durt�}z|d}Wnty.YdS0|dkr<dS|��}t|�\}}|�d�D]Z}|��}|rZ|�d�}|��}||ks�||kr�dSd|}|�|�s�|�|�rZdSqZdS)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.

    NZnoF�*Tr��.)rpr�rxr
rXrd�lstripr)rzr&Zno_proxy�hostonlyrCr_rLrLrM�proxy_bypass_environment�	s*
ruc
Cs6ddlm}ddlm}m}t|�\}}dd�}d|vrD|drDdSd	}zt||��}Wn|yjYn0|�d
d�D]�}	|	s�qxt�d|	�}
|
d	u�r |d	u�r ||
�	d
��}|
�	d�}|d	ur�d|
�	d
��
d�d
}nt|d
d	��}|dksx|dkr�qxd|}||?||?k�r0dSqx|||	�rxdSqxdS)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��fnmatch)�AddressValueError�IPv4AddresscSsd|�d�}ttt|��}t|�dkr8|gd�dd�}|dd>|dd>B|dd	>B|d
BS)Nrrr�)rrrrr�rVr}r:r|rS)rXr�r�rbrd)ZipAddrrBrLrLrM�ip2num
s

z,_proxy_bypass_macosx_sysconf.<locals>.ip2numrrZexclude_simpleTN�
exceptionsrLz(\d+(?:\.\d+)*)(/\d+)?rVr:r|� F)rwZ	ipaddressrxryr
rbr�rfrL�group�count)
rz�proxy_settingsrwrxryrtrCr{ZhostIPr�rrD�maskrLrLrM�_proxy_bypass_macosx_sysconf�	s:

r�cCs`ddlm}t|�\}}|�d�}|D]4}|��}|dkrJd|vrZdSq&|||�r&dSq&dS)a
Return True if the host should bypass the proxy server.

    The proxy override list is obtained from the Windows
    Internet settings proxy override registry value.

    An example of a proxy override value is:
    "www.example.com;*.example.net; 192.168.0.1"
    rrvr:z<local>rrTF)rwr
rXrd)rz�overriderwr�Zproxy_overriderErLrLrM�_proxy_bypass_winreg_override:
s	

r��darwin)�_get_proxy_settings�_get_proxiescCst�}t||�SrN)r�r�)rzr�rLrLrM�proxy_bypass_macosx_sysconfU
sr�cCst�S)z�Return a dictionary of scheme -> proxy server URL mappings.

        This function uses the MacOSX framework SystemConfiguration
        to fetch the proxy information.
        )r�rLrLrLrM�getproxies_macosx_sysconfY
sr�cCs t�}|rt||�St|�SdS)z�Return True, if host should be bypassed.

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

        N)rprur��rzr&rLrLrMr,c
s
r,cCst�p
t�SrN)rpr�rLrLrLrMr5p
sr5c
CsBi}zddl}Wnty&|YS0z�|�|jd�}|�|d�d}|�rt|�|d�d�}d|vr|d|vr|d�|�}|�d�D]J}|�dd	�\}}t�	d
|�s�|dvr�d|}n|d
kr�d|}|||<q�|�
d
��rt�dd|d
�}|�
d��p�||d<|�
d��p||d<|��Wnt
ttf�y<Yn0|S)zxReturn a dictionary of scheme -> proxy server URL mappings.

        Win32 uses the registry to store proxies.

        rN�;Software\Microsoft\Windows\CurrentVersion\Internet Settings�ProxyEnableZProxyServerr�r:zhttp={0};https={0};ftp={0}rVz
(?:[^/:]+)://)r�r�r	rNZsockszsocks://z	^socks://z	socks4://r�r�)�winreg�ImportError�OpenKey�HKEY_CURRENT_USER�QueryValueExr�r�rXrfrLr�rwZCloserqrBr�)r&r��internetSettings�proxyEnableZproxyServer�pr�ZaddressrLrLrM�getproxies_registryu
sL
�����


r�cCs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.

        )rpr�rLrLrLrMr5�
scCs�zddl}Wnty YdS0z6|�|jd�}|�|d�d}t|�|d�d�}WntylYdS0|rv|szdSt||�S)NrFr�r�Z
ProxyOverride)r�r�r�r�r�r�rqr�)rzr�r�r�Z
proxyOverriderLrLrM�proxy_bypass_registry�
s.�����
r�cCs t�}|rt||�St|�SdS)z�Return True, if host should be bypassed.

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

        N)rprur�r�rLrLrMr,�
s
)NNN)N)r�r-r�r�r�Zhttp.clientr�rr[�	posixpathrfr�rr�r~r^rXr?Zurllib.errorrrrZurllib.parserrrrr	r
rrr
rrrrrrrrrZurllib.responserrrDr�rC�__all__�version_infor�rFr�r0r1r`r6r7rg�ASCIIrvr{rrr2rr/rrr"rr r!r"r#r$r%�urandomr�r&r'r(r�r)r�r�rErarr.rvrxr*r�r+r,r-r7r_Z
nturl2pathr4r3rr8r9r^r�r_r5r`r8rarbr�rprur�r��platformZ_scproxyr�r�r�r�r,r5r�r�rLrLrLrM�<module>s�SP
�M
?m$q!+@
ov

+3:5!@W

_
%@

1