o ,]x@sdZddlmZmZmZmZddlmZmZm Z m Z m Z m 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/m0Z0m1Z1dd l2m3Z3m4Z4ddl5Z5ddl6Z6ddl7Z7ddl8Z8ddl9Z9ddl:Z:ddl;Z;ddlZ>dd lmZerdd l?m@Z@ndd lAm@Z@z ddlBZBddlBmCZCWn eDydZEYnwdZEgdZFe:jGddZHdaIde9jJfddZKddZLgZMdddZNddZOere8Pde8jQZRne8PdZRddZSGdddeTZUGd d!d!eTZVd"d#ZWGd$d%d%eTZXGd&d'd'eXZYGd(d)d)eXZZGd*d+d+eXZ[d,d-Z\Gd.d/d/eXZ]Gd0d1d1eTZ^Gd2d3d3e^Z_Gd4d5d5eTZ`Gd6d7d7e`eXZaGd8d9d9e`eXZbe6jcZdGd:d;d;eTZeGdd?d?eXeeZgGd@dAdAeXZhGdBdCdCehZiejedDrGdEdFdFehZkeFldFGdGdHdHeXZmGdIdJdJeXZndKdLZodMdNZpGdOdPdPeXZqdQdRZrGdSdTdTeXZsGdUdVdVesZtdWZue6jvdXkrddYlwmxZxmyZyndZd[Zxd\d]ZyiZzGd^d_d_eTZ{Gd`dadae{Z|da}dbdcZ~dadddeZdadfdgZdadhdiZGdjdkdkeTZdldmZdndoZdpdqZe:jdrkrdddslmZmZdtduZdvdwZdxdyZdzd{ZdSe6jvdXkr|d|d}Zd~d{ZddZddyZdSeZeZdS)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_importdivisionprint_functionunicode_literals)bytesdictfilterinputintmapopenstr)PY2PY3raise_with_tracebackN)email)client)URLError HTTPErrorContentTooShortError)urlparseurlspliturljoinunwrapquoteunquote splittype splithost splitport splituser splitpasswd splitattr splitquery splitvaluesplittagto_bytes urlunparse) addinfourl addclosehook)r)Iterable) SSLContextFT)RequestOpenerDirector BaseHandlerHTTPDefaultErrorHandlerHTTPRedirectHandlerHTTPCookieProcessor ProxyHandlerHTTPPasswordMgrHTTPPasswordMgrWithDefaultRealmAbstractBasicAuthHandlerHTTPBasicAuthHandlerProxyBasicAuthHandlerAbstractDigestAuthHandlerHTTPDigestAuthHandlerProxyDigestAuthHandler HTTPHandler FileHandler FTPHandlerCacheFTPHandlerUnknownHandlerHTTPErrorProcessorurlopeninstall_opener build_opener pathname2url url2pathname getproxies urlretrieve urlcleanup URLopenerFancyURLopenerc Ksd|vr |d}|d=nd}d|vr|d}|d=nd}d|vr(|d}|d=nd}|s0|s0|rbts6tdttj}|jtjO_tj|_|sL|rS| ||n| t |dd}t |} n t durlt a } nt } | |||S)N cadefaultFcapathcafilezSSL support not availableTcontextcheck_hostname) _have_ssl ValueErrorsslr+PROTOCOL_SSLv23options OP_NO_SSLv2 CERT_REQUIRED verify_modeload_verify_locationsset_default_verify_paths HTTPSHandlerrC_openerr ) urldatatimeout _3to2kwargsrLrMrNrP https_handleropenerrdA/usr/lib/python3/dist-packages/future/backports/urllib/request.pyrAs*     rAcCs|adSN)r])rcrdrdrerBsrBc Csbt|\}}tt||}|}|dkr(|s(tj||fWdS|r0t|d}nt j dd}|j }t ||G||f} d} d} d} d} d |vrWt|d } |r_|| | |  || }|shn| t|7} ||| d 7} |r|| | | q`Wdn1swYWdn1swY| dkr| | krtd | | 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. fileNwbF)delete rcontent-lengthContent-LengthTr1retrieval incomplete: got only %i out of %i bytes)r contextlibclosingrAinfoospathnormpathr tempfileNamedTemporaryFilename_url_tempfilesappendr readlenwriter)r^filename reporthookr_url_typersfpheaderstfpresultbssizerzblocknumblockrdrdrerGsV           &rGc CsDtD]}zt|WqtyYqwtdd=tr dadSdSrf)rxrrunlinkEnvironmentErrorr]) temp_filerdrdrerHs  rHz:\d+$cCs<|j}t|d}|dkr|dd}td|d}|S)zReturn request-host, as defined by RFC 2965. Variation from RFC: returned value is lowercased, for convenient comparison. rHost)full_urlr get_header _cut_port_resublower)requestr^hostrdrdre request_hosts   rc@seZdZdidddfddZddZddZd d Zd d Zd dZddZ ddZ ddZ ddZ ddZ ddZddZddZdd Zd!d"Zd#d$Zd)d%d&Zd'd(ZdS)*r,NFc Cst||_t|j\|_|_||_i|_d|_|D] \}}|||qi|_ |dur1t |}||_ ||_ ||_ |dSrf)rrr%fragmentr_r _tunnel_hostitems add_headerunredirected_hdrsrorigin_req_host unverifiablemethod_parse) selfr^r_rrrrkeyvaluerdrdre__init__s  zRequest.__init__cCsRt|j\|_}|jdurtd|jt|\|_|_|jr't|j|_dSdS)Nzunknown url type: %r)rrtyperSrrselectorr)rrestrdrdrer.s zRequest._parsecCs"|jdur|jS|jdurdSdS)z3Return a string indicating the HTTP request method.NPOSTGET)rr_rrdrdre get_method6s  zRequest.get_methodcCs|jr d|j|jfS|jS)Nz%s#%s)rrrrdrdre get_full_url?szRequest.get_full_urlcCsd}tj|tdd||_dS)Nz&Request.add_data method is deprecated.r stacklevelwarningswarnDeprecationWarningr_)rr_msgrdrdreadd_dataG zRequest.add_datacCsd}tj|tdd|jduS)Nz&Request.has_data method is deprecated.rrrrrrdrdrehas_dataLrzRequest.has_datacCd}tj|tdd|jS)Nz&Request.get_data method is deprecated.rrrrrdrdreget_dataQzRequest.get_datacCr)Nz&Request.get_type method is deprecated.rr)rrrrrrdrdreget_typeVrzRequest.get_typecCr)Nz&Request.get_host method is deprecated.rr)rrrrrrdrdreget_host[rzRequest.get_hostcCr)Nz*Request.get_selector method is deprecated.rr)rrrrrrdrdre get_selector`rzRequest.get_selectorcCr)Nz-Request.is_unverifiable method is deprecated.rr)rrrrrrdrdreis_unverifiableerzRequest.is_unverifiablecCr)Nz1Request.get_origin_req_host method is deprecated.rr)rrrrrrdrdreget_origin_req_hostjrzRequest.get_origin_req_hostcCs2|jdkr |js |j|_n||_|j|_||_dS)Nhttps)rrrrr)rrrrdrdre set_proxyqs   zRequest.set_proxycCs |j|jkSrf)rrrrdrdre has_proxyy zRequest.has_proxycC||j|<dSrf)r capitalizerrvalrdrdrer|zRequest.add_headercCrrf)rrrrdrdreadd_unredirected_headerrzRequest.add_unredirected_headercCs||jvp ||jvSrf)rr)r header_namerdrdre has_headers zRequest.has_headercCs|j||j||Srf)rgetr)rrdefaultrdrdrers zRequest.get_headercCs"|j}||jt|Srf)rcopyupdaterlistr)rhdrsrdrdre header_itemss   zRequest.header_itemsrf)__name__ __module__ __qualname__rrrrrrrrrrrrrrrrrrrrdrdrdrer,s.    r,c@sNeZdZddZddZddZddZd ejfd d Z dd d Z ddZ d S)r-cCs6dt}d|fg|_g|_i|_i|_i|_i|_dS)NPython-urllib/%sz User-agent) __version__ addheadershandlers handle_open handle_errorprocess_responseprocess_request)rclient_versionrdrdrers  zOpenerDirector.__init__c CsPt|ds tdt|d}t|D]}|dvrq|d}|d|}||dd}|dr`|d|d}||dd}zt|}Wn tyRYnw|j |i} | |j|<n|dkrj|}|j } n|d krt|}|j } n |d kr~|}|j } nq| |g} | rt| |n| |d }q|rt|j|||dSdS) N add_parentz%expected BaseHandler instance, got %rF)redirect_requestdo_open proxy_open_rerrorr responserT)hasattr TypeErrorrdirfind startswithr rSrrrrr setdefaultbisectinsortryrr) rhandleraddedmethiprotocol conditionjkindlookuprrdrdre add_handlersP          zOpenerDirector.add_handlercCdSrfrdrrdrdreclosezOpenerDirector.closec Gs<||d}|D]}t||}||}|dur|SqdS)Nrd)rgetattr) rchainr meth_nameargsrrfuncrrdrdre _call_chains  zOpenerDirector._call_chainNc Cst|tr |}t|trt||}n |}|dur||_||_|j}|d}|j |gD] }t ||}||}q.| ||} |d}|j |gD] }t ||}||| } qK| 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) isinstancerdecoder r,r_r`rrrr_openr) rfullurlr_r`reqrr processorrrrdrdrer s&        zOpenerDirector.opencCsP||jdd|}|r |S|j}||j||d|}|r|S||jdd|S)Nr default_openrunknown unknown_open)rrr)rrr_rrrdrdrers   zOpenerDirector._opencGs~|dvr|jd}|d}d|}d}|}n |j}|d}d}|||f|}|j|}|r/|S|r=|dd f|}|j|SdS) Nhttprr z http_error_%sr_errorrrhttp_error_default)rr)rprotorrrhttp_err orig_argsrrdrdrer s"   zOpenerDirector.errorrf) rrrrrrrsocket_GLOBAL_DEFAULT_TIMEOUTr rrrdrdrdrer-s / " r-cGsdd}t}ttttttttg}t t dr| t t }|D] }|D]}||r5t||r4||q$t||r?||q$q |D]}||qC|D]}||qM|D]}||ra|}||qX|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|tp t|dS)N __bases__)rrr)objrdrdreisclass/szbuild_opener..isclassHTTPSConnection)r-r2r?r;r/r0r=r<r@r http_clientryr\set issubclassaddrremover)rrrcdefault_classesskipklasscheckhrdrdrerC&s8         rCc@s(eZdZdZddZddZddZdS) r.cC ||_dSrf)parent)rr#rdrdrerO zBaseHandler.add_parentcCrrfrdrrdrdrerRrzBaseHandler.closecCst|dsdS|j|jkS)N handler_orderT)rr%)rotherrdrdre__lt__Vs  zBaseHandler.__lt__N)rrrr%rrr'rdrdrdrer.Ls  r.c@s eZdZdZdZddZeZdS)r@zProcess HTTP error responses.icCsH|j|j|}}}d|krdks"n|jd|||||}|S)N,r )coderrqr#r)rrrr*rrrdrdre http_responsecs  z HTTPErrorProcessor.http_responseN)rrr__doc__r%r+https_responserdrdrdrer@_s  r@c@eZdZddZdS)r/cCst|j||||rf)rr)rrrr*rrrdrdrer qsz*HTTPDefaultErrorHandler.http_error_defaultN)rrrr rdrdrdrer/p r/c@s4eZdZdZdZddZddZeZZZ dZ dS) r0 c sx|}|dvr |dvs|dvr|dkst|j|||||dd}dtfdd |jD}t|||jd d S) aReturn 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)rHEAD)r2r3r4r z%20)rlz content-typec3s(|]\}}|vr||fVqdSrf)r.0kvCONTENT_HEADERSrdre s  z7HTTPRedirectHandler.redirect_request..T)rrr) rrrreplacerrrr,r) rrrr*rrnewurlm newheadersrdr;rer|s  z$HTTPRedirectHandler.redirect_requestc Cs2d|vr |d}n d|vr|d}ndSt|}|jdvr)t||d||f|||js4t|}d|d<t|}t|j|}|||||||}|durNdSt |drv|j } |_ | |d|j ksit | |jkrut|j||j|||ni} |_ |_ | |dd | |<|||jj||jd S) Nlocationurir rftprz+%s - Redirection to url '%s' is not allowed/r  redirect_dictrrr`)rschemerrsrr'rrrrrGr max_repeatsr{max_redirectionsinf_msgrzrr#r r`) rrrr*rrr?urlpartsnewvisitedrdrdrehttp_error_302sB        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) rrrrJrKrrPhttp_error_301http_error_303http_error_307rLrdrdrdrer0ts" 7r0c Cst|\}}|dsd}|}n|dstd||dd}|dkr'd}|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') rFN//zproxy URL with no authority: %rr rk)rrrSrr r!) proxyrIr_scheme authorityenduserinfohostportuserpasswordrdrdre _parse_proxys 2       r]c@s"eZdZdZdddZddZdS)r2dNcCsZ|durt}t|dsJd||_|D]\}}t|d||||jfddqdS)Nkeysproxies must be a mappingz%s_opencSs ||||Srfrd)rrUrrrdrdre/rz'ProxyHandler.__init__..)rFrproxiesrsetattrr)rrcrr^rdrdrer(s zProxyHandler.__init__c Cs|j}t|\}}}}|dur|}|jrt|jrdS|r;|r;dt|t|f} t| d} | dd| t|}| ||||ksM|dkrOdS|j j ||j dS)N%s:%sasciiProxy-authorizationBasic rrH)rr]r proxy_bypassrbase64 b64encodeencoderrrr#r r`) rrrUr orig_type proxy_typer[r\rZ user_passcredsrdrdrer2s" zProxyHandler.proxy_openrf)rrrr%rrrdrdrdrer2$s  r2c@s6eZdZddZddZddZd dd Zd d Zd S)r3cCs i|_dSrf)passwdrrdrdrerPr$zHTTPPasswordMgr.__init__cs\t|tr|g}|jvrij|<dD]tfdd|D}||fj||<qdS)NTFcsg|]}|qSrd) reduce_uri)r8u default_portrrdre [sz0HTTPPasswordMgr.add_password..)rr rqtuple)rrealmrCr[rq reduced_urirdrure add_passwordSs   zHTTPPasswordMgr.add_passwordc Cs`|j|i}dD]$}|||}|D]\}}|D]}|||r+|Sqqq dS)NrrNN)rqrrsr is_suburi) rryauthuridomainsrvreduced_authuriurisauthinforCrdrdrefind_user_password^s  z"HTTPPasswordMgr.find_user_passwordTc Cst|}|dr|d}|d}|dpd}nd}|}d}t|\}}|r?|dur?|dur?ddd|} | dur?d || f}||fS) z@Accept authority or URI and extract only the authority and path.rrr rFNPir z%s:%d)rrr) rrCrvpartsrIrWrsrportdportrdrdrershs$  zHTTPPasswordMgr.reduce_uricCsR||krdS|d|dkrdSt|d|df}t|t|dkr'dSdS)zcCheck if test is below base in a URI tree Both args must be URIs in reduced form. TrFr) posixpath commonprefixr{)rbasetestcommonrdrdrer}szHTTPPasswordMgr.is_suburiN)T)rrrrr{rrsr}rdrdrdrer3Ns  r3c@r.)r4cCs0t|||\}}|dur||fSt|d|Srf)r3r)rryr~r[r\rdrdrers z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)rrrrrdrdrdrer4s r4c@s<eZdZedejZd ddZddZddZ d d Z dS) r5z1(?:.*,)*[ ]*([^ ]+)[ ]+realm=(["']?)([^"']*)\2NcCs(|durt}||_|jj|_d|_dSNr)r3rqr{retried)r password_mgrrdrdrers   z!AbstractBasicAuthHandler.__init__cC d|_dSrrrrdrdrereset_retry_countr$z*AbstractBasicAuthHandler.reset_retry_countc Cs||d}|jdkrt|dd|d|jd7_|rc|d}|dkr0td|tj |}|re| \}}}|dvrJt d t d |dkrg||||} | ra| jdkrad|_| SdSdSdS) Nzbasic auth failedrrbasiczDAbstractBasicAuthHandler does not support the following scheme: '%s')"'zBasic Auth Realm was unquotedr )rrrrsplitrrSr5rxsearchgroupsrr UserWarningretry_http_basic_authr*) rauthreqrrrrImorryrrdrdrehttp_error_auth_reqeds6       z.AbstractBasicAuthHandler.http_error_auth_reqedcCsz|j||\}}|dur;d||f}dt|d}|j|jd|kr+dS| |j||j j ||j dSdS)NrerhrfrH) rqrrjrkrlrrr auth_headerrr#r r`)rrrryr[pwrawauthrdrdrers z.AbstractBasicAuthHandler.retry_http_basic_authrf) rrrrecompileIrrrrrrdrdrdrer5s  r5c@eZdZdZddZdS)r6 AuthorizationcC"|j}|d|||}||S)Nwww-authenticate)rrr)rrrr*rrr^rrdrdrehttp_error_401 z#HTTPBasicAuthHandler.http_error_401N)rrrrrrdrdrdrer6 r6c@r)r7rgcCrNproxy-authenticaterrr)rrrr*rrrWrrdrdrehttp_error_407s z$ProxyBasicAuthHandler.http_error_407N)rrrrrrdrdrdrer7rr7c@sNeZdZdddZddZddZdd Zd d Zd d ZddZ ddZ dS)r8NcCs4|durt}||_|jj|_d|_d|_d|_dSr)r3rqr{r nonce_count last_nonce)rrqrdrdrers  z"AbstractDigestAuthHandler.__init__cCrrrrrdrdrerr$z+AbstractDigestAuthHandler.reset_retry_countcCs~||d}|jdkrt|jdd|d|jd7_|r;|d}|dkr/|||S|dkr=td|dSdS) Nrrzdigest auth failedrrdigestrzEAbstractDigestAuthHandler does not support the following scheme: '%s')rrrrrrretry_http_digest_authrS)rrrrrrrIrdrdrers       z/AbstractDigestAuthHandler.http_error_auth_reqedcCsz|dd\}}ttdt|}|||}|r;d|}|j|jd|kr)dS||j||j j ||j d}|SdS)Nr6rz Digest %srH) rparse_keqv_listrparse_http_listget_authorizationrrrrr#r r`)rrrtoken challengechalauth_valresprdrdrer(s z0AbstractDigestAuthHandler.retry_http_digest_authcCs@d|j|tf}|dtd}t|}|ddS)Nz %s:%s:%s:rf)rtimectimerl _randombyteshashlibsha1 hexdigest)rnoncesbdigrdrdre get_cnonce4s z$AbstractDigestAuthHandler.get_cnoncecCsz|d}|d}|d}|dd}|dd}Wn ty%YdSw||\}} |dur3dS|j||j\} } | durCdS|jdurP||j|} nd} d| || f} d||j f}|d kr||j krs|j d 7_ nd |_ ||_ d |j }| |}d ||||||f}| || |}n|dur| || d|||f}nt d |d| |||j |f}|r|d|7}| r|d| 7}|d|7}|r|d||f7}|S)Nryrqop algorithmMD5opaquez%s:%s:%srerrz%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")rKeyErrorget_algorithm_implsrqrrr_get_entity_digestrrrrrr)rrrryrrrrHKDr[rentdigA1A2ncvaluecnoncenoncebitrespdigrrdrdrer?s^            z+AbstractDigestAuthHandler.get_authorizationcs6|dkr ddn|dkrddfdd}|fS)NrcSt|dSNrf)rmd5rlrxrdrdrerb~z?AbstractDigestAuthHandler.get_algorithm_impls..SHAcSrr)rrrlrrrdrdrerbrcsd||fS)Nrerd)rdrrdrerbsrd)rrrrdrrer{s   z-AbstractDigestAuthHandler.get_algorithm_implscCrrfrd)rr_rrdrdrerrz+AbstractDigestAuthHandler.get_entity_digestrf) rrrrrrrrrrrrdrdrdrer8s    < r8c@s eZdZdZdZdZddZdS)r9zAn authentication protocol defined by RFC 2069 Digest authentication improves on basic authentication because it does not transmit passwords in the clear. rcCs*t|jd}|d|||}||S)Nrr)rrrrrrrr*rrrretryrdrdrers z$HTTPDigestAuthHandler.http_error_401N)rrrr,rr%rrdrdrdrer9s  r9c@seZdZdZdZddZdS)r:Proxy-AuthorizationrcCrrrrrdrdrerrz%ProxyDigestAuthHandler.http_error_407N)rrrrr%rrdrdrdrer:s r:c@s.eZdZd ddZddZddZdd Zd S) AbstractHTTPHandlerrcCr"rf _debuglevel)r debuglevelrdrdrerr$zAbstractHTTPHandler.__init__cCr"rfr)rlevelrdrdreset_http_debuglevelr$z'AbstractHTTPHandler.set_http_debuglevelc CsH|j}|s td|jdurl|j}t|trd}t||ds'|dd|dsld}ztr?t|t j r?t ||j }n t |}t ||j }Wntyct|t ratdt||fYn w|dd||}|rt|j\}} t| \}} |ds|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)rrr_rr rrrrarrayr{itemsize memoryviewr*rSrrrrrr#rr) rrrr_rrmvsel_hostrIselsel_pathrwrrdrdre do_request_sZ           zAbstractHTTPHandler.do_request_c s&|j}|s td||fd|ji|}t|jtfdd|jDdd<tddD|jrUi}d}|vrM|||<|=|j |j|d z| | |j |j Wntjyx}z|t|d }~ww|} |jr|jd |_|| _| j| _| S) zReturn an HTTPResponse object for the request, using http_class. http_class must implement the HTTPConnection API from http.client. rr`c3s$|] \}}|vr||fVqdSrfrdr7rrdrer=s  z.AbstractHTTPHandler.do_open..r Connectioncss |] \}}||fVqdSrf)title)r8rwrrdrdrer=srrN)rrr`rrrrrr set_tunnelrrrr_rrr getresponsesockrr^reasonr) r http_classrhttp_conn_argsrr tunnel_headersproxy_auth_hdrerrrardrrers: "    zAbstractHTTPHandler.do_openNr)rrrrrrrrdrdrdrers   3rc@seZdZddZejZdS)r;cCs|tj|Srf)rrHTTPConnectionrrrdrdre http_open$szHTTPHandler.http_openN)rrrrrr http_requestrdrdrdrer;"s r;rc@s$eZdZdddZddZejZdS)r\rNcCst||||_||_dSrf)rr_context_check_hostname)rrrPrQrdrdrer-s  zHTTPSHandler.__init__cCs|jtj||j|jdS)NrO)rrrr r rrdrdre https_open2s zHTTPSHandler.https_open)rNN)rrrrr rr https_requestrdrdrdrer\+s  r\c@s.eZdZdddZddZddZeZeZdS) r1NcCs2ddlmmm}|dur|}||_dSr)future.backports.http.cookiejar backportsr  cookiejar CookieJar)rrhttp_cookiejarrdrdrer;s zHTTPCookieProcessor.__init__cCs|j||Srf)radd_cookie_header)rrrdrdrer As z HTTPCookieProcessor.http_requestcCs|j|||Srf)rextract_cookies)rrrrdrdrer+Esz!HTTPCookieProcessor.http_responserf)rrrrr r+r r-rdrdrdrer1:s  r1c@r.)r?cCs|j}td|)Nzunknown url type: %s)rr)rrrrdrdrerMs zUnknownHandler.unknown_openN)rrrrrdrdrdrer?Lr/r?cCsNi}|D] }|dd\}}|ddkr |ddkr |dd}|||<q|S)z>Parse list of key=value strings where keys are not duplicated.=rrrrk)r)lparsedeltr9r:rdrdrerQs  rcCsg}d}d}}|D]5}|r||7}d}q |r)|dkrd}q |dkr$d}||7}q |dkr5||d}q |dkr;d}||7}q |rG||dd|DS) 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. rF\Tr,cSg|]}|qSrdstrip)r8partrdrdrerwrz#parse_http_list..)ry)rresrescapercurrdrdrer[s4    rc@s(eZdZddZdZddZddZdS)r<cCsZ|j}|dddkr(|dddkr(|jr(|jdkr(|j|ur&tddS||S)Nr rTrKrF localhost-file:// scheme is supported only on localhost)rr get_namesropen_local_file)rrr^rdrdre file_opens&  zFileHandler.file_openNcCsftjdur0zttddttdt_WtjStjy/tdft_YtjSwtjS)Nr"r )r<namesrxrgethostbyname_ex gethostnamegaierror gethostbynamerrdrdrer$s  zFileHandler.get_namesc Csddlmmm}ddl}|j}|j}t|}zXt |}|j }|j |j dd} | |d} td| p8d|| f} |rFt|\}} |rR| sit||vrn|r[d||} nd|} tt|d| | WSWtdWtdty}zt|d}~ww) NrTusegmtz6Content-type: %s Content-length: %d Last-modified: %s text/plainfile://rbzfile not on local host)future.backports.email.utilsrrutils mimetypesrrrErrstatst_size formatdatest_mtime guess_typemessage_from_stringr_safe_gethostbynamer$r(r OSErrorr)rr email_utilsr3rr} localfilestatsrmodifiedmtyperrorigurlexprdrdrer%sB     zFileHandler.open_local_file)rrrr&r'r$r%rdrdrdrer<s   r<cCs$zt|WStjyYdSwrf)rr+r*rrdrdrer:s  r:c@seZdZddZddZdS)r=c Csddl}ddl}|j}|stdt|\}}|dur|j}nt|}t|\}}|r2t|\}}nd}t |}|p;d}|p?d}zt |}Wnt j yX}zt|d}~wwt |j\} } | d} ttt | } | dd| d} } | r| ds| dd} z_|||||| |j} | rdpd}| D]}t|\}}|d kr|d vr|}q| | |\}}d}||jd}|r|d |7}|dur|dkr|d |7}t|}t|||jWS|jy}ztd |}t|WYd}~dSd}~ww)Nrftp error: no host givenrrFrkrrDraArrrrEzContent-type: %s zContent-length: %d ftp error: %r)ftplibr3rrrFTP_PORTr r r!rrr+rr"rrrr connect_ftpr`r$rupperretrfiler8rrr9r( all_errorsr)rrrJr3rrr[rqrrsattrsdirsrgfwrattrrrretrlenrr@rBexcrdrdreftp_opensf            zFTPHandler.ftp_openc Cst||||||ddS)NF) persistent) ftpwrapper)rr[rqrrrQr`rdrdrerLszFTPHandler.connect_ftpN)rrrrVrLrdrdrdrer=s 5r=c@s<eZdZddZddZddZddZd d Zd d Zd S)r>cCs"i|_i|_d|_d|_d|_dS)Nr<r)cacher`soonestdelay max_connsrrdrdrers  zCacheFTPHandler.__init__cCr"rf)r\)rtrdrdre setTimeoutr$zCacheFTPHandler.setTimeoutcCr"rf)r])rr@rdrdre setMaxConnsr$zCacheFTPHandler.setMaxConnscCsr|||d||f}||jvrt|j|j|<nt|||||||j|<t|j|j|<||j|S)NrF)joinrZrr\r`rX check_cache)rr[rqrrrQr`rrdrdrerL s    zCacheFTPHandler.connect_ftpcCst}|j|kr(t|jD]\}}||kr'|j||j|=|j|=qtt|j|_t |j|j krat|jD]\}}||jkrT|j|=|j|=nqAtt|j|_dSdSrf) rr[rr`rrZrminvaluesr{r])rr^r9r:rdrdrerbs$  zCacheFTPHandler.check_cachecCs0|jD]}|q|j|jdSrf)rZrdrclearr`)rconnrdrdre clear_cache)s  zCacheFTPHandler.clear_cacheN) rrrrr_r`rLrbrgrdrdrdrer>s r>r1nt)rErDcCt|S)zOS-specific conversion from a relative URL of the 'file' scheme to a file system path; not recommended for general use.)rpathnamerdrdrerE8rEcCri)zOS-specific conversion from a file system path to a relative URL of the 'file' scheme; not recommended for general use.)rrjrdrdrerD=rlrDc@seZdZdZdZdeZd*ddZddZdd Z d d Z d d Z d*ddZ d*ddZ d*ddZd+ddZddZd*ddZd*ddZddZerRddZd*d d!Zd"d#Zd$d%Zd&d'Zd*d(d)ZdS),rIa,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).NrcKsdd|jji}tj|tdd|durt}t|ds Jd||_|d|_ |d|_ d |j fg|_ g|_ tj|_d|_t|_dS) NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methodsclassrKrr_r`key_file cert_filez User-Agent) __class__rrrrrFrrcrrnroversionr_URLopener__tempfilesrrr_URLopener__unlink tempcacheftpcache)rrcx509rrdrdrerWs    zURLopener.__init__cC |dSrf)rrrdrdre__del__qrzURLopener.__del__cCrwrf)cleanuprrdrdrertrzURLopener.closec CsV|jr|jD]}z||WqtyYqw|jdd=|jr)|jdSdSrf)rrrsr;rtre)rrgrdrdreryws   zURLopener.cleanupcGs|j|dS)zdAdd a header to be used by the HTTP interface only e.g. u.addheader('Accept', 'sound/basic')N)rry)rrrdrdre addheaderszURLopener.addheaderc CsLtt|}t|dd}|jr&||jvr&|j|\}}t|d}t|||St|\}}|s0d}||jvrK|j|}t|\}} t| \} } | |f}nd}d|} ||_ | dd} t || sn|rh| |||S| ||Sz|dur{t|| |WSt|| ||WStytjy} zttd | WYd} ~ dSd} ~ ww) z6Use URLopener().open(file) instead of open(file, 'r').z%/:=&?~#+!$,;'@()*[]|safer0rgNopen_-rz socket error)rr&rrtr r(rrcrrr>ropen_unknown_proxy open_unknownrrrrrIOError)rrr_r}rrurltyper^rU proxyhostrrrwrrdrdrer s@              zURLopener.opencCst|\}}tdd|)/Overridable interface to open unknown URL type. url errorzunknown url typerr)rrr_rr^rdrdrers  zURLopener.open_unknowncCst|\}}tdd||)rrzinvalid proxy for %sr)rrUrr_rr^rdrdrers zURLopener.open_unknown_proxyc Cs&tt|}|jr||jvr|j|St|\}}|durN|r#|dkrNz||}|}|tt|d|fWSt yM} zWYd} ~ nd} ~ ww| ||}z|} |rat |d} n=ddl } t|\} }t|pod\} }t |pwd\}} t |pd\}} tj|d}| |\}}|j|t|d} zO|| f}|jdur||j|<d}d}d}d}d | vrt| d }|r|||| ||}|sn|t|7}| ||d7}|r||||qW| n| wW|n|w|dkr||krtd ||f||S) ztretrieve(url) returns (filename, headers) for a local object or (tempfilename, headers) for a remote object.Nrgrrhrrrjrkrlrmrn)rr&rtrr%rqrrErrr rur#r"rrrssplitextmkstemprrryfdopenr rzr{r|r)rr^r}r~r_rurl1rrrrrrugarbagerssuffixfdrrrrzrrrdrdreretrievesz                  zURLopener.retrievecCsd}d}t|trt|\}}|rt|\}}t|}|}n:|\}}t|\}}t|\} } | }d}| dkr;d}nt| \}} |rIt|\}}|rRd| || f}t|rX|}|s_tdd|rpt|}t |  d} nd} |rt|}t |  d} nd} ||} i}| rd| |d<| rd| |d <|r||d <d |d <|j D]\}}|||<q|durd |d<| d|||n| jd||dz| }Wn tjytdwd|jkrdkrnn t||jd||jS|||j|j|j|j|S)aMake 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 errorrrfzBasic %srrrrrrz Content-Typerrrz$http protocol error: bad status liner(r)http:)rr rr rrrrirrjrkrlrrrrr BadStatusLinerstatusr(r http_errorrr)rconnection_factoryr^r_ user_passwd proxy_passwdrrrealhostrr proxy_authr http_connrheaderrrrdrdre_open_generic_httpsv             zURLopener._open_generic_httpcCs|tj||S)zUse HTTP protocol.)rrrrr^r_rdrdre open_httpXzURLopener.open_httpc Csbd|}t||r(t||}|dur||||||} n |||||||} | r(| S||||||S)zHandle http errors. Derived class can override this, or provide specific handlers named http_error_DDD where DDD is the 3-digit error code.z http_error_%dN)rrr ) rr^rerrcodeerrmsgrr_rwrrrdrdrer\s  zURLopener.http_errorcCs|t||||d)z>Default error handler: close the connection and raise IOError.N)rrrr^rrrrrdrdrer lszURLopener.http_error_defaultcCstj||j|jdS)N)rnro)rrrnro)rrrdrdre_https_connectionrszURLopener._https_connectioncCs||j||S)zUse HTTPS protocol.)rrrrdrdre open_httpswrzURLopener.open_httpscCsXt|ts td|dddkr'|dddkr'|dddkr'td ||S) z/Use local file or FTP depending on form of URL.zEfile error: proxy support for file protocol currently not implementedNr rTrKrF z localhost/r#)rr rrrSr%)rr^rdrdre open_file{s 4 zURLopener.open_filec CsTddlmmm}ddl}t|\}}t|}zt|}Wnt y4}zt |j |j d}~ww|j } |j|jdd} ||d} td| pMd| | f} |sl|} |dddkrcd |} tt|d | | St|\}}|st|tftvr|} |dddkrd |} n|dd d krtd |tt|d | | St d)zUse local file.rNTr,z6Content-Type: %s Content-Length: %d Last-modified: %s r.rrFr/r0r z./zAlocal file url may start with / or file:. Unknown url of type: %sz#local file error: not on local host)r1rrr2r3rrErrr4r;rstrerrorr}r5r6r7r8r9r(r rrr+r"thishostrS)rr^r<r3rrg localnamer>err?r@rurlfilerrdrdrer%sB     zURLopener.open_local_filec Cslt|ts tdddl}t|\}}|stdt|\}}t|\}}|r.t|\}}nd}t|}t|p8d}t|p>d}t |}|sOddl }|j }nt |}t|\}} t|}|d} | dd| d} } | ry| dsy| dd} | r| dsd| d<|||d| f} t|jtkr|jD]} | | kr|j| }|j| =|qzl| |jvrt||||| |j| <| sd }nd }| D]}t|\}}|d kr|d vr|}q|j| | |\}}|d |d}d}|r|d|7}|dur |dkr |d|7}t|}t||d |WSty5}zt td|WYd}~dSd}~ww)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedrNrDrrFrkrrErrrFzftp:zContent-Type: %s zContent-Length: %d z ftp error %r)!rr rr3rrr r!rrr+rJrKr r"rrar{ru MAXFTPCACHEr_rrXr$rrMrNr8rr9r( ftperrorsr)rr^r3rrsrr[rqrJrPrQrgrr9r:rrSrrrTr@rrBrdrdreopen_ftpsp                  zURLopener.open_ftpc Cs6t|ts tdz |dd\}}Wn tytddw|s$d}|d}|dkrDd ||d vrD||dd }|d |}nd }g}|d t d t t|d||dkrot | dd}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;rrNrzDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %srjrfzlatin-1zContent-Length: %d )rr rrrSrrfindryrstrftimegmtimerj decodebytesrlrrr{rarr9ioStringIOr() rr^r_rsemiencodingrrfrdrdre open_datas:            zURLopener.open_datarfNNN)rrrr,rrrrqrrxrryrzr rrrrrrr rRrrrr%rrrdrdrdrerIJs0   $  B \    :rIc@seZdZdZddZddZd#ddZd d Zd#d d Zd#d dZ d#ddZ  d$ddZ  d$ddZ d#ddZ d#ddZd#ddZd#ddZd%dd Zd!d"ZdS)&rJz?Derived class with handlers for errors we can handle (perhaps).cOs.tj|g|Ri|i|_d|_d|_dS)Nrr1)rIr auth_cachetriesmaxtries)rrkwargsrdrdrer s zFancyURLopener.__init__cCst||d||S)z3Default error handling -- don't raise an exception.r)r(rrdrdrer rz!FancyURLopener.http_error_defaultNc Csl|jd7_|jr'|j|jkr't|dr|j}n|j}d|_|||dd|S|||||||}d|_|S)z%Error 302 -- relocated (temporarily).rhttp_error_500rr!z)Internal Server Error: Redirect Recursion)rrrrr redirect_internal) rr^rrrrr_rrrdrdrerPs zFancyURLopener.http_error_302c Csxd|vr |d}n d|vr|d}ndS|t|jd||}t|}|jdvr7t|||d|||||S)NrBrC:rDz( Redirection to url '%s' is not allowed.)rrrrrIrr ) rr^rrrrr_r?rMrdrdrer%s     z FancyURLopener.redirect_internalcC|||||||S)z*Error 301 -- also relocated (permanently).rPrr^rrrrr_rdrdrerQAzFancyURLopener.http_error_301cCr)z;Error 303 -- also relocated (essentially identical to 302).rrrdrdrerRErzFancyURLopener.http_error_303cCs.|dur|||||||S||||||S)z1Error 307 -- relocated, but turn POST into error.N)rPr rrdrdrerSIszFancyURLopener.http_error_307Fc Cd|vrt|||||||d}td|} | s$t||||||| \} } | dkr:t|||||||sFt||||||d|jd} |durYt|| || St|| || |S)z_Error 401 -- authentication required. This function supports Basic authentication only.r![ ]*([^ ]+)[ ]+realm="([^"]*)"rretry_ _basic_authNrIr rmatchrrrr rr^rrrrr_rstuffrrIryrwrdrdrerP.      zFancyURLopener.http_error_401c Cr)zeError 407 -- proxy authentication required. This function supports Basic authentication only.rrr retry_proxy_rNrrrdrdrerirzFancyURLopener.http_error_407cCt|\}}d||}|jd}t|\}} t| \} } | dd} | | d} || || \} } | s9| s9dSdt| ddt| dd| f} d| | |jd<|durZ||S|||S)Nhttp://r @r%s:%s@%srr{rrcrrget_user_passwdrr rr^ryr_rrr?rUrr proxyselectorrr[rqrdrdreretry_proxy_http_basic_auth           z*FancyURLopener.retry_proxy_http_basic_authcCr)Nhttps://rrrrrr{rrrdrdreretry_proxy_https_basic_authrz+FancyURLopener.retry_proxy_https_basic_authc Ct|\}}|dd}||d}||||\}}|s"|s"dSdt|ddt|dd|f}d||} |dur@|| S|| |S)Nrrrrr{rrrrrr rr^ryr_rrrr[rqr?rdrdrer        z$FancyURLopener.retry_http_basic_authc Cr)Nrrrrr{rrrrdrdreretry_https_basic_authrz%FancyURLopener.retry_https_basic_authrcCs`|d|}||jvr|r|j|=n|j|S|||\}}|s%|r,||f|j|<||fS)Nr)rrprompt_user_passwd)rrryrgrr[rqrdrdrers   zFancyURLopener.get_user_passwdcCsRddl}ztd||f}|d|||f}||fWSty(tYdSw)z#Override this in a GUI environment!rNzEnter username for %s at %s: z#Enter password for %s in %s at %s: r|)getpassr KeyboardInterruptprint)rrryrr[rqrdrdrers  z!FancyURLopener.prompt_user_passwdrf)NFr)rrrr,rr rPrrQrRrSrrrrrrrrrdrdrdrerJs*           rJcCtdur tdatS)z8Return the IP address of the magic hostname 'localhost'.Nr") _localhostrr+rdrdrdrer" r"cCsRtdur'ztttdaWtStjy&ttddaYtSwtS)z,Return the IP addresses of the current host.Nr r") _thishostrxrr(r)r*rdrdrdrersrcCstdur ddl}|jatS)z1Return the set of errors raised by the FTP class.Nr) _ftperrorsrJrO)rJrdrdrersrcCr)z%Return an empty email Message object.Nr) _noheadersrr9rdrdrdre noheadersrrc@sNeZdZdZ  dddZddZdd Zd d Zd d ZddZ ddZ dS)rXz;Class used by open_ftp() for cache of open FTP connections.NTcCs<||_||_||_||_||_||_d|_||_|dSr) r[rqrrrQr`refcount keepaliveinit)rr[rqrrrQr`rWrdrdrer s zftpwrapper.__init__cCs\ddl}d|_||_|j|j|j|j|j|j |j d |j }|j |dS)NrrF)rJbusyFTPrEconnectrrr`loginr[rqrarQcwd)rrJ_targetrdrdrer s  zftpwrapper.initc Csddl}||dvrd}d}nd|}d}z|j|Wn|jy3||j|Ynwd}|rn|snzd|}|j|\}}Wn%|jym}zt|dddkrct t d |WYd}~nd}~ww|s|jd|r|j } z)z|j |Wn|jy}z t d |} || _ | d}~wwW|j | n|j | wd |}nd }|j|\}}d|_t|d |j} |jd7_|| |fS) Nr)rrEzTYPE ArzTYPE zRETR rK550rIzLIST LISTr0)rJ endtransferrEvoidcmdrOr ntransfercmd error_permr rrpwdr __cause__rr)makefile file_closerr) rrgrrJcmdisdirrfrTrrrUftpobjrdrdrerN sV     zftpwrapper.retrfilecCrr)rrrdrdrerK r$zftpwrapper.endtransfercCs d|_|jdkr|dSdS)NFr)rr real_closerrdrdrerN s  zftpwrapper.closecCs:||jd8_|jdkr|js|dSdSdS)Nrr)rrrrrrdrdrerS s  zftpwrapper.file_closecCs0|z|jWdStyYdSwrf)rrErrrrdrdrerY s zftpwrapper.real_close)NT) rrrr,rrrNrrrrrdrdrdrerX s   0 rXcCsHi}tjD]\}}|}|r!|dddkr!|||dd<q|S)aReturn a dictionary of scheme -> proxy server URL mappings. Scan the environment for variables named _proxy; this seems to be the standard convention. If you need a different way, you can pass a proxies dictionary to the [Fancy]URLopener constructor. iN_proxy)rrenvironrr)rcrwrrdrdregetproxies_environmenta s rcCsttjddp tjdd}|dkrdSt|\}}dd|dD}|D]}|r7||s4||r7dSq&d S) zTest 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_proxyrNO_PROXY*rcSrrdr)r8rUrdrdrerw~ rz,proxy_bypass_environment..rr)rrrrrrendswith)rrhostonlyr no_proxy_listrwrdrdreproxy_bypass_environmentq s r c Csddlm}t|\}}dd}d|vr|drdSd}|d d D]f}|s'q"td |}|dur|durKz t|}||}Wn tjyJYq"w||d } |d } | durhd|d  dd } nt | d d} d| } || ?| | ?krdSq"|||rdSq"dS)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|dkr|gddd}|dd>|dd>B|dd >B|d BS) N.r0)rrrrrrrr rrK)rrr r r{)ipAddrrrdrdreip2num s  ,z,_proxy_bypass_macosx_sysconf..ip2numr exclude_simpleTN exceptionsrdz(\d+(?:\.\d+)*)(/\d+)?rr r F) r rrrrrr+rgroupcountr ) rproxy_settingsr r rrhostIPrr@rmaskrdrdre_proxy_bypass_macosx_sysconf s>      rdarwin)_get_proxy_settings _get_proxiescCst}t||Srf)rr)rrrdrdreproxy_bypass_macosx_sysconf s rcCstS)zReturn a dictionary of scheme -> proxy server URL mappings. This function uses the MacOSX framework SystemConfiguration to fetch the proxy information. )rrdrdrdregetproxies_macosx_sysconf srcCtrt|St|Srf)rr rrCrdrdreri sricC tptSrf)rrrdrdrdrerF rrFc Csi}zddl}Wn ty|YSwzf||jd}||dd}|rtt||dd}d|vrU|dD]}|dd\}}td |sOd ||f}|||<q9n|dd d krb||d <nd||d <d||d<d||d<| W|St t t fyY|Sw)zxReturn a dictionary of scheme -> proxy server URL mappings. Win32 uses the registry to store proxies. rN;Software\Microsoft\Windows\CurrentVersion\Internet Settings ProxyEnable ProxyServerrrrz ^([^/:]+)://z%s://%srrr z http://%sz https://%srzftp://%srE) winreg ImportErrorOpenKeyHKEY_CURRENT_USER QueryValueExr rrrClose WindowsErrorrSr)rcr$internetSettings proxyEnable proxyServerpraddressrdrdregetproxies_registry sN          r0cCr zReturn a dictionary of scheme -> proxy server URL mappings. Returns settings gathered from the environment, if specified, or the registry. )rr0rdrdrdrerF s c Cslzddl}Wn tyYdSwz||jd}||dd}t||dd}Wn ty6YdSw|r;|s=dSt|\}}|g}zt |}||krU| |Wn tj y`Ynwzt |}||krp| |Wn tj y{Ynw| d}|D]0} | dkrd|vrdS| dd } | d d } | d d} |D]} t| | tjrdSqqdS) Nrr!r" ProxyOverriderzr rz\.rz.*?)r$r%r&r'r(r r*rrr+ryrgetfqdnrr>rrr) rr$r+r, proxyOverriderawHostraddrfqdnrrrdrdreproxy_bypass_registry sp            r9cCrr1)rr r9rCrdrdreriH sr)r, __future__rrrrfuture.builtinsrrrr r r r r future.utilsrrrrjrrrfuture.backportsrfuture.backports.httprrrrrrparserrrrrrrrrr r!r"r#r$r%r&r'rr(r)rrrrrrsysrruror collectionsr*collections.abcrTr+r%rR__all__rqrr]rrArBrxrGrHrASCIIrrobjectr,r-rCr.r@r/r0r]r2r3r4r5r6r7urandomrr8r9r:rr;rr\ryr1r?rrr<r:r=r>rrw nturl2pathrErDrurIrJrr"rrrrrrrXrr rplatform_scproxyrrrrrirFr0r9rdrdrdresV(  L    ?  y&hH*@ E  z   +4:8  AU  ^ <  - 2