HEX
Server: LiteSpeed
System: Linux ws4.angoweb.net 5.14.0-611.13.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 11 04:57:59 EST 2025 x86_64
User: tswangoe (2287)
PHP: 8.1.33
Disabled: show_source, system, shell_exec, passthru, exec, phpinfo, popen, proc_open
Upload Files
File: //usr/local/lib/python3.9/site-packages/future/backports/urllib/__pycache__/request.cpython-39.pyc
a

q�dgx�@sdZddlmZmZmZmZddlmZmZm	Z	m
Z
mZmZm
Z
mZddlmZmZmZddlZddlZddlZddlZddlmZddlmZdd	lmZmZmZdd
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/m0Z0m1Z1ddl2m3Z3m4Z4ddl5Z5ddl6Z6ddl7Z7ddl8Z8ddl9Z9ddl:Z:ddl;Z;ddl<Z<ddl=Z=ddl>Z>ddlmZe�rpdd
l?m@Z@ndd
lAm@Z@zddlBZBddlBmCZCWneD�y�dZEYn0dZEgd�ZFe:jGdd�ZHdaIde9jJfdd�ZKdd�ZLgZMd�dd�ZNdd�ZOe�re8�Pde8jQ�ZRn
e8�Pd�ZRdd�ZSGdd�deT�ZUGd d!�d!eT�ZVd"d#�ZWGd$d%�d%eT�ZXGd&d'�d'eX�ZYGd(d)�d)eX�ZZGd*d+�d+eX�Z[d,d-�Z\Gd.d/�d/eX�Z]Gd0d1�d1eT�Z^Gd2d3�d3e^�Z_Gd4d5�d5eT�Z`Gd6d7�d7e`eX�ZaGd8d9�d9e`eX�Zbe6jcZdGd:d;�d;eT�ZeGd<d=�d=eXee�ZfGd>d?�d?eXee�ZgGd@dA�dAeX�ZhGdBdC�dCeh�ZiejedD��rvGdEdF�dFeh�ZkeF�ldF�GdGdH�dHeX�ZmGdIdJ�dJeX�ZndKdL�ZodMdN�ZpGdOdP�dPeX�ZqdQdR�ZrGdSdT�dTeX�ZsGdUdV�dVes�ZtdWZue6jvdXk�rddYlwmxZxmyZyndZd[�Zxd\d]�ZyiZzGd^d_�d_eT�Z{Gd`da�dae{�Z|da}dbdc�Z~daddde�Z�da�dfdg�Z�da�dhdi�Z�Gdjdk�dkeT�Z�dldm�Z�dndo�Z�dpdq�Z�e:j�drk�r�ddsl�m�Z�m�Z�dtdu�Z�dvdw�Z�dxdy�Z�dzd{�Z�n6e6jvdXk�r�d|d}�Z�d~d{�Z�dd��Z�d�dy�Z�ne�Z�e�Z�dS)�a�

Ported using Python-Future from the Python 3.3 standard library.

An extensible library for opening URLs using a variety of protocols

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

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

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

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

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

objects of interest:

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

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

BaseHandler --

internals:
BaseHandler and parent
_call_chain conventions

Example usage:

import urllib.request

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

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

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

# install it
urllib.request.install_opener(opener)

f = urllib.request.urlopen('http://www.python.org/')
�)�absolute_import�division�print_function�unicode_literals)�bytes�dict�filter�input�int�map�open�str)�PY2�PY3�raise_with_tracebackN)�email)�client�)�URLError�	HTTPError�ContentTooShortError)�urlparse�urlsplit�urljoin�unwrap�quote�unquote�	splittype�	splithost�	splitport�	splituser�splitpasswd�	splitattr�
splitquery�
splitvalue�splittag�to_bytes�
urlunparse)�
addinfourl�addclosehook)r)�Iterable)�
SSLContextFT)�Request�OpenerDirector�BaseHandler�HTTPDefaultErrorHandler�HTTPRedirectHandler�HTTPCookieProcessor�ProxyHandler�HTTPPasswordMgr�HTTPPasswordMgrWithDefaultRealm�AbstractBasicAuthHandler�HTTPBasicAuthHandler�ProxyBasicAuthHandler�AbstractDigestAuthHandler�HTTPDigestAuthHandler�ProxyDigestAuthHandler�HTTPHandler�FileHandler�
FTPHandler�CacheFTPHandler�UnknownHandler�HTTPErrorProcessor�urlopen�install_opener�build_opener�pathname2url�url2pathname�
getproxies�urlretrieve�
urlcleanup�	URLopener�FancyURLopener�c
Ks�d|vr|d}|d=nd}d|vr4|d}|d=nd}d|vrP|d}|d=nd}|s`|s`|r�tsltd��t�tj�}|jtjO_tj|_|s�|r�|�	||�n|�
�t|dd�}t|�}	nt
dur�t�a
}	nt
}	|	�|||�S)N�	cadefaultF�capath�cafilezSSL support not availableT��context�check_hostname)�	_have_ssl�
ValueError�sslr+�PROTOCOL_SSLv23�options�OP_NO_SSLv2�
CERT_REQUIRED�verify_mode�load_verify_locations�set_default_verify_paths�HTTPSHandlerrC�_openerr)
�url�data�timeoutZ_3to2kwargsrLrMrNrP�
https_handler�opener�rc�I/usr/local/lib/python3.9/site-packages/future/backports/urllib/request.pyrA�s*
rAcCs|adS�N)r])rbrcrcrdrB�srBc	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-Lengthr�1retrieval incomplete: got only %i out of %i bytes)r�
contextlib�closingrA�info�os�path�normpathr�tempfile�NamedTemporaryFile�name�_url_tempfiles�appendr
�read�len�writer)r^�filename�
reporthookr_�url_typerr�fp�headers�tfp�result�bs�sizery�blocknum�blockrcrcrdrG�sH


N��rGc	CsBtD]&}zt�|�Wqty(Yq0qtdd�=tr>dadSre)rwrq�unlink�EnvironmentErrorr])�	temp_filercrcrdrH�s
rHz:\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.

    r��Host)�full_urlr�
