Your IP : 3.146.107.152


Current Path : /opt/alt/python35/lib64/python3.5/urllib/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/urllib/__pycache__/request.cpython-35.opt-1.pyc



��Yf{�)@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,yddl-Z-Wne.k
r�dZ/YnXdZ/dd	d
ddd
ddddddddddddddddddd d!d"d#d$d%d&d'd(g!Z0e
j1dd)�Z2da3dej4d*dd+dd,dd-dd.d�Z5d/d �Z6gZ7dddd0d%�Z8d1d&�Z9e
j:d2e
j;�Z<d3d4�Z=Gd5d�d�Z>Gd6d	�d	�Z?d7d!�Z@Gd8d
�d
�ZAGd9d�deA�ZBGd:d�deA�ZCGd;d�deA�ZDd<d=�ZEGd>d�deA�ZFGd?d�d�ZGGd@d�deG�ZHGdAd�deH�ZIGdBd�d�ZJGdCd�deJeA�ZKGdDd�deJeA�ZLejMZNGdEd�d�ZOGdFd�deAeO�ZPGdGd�deAeO�ZQGdHdI�dIeA�ZRGdJd�deR�ZSeTejUdK�r�GdLdM�dMeR�ZVe0jWdM�GdNd
�d
eA�ZXGdOd�deA�ZYdPdQ�ZZdRdS�Z[GdTd�deA�Z\dUdV�Z]GdWd�deA�Z^GdXd�de^�Z_GdYd�deA�Z`dZZaejbd[kr`dd\lcmdZdmeZend]d#�Zdd^d"�ZeiZfGd_d'�d'�ZgGd`d(�d(eg�Zhdaidadb�Zjdakdcdd�Zldamdedf�Zndaodgdh�ZpGdidj�dj�Zqdkdl�Zrddmdn�Zsdodp�Zte
judqkr�ddrlvmwZwmxZxdsdt�Zydudv�Zzdwdx�Z{dyd$�Z|nNejbd[kr�dzd{�Z}d|d$�Z|d}d~�Z~ddx�Z{nerZ|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�FancyURLopener��cafile�capath�	cadefault�contextc	Cs�|s|s|r{|dk	r*td��ts<td��tjtjjd|d|�}td|�}t|�}nF|r�td|�}t|�}n"tdkr�t�a}nt}|j	|||�S)NzDYou can't pass both context and any of cafile, capath, and cadefaultzSSL support not availabler;r<r>)
�
ValueError�	_have_ssl�sslZcreate_default_contextZPurposeZSERVER_AUTH�HTTPSHandlerr2�_opener�open)	�url�data�timeoutr;r<r=r>Z
https_handler�opener�rI�3/opt/alt/python35/lib64/python3.5/urllib/request.pyr0�s$		cCs
|adS)N)rC)rHrIrIrJr1�scCs�t|�\}}tjt||���:}|j�}|dkrb|rbtjj|�|fS|rzt|d�}n(t	j
dd�}|j}tj
|�|��||f}	d
}
d}d}d}
d	|kr�t|d
�}|r�||
|
|�xW|j|
�}|sP|t|�7}|j|�|
d7}
|r||
|
|�qWWdQRXWdQRX|dkr�||kr�td||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�wbZdeleteFi��rzcontent-lengthzContent-LengthNz1retrieval incomplete: got only %i out of %i bytesi ���)r�
contextlib�closingr0�info�os�path�normpathrD�tempfileZNamedTemporaryFile�name�_url_tempfiles�append�int�read�len�writer)rE�filename�
reporthookrFZurl_typerT�fp�headers�tfp�result�bs�sizer[�blocknum�blockrIrIrJr6�sD	


"cCsTx4tD],}ytj|�Wqtk
r2YqXqWtdd�=trPdadS)z0Clean up temporary files from urlretrieve calls.N)rXrS�unlink�OSErrorrC)Z	temp_filerIrIrJr7�s

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

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

    rN��Host)�full_urlr�
get_header�_cut_port_re�sub�lower)�requestrE�hostrIrIrJ�request_host�s	rsc@s*eZdZdiddddd�Zedd��Zejdd��Zejdd��Zed	d
��Zejdd
��Zejdd
��Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zddd�Zdd �Zd!d"�ZdS)#rNFc	Cs�||_i|_i|_d|_||_d|_x*|j�D]\}}|j||�qCW|dkr{t|�}||_	||_
|r�||_dS)N)rlra�unredirected_hdrs�_datarF�_tunnel_host�items�
add_headerrs�origin_req_host�unverifiable�method)	�selfrErFraryrzr{�key�valuerIrIrJ�__init__
s								zRequest.__init__cCs&|jrdj|j|j�S|jS)Nz{}#{})�fragment�format�	_full_url)r|rIrIrJrls	zRequest.full_urlcCs8t|�|_t|j�\|_|_|j�dS)N)rr�rr��_parse)r|rErIrIrJrl"scCsd|_d|_d|_dS)Nrj)r�r��selector)r|rIrIrJrl)s		cCs|jS)N)ru)r|rIrIrJrF/szRequest.datacCs8||jkr4||_|jd�r4|jd�dS)NzContent-length)ru�
has_header�
remove_header)r|rFrIrIrJrF3s	cCs
d|_dS)N)rF)r|rIrIrJrF=scCsqt|j�\|_}|jdkr:td|j��t|�\|_|_|jrmt|j�|_dS)Nzunknown url type: %r)	rr��typer?rlrrrr�r
)r|�restrIrIrJr�As	zRequest._parsecCs+|jdk	rdnd}t|d|�S)z3Return a string indicating the HTTP request method.N�POST�GETr{)rF�getattr)r|Zdefault_methodrIrIrJ�
get_methodIszRequest.get_methodcCs|jS)N)rl)r|rIrIrJ�get_full_urlNszRequest.get_full_urlcCsJ|jdkr(|jr(|j|_n||_|j|_||_dS)N�https)r�rvrrrlr�)r|rrr�rIrIrJ�	set_proxyQs
	zRequest.set_proxycCs|j|jkS)N)r�rl)r|rIrIrJ�	has_proxyYszRequest.has_proxycCs||j|j�<dS)N)ra�
