AlkantarClanX12

Your IP : 3.142.131.51


Current Path : /usr/lib64/python3.6/urllib/__pycache__/
Upload File :
Current File : //usr/lib64/python3.6/urllib/__pycache__/request.cpython-36.opt-1.pyc

3

��f~��)@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlmZmZmZddlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)ddl*m+Z+m,Z,yddl-Z-Wne.k
�r&dZ/YnXdZ/dd	d
ddd
ddddddddddddddddddd d!d"d#d$d%d&d'd(g!Z0d)e
j1dd*�Z2da3dej4fddddd+�d,d�Z5d-d �Z6gZ7d~d.d%�Z8d/d&�Z9e
j:d0e
j;�Z<d1d2�Z=Gd3d�d�Z>Gd4d	�d	�Z?d5d!�Z@Gd6d
�d
�ZAGd7d�deA�ZBGd8d�deA�ZCGd9d�deA�ZDd:d;�ZEGd<d�deA�ZFGd=d�d�ZGGd>d�deG�ZHGd?d�deH�ZIGd@d�d�ZJGdAd�deJeA�ZKGdBd�deJeA�ZLejMZNGdCd�d�ZOGdDd�deAeO�ZPGdEd�deAeO�ZQGdFdG�dGeA�ZRGdHd�deR�ZSeTejUdI��r2GdJdK�dKeR�ZVe0jWdK�GdLd
�d
eA�ZXGdMd�deA�ZYdNdO�ZZdPdQ�Z[GdRd�deA�Z\dSdT�Z]GdUd�deA�Z^GdVd�de^�Z_GdWd�deA�Z`dXZaejbdYk�r�ddZlcmdZdmeZend[d#�Zdd\d"�ZeiZfGd]d'�d'�ZgGd^d(�d(eg�Zhdaid_d`�Zjdakdadb�Zldamdcdd�Zndaodedf�ZpGdgdh�dh�Zqdidj�Zrddkdl�Zsdmdn�Zte
judok�r�ddplvmwZwmxZxdqdr�Zydsdt�Zzdudv�Z{dwd$�Z|n6ejbdYk�r�dxdy�Z}dzd$�Z|d{d|�Z~d}dv�Z{nerZ|esZ{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('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
Cs�|s|s|rfddl}|jdtd�|dk	r2td��ts>td��tjtjj||d�}t	|d�}t
|�}	n0|r~t	|d�}t
|�}	ntdkr�t
�a}	nt}	|	j|||�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.

    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�&/usr/lib64/python3.6/urllib/request.pyr0�s*<




cCs|adS)N)rF)rKrLrLrMr1�scCs4t|�\}}tjt||����}|j�}|dkrD|rDtjj|�|fS|rTt|d�}nt	j
dd�}|j}tj
|�|��||f}	d
}
d}d}d}
d	|kr�t|d
�}|r�||
|
|�xB|j|
�}|s�P|t|�7}|j|�|
d7}
|r�||
|
|�q�WWdQRXWdQRX|dk�r0||k�r0td||f|	��|	S)aW
    Retrieve a URL into a temporary location on disk.

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

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

    Returns a tuple containing the path to the newly created
    data file as well as the resulting HTTPMessage object.
    �file�wbF)�deletei��rzcontent-lengthzContent-LengthNz1retrieval incomplete: got only %i out of %i bytesi ���)r�
contextlib�closingr0�info�os�path�normpathrG�tempfileZNamedTemporaryFile�name�_url_tempfiles�append�int�read�len�writer)rH�filename�
reporthookrIZurl_typerX�fp�headers�tfp�result�bs�sizer_�blocknum�blockrLrLrMr6�sD


$cCsHx0tD](}ytj|�Wqtk
r,YqXqWtdd�=trDdadS)z0Clean up temporary files from urlretrieve calls.N)r\rW�unlink�OSErrorrF)Z	temp_filerLrLrMr7%s


z:\d+$cCs<|j}t|�d}|dkr&|jdd�}tjd|d�}|j�S)z�Return request-host, as defined by RFC 2965.

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

    rR��Host)�full_urlr�
get_header�_cut_port_re�sub�lower)�requestrH�hostrLrLrM�request_host4srwc@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	Csp||_i|_i|_d|_||_d|_x |j�D]\}}|j||�q.W|dkrVt|�}||_	||_
|rl||_dS)N)rpre�unredirected_hdrs�_datarI�_tunnel_host�items�
add_headerrw�origin_req_host�unverifiable�method)	�selfrHrIrer}r~r�key�valuerLrLrM�__init__FszRequest.__init__cCs|jrdj|j|j�S|jS)Nz{}#{})�fragment�format�	_full_url)r�rLrLrMrpXszRequest.full_urlcCs(t|�|_t|j�\|_|_|j�dS)N)rr�rr��_parse)r�rHrLrLrMrp^s
cCsd|_d|_d|_dS)Nrn)r�r��selector)r�rLrLrMrpescCs|jS)N)ry)r�rLrLrMrIkszRequest.datacCs(||jkr$||_|jd�r$|jd�dS)NzContent-length)ry�
has_header�
remove_header)r�rIrLrLrMrIos

cCs
d|_dS)N)rI)r�rLrLrMrIyscCsNt|j�\|_}|jdkr(td|j��t|�\|_|_|jrJt|j�|_dS)Nzunknown url type: %r)	rr��typerBrprrvr�r
)r��restrLrLrMr�}s
zRequest._parsecCs|jdk	rdnd}t|d|�S)z3Return a string indicating the HTTP request method.N�POST�GETr)rI�getattr)r�Zdefault_methodrLrLrM�
get_method�szRequest.get_methodcCs|jS)N)rp)r�rLrLrM�get_full_url�szRequest.get_full_urlcCs4|jdkr|jr|j|_n||_|j|_||_dS)N�https)r�rzrvrpr�)r�rvr�rLrLrM�	set_proxy�s

zRequest.set_proxycCs|j|jkS)N)r�rp)r�rLrLrM�	has_proxy�szRequest.has_proxycCs||j|j�<dS)N)re�
capitalize)r�r��valrLrLrMr|�szRequest.add_headercCs||j|j�<dS)N)rxr�)r�r�r�rLrLrM�add_unredirected_header�szRequest.add_unredirected_headercCs||jkp||jkS)N)rerx)r��header_namerLrLrMr��s
zRequest.has_headercCs|jj||jj||��S)N)re�getrx)r�r��defaultrLrLrMrq�szRequest.get_headercCs |jj|d�|jj|d�dS)N)re�poprx)r�r�rLrLrMr��szRequest.remove_headercCs"|jj�}|j|j�t|j��S)N)rx�copy�updatere�listr{)r��hdrsrLrLrM�header_items�s
zRequest.header_items)N)�__name__�
__module__�__qualname__r��propertyrp�setter�deleterrIr�r�r�r�r�r|r�r�rqr�r�rLrLrLrMrDs(

c@sNeZdZdd�Zdd�Zdd�Zdd�Zd	ejfd
d�Z	ddd
�Z
dd�Zd	S)rcCs6dt}d|fg|_g|_i|_i|_i|_i|_dS)NzPython-urllib/%sz
User-agent)�__version__�
addheaders�handlers�handle_open�handle_error�process_response�process_request)r�Zclient_versionrLrLrMr��szOpenerDirector.__init__cCsZt|d�stdt|���d}�xt|�D�]}|dkr:q*|jd�}|d|�}||dd�}|jd	�r�|jd�|d}||dd�}yt|�}Wntk
r�YnX|jj	|i�}	|	|j|<n>|d
kr�|}|j
}	n*|dkr�|}|j}	n|dkr*|}|j}	nq*|	j
|g�}
|
�r&tj|
|�n
|
j|�d
}q*W|�rVtj|j|�|j|�dS)N�
add_parentz%expected BaseHandler instance, got %rF�redirect_request�do_open�
proxy_open�_rR�errorrG�responseruT)r�r�r�)�hasattr�	TypeErrorr��dir�find�
startswithr^rBr�r�r�r�r��
setdefault�bisectZinsortr]r�r�)r��handlerZadded�meth�i�protocolZ	condition�j�kind�lookupr�rLrLrM�add_handler�sJ