get_header�_cut_port_re�sub�lower)�requestr^�hostrcrcrd�request_hostsr�c@s�eZdZdidddfdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd)d%d&�Zd'd(�ZdS)*r,NFc	Cs�t|�|_t|j�\|_|_||_i|_d|_|��D]\}}|�||�q6i|_	|durbt
|�}||_||_||_
|��dSre)rr�r%�fragmentr_r��_tunnel_host�items�
add_header�unredirected_hdrsr��origin_req_host�unverifiable�method�_parse)	�selfr^r_r�r�r�r��key�valuercrcrd�__init__s
zRequest.__init__cCsNt|j�\|_}|jdur(td|j��t|�\|_|_|jrJt|j�|_dS)Nzunknown url type: %r)rr��typerSrr��selectorr)r��restrcrcrdr�.s
zRequest._parsecCs&|jdur|jS|jdurdSdSdS)z3Return a string indicating the HTTP request method.N�POST�GET)r�r_�r�rcrcrd�
get_method6s


zRequest.get_methodcCs |jrd|j|jfS|jSdS)Nz%s#%s)r�r�r�rcrcrd�get_full_url?szRequest.get_full_urlcCsd}tj|tdd�||_dS)Nz&Request.add_data method is deprecated.r��
stacklevel��warnings�warn�DeprecationWarningr_)r�r_�msgrcrcrd�add_dataGszRequest.add_datacCsd}tj|tdd�|jduS)Nz&Request.has_data method is deprecated.rr�r��r�r�rcrcrd�has_dataLszRequest.has_datacCsd}tj|tdd�|jS)Nz&Request.get_data method is deprecated.rr�r�r�rcrcrd�get_dataQszRequest.get_datacCsd}tj|tdd�|jS)Nz&Request.get_type method is deprecated.rr�)r�r�r�r�r�rcrcrd�get_typeVszRequest.get_typecCsd}tj|tdd�|jS)Nz&Request.get_host method is deprecated.rr�)r�r�r�r�r�rcrcrd�get_host[szRequest.get_hostcCsd}tj|tdd�|jS)Nz*Request.get_selector method is deprecated.rr�)r�r�r�r�r�rcrcrd�get_selector`szRequest.get_selectorcCsd}tj|tdd�|jS)Nz-Request.is_unverifiable method is deprecated.rr�)r�r�r�r�r�rcrcrd�is_unverifiableeszRequest.is_unverifiablecCsd}tj|tdd�|jS)Nz1Request.get_origin_req_host method is deprecated.rr�)r�r�r�r�r�rcrcrd�get_origin_req_hostjszRequest.get_origin_req_hostcCs2|jdkr|js|j|_n||_|j|_||_dS)N�https)r�r�r�r�r�)r�r�r�rcrcrd�	set_proxyqs

zRequest.set_proxycCs|j|jkSre)r�r�r�rcrcrd�	has_proxyyszRequest.has_proxycCs||j|��<dSre)r��
capitalize�r�r��valrcrcrdr�|szRequest.add_headercCs||j|��<dSre)r�r�r�rcrcrd�add_unredirected_header�szRequest.add_unredirected_headercCs||jvp||jvSre)r�r�)r��header_namercrcrd�
has_header�s
�zRequest.has_headercCs|j�||j�||��Sre)r��getr�)r�r��defaultrcrcrdr��s�zRequest.get_headercCs"|j��}|�|j�t|���Sre)r��copy�updater��listr�)r��hdrsrcrcrd�header_items�s
zRequest.header_items)N)�__name__�
__module__�__qualname__r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rcrcrcrdr,s,�
	
r,c@sNeZdZdd�Zdd�Zdd�Zdd�Zd	ejfd
d�Z	ddd
�Z
dd�Zd	S)r-cCs6dt}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��client_versionrcrcrdr��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�_r�errorr�responser�T)�hasattr�	TypeErrorr��dir�find�
startswithr
rSr�r�r�r�r��
setdefault�bisect�insortrxr�r�)r��handler�added�meth�i�protocol�	condition�j�kind�lookupr�rcrcrd�add_handler�sL
�


zOpenerDirector.add_handlercCsdSrercr�rcrcrd�close�szOpenerDirector.closec	Gs<|�|d�}|D]&}t||�}||�}|dur|SqdS)Nrc)r��getattr)	r��chainr��	meth_name�argsr�r��funcr�rcrcrd�_call_chain�s
zOpenerDirector._call_chainNc
Cs�t|t�r|��}t|t�r(t||�}n|}|dur:||_||_|j}|d}|j�	|g�D]}t
||�}||�}q\|�||�}	|d}|j�	|g�D]}t
||�}|||	�}	q�|	S)z�
        Accept a URL or a Request object

        Python-Future: if the URL is passed as a byte-string, decode it first.
        N�_request�	_response)
�
isinstancer�decoder
r,r_r`r�r�r�r��_openr�)
r��fullurlr_r`�reqr�r��	processorr�r�rcrcrdr�s&




zOpenerDirector.opencCsP|�|jdd|�}|r|S|j}|�|j||d|�}|r>|S|�|jdd|�S)Nr��default_openr��unknown�unknown_open)r�r�r�)r�r�r_r�r�rcrcrdr��s 
���
�zOpenerDirector._opencGs~|dvr,|jd}|d}d|}d}|}n|j}|d}d}|||f|}|j|�}|r^|S|rz|dd	f|}|j|�SdS)
N��httpr�r�z
http_error_%sr�_errorrr��http_error_default)r�r�)r��protor�rr��http_err�	orig_argsr�rcrcrdr�
s 

zOpenerDirector.error)N)r�r�r�r�r�r�r��socket�_GLOBAL_DEFAULT_TIMEOUTrr�r�rcrcrcrdr-�s/"
r-cGs�dd�}t�}ttttttttg}t	t
d�r6|�t�t
�}|D]@}|D]6}||�rjt||�r~|�|�qHt||�rH|�|�qHq@|D]}|�|�q�|D]}|�|��q�|D]}||�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.
    cSst|t�pt|d�S)N�	__bases__)r�r�r�)�objrcrcrd�isclass/szbuild_opener.<locals>.isclass�HTTPSConnection)r-r2r?r;r/r0r=r<r@r��http_clientrxr\�set�
issubclass�addr��remover�)r�rrb�default_classes�skip�klass�check�hrcrcrdrC&s2	�



rCc@s(eZdZdZdd�Zdd�Zdd�ZdS)	r.��cCs
||_dSre)�parent)r�rrcrcrdr�OszBaseHandler.add_parentcCsdSrercr�rcrcrdr�RszBaseHandler.closecCst|d�sdS|j|jkS)N�
handler_orderT)r�r)r��otherrcrcrd�__lt__Vs
zBaseHandler.__lt__N)r�r�r�rr�r�rrcrcrcrdr.Lsr.c@s eZdZdZdZdd�ZeZdS)r@zProcess HTTP error responses.i�cCsH|j|j|��}}}d|kr,dksDn|j�d|||||�}|S)N���,r)�coder�rprr�)r�r�r�rr�r�rcrcrd�
http_responsecs�z HTTPErrorProcessor.http_responseN)r�r�r��__doc__rr �https_responsercrcrcrdr@_sr@c@seZdZdd�ZdS)r/cCst|j||||��dSre)rr�)r�r�rrr�r�rcrcrdrqsz*HTTPDefaultErrorHandler.http_error_defaultN)r�r�r�rrcrcrcrdr/psr/c@s4eZdZdZdZdd�Zdd�ZeZZZ	dZ
dS)	r0��
c	sx|��}|dvr|dvs:|dvr(|dks:t|j||||��|�dd�}d�t�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��HEAD)r%r&r'r�� z%20)rkzcontent-typec3s&|]\}}|���vr||fVqdSre)r���.0�k�v��CONTENT_HEADERSrcrd�	<genexpr>�s
�z7HTTPRedirectHandler.redirect_request.<locals>.<genexpr>T)r�r�r�)	r�rr��replacerr�r�r,r�)	r�r�rrr�r��newurl�m�
newheadersrcr.rdr�|s
���z$HTTPRedirectHandler.redirect_requestc
Cs2d|vr|d}nd|vr$|d}ndSt|�}|jdvrRt||d||f||��|jsht|�}d|d<t|�}t|j|�}|�||||||�}|dur�dSt	|d�r�|j
}	|_
|	�|d�|jks�t
|	�|jkr�t|j||j|||��ni}	|_
|_
|	�|d�d	|	|<|��|��|jj||jd
�S)N�location�uri�rr��ftpr�z+%s - Redirection to url '%s' is not allowed�/r�
redirect_dictrr�r`)r�schemerrrr�r'rr�r�r�r:r��max_repeatsrz�max_redirections�inf_msgryr�rrr`)
r�r�rrr�r�r2�urlparts�new�visitedrcrcrd�http_error_302�sB