capitalize)r|r}�valrIrIrJrx\szRequest.add_headercCs||j|j�<dS)N)rtr�)r|r}r�rIrIrJ�add_unredirected_header`szRequest.add_unredirected_headercCs||jkp||jkS)N)rart)r|�header_namerIrIrJr�dszRequest.has_headercCs"|jj||jj||��S)N)ra�getrt)r|r��defaultrIrIrJrmhs	zRequest.get_headercCs*|jj|d�|jj|d�dS)N)ra�poprt)r|r�rIrIrJr�mszRequest.remove_headercCs/|jj�}|j|j�t|j��S)N)rt�copy�updatera�listrw)r|�hdrsrIrIrJ�header_itemsqszRequest.header_items)�__name__�
__module__�__qualname__r�propertyrl�setter�deleterrFr�r�r�r�r�rxr�r�rmr�r�rIrIrIrJrs(
c@speZdZdd�Zdd�Zdd�Zdd�Zd	ejd
d�Z	d	dd
�Z
dd�Zd	S)rcCsMdt}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_versionrIrIrJrws
				zOpenerDirector.__init__cCs�t|d�s%tdt|���d}xut|�D]g}|dkrMq8|jd�}|d|�}||dd�}|jd	�r|jd�|d}||dd�}yt|�}Wntk
r�YnX|jj	|i�}	|	|j|<n]|d
kr"|}|j
}	n?|dkr@|}|j}	n!|dkr8|}|j}	nq8|	j
|g�}
|
r�tj|
|�n
|
j|�d
}q8W|r�tj|j|�|j|�dS)N�
add_parentz%expected BaseHandler instance, got %rF�redirect_request�do_open�
proxy_open�_rN�errorrD�responserqT)r�r�r�)�hasattr�	TypeErrorr��dir�find�
startswithrZr?r�r�r�r�r��
setdefault�bisectZinsortrYr�r�)r|�handlerZadded�meth�i�protocolZ	condition�j�kind�lookupr�rIrIrJ�add_handler�sJ


zOpenerDirector.add_handlercCsdS)NrI)r|rIrIrJ�close�szOpenerDirector.closec	GsR|j|f�}x9|D]1}t||�}||�}|dk	r|SqWdS)N)r�r�)	r|�chainr��	meth_name�argsr�r��funcrcrIrIrJ�_call_chain�s
zOpenerDirector._call_chainNc
Cs�t|t�r!t||�}n|}|dk	r<||_||_|j}|d}x8|jj|g�D]!}t||�}||�}qnW|j	||�}	|d}x;|j
j|g�D]$}t||�}|||	�}	q�W|	S)NZ_requestZ	_response)�
isinstance�strrrFrGr�r�r�r��_openr�)
r|�fullurlrFrG�reqr�r�Z	processorr�r�rIrIrJrD�s"			

zOpenerDirector.opencCsp|j|jdd|�}|r%|S|j}|j|j||d|�}|rW|S|j|jdd|�S)Nr�Zdefault_openr��unknown�unknown_open)r�r�r�)r|r�rFrcr�rIrIrJr��s	
zOpenerDirector._opencGs�|d
kr<|jd}|d}d|}d}|}n|j}|d}d}|||f|}|j|�}|r�|S|r�|dd	f|}|j|�SdS)N�httpr��z
http_error_%srNZ_errorrr��http_error_default)r�r�)r�r�)r|�protor��dictr�Zhttp_errZ	orig_argsrcrIrIrJr��s 


		
zOpenerDirector.error)r�r�r�rr�r�r��socket�_GLOBAL_DEFAULT_TIMEOUTrDr�r�rIrIrIrJrvs/c	Gs7t�}ttttttttt	g	}t
tjd�rI|j
t�t�}xi|D]a}xX|D]P}t|t�r�t||�r�|j|�qft||�rf|j|�qfWqYWx|D]}|j|�q�Wx|D]}|j|��q�Wx3|D]+}t|t�r"|�}|j|�qW|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��clientrYrB�setr�r��
issubclass�add�remover�)r�rHZdefault_classes�skip�klassZcheck�hrIrIrJr2s0					
	




	c@s:eZdZdZdd�Zdd�Zdd�ZdS)	ri�cCs
||_dS)N)�parent)r|r�rIrIrJr�+szBaseHandler.add_parentcCsdS)NrI)r|rIrIrJr�.szBaseHandler.closecCs#t|d�sdS|j|jkS)N�
handler_orderT)r�r�)r|�otherrIrIrJ�__lt__2szBaseHandler.__lt__N)r�r�r�r�r�r�r�rIrIrIrJr(sc@s.eZdZdZdZdd�ZeZdS)r/zProcess HTTP error responses.i�cCsa|j|j|j�}}}d|ko7dkns]|jjd|||||�}|S)N��i,r�)�code�msgrRr�r�)r|rqr�r�r�r�rIrIrJ�
http_response?s
 	z HTTPErrorProcessor.http_responseN)r�r�r��__doc__r�r��https_responserIrIrIrJr/;sc@seZdZdd�ZdS)rcCst|j||||��dS)N)rrl)r|r�r`r�r�r�rIrIrJr�Msz*HTTPDefaultErrorHandler.http_error_defaultN)r�r�r�r�rIrIrIrJrLsc@sHeZdZdZdZdd�Zdd�ZeZZZ	dZ
dS)	r��
c	s�|j�}|dkr$|dkp9|dko9|dksWt|j||||��|jdd	�}d�t�fdd
�|jj�D��}t|d|d|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-typec3s3|])\}}|j��kr||fVqdS)N)rp)�.0�k�v)�CONTENT_HEADERSrIrJ�	<genexpr>ss	z7HTTPRedirectHandler.redirect_request.<locals>.<genexpr>raryrzT)r�r�r�r�)r�r�)r�r�r�)r�r�)	r�rrl�replacer�rarwrry)	r|r�r`r�r�ra�newurl�mZ
newheadersrI)r�rJr�Xs
(		z$HTTPRedirectHandler.redirect_requestc
Cs�d|kr|d}nd|kr2|d}ndSt|�}|jdkrst||d||f||��|jr�|jr�t|�}d|d	<t|�}t|d
ddtj	�}t
|j|�}|j||||||�}|dkrdSt
|d
�rw|j}	|_|	j|d�|jksRt|	�|jkr�t|j||j|||��ni}	|_|_|	j|d�d|	|<|j�|j�|jj|d|j�S)N�location�urir�r��ftprjz+%s - Redirection to url '%s' is not allowed�/r��encodingz
iso-8859-1�safe�
redirect_dictrrNrG)r�r�r�rj)r�schemerrTZnetlocr�rr	�stringZpunctuationrrlr�r�r�r��max_repeatsr\�max_redirections�inf_msgr[r�r�rDrG)
r|r�r`r�r�rar��urlparts�newZvisitedrIrIrJ�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_307rrIrIrIrJrPs&<c	Cs�t|�\}}|jd�s0d}|}nS|jd�sOtd|��|jdd�}|dkrsd}|d|�}t|�\}}|dk	r�t|�\}}n
d}}||||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�rNrO)rr�r?r�rr)	�proxyrZr_scheme�	authority�endZuserinfo�hostport�user�passwordrIrIrJ�_parse_proxy�s	
rc@s1eZdZdZddd�Zdd�ZdS)r�dNc	Cse|dkrt�}||_x@|j�D]2\}}t|d||||jdd��q+WdS)Nz%s_opencSs||||�S)NrI)�rrr�r�rIrIrJ�<lambda>�sz'ProxyHandler.__init__.<locals>.<lambda>)r5�proxiesrw�setattrr�)r|rr�rErIrIrJr�s		
zProxyHandler.__init__cCs�|j}t|�\}}}}|dkr3|}|jrOt|j�rOdS|r�|r�dt|�t|�f}	tj|	j��jd�}
|j	dd|
�t|�}|j
||�||ks�|dkr�dS|jj|d|j
�SdS)Nz%s:%s�asciizProxy-authorizationzBasic r�rG)r�rrr�proxy_bypassr
�base64�	b64encode�encode�decoderxr�r�rDrG)r|r�rr�Z	orig_typeZ
proxy_typerrrZ	user_passZcredsrIrIrJr��s 	zProxyHandler.proxy_open)r�r�r�r�rr�rIrIrIrJr�s
c@sOeZdZdd�Zdd�Zdd�Zddd	�Zd
d�ZdS)
r cCs
i|_dS)N)�passwd)r|rIrIrJr
szHTTPPasswordMgr.__init__cs�t|t�r|g}|�jkr4i�j|<xGdD]?�t��fdd�|D��}||f�j||<q;WdS)NTFcs"g|]}�j|���qSrI)�
reduce_uri)r��u)�default_portr|rIrJ�
<listcomp>s	z0HTTPPasswordMgr.add_password.<locals>.<listcomp>)TF)r�r�r�tuple)r|�realmr�rr�reduced_urirI)r r|rJ�add_password
s	