zOpenerDirector.add_handlercCsdS)NrL)r�rLrLrM�close�szOpenerDirector.closec	Gs<|j|f�}x*|D]"}t||�}||�}|dk	r|SqWdS)N)r�r�)	r��chainr��	meth_name�argsr�r��funcrgrLrLrM�_call_chain�s

zOpenerDirector._call_chainNc
Cs�t|t�rt||�}n|}|dk	r(||_||_|j}|d}x(|jj|g�D]}t||�}||�}qLW|j	||�}	|d}x*|j
j|g�D]}t||�}|||	�}	q�W|	S)NZ_requestZ	_response)�
isinstance�strrrIrJr�r�r�r��_openr�)
r��fullurlrIrJ�reqr�r�Z	processorr�r�rLrLrMrG�s"


zOpenerDirector.opencCsP|j|jdd|�}|r|S|j}|j|j||d|�}|r>|S|j|jdd|�S)Nr�Zdefault_openr��unknown�unknown_open)r�r�r�)r�r�rIrgr�rLrLrMr�s


zOpenerDirector._opencGs~|d
kr,|jd}|d}d|}d}|}n|j}|d}d}|||f|}|j|�}|r^|S|rz|dd	f|}|j|�SdS)N�httpr�r:z
http_error_%srRZ_errorrr��http_error_default)r�r�)r�r�)r��protor��dictr�Zhttp_errZ	orig_argsrgrLrLrMr�'s 

zOpenerDirector.error)N)r�r�r�r�r�r�r��socket�_GLOBAL_DEFAULT_TIMEOUTrGr�r�rLrLrLrMr�s/
c	Gs�t�}ttttttttt	g	}t
tjd�r2|j
t�t�}xN|D]F}x@|D]8}t|t�rlt||�r�|j|�qHt||�rH|j|�qHWq>Wx|D]}|j|�q�Wx|D]}|j|��q�Wx&|D]}t|t�r�|�}|j|�q�W|S)a*Create an opener object from a list of handlers.

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

    If any of the handlers passed as arguments are subclasses of the
    default handlers, the default handlers will not be used.
    �HTTPSConnection)rrr.r)rrr+r*r/r-r�r��clientr]rE�setr�r��
issubclass�add�remover�)r�rKZdefault_classes�skip�klassZcheck�hrLrLrMr2@s0	









c@s(eZdZdZdd�Zdd�Zdd�ZdS)	ri�cCs
||_dS)N)�parent)r�r�rLrLrMr�gszBaseHandler.add_parentcCsdS)NrL)r�rLrLrMr�jszBaseHandler.closecCst|d�sdS|j|jkS)N�
handler_orderT)r�r�)r��otherrLrLrM�__lt__ns
zBaseHandler.__lt__N)r�r�r�r�r�r�r�rLrLrLrMrdsc@s eZdZdZdZdd�ZeZdS)r/zProcess HTTP error responses.i�cCsJ|j|j|j�}}}d|ko*dknsF|jjd|||||�}|S)N��i,r�)�code�msgrVr�r�)r�rur�r�r�r�rLrLrM�
http_response{s
z HTTPErrorProcessor.http_responseN)r�r�r��__doc__r�r��https_responserLrLrLrMr/wsc@seZdZdd�ZdS)rcCst|j||||��dS)N)rrp)r�r�rdr�r�r�rLrLrMr��sz*HTTPDefaultErrorHandler.http_error_defaultN)r�r�r�r�rLrLrLrMr�sc@s4eZdZdZdZdd�Zdd�ZeZZZ	dZ
dS)	r��
c	sx|j�}|dkr|dkp&|dko&|dks:t|j||||��|jdd	�}d�t�fdd
�|jj�D��}t|||jdd�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.
        �-�.�/�3r��HEADr�� z%20�content-length�content-typec3s&|]\}}|j��kr||fVqdS)N)rt)�.0�k�v)�CONTENT_HEADERSrLrM�	<genexpr>�sz7HTTPRedirectHandler.redirect_request.<locals>.<genexpr>T)rer}r~)r�r�r�r�)r�r�)r�r�r�)r�r�)	r�rrp�replacer�rer{rr})	r�r�rdr�r�re�newurl�mZ
newheadersrL)r�rMr��s
z$HTTPRedirectHandler.redirect_requestc
CsNd|kr|d}nd|kr$|d}ndSt|�}|jdkrRt||d||f||��|jrp|jrpt|�}d|d	<t|�}t|d
tj	d�}t
|j|�}|j||||||�}|dkr�dSt
|d��r|j}	|_|	j|d
�|jks�t|	�|jk�rt|j||j|||��ni}	|_|_|	j|d
�d|	|<|j�|j�|jj||jd�S)N�location�urir�r��ftprnz+%s - Redirection to url '%s' is not allowed�/r:z
iso-8859-1)�encoding�safe�
redirect_dictrrR)rJ)r�r�r�rn)r�schemerrXZnetlocr�rr	�stringZpunctuationrrpr�r�rr��max_repeatsr`�max_redirections�inf_msgr_r�r�rGrJ)
r�r�rdr�r�rer��urlparts�newZvisitedrLrLrM�http_error_302�s@



z"HTTPRedirectHandler.http_error_302zoThe HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
N)r�r�r�rrr�r
�http_error_301�http_error_303�http_error_307rrLrLrLrMr�s&<c	Cs�t|�\}}|jd�s d}|}n:|jd�s6td|��|jdd�}|dkrNd}|d|�}t|�\}}|dk	r|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�Nz//zproxy URL with no authority: %rr:rRrS)rr�rBr�rr)	�proxyrZr_scheme�	authority�endZuserinfo�hostport�user�passwordrLrLrM�_parse_proxy�s

rc@s"eZdZdZddd�Zdd�ZdS)r�dNcCsL|dkrt�}||_x2|j�D]&\}}t|d||||jfdd��qWdS)Nz%s_opencSs||||�S)NrL)�rrr�r�rLrLrM�<lambda>%sz'ProxyHandler.__init__.<locals>.<lambda>)r5�proxiesr{�setattrr�)r�rr�rHrLrLrMr�s
zProxyHandler.__init__cCs�|j}t|�\}}}}|dkr"|}|jr6t|j�r6dS|rv|rvdt|�t|�f}	tj|	j��jd�}
|j	dd|
�t|�}|j
||�||ks�|dkr�dS|jj||j
d�SdS)Nz%s:%s�asciizProxy-authorizationzBasic r�)rJ)r�rrv�proxy_bypassr
�base64�	b64encode�encode�decoder|r�r�rGrJ)r�r�rr�Z	orig_typeZ
proxy_typerrrZ	user_passZcredsrLrLrMr�(s zProxyHandler.proxy_open)N)r�r�r�r�r�r�rLrLrLrMrs

c@s6eZdZdd�Zdd�Zdd�Zd
dd	�Zd
d�ZdS)r cCs
i|_dS)N)�passwd)r�rLrLrMr�FszHTTPPasswordMgr.__init__cs`t|t�r|g}|�jkr$i�j|<x6dD].�t��fdd�|D��}||f�j||<q*WdS)NTFcsg|]}�j|���qSrL)�
reduce_uri)r��u)�default_portr�rLrM�
<listcomp>Qsz0HTTPPasswordMgr.add_password.<locals>.<listcomp>)TF)r�r�r �tuple)r��realmr�rr �reduced_urirL)r#r�rM�add_passwordIs