�
��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�rC�http_error_301�http_error_303�http_error_307r?rcrcrcrdr0ts"7r0c	Cs�t|�\}}|�d�s d}|}n:|�d�s6td|��|�dd�}|dkrNd}|d|�}t|�\}}|dur|t|�\}}nd}}||||fS)a3Return (scheme, user, password, host/port) given a URL or an authority.

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

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

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

    Examples of authority parsing:

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

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

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

    Same examples, but with URLs instead:

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

    Everything after the authority is ignored:

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

    Test for no trailing '/' case:

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

    r9N�//zproxy URL with no authority: %rrrj)rr�rSr�r r!)	�proxyr<�r_scheme�	authority�end�userinfo�hostport�user�passwordrcrcrd�_parse_proxy�s2

rPc@s"eZdZdZddd�Zdd�ZdS)r2�dNcCsZ|durt�}t|d�s Jd��||_|��D]&\}}t|d||||jfdd��q.dS)N�keys�proxies must be a mappingz%s_opencSs||||�Srerc)�rrHr�r�rcrcrd�<lambda>/sz'ProxyHandler.__init__.<locals>.<lambda>)rFr��proxiesr��setattrr�)r�rVr�r^rcrcrdr�(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�rPr��proxy_bypassr�base64�	b64encode�encoder�r�r�rrr`)r�r�rHr��	orig_type�
proxy_typerNrOrM�	user_pass�credsrcrcrdr�2s"�zProxyHandler.proxy_open)N)r�r�r�rr�r�rcrcrcrdr2$s

r2c@s6eZdZdd�Zdd�Zdd�Zd
dd	�Zd
d�ZdS)r3cCs
i|_dSre)�passwdr�rcrcrdr�PszHTTPPasswordMgr.__init__cs\t|t�r|g}|�jvr$i�j|<dD].�t��fdd�|D��}||f�j||<q(dS)N�TFcsg|]}��|���qSrc)�
reduce_uri)r+�u��default_portr�rcrd�
<listcomp>[�z0HTTPPasswordMgr.add_password.<locals>.<listcomp>)r�r
rd�tuple)r��realmr6rNrd�reduced_urircrhrd�add_passwordSs


�zHTTPPasswordMgr.add_passwordc	Cs`|j�|i�}dD]H}|�||�}|��D].\}}|D] }|�||�r6|Sq6q*qdS)Nre�NN)rdr�rfr��	is_suburi)	r�rm�authuri�domainsri�reduced_authuri�uris�authinfor6rcrcrd�find_user_password^s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.rrrr9N�Pi�rz%s:%d)rrr�)
r�r6ri�partsr<rJrrr��port�dportrcrcrdrfhs$��zHTTPPasswordMgr.reduce_uricCsR||krdS|d|dkr dSt�|d|df�}t|�t|d�krNdSdS)zcCheck if test is below base in a URI tree

        Both args must be URIs in reduced form.
        TrFr)�	posixpath�commonprefixrz)r��base�test�commonrcrcrdrqszHTTPPasswordMgr.is_suburiN)T)r�r�r�r�rorwrfrqrcrcrcrdr3Ns


r3c@seZdZdd�ZdS)r4cCs0t�|||�\}}|dur"||fSt�|d|�Sre)r3rw)r�rmrrrNrOrcrcrdrw�s�z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r�r�r�rwrcrcrcrdr4�sr4c@s<eZdZe�dej�Zddd�Zdd�Zdd�Z	d	d
�Z
dS)r5z1(?:.*,)*[ 	]*([^ 	]+)[ 	]+realm=(["']?)([^"']*)\2NcCs(|durt�}||_|jj|_d|_dS�Nr)r3rdro�retried)r��password_mgrrcrcrdr��s

z!AbstractBasicAuthHandler.__init__cCs
d|_dSr��r�r�rcrcrd�reset_retry_count�sz*AbstractBasicAuthHandler.reset_retry_countc
Cs�|�|d�}|jdkr,t|��dd|d��n|jd7_|r�|��d}|��dkrdtd|��nftj�	|�}|r�|�
�\}}}|dvr�t�d	t
d
�|��dkr�|�|||�}	|	r�|	jdkr�d|_|	SdS)N��zbasic auth failedrr�basiczDAbstractBasicAuthHandler does not support the following scheme: '%s')�"�'zBasic Auth Realm was unquotedr)r�r�rr��splitr�rSr5�rx�search�groupsr�r��UserWarning�retry_http_basic_authr)
r��authreqr�r�r�r<�morrmr�rcrcrd�http_error_auth_reqed�s0
���z.AbstractBasicAuthHandler.http_error_auth_reqedcCs~|j�||�\}}|durvd||f}dt�|����d�}|j�|jd�|krVdS|�	|j|�|j
j||jd�SdSdS)NrXr[rYr;)
rdrwr]r^r_r�r�r��auth_headerr�rrr`)r�r�r�rmrN�pw�raw�authrcrcrdr��sz.AbstractBasicAuthHandler.retry_http_basic_auth)N)r�r�r��re�compile�Ir�r�r�r�r�rcrcrcrdr5�s�
 r5c@seZdZdZdd�ZdS)r6�
AuthorizationcCs"|j}|�d|||�}|��|S)N�www-authenticate)r�r�r�)r�r�rrr�r�r^r�rcrcrd�http_error_401�s�z#HTTPBasicAuthHandler.http_error_401N)r�r�r�r�r�rcrcrcrdr6�sr6c@seZdZdZdd�ZdS)r7rZcCs"|j}|�d|||�}|��|S�N�proxy-authenticate�r�r�r�)r�r�rrr�r�rJr�rcrcrd�http_error_407�s�z$ProxyBasicAuthHandler.http_error_407N)r�r�r�r�r�rcrcrcrdr7�sr7c@sNeZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dS)r8NcCs4|durt�}||_|jj|_d|_d|_d|_dSr�)r3rdror��nonce_count�
last_nonce)r�rdrcrcrdr�s
z"AbstractDigestAuthHandler.__init__cCs
d|_dSr�r�r�rcrcrdr�sz+AbstractDigestAuthHandler.reset_retry_countcCs||�|d�}|jdkr*t|jdd|d��n|jd7_|rx|��d}|��dkr`|�||�S|��dkrxtd|��dS)	Nr�r�zdigest auth failedrr�digestr�zEAbstractDigestAuthHandler does not support the following scheme: '%s')r�r�rr�r�r��retry_http_digest_authrS)r�r�r�r�r�r�r<rcrcrdr�s

��z/AbstractDigestAuthHandler.http_error_auth_reqedcCsz|�dd�\}}ttdt|���}|�||�}|rvd|}|j�|jd�|krRdS|�|j|�|j	j
||jd�}|SdS)Nr)rz	Digest %sr;)r��parse_keqv_listr�parse_http_list�get_authorizationr�r�r�r�rrr`)r�r�r��token�	challenge�chal�auth_val�resprcrcrdr�(sz0AbstractDigestAuthHandler.retry_http_digest_authcCs@d|j|t��f}|�d�td�}t�|���}|dd�S)Nz	%s:%s:%s:rY��)r��time�ctimer_�_randombytes�hashlib�sha1�	hexdigest)r��nonce�s�b�digrcrcrd�
get_cnonce4sz$AbstractDigestAuthHandler.get_cnoncecCs�z6|d}|d}|�d�}|�dd�}|�dd�}WntyJYdS0|�|�\}}	|durfdS|j�||j�\}
}|
dur�dS|jdur�|�|j|�}nd}d|
||f}
d|��|j	f}|d	k�r.||j
kr�|jd
7_nd
|_||_
d|j}|�|�}d||||||�f}|	||
�|�}n2|du�rT|	||
�d|||�f�}nt
d
|��d|
|||j	|f}|�r�|d|7}|�r�|d|7}|d|7}|�r�|d||f7}|S)Nrmr��qop�	algorithm�MD5�opaquez%s:%s:%srXr�rz%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_implsrdrwr�r_�get_entity_digestr�r�r�r�r�r)r�r�r�rmr�r�r�r��H�KDrNr��entdig�A1�A2�ncvalue�cnonce�noncebit�respdigr~rcrcrdr�?s\

�





��z+AbstractDigestAuthHandler.get_authorizationcs6|dkrdd��n|dkr"dd���fdd�}�|fS)Nr�cSst�|�d����S�NrY)r��md5r_r���xrcrcrdrU~rkz?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>�SHAcSst�|�d����Sr�)r�r�r_r�r�rcrcrdrU�rkcs�d||f�S)NrXrc)r��d�r�rcrdrU�rkrc)r�r�r�rcr�rdr�{s
z-AbstractDigestAuthHandler.get_algorithm_implscCsdSrerc)r�r_r�rcrcrdr��sz+AbstractDigestAuthHandler.get_entity_digest)N)r�r�r�r�r�r�r�r�r�r�r�rcrcrcrdr8�s
	<
r8c@s eZdZdZdZdZdd�ZdS)r9z�An authentication protocol defined by RFC 2069

    Digest authentication improves on basic authentication because it
    does not transmit passwords in the clear.
    r���cCs*t|j�d}|�d|||�}|��|S)Nrr�)rr�r�r��r�r�rrr�r�r��retryrcrcrdr��s�z$HTTPDigestAuthHandler.http_error_401N)r�r�r�r!r�rr�rcrcrcrdr9�sr9c@seZdZdZdZdd�ZdS)r:�Proxy-Authorizationr�cCs"|j}|�d|||�}|��|Sr�r�r�rcrcrdr��s�z%ProxyDigestAuthHandler.http_error_407N)r�r�r�r�rr�rcrcrcrdr:�sr:c@s.eZdZddd�Zdd�Zdd�Zdd	�Zd
S)�AbstractHTTPHandlerrcCs
||_dSre��_debuglevel)r��
debuglevelrcrcrdr��szAbstractHTTPHandler.__init__cCs
||_dSrer�)r��levelrcrcrd�set_http_debuglevel�sz'AbstractHTTPHandler.set_http_debuglevelc
CsN|j}|std��|jdur�|j}t|t�r8d}t|��|�d�sN|�dd�|�d�s�d}z:tr~t|t	j	�r~t
|�|j}nt|�}t
|�|j}Wn0ty�t|t
�r�tdt|�|f��Yn0|�dd|�|}|��r�t|j�\}}	t|	�\}}
|�d��s|�d|�|jjD]*\}}|��}|�|��s|�||��q|S)	N�
no host givenzLPOST data should be bytes or an iterable of bytes. It cannot be of type str.zContent-type�!application/x-www-form-urlencodedzContent-lengthzBContent-Length should be specified for iterable data of type %r %rz%dr�)r�rr_r�r
r�r�r�r�arrayrz�itemsize�
memoryviewr*rSr�r�rr�rrr�r�)
r�r�r�r_r�r��mv�sel_hostr<�sel�sel_pathrvr�rcrcrd�do_request_�sR