zHTTPPasswordMgr.add_passwordc	Cs�|jj|i�}xddD]\}|j||�}xA|j�D]3\}}x$|D]}|j||�rT|SqTWqAWqWdS)NTF)TF)NN)rr�rrw�	is_suburi)	r|r#�authuriZdomainsr �reduced_authuriZurisZauthinfor�rIrIrJ�find_user_passwords

z"HTTPPasswordMgr.find_user_passwordTc
Cs�t|�}|dr=|d}|d}|dp7d}nd}|}d}t|�\}}|r�|dkr�|dk	r�dddd	ij|�}	|	dk	r�d
||	f}||fS)z@Accept authority or URI and extract only the authority and path.rNrr�r�Nr��Pr�i�z%s:%d)rr
r�)
r|r�r �partsrrrTrr�portZdportrIrIrJr"s 


	zHTTPPasswordMgr.reduce_uricCsi||krdS|d|dkr(dStj|d|df�}t|�t|d�kredSdS)zcCheck if test is below base in a URI tree

        Both args must be URIs in reduced form.
        TrFrN)�	posixpath�commonprefixr\)r|�base�test�commonrIrIrJr&9szHTTPPasswordMgr.is_suburiN)r�r�r�rr%r)rr&rIrIrIrJr s

c@seZdZdd�ZdS)r!cCsDtj|||�\}}|dk	r1||fStj|d|�S)N)r r))r|r#r'rrrIrIrJr)Js

z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r�r�r�r)rIrIrIrJr!HscsReZdZ�fdd�Zd�fdd�Zddd�Zdd	�Z�S)
r"cs i|_t�j||�dS)N)�
authenticated�superr)r|r��kwargs)�	__class__rIrJrTs	z%HTTPPasswordMgrWithPriorAuth.__init__FcsR|j||�|dk	r5t�jd|||�t�j||||�dS)N)�update_authenticatedr3r%)r|r#r�rr�is_authenticated)r5rIrJr%Xsz)HTTPPasswordMgrWithPriorAuth.add_passwordcCs]t|t�r|g}x>dD]6}x-|D]%}|j||�}||j|<q,WqWdS)NTF)TF)r�r�rr2)r|r�r7r rr$rIrIrJr6_s	

z1HTTPPasswordMgrWithPriorAuth.update_authenticatedcCsXxQdD]I}|j||�}x.|jD]#}|j||�r)|j|Sq)WqWdS)NTF)TF)rr2r&)r|r'r r(r�rIrIrJr7is

z-HTTPPasswordMgrWithPriorAuth.is_authenticated)r�r�r�rr%r6r7rIrI)r5rJr"Rs
c@speZdZejdej�Zddd�Zdd�Zdd�Z	d	d
�Z
dd�Ze
ZeZ
dS)
r#z1(?:.*,)*[ 	]*([^ 	]+)[ 	]+realm=(["']?)([^"']*)\2NcCs1|dkrt�}||_|jj|_dS)N)r rr%)r|Zpassword_mgrrIrIrJrs		z!AbstractBasicAuthHandler.__init__c	Cs�|j|d�}|r�|j�d}|j�dkrMtd|��nqtjj|�}|r�|j�\}}}|dkr�tj	dt
d�|j�dkr�|j|||�SdS)	Nr�basiczDAbstractBasicAuthHandler does not support the following scheme: '%s'�"�'zBasic Auth Realm was unquotedr�)r9r:)r��splitrpr?r#�rx�search�groups�warnings�warn�UserWarning�retry_http_basic_auth)	r|�authreqrrr�rarZmor	r#rIrIrJ�http_error_auth_reqed�s
	
z.AbstractBasicAuthHandler.http_error_auth_reqedcCs�|jj||�\}}|dk	r�d||f}dtj|j��jd�}|j|jd�|krxdS|j|j|�|j	j
|d|j�SdSdS)Nz%s:%szBasic rrG)rr)rrrrrm�auth_headerr�r�rDrG)r|rrr�r#r�pw�raw�authrIrIrJrB�s"z.AbstractBasicAuthHandler.retry_http_basic_authcCs�t|jd�s)|jj|j�r-|S|jd�s�|jjd|j�\}}dj||�j�}tj	|�j
�}|jddj|j���|S)Nr7�
Authorizationz{0}:{1}zBasic {})
r�rr7rlr�r)r�rrZstandard_b64encoderr��strip)r|r�rrZcredentialsZauth_strrIrIrJ�http_request�s	z%AbstractBasicAuthHandler.http_requestcCsdt|jd�r`d|jko,dknrJ|jj|jd�n|jj|jd�|S)Nr7r�i,TF)r�rr�r6rl)r|r�r�rIrIrJr��s
z&AbstractBasicAuthHandler.http_response)r�r�r��re�compile�Ir<rrDrBrKr��
https_requestr�rIrIrIrJr#qs	
c@s"eZdZdZdd�ZdS)r$rIcCs%|j}|jd|||�}|S)Nzwww-authenticate)rlrD)r|r�r`r�r�rarEr�rIrIrJ�http_error_401�s		z#HTTPBasicAuthHandler.http_error_401N)r�r�r�rErPrIrIrIrJr$�sc@s"eZdZdZdd�ZdS)r%zProxy-authorizationcCs%|j}|jd|||�}|S)Nzproxy-authenticate)rrrD)r|r�r`r�r�rarr�rIrIrJ�http_error_407�s		z$ProxyBasicAuthHandler.http_error_407N)r�r�r�rErQrIrIrIrJr%�sc@sseZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dS)r&NcCsL|dkrt�}||_|jj|_d|_d|_d|_dS)Nr)r rr%�retried�nonce_count�
last_nonce)r|rrIrIrJr�s				z"AbstractDigestAuthHandler.__init__cCs
d|_dS)Nr)rR)r|rIrIrJ�reset_retry_count�sz+AbstractDigestAuthHandler.reset_retry_countcCs�|j|d�}|jdkr?t|jdd|d��n|jd7_|r�|j�d}|j�dkr�|j||�S|j�dkr�td|��dS)	N�i�zdigest auth failedrNrZdigestr8zEAbstractDigestAuthHandler does not support the following scheme: '%s')r�rRrrlr;rp�retry_http_digest_authr?)r|rErrr�rarCrrIrIrJrD�sz/AbstractDigestAuthHandler.http_error_auth_reqedcCs�|jdd�\}}ttdt|���}|j||�}|r�d|}|jj|jd�|krwdS|j|j|�|j	j
|d|j�}|SdS)Nr�rNz	Digest %srG)r;�parse_keqv_list�filter�parse_http_list�get_authorizationrar�rEr�r�rDrG)r|r�rH�tokenZ	challenge�chalZauth_valZresprIrIrJrW
s
z0AbstractDigestAuthHandler.retry_http_digest_authcCsXd|j|tj�f}|jd�td�}tj|�j�}|dd�S)Nz	%s:%s:%s:rrM�)rS�timeZctimer�_randombytes�hashlib�sha1�	hexdigest)r|�nonce�s�b�digrIrIrJ�
get_cnoncesz$AbstractDigestAuthHandler.get_cnoncecCsVyK|d}|d}|jd�}|jdd�}|jdd�}Wntk
rcdSYnX|j|�\}}	|dkr�dS|jj||j�\}
}|
dkr�dS|jdk	r�|j|j|�}nd}d|
||f}
d|j�|j	f}|d	kr�||j
kr=|jd
7_nd
|_||_
d|j}|j|�}d||||||�f}|	||
�|�}nD|dkr�|	||
�d|||�f�}nt
d
|��d|
|||j	|f}|r|d|7}|r*|d|7}|d|7}|rR|d||f7}|S)Nr#rd�qop�	algorithm�MD5�opaquez%s:%s:%sz%s:%srHrNz%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_implsrr)rlrF�get_entity_digestr�r�rTrSrhr)r|r�r]r#rdrirjrl�H�KDrrFZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr/rIrIrJr[!sV


	
		