zHTTPPasswordMgr.add_passwordc	Cs`|jj|i�}xLdD]D}|j||�}x2|j�D]&\}}x|D]}|j||�r<|Sq<Wq.WqWdS)NTF)TF)NN)r r�r!r{�	is_suburi)	r�r&�authuriZdomainsr#�reduced_authuriZurisZauthinfor�rLrLrM�find_user_passwordTs

z"HTTPPasswordMgr.find_user_passwordTc
Cs�t|�}|dr.|d}|d}|dp*d}nd}|}d}t|�\}}|r~|dkr~|dk	r~ddd�j|�}	|	dk	r~d	||	f}||fS)
z@Accept authority or URI and extract only the authority and path.rRrr:r�N�Pi�)r�r�z%s:%d)rr
r�)
r�r�r#�partsrrrXrv�portZdportrLrLrMr!^s zHTTPPasswordMgr.reduce_uricCsR||krdS|d|dkr dStj|d|df�}t|�t|d�krNdSdS)zcCheck if test is below base in a URI tree

        Both args must be URIs in reduced form.
        TrFrR)�	posixpath�commonprefixr`)r��base�test�commonrLrLrMr)uszHTTPPasswordMgr.is_suburiN)T)r�r�r�r�r(r,r!r)rLrLrLrMr Ds


c@seZdZdd�ZdS)r!cCs0tj|||�\}}|dk	r"||fStj|d|�S)N)r r,)r�r&r*rrrLrLrMr,�s

z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r�r�r�r,rLrLrLrMr!�scs<eZdZ�fdd�Zd
�fdd�	Zddd�Zdd	�Z�ZS)r"csi|_t�j||�dS)N)�
authenticated�superr�)r�r��kwargs)�	__class__rLrMr��sz%HTTPPasswordMgrWithPriorAuth.__init__Fcs<|j||�|dk	r&t�jd|||�t�j||||�dS)N)�update_authenticatedr6r()r�r&r�rr �is_authenticated)r8rLrMr(�sz)HTTPPasswordMgrWithPriorAuth.add_passwordcCsFt|t�r|g}x0dD](}x"|D]}|j||�}||j|<q WqWdS)NTF)TF)r�r�r!r5)r�r�r:r#r"r'rLrLrMr9�s


z1HTTPPasswordMgrWithPriorAuth.update_authenticatedcCsDx>dD]6}|j||�}x$|jD]}|j||�r|j|SqWqWdS)NTF)TF)r!r5r))r�r*r#r+r�rLrLrMr:�s

z-HTTPPasswordMgrWithPriorAuth.is_authenticated)F)F)r�r�r�r�r(r9r:�
__classcell__rLrL)r8rMr"�s

c@sTeZdZejdej�Zddd�Zdd�Zdd�Z	d	d
�Z
dd�Zd
d�ZeZ
eZdS)r#z1(?:^|,)[ 	]*([^ 	,]+)[ 	]+realm=(["']?)([^"']*)\2NcCs"|dkrt�}||_|jj|_dS)N)r r r()r�Zpassword_mgrrLrLrMr��sz!AbstractBasicAuthHandler.__init__ccstd}xFtjj|�D]6}|j�\}}}|d	kr:tjdtd�||fVd}qW|sp|rb|j�d}nd}|dfVdS)
NF�"�'zBasic Auth Realm was unquoted�Trrn)r<r=)r#�rx�finditer�groupsr?r@�UserWarning�split)r��headerZfound_challengeZmorr	r&rLrLrM�_parse_realm�s
z%AbstractBasicAuthHandler._parse_realmc	Cs~|j|�}|sdSd}xL|D]D}x>|j|�D]0\}}|j�dkrF|}q,|dk	r,|j|||�Sq,WqW|dk	rztd|f��dS)N�basiczBAbstractBasicAuthHandler does not support the following scheme: %r)Zget_allrErt�retry_http_basic_authrB)	r��authreqrvr�reZunsupportedrDrr&rLrLrM�http_error_auth_reqed�s

z.AbstractBasicAuthHandler.http_error_auth_reqedcCs||jj||�\}}|dk	rtd||f}dtj|j��jd�}|j|jd�|krTdS|j|j|�|j	j
||jd�SdSdS)Nz%s:%szBasic r)rJ)r r,rrrrrq�auth_headerr�r�rGrJ)r�rvr�r&r�pw�raw�authrLrLrMrG�sz.AbstractBasicAuthHandler.retry_http_basic_authcCsxt|jd�s|jj|j�r"|S|jd�st|jjd|j�\}}dj||�j�}tj	|�j
�}|jddj|j���|S)Nr:�
Authorizationz{0}:{1}zBasic {})
r�r r:rpr�r,r�rrZstandard_b64encoderr��strip)r�r�rr ZcredentialsZauth_strrLrLrM�http_requests
z%AbstractBasicAuthHandler.http_requestcCsLt|jd�rHd|jko dknr8|jj|jd�n|jj|jd�|S)Nr:r�i,TF)r�r r�r9rp)r�r�r�rLrLrMr�s
z&AbstractBasicAuthHandler.http_response)N)r�r�r��re�compile�Ir?r�rErIrGrPr��
https_requestr�rLrLrLrMr#�s