�

���zAbstractHTTPHandler.do_request_c

s4|j}|std��||fd|ji|��}t|j����t�fdd�|j��D���d�d<tdd����D���|jr�i}d}|�vr��|||<�|=|j	|j|d	�z|�
|��|j|j
��Wn6tjy�}z|��t|��WYd
}~n*d
}~00|��}	|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�r`c3s"|]\}}|�vr||fVqdSrercr*�r�rcrdr0�s
�z.AbstractHTTPHandler.do_open.<locals>.<genexpr>r��
Connectioncss|]\}}|��|fVqdSre)�title)r+rvr�rcrcrdr0�rkr�r�N)r�rr`rr�r�r�r�r��
set_tunnelr�r�r�r_rr�r��getresponse�sockr�r^�reasonr�)
r��
http_classr��http_conn_argsr�r�tunnel_headers�proxy_auth_hdr�errrTrcr�rdr��s6
"

zAbstractHTTPHandler.do_openN)r)r�r�r�r�r�r�r�rcrcrcrdr��s
3r�c@seZdZdd�ZejZdS)r;cCs|�tj|�Sre)r�r�HTTPConnection�r�r�rcrcrd�	http_open$szHTTPHandler.http_openN)r�r�r�r�r�r��http_requestrcrcrcrdr;"sr;r
c@s$eZdZddd�Zdd�ZejZdS)r\rNcCst�||�||_||_dSre)r�r��_context�_check_hostname)r�r�rPrQrcrcrdr�-szHTTPSHandler.__init__cCs|jtj||j|jd�S)NrO)r�rr
r�r�r�rcrcrd�
https_open2s
�zHTTPSHandler.https_open)rNN)r�r�r�r�r�r�r��
https_requestrcrcrcrdr\+s
r\c@s.eZdZddd�Zdd�Zdd�ZeZeZdS)	r1NcCs2ddlmmm}|dur(|��}||_dSr�)Zfuture.backports.http.cookiejar�	backportsr�	cookiejar�	CookieJar)r�r��http_cookiejarrcrcrdr�;szHTTPCookieProcessor.__init__cCs|j�|�|Sre)r��add_cookie_header)r�r�rcrcrdr�Asz HTTPCookieProcessor.http_requestcCs|j�||�|Sre)r��extract_cookies)r�r�r�rcrcrdr Esz!HTTPCookieProcessor.http_response)N)r�r�r�r�r�r r�r"rcrcrcrdr1:s

r1c@seZdZdd�ZdS)r?cCs|j}td|��dS)Nzunknown url type: %s)r�r)r�r�r�rcrcrdr�MszUnknownHandler.unknown_openN)r�r�r�r�rcrcrcrdr?Lsr?cCsNi}|D]@}|�dd�\}}|ddkr@|ddkr@|dd�}|||<q|S)z>Parse list of key=value strings where keys are not duplicated.�=rrr�rj)r�)�l�parsed�eltr,r-rcrcrdr�Qs
r�cCs�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.
    r�F�\Tr��,cSsg|]}|���qSrc��strip)r+�partrcrcrdrj�rkz#parse_http_list.<locals>.<listcomp>)rx)r��resr�escaper�currcrcrdr�[s4	


r�c@s(eZdZdd�ZdZdd�Zdd�ZdS)r<cCs\|j}|dd�dkrN|dd�dkrN|jrN|jdkrN|j|��urXtd��n
|�|�SdS)NrrGrKr9�	localhost�-file:// scheme is supported only on localhost)r�r��	get_namesr�open_local_file)r�r�r^rcrcrd�	file_open�s&�
zFileHandler.file_openNcCs^tjdurXz*tt�d�dt�t���d�t_Wn"tjyVt�d�ft_Yn0tjS)Nrr)r<�namesrlr�gethostbyname_ex�gethostname�gaierror�
gethostbynamer�rcrcrdr�s
��
zFileHandler.get_namesc
Csddlmmm}ddl}|j}|j}t|�}z�t�	|�}|j
}|j|jdd�}	|�
|�d}
t�d|
ppd||	f�}|r�t|�\}}|r�|s�t|�|��vr�|r�d||}
nd|}
tt|d�||
�WSWn.t�y}zt|��WYd}~n
d}~00td��dS)	NrT��usegmtz6Content-type: %s
Content-length: %d
Last-modified: %s
�
text/plain�file://�rbzfile not on local host)�future.backports.email.utilsr�r�utils�	mimetypesr�r�rErq�stat�st_size�
formatdate�st_mtime�
guess_type�message_from_stringr�_safe_gethostbynamerr(r�OSErrorr)r�r��email_utilsrr�r|�	localfile�statsr��modified�mtyper�rz�origurl�exprcrcrdr�s:
����zFileHandler.open_local_file)r�r�r�rrrrrcrcrcrdr<�s
r<cCs(zt�|�WStjy"YdS0dSre)rrr�r�rcrcrdr$�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
jy�}zt|��WYd}~n
d}~00t
|j�\}	}
|	�d�}ttt	|��}|dd�|d}}|�r|d�s|dd�}z�|�||||||j�}
|�r:d�p<d}|
D]2}t|�\}}|��d	k�rB|d
v�rB|��}�qB|
�||�\}}d}|�|j�d}|�r�|d|7}|du�r�|dk�r�|d|7}t�|�}t|||j�WS|j�y }z td
|�}t|�WYd}~n
d}~00dS)Nr�ftp error: no host givenr�r9rjrr��Dr���a�Ar�r�r�r/zContent-type: %s
zContent-length: %d
�
ftp error: %r)�ftplibrr�rr�FTP_PORTr
r r!rrrr�r"r�r�r�r�connect_ftpr`r$r��upper�retrfiler"r�rr#r(�
all_errorsr)r�r�r4rr�rzrNrdr�rr�attrs�dirsrf�fwr��attrr�r�retrlenr�r*r,�excrcrcrd�ftp_open�s^
�
zFTPHandler.ftp_openc	Cst||||||dd�S)NF)�
persistent)�
ftpwrapper)r�rNrdr�rzr;r`rcrcrdr6�s�zFTPHandler.connect_ftpN)r�r�r�r@r6rcrcrcrdr=�s5r=c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)r>cCs"i|_i|_d|_d|_d|_dS)Nr�<r�)�cacher`�soonest�delay�	max_connsr�rcrcrdr��s
zCacheFTPHandler.__init__cCs
||_dSre)rF)r��trcrcrd�
setTimeoutszCacheFTPHandler.setTimeoutcCs
||_dSre)rG)r�r3rcrcrd�setMaxConnsszCacheFTPHandler.setMaxConnscCsr|||d�|�|f}||jvr4t��|j|j|<n,t||||||�|j|<t��|j|j|<|��|j|S)Nr9)�joinrDr�rFr`rB�check_cache)r�rNrdr�rzr;r`r�rcrcrdr6
s