(
z+AbstractDigestAuthHandler.get_authorizationcsb|dkrdd��n+|dkr6dd��ntd|���fdd�}�|fS)NrkcSstj|jd��j�S)Nr)raZmd5rrc)�xrIrIrJr`sz?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAcSstj|jd��j�S)Nr)rarbrrc)rrrIrIrJrbsz.Unsupported digest authentication algorithm %rcs�d||f�S)Nz%s:%srI)re�d)rprIrJrgs)r?)r|rjrqrI)rprJrn]s
z-AbstractDigestAuthHandler.get_algorithm_implscCsdS)NrI)r|rFr]rIrIrJrojsz+AbstractDigestAuthHandler.get_entity_digest)r�r�r�rrUrDrWrhr[rnrorIrIrIrJr&�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.
    rIi�cCs9t|j�d}|jd|||�}|j�|S)NrNzwww-authenticate)rrlrDrU)r|r�r`r�r�rarr�retryrIrIrJrPys
	
z$HTTPDigestAuthHandler.http_error_401N)r�r�r�r�rEr�rPrIrIrIrJr'osc@s(eZdZdZdZdd�ZdS)r(zProxy-Authorizationi�cCs/|j}|jd|||�}|j�|S)Nzproxy-authenticate)rrrDrU)r|r�r`r�r�rarrrtrIrIrJrQ�s
		
z%ProxyDigestAuthHandler.http_error_407N)r�r�r�rEr�rQrIrIrIrJr(�sc@sCeZdZddd�Zdd�Zdd�Zdd	�Zd
S)�AbstractHTTPHandlerrcCs
||_dS)N)�_debuglevel)r|�
debuglevelrIrIrJr�szAbstractHTTPHandler.__init__cCs
||_dS)N)rv)r|�levelrIrIrJ�set_http_debuglevel�sz'AbstractHTTPHandler.set_http_debuglevelc
Cs�|j}|std��|jdk	r�|j}t|t�rTd}t|��|jd�ss|jdd�|jd�s�yt|�}Wn@tk
r�t|t	j
�r�tdt|�|f��Yn"X|jddt
|�|j�|}|j�r/t|j�\}}t|�\}}	|jd�sN|jd|�xE|jjD]7\}
}|
j�}
|j|
�s[|j|
|�q[W|S)	Nz
no host givenzLPOST data should be bytes or an iterable of bytes. It cannot be of type str.zContent-typez!application/x-www-form-urlencodedzContent-lengthzBContent-Length should be specified for iterable data of type %r %rz%drk)rrrrFr�r�r�r�r��
memoryview�collections�Iterabler?r�r\�itemsizer�rr�rr�r�r�)r|rqrrrFr�ZmvZsel_hostrZselZsel_pathrWr~rIrIrJ�do_request_�sB		
	zAbstractHTTPHandler.do_request_c
s�|j}|std��||d|j|�}|j|j�t|j���jt�fdd�|jj	�D���d�d<tdd��j	�D���|j
r�i}d}|�kr��|||<�|=|j|j
d	|�yjy&|j|j
�|j|j��Wn1tk
rR}zt|��WYd
d
}~XnX|j�}	Wn|j��YnX|jr�|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 givenrGc3s-|]#\}}|�kr||fVqdS)NrI)r�r�r�)rarIrJr��s	z.AbstractHTTPHandler.do_open.<locals>.<genexpr>r��
Connectioncss'|]\}}|j�|fVqdS)N)�title)r�rWr�rIrIrJr��szProxy-AuthorizationraN)rrrrGZset_debuglevelrvr�rtr�rarwrvZ
set_tunnelrqr�r�rFri�getresponser�Zsockr�rE�reasonr�)
r|Z
http_classr�Zhttp_conn_argsrrr�Ztunnel_headersZproxy_auth_hdr�errrrI)rarJr��s>	/
	&
	
	zAbstractHTTPHandler.do_openN)r�r�r�rryr~r�rIrIrIrJru�s(ruc@s%eZdZdd�ZejZdS)r)cCs|jtjj|�S)N)r�r�r��HTTPConnection)r|r�rIrIrJ�	http_openszHTTPHandler.http_openN)r�r�r�r�rur~rKrIrIrIrJr)�sr�c@s:eZdZddddd�Zdd�ZejZdS)rBrNcCs&tj||�||_||_dS)N)rur�_context�_check_hostname)r|rwr>�check_hostnamerIrIrJr