c@seZdZdZdd�ZdS)r$rNcCs|j}|jd|||�}|S)Nzwww-authenticate)rprI)r�r�rdr�r�rerHr�rLrLrM�http_error_401 s
z#HTTPBasicAuthHandler.http_error_401N)r�r�r�rJrUrLrLrLrMr$sc@seZdZdZdd�ZdS)r%zProxy-authorizationcCs|j}|jd|||�}|S)Nzproxy-authenticate)rvrI)r�r�rdr�r�rerr�rLrLrM�http_error_407+s
z$ProxyBasicAuthHandler.http_error_407N)r�r�r�rJrVrLrLrLrMr%'sc@sNeZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dS)r&NcCs4|dkrt�}||_|jj|_d|_d|_d|_dS)Nr)r r r(�retried�nonce_count�
last_nonce)r�r rLrLrMr�Es
z"AbstractDigestAuthHandler.__init__cCs
d|_dS)Nr)rW)r�rLrLrM�reset_retry_countNsz+AbstractDigestAuthHandler.reset_retry_countcCs||j|d�}|jdkr*t|jdd|d��n|jd7_|rx|j�d}|j�dkr`|j||�S|j�dkrxtd|��dS)	N�i�zdigest auth failedrRrZdigestrFzEAbstractDigestAuthHandler does not support the following scheme: '%s')r�rWrrprCrt�retry_http_digest_authrB)r�rJrvr�rerHrrLrLrMrIQs


z/AbstractDigestAuthHandler.http_error_auth_reqedcCsz|jdd�\}}ttdt|���}|j||�}|rvd|}|jj|jd�|krRdS|j|j|�|j	j
||jd�}|SdS)Nr�rRz	Digest %s)rJ)rC�parse_keqv_list�filter�parse_http_list�get_authorizationrer�rJr�r�rGrJ)r�r�rM�tokenZ	challenge�chalZauth_valZresprLrLrMr\esz0AbstractDigestAuthHandler.retry_http_digest_authcCs@d|j|tj�f}|jd�td�}tj|�j�}|dd�S)Nz	%s:%s:%s:rrQ�)rX�timeZctimer�_randombytes�hashlib�sha1�	hexdigest)r��nonce�s�b�digrLrLrM�
get_cnonceqsz$AbstractDigestAuthHandler.get_cnoncecCs�y6|d}|d}|jd�}|jdd�}|jdd�}Wntk
rJdSX|j|�\}}	|dkrfdS|jj||j�\}
}|
dkr�dS|jdk	r�|j|j|�}nd}d|
||f}
d|j�|j	f}|d	k�r.||j
kr�|jd
7_nd
|_||_
d|j}|j|�}d||||||�f}|	||
�|�}n2|dk�rT|	||
�d|||�f�}nt
d
|��d|
|||j	|f}|�r�|d|7}|�r�|d|7}|d|7}|�r�|d||f7}|S)Nr&ri�qop�	algorithm�MD5�opaquez%s:%s:%sz%s:%srMrRz%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_implsr r,rprI�get_entity_digestr�r�rYrXrmr)r�r�rbr&rirnrorq�H�KDrrKZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr2rLrLrMr`|sV








z+AbstractDigestAuthHandler.get_authorizationcsD|dkrdd��n|dkr$dd��ntd|���fdd�}�|fS)NrpcSstj|jd��j�S)Nr)rfZmd5rrh)�xrLrLrMr�sz?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAcSstj|jd��j�S)Nr)rfrgrrh)rwrLrLrMr�sz.Unsupported digest authentication algorithm %rcs�d||f�S)Nz%s:%srL)rj�d)rurLrMr�s)rB)r�rorvrL)rurMrs�s

z-AbstractDigestAuthHandler.get_algorithm_implscCsdS)NrL)r�rIrbrLrLrMrt�sz+AbstractDigestAuthHandler.get_entity_digest)N)r�r�r�r�rZrIr\rmr`rsrtrLrLrLrMr&:s
	<
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.
    rNi�cCs*t|j�d}|jd|||�}|j�|S)NrRzwww-authenticate)rrprIrZ)r�r�rdr�r�rerv�retryrLrLrMrU�s

z$HTTPDigestAuthHandler.http_error_401N)r�r�r�r�rJr�rUrLrLrLrMr'�sc@seZdZdZdZdd�ZdS)r(zProxy-Authorizationi�cCs"|j}|jd|||�}|j�|S)Nzproxy-authenticate)rvrIrZ)r�r�rdr�r�rervryrLrLrMrV�s

z%ProxyDigestAuthHandler.http_error_407N)r�r�r�rJr�rVrLrLrLrMr(�sc@s6eZdZd
dd�Zdd�Zdd�Zdd	�Zd
d�ZdS)�AbstractHTTPHandlerrcCs
||_dS)N)�_debuglevel)r��
debuglevelrLrLrMr��szAbstractHTTPHandler.__init__cCs
||_dS)N)r{)r��levelrLrLrM�set_http_debuglevel�sz'AbstractHTTPHandler.set_http_debuglevelcCstjjj|j|j��S)N)r�r��HTTPConnection�_get_content_lengthrIr�)r�rurLrLrMr��sz'AbstractHTTPHandler._get_content_lengthcCs
|j}|std��|jdk	r�|j}t|t�r8d}t|��|jd�sN|jdd�|jd�r�|jd�r�|j|�}|dk	r�|jdt|��n|jdd�|}|j	�r�t
|j�\}}t|�\}}	|jd�s�|jd|�x2|j
jD]&\}
}|
j�}
|j|
�s�|j|
|�q�W|S)	Nz
no host givenz\POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.zContent-typez!application/x-www-form-urlencodedzContent-lengthzTransfer-encodingZchunkedro)rvrrIr�r�r�r�r�r�r�rr�rr�r�r�)r�rurvrIr�Zcontent_lengthZsel_hostrZselZsel_pathr[r�rLrLrM�do_request_�s>





zAbstractHTTPHandler.do_request_c
s\|j}|std��||fd|ji|��}|j|j�t|j���jt�fdd�|jj	�D���d�d<tdd��j	�D���|j
r�i}d}|�kr��|||<�|=|j|j
|d	�y`y&|j|j
�|j|j�|jd
�d�Wn,tk
�r
}zt|��WYdd}~XnX|j�}	Wn|j��YnX|j�rF|jj�d|_|j�|	_|	j|	_|	S)
z�Return an HTTPResponse object for the request, using http_class.

        http_class must implement the HTTPConnection API from http.client.
        z
no host givenrJc3s"|]\}}|�kr||fVqdS)NrL)r�r�r�)rerLrMr�)sz.AbstractHTTPHandler.do_open.<locals>.<genexpr>r��
Connectioncss|]\}}|j�|fVqdS)N)�title)r�r[r�rLrLrMr�6szProxy-Authorization)rezTransfer-encoding)Zencode_chunkedN)rvrrJZset_debuglevelr{r�rxr�rer{rzZ
set_tunnelrur�r�rIr�rm�getresponser�Zsockr�rH�reasonr�)
r�Z
http_classr�Zhttp_conn_argsrvr�Ztunnel_headersZproxy_auth_hdr�errrrL)rerMr�s@
"

zAbstractHTTPHandler.do_openN)r)r�r�r�r�r~r�r�r�rLrLrLrMrz�s