�
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����|_dSre)r�rEr�r`r�rDr��min�valuesrzrG)r�rHr,r-rcrcrdrLs


zCacheFTPHandler.check_cachecCs0|j��D]}|��q
|j��|j��dSre)rDrNr��clearr`)r��connrcrcrd�clear_cache)s

zCacheFTPHandler.clear_cacheN)	r�r�r�r�rIrJr6rLrQrcrcrcrdr>�sr>r$�nt)rErDcCst|�S)zOS-specific conversion from a relative URL of the 'file' scheme
        to a file system path; not recommended for general use.)r��pathnamercrcrdrE8srEcCst|�S)zOS-specific conversion from a file system path to a relative URL
        of the 'file' scheme; not recommended for general use.)rrSrcrcrdrD=srDc@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)3rIa,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�cKs�dd|jji}tj|tdd�|dur.t�}t|d�s@Jd��||_|�d�|_	|�d�|_
d	|jfg|_g|_
tj|_d|_t|_dS)
NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methods�classrKr�rRrS�key_file�	cert_filez
User-Agent)�	__class__r�r�r�r�rFr�rVr�rVrW�versionr��_URLopener__tempfilesrqr��_URLopener__unlink�	tempcache�ftpcache)r�rV�x509r�rcrcrdr�Ws
�zURLopener.__init__cCs|��dSre)r�r�rcrcrd�__del__qszURLopener.__del__cCs|��dSre)�cleanupr�rcrcrdr�tszURLopener.closec	CsT|jr@|jD]&}z|�|�Wqty0Yq0q|jdd�=|jrP|j��dSre)rZr[r%r\rO)r�rfrcrcrdr`ws
zURLopener.cleanupcGs|j�|�dS)zdAdd a header to be used by the HTTP interface only
        e.g. u.addheader('Accept', 'sound/basic')N)r�rx)r�r�rcrcrd�	addheader�szURLopener.addheaderc
CsZtt|��}t|dd�}|jrL||jvrL|j|\}}t|d�}t|||�St|�\}}|s`d}||jvr�|j|}t|�\}}	t|	�\}
}|
|f}nd}d|}||_	|�
dd�}t||�s�|r�|�|||�S|�
||�Sz.|dur�t||�|�WSt||�||�WSWnJt�y �Yn6tj�yT}
zttd	|
��WYd}
~
n
d}
~
00dS)
z6Use URLopener().open(file) instead of open(file, 'r').z%/:=&?~#+!$,;'@()*[]|��saferrfN�open_�-r�zsocket error)rr&rr\rr(rrVrr�r1r��open_unknown_proxy�open_unknownr�rrr�r�IOError)r�r�r_r|r�r�urltyper^rH�	proxyhostr�r�rvr�rcrcrdr�s<




zURLopener.opencCst|�\}}tdd|��dS)�/Overridable interface to open unknown URL type.�	url errorzunknown url typeN�rrh)r�r�r_r�r^rcrcrdrg�szURLopener.open_unknowncCs t|�\}}tdd||��dS)rkrlzinvalid proxy for %sNrm)r�rHr�r_r�r^rcrcrdrf�szURLopener.open_unknown_proxyc
Cs6tt|��}|jr&||jvr&|j|St|�\}}|dur�|rF|dkr�z0|�|�}|��}|��tt|�d�|fWSt	y�}	zWYd}	~	n
d}	~	00|�
||�}�zV|��}
|r�t
|d�}n|ddl}t|�\}
}t|p�d�\}
}t|p�d�\}}
t
|�pd�\}}
tj�|�d}|�|�\}}|j�|�t�|d�}z�||
f}|jdu�r`||j|<d}d}d}d}d	|
v�r�t|
d
�}|�r�||||�|�|�}|�s��q�|t|�7}|�|�|d7}|�r�||||��q�W|��n
|��0W|��n
|��0|dk�r2||k�r2td||f|��|S)ztretrieve(url) returns (filename, headers) for a local object
        or (tempfilename, headers) for a remote object.Nrfrrgrr�rirjrkrlrm)rr&r\rrrpr�rErrhrrtr#r"rqrr�splitext�mkstemprZrx�fdopenr
ryrzr{r)r�r^r|r}r_r��url1rr�r�r�r�rt�garbagerr�suffix�fdr�r�r�ryr�r�rcrcrd�retrieve�sp





��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|
��}Wntj�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.
        Nrz	%s://%s%sz
http errorr�rYzBasic %sr�r�r�r�r�r�zContent-Typer�r�r�z$http protocol error: bad status linerr�http:)r�r
rr rrr�r\rhr]r^r_r�r�r�r�r�
BadStatusLiner�statusr(r��
http_errorrr�)r��connection_factoryr^r_�user_passwd�proxy_passwdr�r��realhostrir��
proxy_authr��	http_connr��headerr�r�rcrcrd�_open_generic_http�st


��zURLopener._open_generic_httpcCs|�tj||�S)zUse HTTP protocol.)r�rr��r�r^r_rcrcrd�	open_httpXszURLopener.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�r^r�errcode�errmsgr�r_rvr�r�rcrcrdry\s

zURLopener.http_errorcCs|��t||||d��dS)z>Default error handler: close the connection and raise IOError.N)r�r�r�r^rr�r�r�rcrcrdrlszURLopener.http_error_defaultcCstj||j|jd�S)N)rVrW)rr
rVrW)r�r�rcrcrd�_https_connectionrs�zURLopener._https_connectioncCs|�|j||�S)zUse HTTPS protocol.)r�r�r�rcrcrd�
open_httpswszURLopener.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 implementedNrrGrKr9�z
localhost/r
)r�r
rr�rSr)r�r^rcrcrd�	open_file{s

4
zURLopener.open_filec
Cslddlmmm}ddl}t|�\}}t|�}zt�|�}Wn2t	yt}zt
|j|j��WYd}~n
d}~00|j
}	|j|jdd�}
|�|�d}t�d|p�d|	|
f�}|s�|}
|dd�dkr�d	|}
tt|d
�||
�St|�\}}|�s`t�|�t�ft�v�r`|}
|dd�dk�r0d	|}
n|dd�dk�rNtd
|��tt|d
�||
�St
d��dS)zUse local file.rNTrz6Content-Type: %s
Content-Length: %d
Last-modified: %s
rrr9rrrz./zAlocal file url may start with / or file:. Unknown url of type: %sz#local file error: not on local host)rr�rrrrrErqrr%r�strerrorr|rr r!r"r#r(rrrrr�thishostrS)r�r^r&rr�rf�	localnamer(�er�r)r*r��urlfilerzrcrcrdr�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�rb|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~}zt td|��WYd}~n
d}~00dS)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedrNr.r�r9rjrr/r�r�r0zftp:zContent-Type: %s
zContent-Length: %d
zftp error %r)!r�r
rrrrr r!rrrr4r5r
r"r�rKrzr]�MAXFTPCACHErRr�rBr$r�r7r8r"rr#r(�	ftperrorsr)r�r^rr�rrrzrNrdr4r:r;rfr�r,r-r�r=r�rr>r*r�r,rcrcrd�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 implementedrrz
data errorzbad data URLztext/plain;charset=US-ASCII�;rrNr�zDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %sr]rYzlatin-1zContent-Length: %d�
)r�r
rr�rSrh�rfindrxr��strftime�gmtimer]�decodebytesr_r�rrzrKrr#�io�StringIOr()	r�r^r_r��semi�encodingr�r��frcrcrd�	open_data�s8

�




zURLopener.open_data)N)N)N)N)NNN)N)N)N)N)r�r�r�r!rZr�rYr�r_r�r`rarrgrfrur�r�ryrrRr�r�r�rr�r�rcrcrcrdrIJs.