s	zHTTPSHandler.__init__cCs(|jtjj|d|jd|j�S)Nr>r�)r�r�r�r�r�r�)r|r�rIrIrJ�
https_openszHTTPSHandler.https_open)r�r�r�rr�rur~rOrIrIrIrJrBsrBc@sCeZdZddd�Zdd�Zdd�ZeZeZdS)rNcCs4ddl}|dkr'|jj�}||_dS)Nr)Zhttp.cookiejar�	cookiejarZ	CookieJar)r|r�r�rIrIrJrszHTTPCookieProcessor.__init__cCs|jj|�|S)N)r�Zadd_cookie_header)r|rqrIrIrJrKsz HTTPCookieProcessor.http_requestcCs|jj||�|S)N)r�Zextract_cookies)r|rqr�rIrIrJr�"sz!HTTPCookieProcessor.http_response)r�r�r�rrKr�rOr�rIrIrIrJrs
c@seZdZdd�ZdS)r.cCs|j}td|��dS)Nzunknown url type: %s)r�r)r|r�r�rIrIrJr�*s	zUnknownHandler.unknown_openN)r�r�r�r�rIrIrIrJr.)scCsmi}x`|D]X}|jdd�\}}|ddkr[|ddkr[|dd�}|||<q
W|S)z>Parse list of key=value strings where keys are not duplicated.�=rNrr9rOrO)r;)�lZparsedZeltr�r�rIrIrJrX.s
 rXcCs�g}d}d}}x�|D]�}|r<||7}d}q|ry|dkrZd}qn|dkrld}||7}q|dkr�|j|�d}q|dkr�d}||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.
    rjF�\Tr9�,cSsg|]}|j��qSrI)rJ)r��partrIrIrJr!as	z#parse_http_list.<locals>.<listcomp>)rY)re�resr��escaper	ZcurrIrIrJrZ8s4	





rZc@s:eZdZdd�ZdZdd�Zdd�ZdS)r*cCs�|j}|dd�dkrq|dd�dkrq|jrq|jdkrq|j|j�kr~td��n
|j|�SdS)Nr�z//r:r��	localhostz-file:// scheme is supported only on localhost)r�rr�	get_namesr�open_local_file)r|r�rErIrIrJ�	file_openes	5zFileHandler.file_openNcCsztjdkrsy7ttjd�dtjtj��d�t_Wn*tjk
rrtjd�ft_YnXtjS)Nr�r�)r*�namesr"r��gethostbyname_ex�gethostname�gaierror�
gethostbyname)r|rIrIrJr�ps$zFileHandler.get_namescCs^ddl}ddl}|j}|j}t|�}y�tj|�}|j}|jj	|j
dd�}	|j|�d}
|jd|
p�d||	f�}|r�t
|�\}}|s�|rt|�|j�kr|r�d||}
n
d|}
tt|d�||
�SWn1tk
rM}zt|��WYdd}~XnXtd��dS)	Nr�usegmtTz6Content-type: %s
Content-length: %d
Last-modified: %s
z
text/plainzfile://�rbzfile not on local host)�email.utils�	mimetypesrrr�r4rS�stat�st_size�utils�
formatdate�st_mtime�
guess_type�message_from_stringr
�_safe_gethostbynamer�rrDrir)r|r��emailr�rrr^Z	localfile�statsre�modified�mtyperar,Zorigurl�exprIrIrJr�{s0			
zFileHandler.open_local_file)r�r�r�r�r�r�r�rIrIrIrJr*cs
cCs1ytj|�SWntjk
r,dSYnXdS)N)r�r�r�)rrrIrIrJr��sr�c@s(eZdZdd�Zdd�ZdS)r+cCs�ddl}ddl}|j}|s3td��t|�\}}|dkr]|j}nt|�}t|�\}}|r�t|�\}}nd}t	|�}|p�d}|p�d}yt
j|�}Wn1tk
r}zt|��WYdd}~XnXt
|j�\}	}
|	jd�}ttt	|��}|dd�|d}}|r||dr||dd�}y|j||||||j�}
|r�dp�d}xJ|
D]B}t|�\}}|j�dkr�|dkr�|j�}q�W|
j||�\}}d}|j|j�d}|rG|d
|7}|dk	rm|dkrm|d|7}tj|�}t|||j�SWnQ|jk
r�}z.td|�}|jtj �d��WYdd}~XnXdS)Nrzftp error: no host givenrjr�rNrN�Dr��a�Ar�rszContent-type: %s
zContent-length: %d
z
ftp error: %rr�rOrO)r�r�r�rNrsr�)!�ftplibr�rrrr
�FTP_PORTrZrrr
r�r�rirr�r;r��map�connect_ftprGrrp�upper�retrfiler�rlr�r�r�
all_errors�with_traceback�sys�exc_info)r|r�r�r�rrr,rrr�rT�attrs�dirsrKZfwr��attrr~r`�retrlenrar�r��excrIrIrJ�ftp_open�s\	!
zFTPHandler.ftp_openc	Cst||||||dd�S)N�
persistentF)�
ftpwrapper)r|rrrrr,r�rGrIrIrJr��szFTPHandler.connect_ftpN)r�r�r�r�r�rIrIrIrJr+�s5c@sXeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)r,cCs1i|_i|_d|_d|_d|_dS)Nr�<r^)�cacherG�soonest�delay�	max_conns)r|rIrIrJr�s
				zCacheFTPHandler.__init__cCs
||_dS)N)r�)r|�trIrIrJ�
setTimeout�szCacheFTPHandler.setTimeoutcCs
||_dS)N)r�)r|r�rIrIrJ�setMaxConns�szCacheFTPHandler.setMaxConnscCs�|||dj|�|f}||jkrJtj�|j|j|<n<t||||||�|j|<tj�|j|j|<|j�|j|S)Nr�)�joinr�r_r�rGr��check_cache)r|rrrrr,r�rGr}rIrIrJr��s
zCacheFTPHandler.connect_ftpcCstj�}|j|krrxTt|jj��D]=\}}||kr1|j|j�|j|=|j|=q1Wtt|jj���|_t	|j�|j
krxGt|jj��D]0\}}||jkr�|j|=|j|=Pq�Wtt|jj���|_dS)N)r_r�r�rGrwr�r��min�valuesr\r�)r|r�r�r�rIrIrJr��s"
"

zCacheFTPHandler.check_cachecCsBx!|jj�D]}|j�qW|jj�|jj�dS)N)r�r�r��clearrG)r|�connrIrIrJ�clear_caches
zCacheFTPHandler.clear_cacheN)	r�r�r�rr�r�r�r�r�rIrIrIrJr,�sc@seZdZdd�ZdS)r-cCs�|j}|jdd�\}}|jdd�\}}t|�}|jd�rstj|�}|dd�}|sd}tjd|t|�f�}t	t
j|�||�S)	N�:rNr�z;base64�ztext/plain;charset=US-ASCIIz$Content-type: %s
Content-length: %d
i����)rlr;r�endswithr�decodebytesr�r�r\r�io�BytesIO)r|r�rErrFZ	mediatyperarIrIrJ�	data_opens
		zDataHandler.data_openN)r�r�r�r�rIrIrIrJr-sr��nt)r4r3cCs
t|�S)zOS-specific conversion from a relative URL of the 'file' scheme
        to a file system path; not recommended for general use.)r
)�pathnamerIrIrJr42scCs
t|�S)zOS-specific conversion from a file system path to a relative URL
        of the 'file' scheme; not recommended for general use.)r	)r�rIrIrJr37sc@s1eZdZdZdZdeZddd�Zdd�Zdd	�Z	d