&rzc@seZdZdd�ZejZdS)r)cCs|jtjj|�S)N)r�r�r�r)r�r�rLrLrM�	http_open`szHTTPHandler.http_openN)r�r�r�r�rzr�rPrLrLrLrMr)^sr�c@s$eZdZddd�Zdd�ZejZdS)rErNcCstj||�||_||_dS)N)rzr��_context�_check_hostname)r�r|r>�check_hostnamerLrLrMr�iszHTTPSHandler.__init__cCs|jtjj||j|jd�S)N)r>r�)r�r�r�r�r�r�)r�r�rLrLrM�
https_opennszHTTPSHandler.https_open)rNN)r�r�r�r�r�rzr�rTrLrLrLrMrEgs
rEc@s.eZdZddd�Zdd�Zdd�ZeZeZdS)	rNcCs$ddl}|dkr|jj�}||_dS)Nr)Zhttp.cookiejar�	cookiejarZ	CookieJar)r�r�r�rLrLrMr�ws
zHTTPCookieProcessor.__init__cCs|jj|�|S)N)r�Zadd_cookie_header)r�rurLrLrMrP}sz HTTPCookieProcessor.http_requestcCs|jj||�|S)N)r�Zextract_cookies)r�rur�rLrLrMr��sz!HTTPCookieProcessor.http_response)N)r�r�r�r�rPr�rTr�rLrLrLrMrvs

c@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.�scCsRi}xH|D]@}|jdd�\}}|ddkrB|ddkrB|dd�}|||<q
W|S)z>Parse list of key=value strings where keys are not duplicated.�=rRrr<rSrS)rC)�lZparsedZeltr�r�rLrLrMr]�s
r]cCs�g}d}d}}xt|D]l}|r,||7}d}q|rV|dkr@d}qn|dkrLd}||7}q|dkrn|j|�d}q|dkrzd}||7}qW|r�|j|�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.
    rnF�\Tr<�,cSsg|]}|j��qSrL)rO)r��partrLrLrMr$�sz#parse_http_list.<locals>.<listcomp>)r])rj�resr��escaper	ZcurrLrLrMr_�s4	


r_c@s(eZdZdd�ZdZdd�Zdd�ZdS)r*cCs\|j}|dd�dkrN|dd�dkrN|jrN|jdkrN|j|j�krXtd��n
|j|�SdS)Nr:z//r>r��	localhostz-file:// scheme is supported only on localhost)r�rv�	get_namesr�open_local_file)r�r�rHrLrLrM�	file_open�s&

zFileHandler.file_openNcCs`tjdkrZy*ttjd�dtjtj��d�t_Wn$tjk
rXtjd�ft_YnXtjS)Nr�r:)r*�namesr%r��gethostbyname_ex�gethostname�gaierror�
gethostbyname)r�rLrLrMr��s
zFileHandler.get_namescCsddl}ddl}|j}|j}t|�}y�tj|�}|j}|jj	|j
dd�}	|j|�d}
|jd|
pbd||	f�}|r~t
|�\}}|s�|r�t|�|j�kr�|r�d||}
nd|}
tt|d�||
�SWn*tk
r�}zt|��WYdd}~XnXtd��dS)	NrT)�usegmtz6Content-type: %s
Content-length: %d
Last-modified: %s
z
text/plainzfile://�rbzfile not on local host)�email.utils�	mimetypesrvr�r4rW�stat�st_size�utils�
formatdate�st_mtime�
guess_type�message_from_stringr
�_safe_gethostbynamer�rrGrmr)r�r��emailr�rvrbZ	localfile�statsri�modified�mtyperer/Zorigurl�exprLrLrMr��s0
zFileHandler.open_local_file)r�r�r�r�r�r�r�rLrLrLrMr*�s
cCs&y
tj|�Stjk
r dSXdS)N)r�r�r�)rvrLrLrMr��s
r�c@seZdZdd�Zdd�ZdS)r+cCs.ddl}ddl}|j}|s"td��t|�\}}|dkr>|j}nt|�}t|�\}}|rdt|�\}}nd}t	|�}|pvd}|p~d}yt
j|�}Wn*tk
r�}zt|��WYdd}~XnXt
|j�\}	}
|	jd�}ttt	|��}|dd�|d}}|�r|d�r|dd�}y�|j||||||j�}
|�r8d�p:d}x:|
D]2}t|�\}}|j�dk�rB|dk�rB|j�}�qBW|
j||�\}}d}|j|j�d}|�r�|d
|7}|dk	�r�|dk�r�|d|7}tj|�}t|||j�S|jk
�r(}z$td|�}|jtj �d��WYdd}~XnXdS)Nrzftp error: no host givenrnr�rRrS�Dr��a�Ar�rxzContent-type: %s
zContent-length: %d
z
ftp error: %rr:rSrS)r�r�r�rSrxr�)!�ftplibr�rvrr
�FTP_PORTr^rrr
r�r�rmrr�rCr��map�connect_ftprJrrt�upper�retrfiler�rpr�r�r�
all_errors�with_traceback�sys�exc_info)r�r�r�r�rvr/rr r�rX�attrs�dirsrN�fwr��attrr�rd�retrlenrer�r��excrLrLrM�ftp_open�s\



zFTPHandler.ftp_openc	Cst||||||dd�S)NF)�
persistent)�
ftpwrapper)r�rr rvr/r�rJrLrLrMr�1szFTPHandler.connect_ftpN)r�r�r�r�r�rLrLrLrMr+�s5c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)r,cCs"i|_i|_d|_d|_d|_dS)Nr�<rc)�cacherJ�soonest�delay�	max_conns)r�rLrLrMr�8s
zCacheFTPHandler.__init__cCs
||_dS)N)r�)r��trLrLrM�
setTimeout?szCacheFTPHandler.setTimeoutcCs
||_dS)N)r�)r�r�rLrLrM�setMaxConnsBszCacheFTPHandler.setMaxConnscCsr|||dj|�|f}||jkr4tj�|j|j|<n,t||||||�|j|<tj�|j|j|<|j�|j|S)Nr�)�joinr�rdr�rJr��check_cache)r�rr rvr/r�rJr�rLrLrMr�Es

zCacheFTPHandler.connect_ftpcCs�tj�}|j|krTx@t|jj��D].\}}||kr"|j|j�|j|=|j|=q"Wtt|jj���|_t	|j�|j
kr�x6t|jj��D]$\}}||jkr�|j|=|j|=Pq�Wtt|jj���|_dS)N)rdr�r�rJr{r�r��min�valuesr`r�)r�r�r�r�rLrLrMr�Ps

zCacheFTPHandler.check_cachecCs4x|jj�D]}|j�qW|jj�|jj�dS)N)r�r�r��clearrJ)r��connrLrLrM�clear_cacheds
zCacheFTPHandler.clear_cacheN)	r�r�r�r�r�r�r�r�r�rLrLrLrMr,5sc@seZdZdd�ZdS)r-cCs~|j}|jdd�\}}|jdd�\}}t|�}|jd�rNtj|�}|dd�}|sVd}tjd|t|�f�}t	t
j|�||�S)	N�:rRr�z;base64�ztext/plain;charset=US-ASCIIz$Content-type: %s
Content-length: %d
i����)rprCr�endswithr�decodebytesr�r�r`r�io�BytesIO)r�r�rHrrIZ	mediatypererLrLrM�	data_openks