$


B\


	 :rIc@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).rJz?Derived class with handlers for errors we can handle (perhaps).cOs.tj|g|�Ri|��i|_d|_d|_dS)Nrr$)rIr��
auth_cache�tries�maxtries)r�r��kwargsrcrcrdr�szFancyURLopener.__init__cCst||d||�S)z3Default error handling -- don't raise an exception.rv)r(r�rcrcrdrsz!FancyURLopener.http_error_defaultNc	Csl|jd7_|jrN|j|jkrNt|d�r2|j}n|j}d|_|||dd|�S|�||||||�}d|_|S)z%Error 302 -- relocated (temporarily).r�http_error_500rrz)Internal Server Error: Redirect Recursion)r�r�r�r�r�redirect_internal)	r�r^rr�r�r�r_r�r�rcrcrdrCs
��zFancyURLopener.http_error_302c	Csxd|vr|d}nd|vr$|d}ndS|��t|jd||�}t|�}|jdvrnt|||d|||��|�|�S)Nr5r6�:r7z( Redirection to url '%s' is not allowed.)r�rr�rr<rr)	r�r^rr�r�r�r_r2r@rcrcrdr�%s 


��z FancyURLopener.redirect_internalcCs|�||||||�S)z*Error 301 -- also relocated (permanently).�rC�r�r^rr�r�r�r_rcrcrdrDAszFancyURLopener.http_error_301cCs|�||||||�S)z;Error 303 -- also relocated (essentially identical to 302).r�r�rcrcrdrEEszFancyURLopener.http_error_303cCs2|dur|�||||||�S|�|||||�SdS)z1Error 307 -- relocated, but turn POST into error.N)rCrr�rcrcrdrFIszFancyURLopener.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.r��![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r��retry_�_basic_authN�rIrr��matchr�r�r�r��
r�r^rr�r�r�r_r��stuffr�r<rmrvrcrcrdr�Ps.
�
�
��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.r�r�r��retry_proxy_r�Nr�r�rcrcrdr�is.
�
�
��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�@r�%s:%s@%sr�rb�rrVrr��get_user_passwdrr�r�r^rmr_r�r�r2rHrirj�
proxyselectorr�rNrdrcrcrd�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�r�rr�r�rbr�r�rcrcrd�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)Nr�rr�r�rbr��rr�r�rr�
r�r^rmr_r�r�r�rNrdr2rcrcrdr��s�
z$FancyURLopener.retry_http_basic_authc
Cs�t|�\}}|�d�d}||d�}|�|||�\}}|sD|sDdSdt|dd�t|dd�|f}d||}	|dur�|�|	�S|�|	|�SdS)Nr�rr�r�rbr�r�r�rcrcrd�retry_https_basic_auth�s�
z%FancyURLopener.retry_https_basic_authrcCs`|d|��}||jvr2|r(|j|=n
|j|S|�||�\}}|sJ|rX||f|j|<||fS)Nr�)r�r��prompt_user_passwd)r�r�rmrQr�rNrdrcrcrdr��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: rp)�getpassr	�KeyboardInterrupt�print)r�r�rmr�rNrdrcrcrdr��s�
z!FancyURLopener.prompt_user_passwd)N)N)N)N)NF)NF)N)N)N)N)r)r�r�r�r!r�rrCr�rDrErFr�r�r�r�r�r�r�r�rcrcrcrdrJs(



�
�





rJcCstdurt�d�atS)z8Return the IP address of the magic hostname 'localhost'.Nr)�
_localhostrrrcrcrcrdr�s
rcCsNtdurJztt�t���d�aWn&tjyHtt�d�d�aYn0tS)z,Return the IP addresses of the current host.Nrr)�	_thishostrlrrrrrcrcrcrdr��sr�cCstdurddl}|jatS)z1Return the set of errors raised by the FTP class.Nr)�
_ftperrorsr4r9)r4rcrcrdr��sr�cCstdurt�d�atS)z%Return an empty email Message object.Nr�)�
_noheadersrr#rcrcrcrd�	noheaders�s
r�c@sJeZdZdZddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dS)rBz;Class used by open_ftp() for cache of open FTP connections.NTcCs<||_||_||_||_||_||_d|_||_|��dSr�)	rNrdr�rzr;r`�refcount�	keepalive�init)r�rNrdr�rzr;r`rArcrcrdr�	szftpwrapper.__init__cCs\ddl}d|_|��|_|j�|j|j|j�|j�|j	|j
�d�|j�}|j�
|�dS)Nrr9)r4�busy�FTPr8�connectr�rzr`�loginrNrdrKr;�cwd)r�r4�_targetrcrcrdr�	s
zftpwrapper.initc
Cs�ddl}|��|dvr"d}d}nd|}d}z|j�|�Wn(|jyf|��|j�|�Yn0d}|r�|s�zd|}|j�|�\}}WnJ|jy�}z0t|�dd�dkr�t	t
d	|��WYd}~n
d}~00|�s�|j�d�|�rx|j��}	z`z|j�|�Wn>|j�yN}z"t
d	|�}
||
_
|
�WYd}~n
d}~00W|j�|	�n|j�|	�0d
|}nd}|j�|�\}}d|_t|�d�|j�}|jd7_|��||fS)
Nr)r�r/zTYPE ArzTYPE zRETR rK�550r3zLIST �LISTr)r4�endtransferr8�voidcmdr9r��ntransfercmd�
error_permr
rr�pwdr��	__cause__r�r)�makefile�
file_closer�r�)r�rfr�r4�cmd�isdirrPr>r�r�r?�ftpobjrcrcrdr8	sJ&

zftpwrapper.retrfilecCs
d|_dSr�)r�r�rcrcrdr�K	szftpwrapper.endtransfercCsd|_|jdkr|��dS)NFr)r�r��
real_closer�rcrcrdr�N	s
zftpwrapper.closecCs2|��|jd8_|jdkr.|js.|��dS)Nrr)r�r�r�r�r�rcrcrdr�S	szftpwrapper.file_closecCs0|��z|j��Wnt�y*Yn0dSre)r�r8r�r�r�rcrcrdr�Y	s
zftpwrapper.real_close)NT)r�r�r�r!r�r�r8r�r�r�r�rcrcrcrdrB	s�
	0rBcCsHi}tj��D]4\}}|��}|r|dd�dkr|||dd�<q|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�_proxy)rq�environr�r�)rVrvr�rcrcrd�getproxies_environmenta	s	r�cCsttj�dd�ptj�dd�}|dkr(dSt|�\}}dd�|�d�D�}|D]"}|rL|�|�sh|�|�rLdSqLd	S)
z�Test if proxies should not be used for a particular host.

    Checks the environment for a variable named no_proxy, which should
    be a list of DNS suffixes separated by commas, or '*' for all hosts.
    �no_proxyr�ZNO_PROXY�*rcSsg|]}|���qSrcr)r+rHrcrcrdrj~	rkz,proxy_bypass_environment.<locals>.<listcomp>rr)rqr�r�rr��endswith)r�r��hostonlyrzZ
no_proxy_listrvrcrcrd�proxy_bypass_environmentq	sr�c	Csddlm}t|�\}}dd�}d|vr4|dr4dSd}|�d	d
�D]�}|sNqDt�d|�}|du�r|dur�zt�|�}||�}Wntjy�YqDYn0||�d��}	|�d
�}
|
dur�d|�d��	d�d}
nt
|
dd��}
d|
}
||
?|	|
?k�rdSqD|||�rDdSqDdS)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)�fnmatchcSsd|�d�}ttt|��}t|�dkr8|gd�dd�}|dd>|dd>B|dd	>B|d
BS)N�.r#)rrrrr�rr�rr�rK)r�r�rr
rz)�ipAddrryrcrcrd�ip2num�	s

z,_proxy_bypass_macosx_sysconf.<locals>.ip2numr��exclude_simpleTN�
exceptionsrcz(\d+(?:\.\d+)*)(/\d+)?rrr�� F)r�rr�r�r�rrr��group�countr
)r��proxy_settingsr�r�rzr��hostIPr�r3r~�maskrcrcrd�_proxy_bypass_macosx_sysconf�	s8




r��darwin)�_get_proxy_settings�_get_proxiescCst�}t||�Sre)r�r�)r�r�rcrcrd�proxy_bypass_macosx_sysconf�	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�rcrcrcrd�getproxies_macosx_sysconf�	sr�cCst�rt|�St|�SdSre)r�r�r�r-rcrcrdr\�	sr\cCst�p
t�Sre)r�r�rcrcrcrdrF�	srFc
Csi}zddl}Wnty&|YS0z�|�|jd�}|�|d�d}|r�t|�|d�d�}d|vr�|�d�D]4}|�dd�\}}t�d	|�s�d
||f}|||<qrn>|dd�dkr�||d
<n$d||d
<d||d<d||d<|�	�Wnt
ttf�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�ProxyEnableZProxyServerrr�rz^([^/:]+)://z%s://%sr�rvrz	http://%sz
https://%sr�zftp://%sr8)
�winreg�ImportError�OpenKey�HKEY_CURRENT_USER�QueryValueExr
r�r�r��Close�WindowsErrorrSr�)rVr��internetSettings�proxyEnableZproxyServer�pr��addressrcrcrd�getproxies_registry�	sF
�����
rcCst�p
t�S)��Return a dictionary of scheme -> proxy server URL mappings.

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

        )r�rrcrcrcrdrF
scCsvzddl}Wnty YdS0z6|�|jd�}|�|d�d}t|�|d�d�}WntylYdS0|rv|szdSt|�\}}|g}z t�	|�}||kr�|�
|�Wntjy�Yn0z t�|�}||kr�|�
|�Wntjy�Yn0|�
d�}|D]j}	|	dk�r$d|v�r$dS|	�dd	�}	|	�d
d�}	|	�dd�}	|D] }
t�|	|
tj��rLdS�qL�qdS)
Nrr�r�Z
ProxyOverrider�z<local>r�rz\.r�z.*�?)r�r�r�r�r�r
rrrrrxr��getfqdnr�r1r�r�r�)r�r�rrZ
proxyOverrideZrawHostrz�addrZfqdnrr�rcrcrd�proxy_bypass_registry
s`�����





r
cCst�rt|�St|�SdS)rN)r�r�r
r-rcrcrdr\H
s)NNN)�r!�
__future__rrrrZfuture.builtinsrrrr	r
rrr
Zfuture.utilsrrrr]r�r�r�Zfuture.backportsrZfuture.backports.httprrr�rrr�parserrrrrrrrrr r!r"r#r$r%r&r'r�r(r)r�rqr|r�r�sysr�rtrnr��collectionsr*�collections.abcrTr+r�rR�__all__rYr�r]r	rArBrwrGrHr��ASCIIr�r��objectr,r-rCr.r@r/r0rPr2r3r4r5r6r7�urandomr�r8r9r:r�r;r�r\rxr1r?r�r�r<r$r=r>r�rvZ
nturl2pathrErDr]rIrJr�rr�r�r�r�r�r�rBr�r�r��platformZ_scproxyr�r�r�r�r\rFrr
rcrcrcrd�<module>s�V(L

?
y&hH*@
Ez

+4:8AU

^<

-	2