d�Z
dd
�Zddd�Zddd�Z
ddd�Zddddd�Zdd�Zddd�Zddd�Zdd�Zer�dd�Zdd d!�Zd"d#�Zd$d%�Zd&d'�Zdd(d)�ZdS)*r8a,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/%scKs�dd|jji}tj|tdd�|dkrAt�}||_|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�class�
stacklevelr:�key_file�	cert_filez
User-Agent�Accept�*/*)r�r�)r5r�r?r@�DeprecationWarningr5rr�r�r��versionr��_URLopener__tempfilesrSrh�_URLopener__unlink�	tempcache�ftpcache)r|rZx509r�rIrIrJrMs				zURLopener.__init__cCs|j�dS)N)r�)r|rIrIrJ�__del__gszURLopener.__del__cCs|j�dS)N)�cleanup)r|rIrIrJr�jszURLopener.closecCsm|jrSx7|jD],}y|j|�Wqtk
r>YqXqW|jdd�=|jri|jj�dS)N)r�r�rir�r�)r|rKrIrIrJr�ms	
		zURLopener.cleanupcGs|jj|�dS)zdAdd a header to be used by the HTTP interface only
        e.g. u.addheader('Accept', 'sound/basic')N)r�rY)r|r�rIrIrJ�	addheader{szURLopener.addheadercCs�tt|��}t|dd�}|jrn||jkrn|j|\}}t|d�}t|||�St|�\}}|s�d}||jkr�|j|}t|�\}}	t|	�\}
}|
|f}nd}d|}||_	|j
dd�}t||�s"|d	krK|r;|j|||�S|j
||�Sy9|dkrmt||�|�St||�||�SWnattfk
r��YnGtk
r�}
z'td
|
�jtj�d��WYdd}
~
XnXdS)z6Use URLopener().open(file) instead of open(file, 'r').r�z%/:=&?~#+!$,;'@()*[]|r�rKNZopen_�-r�r�zsocket errorr�)rrr	r�rDrrrrr�r�r��open_unknown_proxy�open_unknownr�rrrir�r�r�)r|r�rFr^rar`�urltyperEr�	proxyhostrrr�rWr�rIrIrJrD�s<

	zURLopener.opencCs(t|�\}}tdd|��dS)z/Overridable interface to open unknown URL type.z	url errorzunknown url typeN)rri)r|r�rFr�rErIrIrJr��szURLopener.open_unknowncCs,t|�\}}tdd||��dS)z/Overridable interface to open unknown URL type.z	url errorzinvalid proxy for %sN)rri)r|rr�rFr�rErIrIrJr��szURLopener.open_unknown_proxyc Cs�tt|��}|jr5||jkr5|j|St|�\}}|dkr�|sf|dkr�yC|j|�}|j�}|j�tt|�d�|fSWn%t	k
r�}	zWYdd}	~	XnX|j
||�}z�|j�}
|r
t
|d�}n�ddl}t|�\}
}t|p4d�\}
}t|pLd�\}}
t
|pdd�\}}
tjj|�d}|j|�\}}|jj|�tj|d�}z�||
f}|jdk	r�||j|<d}d
}d}d}d	|
krt|
d
�}|r2||||�xW|j|�}|sKP|t|�7}|j|�|d7}|r5||||�q5WWd|j�XWd|j�X|dkr�||kr�td||f|��|S)ztretrieve(url) returns (filename, headers) for a local object
        or (tempfilename, headers) for a remote object.NrKrNrLrrjirMzcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytesi rO)rrr�rr�rRr�r4rrirDrVrrrSrT�splitextZmkstempr�rY�fdopenrZr[r\r]r)r|rEr^r_rFr�Zurl1r`r�r�rarbrVZgarbagerT�suffix�fdrcrdrer[rfrgrIrIrJ�retrieve�sl



zURLopener.retrievecCs�d}d}t|t�rZt|�\}}|rQt|�\}}t|�}|}n�|\}}t|�\}}t|�\}	}
|
}d}|	j�dkr�d}nUt|
�\}}
|r�t|�\}}|r�d|	||
f}t|�r|}|stdd��|rNt|�}t	j
|j��jd�}nd}|r�t|�}t	j
|j��jd�}nd}||�}
i}|r�d||d<|r�d||d	<|r�||d
<d|d<x!|j
D]\}}|||<q�W|dk	r4d
|d<|
jd|||�n|
jd|d|�y|
j�}Wn$tjjk
r�td��YnXd|jko�dknr�t||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-AuthorizationrIrkr�rz!application/x-www-form-urlencodedzContent-Typer�r�raz$http protocol error: bad status liner�i,zhttp:)r�r�rrr
rrprrirrrrr�rqr�r�r�Z
BadStatusLinerZstatusrr��
http_errorr`r�)r|Zconnection_factoryrErFZuser_passwdZproxy_passwdrrr�Zrealhostr�r�Z
proxy_authrHZ	http_connra�headerr~r�rIrIrJ�_open_generic_http�sr
		!!



	zURLopener._open_generic_httpcCs|jtjj||�S)zUse HTTP protocol.)r�r�r�r�)r|rErFrIrIrJ�	open_httpNszURLopener.open_httpc
Cs�d|}t||�rtt||�}|dkrO||||||�}	n|||||||�}	|	rt|	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|rEr`�errcode�errmsgrarFrWr{rcrIrIrJr�Rs
zURLopener.http_errorcCs&|j�t||||d��dS)z>Default error handler: close the connection and raise OSError.N)r�r)r|rEr`r�r�rarIrIrJr�bs
zURLopener.http_error_defaultcCs"tjj|d|jd|j�S)Nr�r�)r�r�r�r�r�)r|rrrIrIrJ�_https_connectionhs	zURLopener._https_connectioncCs|j|j||�S)zUse HTTPS protocol.)r�r�)r|rErFrIrIrJ�
open_httpsmszURLopener.open_httpscCs�t|t�std��|dd�dkrr|dd�dkrr|dd�j�dkrrtd	��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�rrpr?r�)r|rErIrIrJ�	open_fileqs
HzURLopener.open_filecCs�ddl}ddl}t|�\}}t|�}ytj|�}Wn:tk
r�}zt|j|j	��WYdd}~XnX|j
}	|jj|j
dd�}
|j|�d}|jd|p�d|	|
f�}|s$|}
|dd�dkrd	|}
tt|d
�||
�St|�\}}|r�tj|�t�ft�kr�|}
|dd�dkr�d	|}
n&|dd�dkr�td
|��tt|d
�||
�Std��dS)zUse local file.rNr�Tz6Content-Type: %s
Content-Length: %d
Last-modified: %s
z
text/plainrNr�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r4rSr�rir�strerrorr^r�r�r�r�r�r�rrDr
r�r�r��thishostr?)r|rEr�r�rrrKZ	localnamer��erer�r�raZurlfiler,rIrIrJr�zs:(	
"
zURLopener.open_local_filecCsst|t�std��ddl}t|�\}}|sKtd��t|�\}}t|�\}}|r�t|�\}}nd}t|�}t|p�d�}t|p�d�}t	j
|�}|s�ddl}|j}nt
|�}t|�\}}	t|�}|jd�}
|
dd�|
d}
}|
rb|
drb|
dd�}
|
r}|
dr}d|
d<|||dj|
�f}t|j�tkr�xDt|j�D]3}
|
|kr�|j|
}|j|
=|j�q�Wy-||jkr%t|||||
�|j|<|s4d}nd	}xJ|	D]B}t|�\}}|j�d
krA|dkrA|j�}qAW|j|j||�\}}|jd|�d}d}|r�|d|7}|dk	r�|dkr�|d|7}tj|�}t||d|�SWnKt�k
rn}z(td|�j t!j"�d��WYdd}~XnXdS)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedrNzftp error: no host givenrjr�rNr�rNr�r�r�r�rszftp:zContent-Type: %s
zContent-Length: %d
zftp error %rr�rOrO)r�r�r�rNrsr�)#r�r�rr�rr
rrr
r�r�r�r�rZrr;r�r\r��MAXFTPCACHEr�r�r�rrpr�r�r�r�r�r�	ftperrorsr�r�r�)r|rEr�rrrTr,rrr�r�r�rKr}r�r�r�r�r~r`r�r�rar�rIrIrJ�open_ftp�sp


	
zURLopener.open_ftpc	Cs�t|t�std��y|jdd�\}}Wn!tk
rZtdd��YnX|sgd}|jd�}|dkr�d	||d
�kr�||dd
�}|d
|�}nd}g}|jdtj	d
tj
tj����|jd|�|dkr8tj|j
d��jd�}nt|�}|jdt|��|jd�|j|�dj|�}tj|�}tj|�}t|||�S)zUse "data" URL.zEdata error: proxy support for data protocol currently not implementedr�rNz
data errorzbad data URLztext/plain;charset=US-ASCII�;rr�NrjzDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %srrzlatin-1zContent-Length: %d�
)r�r�rr;r?ri�rfindrYr_ZstrftimeZgmtimerr�rrr
r\r�r�r�r��StringIOr)	r|rErFr�Zsemir�r�ra�frIrIrJ�	open_data�s6
"$

zURLopener.open_data)r�r�r�r�r�r�r�rr�r�r�r�rDr�r�r�r�r�r�r�r@r�r�r�r�rrrIrIrIrJr8@s.
$B\	 :c@s�eZdZdZdd�Zdd�Zddd�Zd	d
�Zddd�Zdd
d�Z	ddd�Z
dddd�Zdddd�Zddd�Z
ddd�Zddd�Zddd�Zddd �Zd!d"�ZdS)#r9z?Derived class with handlers for errors we can handle (perhaps).cOs2tj|||�i|_d|_d|_dS)Nrr�)r8r�
auth_cache�tries�maxtries)r|r�r4rIrIrJrs		zFancyURLopener.__init__cCst||d||�S)z3Default error handling -- don't raise an exception.zhttp:)r)r|rEr`r�r�rarIrIrJr�sz!FancyURLopener.http_error_defaultNc	
Cs�|jd7_z{|jrg|j|jkrgt|d�rH|j}n	|j}|||dd|�S|j||||||�}|SWdd|_XdS)z%Error 302 -- relocated (temporarily).rN�http_error_500i�z)Internal Server Error: Redirect RecursionNr)r	r
r�rr��redirect_internal)	r|rEr`r�r�rarFr�rcrIrIrJrs	zFancyURLopener.http_error_302c	Cs�d|kr|d}nd|kr2|d}ndS|j�t|jd||�}t|�}|jd	kr�t|||d|||��|j|�S)
Nr�r�r�r�r�r�rjz( Redirection to url '%s' is not allowed.)r�r�r�rj)r�rr�rrrrD)	r|rEr`r�r�rarFr�rrIrIrJrs


	z FancyURLopener.redirect_internalcCs|j||||||�S)z*Error 301 -- also relocated (permanently).)r)r|rEr`r�r�rarFrIrIrJr9szFancyURLopener.http_error_301cCs|j||||||�S)z;Error 303 -- also relocated (essentially identical to 302).)r)r|rEr`r�r�rarFrIrIrJr	=szFancyURLopener.http_error_303cCsE|dkr(|j||||||�S|j|||||�SdS)z1Error 307 -- relocated, but turn POST into error.N)rr�)r|rEr`r�r�rarFrIrIrJr
AszFancyURLopener.http_error_307Fc
Csd|kr(tj||||||�|d}tjd|�}	|	sftj||||||�|	j�\}
}|
j�dkr�tj||||||�|s�tj||||||�d|jd}|dkr�t||�||�St||�|||�SdS)z_Error 401 -- authentication required.
        This function supports Basic authentication only.zwww-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r8Zretry_�_basic_authN)r8r�rL�matchr>rpr�r�)
r|rEr`r�r�rarFrt�stuffrrr#rWrIrIrJrPHs&



zFancyURLopener.http_error_401c
Csd|kr(tj||||||�|d}tjd|�}	|	sftj||||||�|	j�\}
}|
j�dkr�tj||||||�|s�tj||||||�d|jd}|dkr�t||�||�St||�|||�SdS)zeError 407 -- proxy authentication required.
        This function supports Basic authentication only.zproxy-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r8Zretry_proxy_r
N)r8r�rLrr>rpr�r�)
r|rEr`r�r�rarFrtrrrr#rWrIrIrJrQas&



zFancyURLopener.http_error_407cCst|�\}}d||}|jd}t|�\}}	t|	�\}	}
|	jd�d}|	|d�}	|j|	||�\}}
|p�|
s�dSdt|dd�t|
dd�|	f}	d|	|
|jd<|dkr�|j|�S|j||�SdS)Nzhttp://r��@rNz%s:%s@%sr�rj)rrrr��get_user_passwdr	rD)r|rEr#rFrrr�r�rr�r��
proxyselectorr�rrrIrIrJ�retry_proxy_http_basic_authzs 

z*FancyURLopener.retry_proxy_http_basic_authcCst|�\}}d||}|jd}t|�\}}	t|	�\}	}
|	jd�d}|	|d�}	|j|	||�\}}
|p�|
s�dSdt|dd�t|
dd�|	f}	d|	|
|jd<|dkr�|j|�S|j||�SdS)Nzhttps://r�rrNz%s:%s@%sr�rj)rrrr�rr	rD)r|rEr#rFrrr�r�rr�r�rr�rrrIrIrJ�retry_proxy_https_basic_auth�s 

z+FancyURLopener.retry_proxy_https_basic_authc
Cs�t|�\}}|jd�d}||d�}|j|||�\}}|pY|s`dSdt|dd�t|dd�|f}d||}	|dkr�|j|	�S|j|	|�SdS)NrrNz%s:%s@%sr�rjzhttp://)rr�rr	rD)
r|rEr#rFrrr�r�rrr�rIrIrJrB�s
z$FancyURLopener.retry_http_basic_authc
Cs�t|�\}}|jd�d}||d�}|j|||�\}}|pY|s`dSdt|dd�t|dd�|f}d||}	|dkr�|j|	�S|j|	|�SdS)NrrNz%s:%s@%sr�rjzhttps://)rr�rr	rD)
r|rEr#rFrrr�r�rrr�rIrIrJ�retry_https_basic_auth�s
z%FancyURLopener.retry_https_basic_authrcCs�|d|j�}||jkrA|r6|j|=n|j|S|j||�\}}|se|rx||f|j|<||fS)Nr)rpr�prompt_user_passwd)r|rrr#r�r}rrrIrIrJr�s
zFancyURLopener.get_user_passwdcCspddl}y@td||f�}|jd|||f�}||fSWntk
rkt�dSYnXdS)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|rrr#rrrrIrIrJr�s	
z!FancyURLopener.prompt_user_passwd)r�r�r�r�rr�rrrr	r
rPrQrrrBrrrrIrIrIrJr9�s$cCstdkrtjd�atS)z8Return the IP address of the magic hostname 'localhost'.Nr�)�
_localhostr�r�rIrIrIrJr��sr�cCsdtdkr`y#ttjtj��d�aWn.tjk
r_ttjd�d�aYnXtS)z,Return the IP addresses of the current host.Nr�r�)�	_thishostr"r�r�r�r�rIrIrIrJr��s#r�cCs%tdkr!ddl}|jatS)z1Return the set of errors raised by the FTP class.Nr)�
_ftperrorsr�r�)r�rIrIrJr�s	rcCstdkrtjd�atS)z%Return an empty email Message object.Nrj)�
_noheadersr�r�rIrIrIrJ�	noheaders�src@speZdZdZdddd�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	Csr||_||_||_||_||_||_d|_||_y|j�Wn|j	��YnXdS)Nr)
rrrrr,r�rG�refcount�	keepalive�initr�)r|rrrrr,r�rGr�rIrIrJr�s								
zftpwrapper.__init__cCs�ddl}d|_|j�|_|jj|j|j|j�|jj|j	|j
�dj|j�}|jj
|�dS)Nrr�)r��busyZFTPr�Zconnectrrr,rGZloginrrr�r��cwd)r|r�Z_targetrIrIrJr"	s	zftpwrapper.initc-Cs1ddl}|j�|dkr1d}d}nd|}d}y|jj|�Wn/|jk
r�|j�|jj|�YnXd}|r*|r*y&d|}|jj|�\}}Wng|jk
r)}zDt|�dd�d	krt	d
|�j
tj�d��WYdd}~XnX|s�|jjd�|r�|jj
�}	zVy|jj|�Wn;|jk
r�}zt	d
|�|�WYdd}~XnXWd|jj|	�Xd|}nd
}|jj|�\}}d|_t|jd�|j�}
|jd7_|j�|
|fS)Nrrsr�zTYPE ArNzTYPE zRETR r:Z550z
ftp error: %rr�zLIST ZLISTr�)rsr�)r��endtransferr�Zvoidcmdr�r"ZntransfercmdZ
error_permr�rr�r�r��pwdr$r#r�makefile�
file_closer r�)r|rKr�r��cmd�isdirr�r�r�r&ZftpobjrIrIrJr�	sN
	



&*
	
zftpwrapper.retrfilecCs
d|_dS)Nr)r#)r|rIrIrJr%D	szftpwrapper.endtransfercCs&d|_|jdkr"|j�dS)NFr)r!r �
real_close)r|rIrIrJr�G	s	zftpwrapper.closecCs@|j�|jd8_|jdkr<|jr<|j�dS)NrNr)r%r r!r+)r|rIrIrJr(L	s
zftpwrapper.file_closecCs7|j�y|jj�Wnt�k
r2YnXdS)N)r%r�r�r)r|rIrIrJr+R	s

zftpwrapper.real_close)r�r�r�r�rr"r�r%r�r(r+rIrIrIrJr��s	-r�cCs�i}xYtjj�D]H\}}|j�}|r|dd�dkr|||dd�<qWdtjkr�|jdd�xvtjj�D]e\}}|dd�dkr�|j�}|r�|||dd	�<q�|j|dd
�d�q�W|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����)rS�environrwrpr�)rrWr~rIrIrJ�getproxies_environmentZ	s	r/c
Cs�|dkrt�}y|d}Wntk
r;dSYnX|dkrLdSt|�\}}dd�|jd�D�}xp|D]h}|r�|jd	�}tj|�}d
|}tj||tj�s�tj||tj�r�dSq�WdS)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�*rNcSsg|]}|j��qSrI)rJ)r�rrIrIrJr!�	s	z,proxy_bypass_environment.<locals>.<listcomp>r��.z
(.+\.)?%s$)	r/rmr
r;�lstriprLr�rrN)rrrZno_proxy�hostonlyr,Z
no_proxy_listrW�patternrIrIrJ�proxy_bypass_environmenty	s&	
	

r6c
Cszddlm}t|�\}}dd�}d|krH|drHdSd}x%|jd	f�D]}|spqatjd
|�}|dk	r_|dkr�ytj|�}||�}Wntk
r�waYnX||jd��}	|jd�}
|
dkr$d
|jd�j	d�d}
nt
|
dd��}
d|
}
||
?|	|
?krrdSqa|||�radSqaWdS)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)�fnmatchcSs�|jd�}ttt|��}t|�dkrV|ddddgdd�}|dd>|dd>B|dd>B|d	BS)
Nr2r�r�rNr^r�rMr:)r;r�r�rZr\)ZipAddrr+rIrIrJ�ip2num�	s
 z,_proxy_bypass_macosx_sysconf.<locals>.ip2numr2Zexclude_simpleTN�
exceptionsz(\d+(?:\.\d+)*)(/\d+)?rNr�rM� F)r7r
r�rLrr�r�ri�group�countrZ)rr�proxy_settingsr7r4r,r9ZhostIPr~r�r/�maskrIrIrJ�_proxy_bypass_macosx_sysconf�	s:

#
r@�darwin)�_get_proxy_settings�_get_proxiescCst�}t||�S)N)rBr@)rrr>rIrIrJ�proxy_bypass_macosx_sysconf�	s	rDcCst�S)z�Return a dictionary of scheme -> proxy server URL mappings.

        This function uses the MacOSX framework SystemConfiguration
        to fetch the proxy information.
        )rCrIrIrIrJ�getproxies_macosx_sysconf�	srEcCs*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)r/r6rD)rrrrIrIrJr�	s	
rcCst�pt�S)N)r/rErIrIrIrJr5�	scCsfi}yddl}Wntk
r.|SYnXy|j|jd�}|j|d�d}|r9t|j|d�d�}d|kr�x�|jd�D]J}|jdd�\}}tjd	|�s�d
||f}|||<q�WnM|dd�dkr||d
<n*d||d
<d||d<d||d<|j	�Wnt
ttfk
raYnX|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�rrNz^([^/:]+)://z%s://%srVzhttp:r�z	http://%sz
https://%sr�zftp://%sr�)
�winreg�ImportError�OpenKey�HKEY_CURRENT_USER�QueryValueExr�r;rLrZCloserir?r�)rrG�internetSettings�proxyEnableZproxyServer�pr�ZaddressrIrIrJ�getproxies_registry�	s8
			

rOcCst�pt�S)z�Return a dictionary of scheme -> proxy server URL mappings.

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

        )r/rOrIrIrIrJr5(
sc&Cs�yddl}Wntk
r(dSYnXyK|j|jd�}|j|d�d}t|j|d�d�}Wntk
r�dSYnX|s�|r�dSt|�\}}|g}y,tj	|�}||kr�|j
|�Wntk
r�YnXy,tj|�}||kr&|j
|�Wntk
r;YnX|jd�}x�|D]�}	|	dkrtd|krtdS|	j
dd	�}	|	j
d
d�}	|	j
dd�}	x*|D]"}
tj|	|
tj�r�dSq�WqRWdS)
Nrz;Software\Microsoft\Windows\CurrentVersion\Internet SettingsrFZ
ProxyOverriderz<local>r2rNz\.r1z.*�?)rGrHrIrJrKr�rir
r�r�rYZgetfqdnr;r�rLrrN)rrrGrLrMZ
proxyOverrideZrawHostr,ZaddrZfqdnr0r�rIrIrJ�proxy_bypass_registry1
sR
			

		



rQcCs*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)r/r6rQ)rrrrIrIrJrc
s	
)r�rr�r�raZhttp.clientr�r�rSr-rLr�rr�r_r{rVrPr?Zurllib.errorrrrZurllib.parserrrrr	r
rrr
rrrrrrrrrZurllib.responserrrArHr@�__all__r�r�rCr�r0r1rXr6r7rM�ASCIIrnrsrrr2rr/rrrrr r!r"r#r$r%�urandomr`r&r'r(rur)r�r�rBrYrr.rXrZr*r�r+r,r-r�rWZ
nturl2pathr4r3r�r8r9rr�rr�rrrrr�r/r6r@�platformZ_scproxyrBrCrDrErr5rOrQrIrIrIrJ�<module>Ds�v
					!?n�$q*@
P	�r


+3:5!���

_#<

-	2

?>