zDataHandler.data_openN)r�r�r�r�rLrLrLrMr-jsr��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�scCst|�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�sc@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).NzPython-urllib/%scKszdd|jji}tj|tdd�|dkr.t�}||_|jd�|_|jd�|_	d|j
fd
g|_g|_t
j|_d|_t|_dS)NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methods�classr>)�
stacklevel�key_file�	cert_filez
User-Agent�Accept�*/*)r�r�)r8r�r?r@rAr5rr�r�r��versionr��_URLopener__tempfilesrWrl�_URLopener__unlink�	tempcache�ftpcache)r�rZx509r�rLrLrMr��szURLopener.__init__cCs|j�dS)N)r�)r�rLrLrM�__del__�szURLopener.__del__cCs|j�dS)N)�cleanup)r�rLrLrMr��szURLopener.closecCsZ|jrFx2|jD](}y|j|�Wqtk
r4YqXqW|jdd�=|jrV|jj�dS)N)r�r�rmr�r�)r�rNrLrLrMr��s
zURLopener.cleanupcGs|jj|�dS)zdAdd a header to be used by the HTTP interface only
        e.g. u.addheader('Accept', 'sound/basic')N)r�r])r�r�rLrLrM�	addheader�szURLopener.addheadercCsntt|��}t|dd�}|jrL||jkrL|j|\}}t|d�}t|||�St|�\}}|s`d}||jkr�|j|}t|�\}}	t|	�\}
}|
|f}nd}d|}||_	|j
dd�}t||�s�|d	kr�|r�|j|||�S|j
||�Sy,|dk�rt||�|�St||�||�SWnVttfk
�r.�Yn<tk
�rh}
ztd
|
�jtj�d��WYdd}
~
XnXdS)z6Use URLopener().open(file) instead of open(file, 'r').z%/:=&?~#+!$,;'@()*[]|)rr�rNNZopen_�-r�r�zsocket errorr:)rrr	r�rGrrrrr�r�r��open_unknown_proxy�open_unknownr�rrrmr�r�r�)r�r�rIrbrerd�urltyperHr�	proxyhostrvr�r[r�rLrLrMrG�s<




zURLopener.opencCst|�\}}tdd|��dS)z/Overridable interface to open unknown URL type.z	url errorzunknown url typeN)rrm)r�r�rIr�rHrLrLrMr�szURLopener.open_unknowncCs t|�\}}tdd||��dS)z/Overridable interface to open unknown URL type.z	url errorzinvalid proxy for %sN)rrm)r�rr�rIr�rHrLrLrMr�	szURLopener.open_unknown_proxyc Cs&tt|��}|jr&||jkr&|j|St|�\}}|dkr�|sH|dkr�y.|j|�}|j�}|j�tt|�d�|fSt	k
r�}	zWYdd}	~	XnX|j
||�}�zH|j�}
|r�t
|d�}n|ddl}t|�\}
}t|p�d�\}
}t|p�d�\}}
t
|�pd�\}}
tjj|�d}|j|�\}}|jj|�tj|d�}z�||
f}|jdk	�r^||j|<d}d
}d}d}d	|
k�r�t|
d
�}|�r�||||�xH|j|�}|�s�P|t|�7}|j|�|d7}|�r�||||��q�WWd|j�XWd|j�X|dk�r"||k�r"td||f|��|S)ztretrieve(url) returns (filename, headers) for a local object
        or (tempfilename, headers) for a remote object.NrNrRrOrrnirQzcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytesi rS)rrr�rr�rVr�r4rrmrGrZrrrWrX�splitextZmkstempr�r]�fdopenr^r_r`rar)r�rHrbrcrIr�Zurl1rdr�r�rerfrZZgarbagerX�suffix�fdrgrhrir_rjrkrLrLrM�retrievesl






zURLopener.retrievecCs(d}d}t|t�r<t|�\}}|r6t|�\}}t|�}|}nt|\}}t|�\}}t|�\}	}
|
}d}|	j�dkrvd}n:t|
�\}}
|r�t|�\}}|r�d|	||
f}t|�r�|}|s�tdd��|r�t|�}t	j
|j��jd�}nd}|�rt|�}t	j
|j��jd�}nd}||�}
i}|�r*d||d<|�r<d||d	<|�rJ||d
<d|d<x|j
D]\}}|||<�qZW|dk	�r�d
|d<|
jd|||�n|
jd||d�y|
j�}Wn"tjjk
�r�td��YnXd|jk�o�dkn�rt||jd||j�S|j||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 errorz
no host givenrzBasic %szProxy-AuthorizationrNror�r�z!application/x-www-form-urlencodedzContent-Typer�r�)rez$http protocol error: bad status liner�i,zhttp:)r�r�rrr
rrtrrmrrrrr�rur�r�r�Z
BadStatusLinerZstatusrr��
http_errorrdr�)r�Zconnection_factoryrHrIZuser_passwdZproxy_passwdrvr�Zrealhostr�r�Z
proxy_authrMZ	http_connrerDr�r�rLrLrM�_open_generic_httpQsr



zURLopener._open_generic_httpcCs|jtjj||�S)zUse HTTP protocol.)r�r�r�r)r�rHrIrLrLrM�	open_http�szURLopener.open_httpc
Csbd|}t||�rPt||�}|dkr6||||||�}	n|||||||�}	|	rP|	S|j|||||�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�rHrd�errcode�errmsgrerIr[rrgrLrLrMr��s

zURLopener.http_errorcCs|j�t||||d��dS)z>Default error handler: close the connection and raise OSError.N)r�r)r�rHrdr�r�rerLrLrMr��szURLopener.http_error_defaultcCstjj||j|jd�S)N)r�r�)r�r�r�r�r�)r�rvrLrLrM�_https_connection�szURLopener._https_connectioncCs|j|j||�S)zUse HTTPS protocol.)r�r�)r�rHrIrLrLrM�
open_https�szURLopener.open_httpscCs^t|t�std��|dd�dkrP|dd�dkrP|dd�j�dkrPtd	��n
|j|�SdS)
z/Use local file or FTP depending on form of URL.zEfile error: proxy support for file protocol currently not implementedNr:z//r>r��z
localhost/z-file:// scheme is supported only on localhost)r�r�rrtrBr�)r�rHrLrLrM�	open_file�s

4
zURLopener.open_filecCs\ddl}ddl}t|�\}}t|�}ytj|�}Wn0tk
rb}zt|j|j	��WYdd}~XnX|j
}	|jj|j
dd�}
|j|�d}|jd|p�d|	|
f�}|s�|}
|dd�dkr�d	|}
tt|d
�||
�St|�\}}|o�tj|�t�ft�k�rP|}
|dd�dk�r d	|}
n|dd�dk�r>td
|��tt|d
�||
�Std��dS)zUse local file.rNT)r�z6Content-Type: %s
Content-Length: %d
Last-modified: %s
z
text/plainrRr�zfile://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r4rWr�rmr�strerrorrbr�r�r�r�r�r�rrGr
r�r�r��thishostrB)r�rHr�r�rvrNZ	localnamer��erir�r�reZurlfiler/rLrLrMr��s: 
zURLopener.open_local_filecCs�t|t�std��ddl}t|�\}}|s2td��t|�\}}t|�\}}|r\t|�\}}nd}t|�}t|ppd�}t|p|d�}t	j
|�}|s�ddl}|j}nt
|�}t|�\}}	t|�}|jd�}
|
dd�|
d}
}|
o�|
d�r�|
dd�}
|
�r|
d�rd|
d<|||dj|
�f}t|j�tk�rlx8t|j�D]*}
|
|k�r>|j|
}|j|
=|j��q>Wy�||jk�r�t|||||
�|j|<|�s�d}nd	}x:|	D]2}t|�\}}|j�d
k�r�|dk�r�|j�}�q�W|j|j||�\}}|jd|�d}d}|�r|d|7}|dk	�r:|dk�r:|d|7}tj|�}t||d|�St�k
�r�}z td|�j t!j"�d��WYdd}~XnXdS)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedrNzftp error: no host givenrnr�rRr�rSr�r�r�r�rxzftp:zContent-Type: %s
zContent-Length: %d
zftp error %rr:rSrS)r�r�r�rSrxr�)#r�r�rr�rr
rrr
r�r�r�r�r^rrCr�r`r��MAXFTPCACHEr�r�r�rrtr�r�r�r�r�r�	ftperrorsr�r�r�)r�rHr�rvrXr/rr r�r�r�rNr�r�r�r�r�r�rdr�r�rer�rLrLrM�open_ftp�sp







zURLopener.open_ftpc	Cs<t|t�std��y|jdd�\}}Wntk
rDtdd��YnX|sNd}|jd�}|dkr�d	||d
�kr�||dd
�}|d
|�}nd}g}|jdtj	d
tj
tj����|jd|�|dkr�tj|j
d��jd�}nt|�}|jdt|��|jd�|j|�dj|�}tj|�}tj|�}t|||�S)zUse "data" URL.zEdata error: proxy support for data protocol currently not implementedr�rRz
data errorzbad data URLztext/plain;charset=US-ASCII�;rr�NrnzDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %srrzlatin-1zContent-Length: %d�
)r�r�rrCrBrm�rfindr]rdZstrftimeZgmtimerr�rrr
r`r�r�r�r��StringIOr)	r�rHrIr�Zsemirr�re�frLrLrM�	open_data3s6






zURLopener.open_data)N)N)N)N)NNN)N)N)N)N)r�r�r�r�r�r�r�r�r�r�r�r�rGr�r�r�r�r�r�r�rCr�r�r�r�rrrLrLrLrMr8�s.

$


B\


	 :c@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|f|�|�i|_d|_d|_dS)Nrr�)r8r��
auth_cache�tries�maxtries)r�r�r7rLrLrMr�`szFancyURLopener.__init__cCst||d||�S)z3Default error handling -- don't raise an exception.zhttp:)r)r�rHrdr�r�rerLrLrMr�fsz!FancyURLopener.http_error_defaultNc	
Csn|jd7_zR|jrJ|j|jkrJt|d�r4|j}n|j}|||dd|�S|j||||||�}|Sd|_XdS)z%Error 302 -- relocated (temporarily).rR�http_error_500i�z)Internal Server Error: Redirect RecursionNr)r
rr�rr��redirect_internal)	r�rHrdr�r�rerIr�rgrLrLrMr
js
zFancyURLopener.http_error_302c	Csxd|kr|d}nd|kr$|d}ndS|j�t|jd||�}t|�}|jd	krnt|||d|||��|j|�S)
Nr�r�r�r�r�r�rnz( Redirection to url '%s' is not allowed.)r�r�r�rn)r�rr�rrrrG)	r�rHrdr�r�rerIr�rrLrLrMr
|s


z FancyURLopener.redirect_internalcCs|j||||||�S)z*Error 301 -- also relocated (permanently).)r
)r�rHrdr�r�rerIrLrLrMr�szFancyURLopener.http_error_301cCs|j||||||�S)z;Error 303 -- also relocated (essentially identical to 302).)r
)r�rHrdr�r�rerIrLrLrMr�szFancyURLopener.http_error_303cCs2|dkr|j||||||�S|j|||||�SdS)z1Error 307 -- relocated, but turn POST into error.N)r
r�)r�rHrdr�r�rerIrLrLrMr
�szFancyURLopener.http_error_307Fc
Cs�d|krtj||||||�|d}tjd|�}	|	sHtj||||||�|	j�\}
}|
j�dkrttj||||||�|s�tj||||||�d|jd}|dkr�t||�||�St||�|||�SdS)z_Error 401 -- authentication required.
        This function supports Basic authentication only.zwww-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"rFZretry_�_basic_authN)r8r�rQ�matchrArtr�r�)
r�rHrdr�r�rerIry�stuffrrr&r[rLrLrMrU�s&





zFancyURLopener.http_error_401c
Cs�d|krtj||||||�|d}tjd|�}	|	sHtj||||||�|	j�\}
}|
j�dkrttj||||||�|s�tj||||||�d|jd}|dkr�t||�||�St||�|||�SdS)zeError 407 -- proxy authentication required.
        This function supports Basic authentication only.zproxy-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"rFZretry_proxy_rN)r8r�rQrrArtr�r�)
r�rHrdr�r�rerIryrrrr&r[rLrLrMrV�s&





zFancyURLopener.http_error_407cCs�t|�\}}d||}|jd}t|�\}}	t|	�\}	}
|	jd�d}|	|d�}	|j|	||�\}}
|pl|
srdSdt|dd�t|
dd�|	f}	d|	|
|jd<|dkr�|j|�S|j||�SdS)Nzhttp://r��@rRz%s:%s@%srn)r)rrrr��get_user_passwdr	rG)r�rHr&rIrvr�r�rr�r��
proxyselectorr�rr rLrLrM�retry_proxy_http_basic_auth�s 

z*FancyURLopener.retry_proxy_http_basic_authcCs�t|�\}}d||}|jd}t|�\}}	t|	�\}	}
|	jd�d}|	|d�}	|j|	||�\}}
|pl|
srdSdt|dd�t|
dd�|	f}	d|	|
|jd<|dkr�|j|�S|j||�SdS)Nzhttps://r�rrRz%s:%s@%srn)r)rrrr�rr	rG)r�rHr&rIrvr�r�rr�r�rr�rr rLrLrM�retry_proxy_https_basic_auth�s 

z+FancyURLopener.retry_proxy_https_basic_authc
Cs�t|�\}}|jd�d}||d�}|j|||�\}}|p>|sDdSdt|dd�t|dd�|f}d||}	|dkr�|j|	�S|j|	|�SdS)NrrRz%s:%s@%srn)rzhttp://)rr�rr	rG)
r�rHr&rIrvr�r�rr r�rLrLrMrG�s
z$FancyURLopener.retry_http_basic_authc
Cs�t|�\}}|jd�d}||d�}|j|||�\}}|p>|sDdSdt|dd�t|dd�|f}d||}	|dkr�|j|	�S|j|	|�SdS)NrrRz%s:%s@%srn)rzhttps://)rr�rr	rG)
r�rHr&rIrvr�r�rr r�rLrLrM�retry_https_basic_auth	s
z%FancyURLopener.retry_https_basic_authrcCs`|d|j�}||jkr2|r(|j|=n
|j|S|j||�\}}|sJ|rX||f|j|<||fS)Nr)rtr	�prompt_user_passwd)r�rvr&r�r�rr rLrLrMr	s


zFancyURLopener.get_user_passwdcCsTddl}y,td||f�}|jd|||f�}||fStk
rNt�dSXdS)z#Override this in a GUI environment!rNzEnter username for %s at %s: z#Enter password for %s in %s at %s: )NN)�getpass�input�KeyboardInterrupt�print)r�rvr&rrr rLrLrMr$	sz!FancyURLopener.prompt_user_passwd)N)N)N)N)NF)NF)N)N)N)N)r)r�r�r�r�r�r�r
r
rrr
rUrVrrrGrrrrLrLrLrMr9]s$










cCstdkrtjd�atS)z8Return the IP address of the magic hostname 'localhost'.Nr�)�
_localhostr�r�rLrLrLrMr�4	s
r�cCsPtdkrLyttjtj��d�aWn(tjk
rJttjd�d�aYnXtS)z,Return the IP addresses of the current host.Nr:r�)�	_thishostr%r�r�r�r�rLrLrLrMr�<	sr�cCstdkrddl}|jatS)z1Return the set of errors raised by the FTP class.Nr)�
_ftperrorsr�r�)r�rLrLrMrG	srcCstdkrtjd�atS)z%Return an empty email Message object.Nrn)�
_noheadersr�r�rLrLrLrM�	noheadersP	s
r c@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.NTc	CsX||_||_||_||_||_||_d|_||_y|j�Wn|j	��YnXdS)Nr)
rr rvr/r�rJ�refcount�	keepalive�initr�)r�rr rvr/r�rJr�rLrLrMr�]	szftpwrapper.__init__cCs\ddl}d|_|j�|_|jj|j|j|j�|jj|j	|j
�dj|j�}|jj
|�dS)Nrr�)r��busyZFTPr�Zconnectrvr/rJZloginrr r�r��cwd)r�r�Z_targetrLrLrMr#m	s
zftpwrapper.initc-Cs�ddl}|j�|dkr"d}d}nd|}d}y|jj|�Wn*|jk
rh|j�|jj|�YnXd}|r�|r�yd|}|jj|�\}}WnR|jk
r�}z4t|�dd�d	kr�t	d
|�j
tj�d��WYdd}~XnX|�s�|jjd�|�rn|jj
�}	zJy|jj|�Wn4|jk
�rP}zt	d
|�|�WYdd}~XnXWd|jj|	�Xd|}nd
}|jj|�\}}d|_t|jd�|j�}
|jd7_|j�|
|fS)Nrrxr�zTYPE ArRzTYPE zRETR r>Z550z
ftp error: %rr:zLIST ZLISTr�)rxr�)r��endtransferr�Zvoidcmdr�r#ZntransfercmdZ
error_permr�rr�r�r��pwdr%r$r�makefile�
file_closer!r�)r�rNr�r��cmd�isdirr�r�r�r'ZftpobjrLrLrMr�v	sN
 
$
zftpwrapper.retrfilecCs
d|_dS)Nr)r$)r�rLrLrMr&�	szftpwrapper.endtransfercCsd|_|jdkr|j�dS)NFr)r"r!�
real_close)r�rLrLrMr��	s
zftpwrapper.closecCs4|j�|jd8_|jdkr0|jr0|j�dS)NrRr)r&r!r"r,)r�rLrLrMr)�	szftpwrapper.file_closecCs2|j�y|jj�Wnt�k
r,YnXdS)N)r&r�r�r)r�rLrLrMr,�	s
zftpwrapper.real_close)NT)r�r�r�r�r�r#r�r&r�r)r,rLrLrLrMr�Z	s
	-r�cCs�i}xBtjj�D]4\}}|j�}|r|dd�dkr|||dd�<qWdtjkr^|jdd�xXtjj�D]J\}}|dd�dkrj|j�}|r�|||dd	�<qj|j|dd
�d�qjW|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.

    �N�_proxyZREQUEST_METHODr�i����i����i����i����i����)rW�environr{rtr�)rr[r�rLrLrM�getproxies_environment�	s	
r0c
Cs�|dkrt�}y|d}Wntk
r.dSX|dkr<dSt|�\}}dd�|jd�D�}xP|D]H}|rb|jd	�}tj|�}d
|}tj||tj�s�tj||tj�rbdSqbWdS)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.

    N�nor�*rRcSsg|]}|j��qSrL)rO)r�rrLrLrMr$�	sz,proxy_bypass_environment.<locals>.<listcomp>r��.z
(.+\.)?%s$)	r0rrr
rC�lstriprQr�rrS)rvrZno_proxy�hostonlyr/Z
no_proxy_listr[�patternrLrLrM�proxy_bypass_environment�	s&


r7c
Csddlm}t|�\}}dd�}d|kr4|dr4dSd}x�|jd	f�D]�}|sPqFtjd
|�}|dk	�r|dkr�ytj|�}||�}Wntk
r�wFYnX||jd��}	|jd�}
|
dkr�d
|jd�j	d�d}
nt
|
dd��}
d|
}
||
?|	|
?k�rdSqF|||�rFdSqFWdS)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)�fnmatchcSsh|jd�}ttt|��}t|�dkr<|ddddgdd�}|dd>|dd>B|dd>B|d	BS)
Nr3r�r�rRrcr:rQr>)rCr�r�r^r`)ZipAddrr.rLrLrM�ip2num
s

z,_proxy_bypass_macosx_sysconf.<locals>.ip2numr3Zexclude_simpleTN�
exceptionsz(\d+(?:\.\d+)*)(/\d+)?rRr:rQ� F)r8r
r�rQrr�r�rm�group�countr^)rv�proxy_settingsr8r5r/r:ZhostIPr�r�r2�maskrLrLrM�_proxy_bypass_macosx_sysconf�	s:



rA�darwin)�_get_proxy_settings�_get_proxiescCst�}t||�S)N)rCrA)rvr?rLrLrM�proxy_bypass_macosx_sysconf:
srEcCst�S)z�Return a dictionary of scheme -> proxy server URL mappings.

        This function uses the MacOSX framework SystemConfiguration
        to fetch the proxy information.
        )rDrLrLrLrM�getproxies_macosx_sysconf>
srFcCs 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)r0r7rE)rvrrLrLrMrH
s
rcCst�p
t�S)N)r0rFrLrLrLrMr5U
scCsi}yddl}Wntk
r$|SXy�|j|jd�}|j|d�d}|r�t|j|d�d�}d|kr�x�|jd�D]4}|jdd�\}}tjd	|�s�d
||f}|||<qrWn>|dd�dkr�||d
<n$d||d
<d||d<d||d<|j	�Wnt
ttfk
�rYnX|S)zxReturn a dictionary of scheme -> proxy server URL mappings.

        Win32 uses the registry to store proxies.

        rNz;Software\Microsoft\Windows\CurrentVersion\Internet Settings�ProxyEnableZProxyServerr�rrRz^([^/:]+)://z%s://%sr[zhttp:r�z	http://%sz
https://%sr�zftp://%sr�)
�winreg�ImportError�OpenKey�HKEY_CURRENT_USER�QueryValueExr�rCrQrZClosermrBr�)rrH�internetSettings�proxyEnableZproxyServer�pr�ZaddressrLrLrM�getproxies_registryZ
s8

rPcCs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.

        )r0rPrLrLrLrMr5�
sc&Cs~yddl}Wntk
r dSXy6|j|jd�}|j|d�d}t|j|d�d�}Wntk
rldSX|sz|r~dSt|�\}}|g}y tj	|�}||kr�|j
|�Wntk
r�YnXy tj|�}||kr�|j
|�Wntk
�r�YnX|jd�}xp|D]h}	|	dk�r*d|k�r*dS|	j
dd	�}	|	j
d
d�}	|	j
dd�}	x$|D]}
tj|	|
tj��rTdS�qTW�qWdS)
Nrz;Software\Microsoft\Windows\CurrentVersion\Internet SettingsrGZ
ProxyOverriderz<local>r3rRz\.r2z.*�?)rHrIrJrKrLr�rmr
r�r�r]ZgetfqdnrCr�rQrrS)rvrHrMrNZ
proxyOverrideZrawHostr/ZaddrZfqdnr3r�rLrLrM�proxy_bypass_registry�
sR







rRcCs 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)r0r7rR)rvrrLrLrMr�
s
)NNN)N)r�rr�r�rfZhttp.clientr�r�rWr0rQr�rr�rd�collectionsrZrTr?Zurllib.errorrrrZurllib.parserrrrr	r
rrr
rrrrrrrrrZurllib.responserrrDrIrC�__all__�version_infor�rFr�r0r1r\r6r7rR�ASCIIrrrwrrr2rr/rrrrr r!r"r#r$r%�urandomrer&r'r(rzr)r�r�rEr]rr.r]r_r*r�r+r,r-rr[Z
nturl2pathr4r3r�r8r9rr�rr�rrrr r�r0r7rA�platformZ_scproxyrCrDrErFrr5rPrRrLrLrLrM�<module>Ds�P
T
?n$q*@
ov

+3:5!AW

_
#<

-	2