o ]Lb;b@sddlmZddlZddlZddlZddlmZddlmZddl m Z m Z m Z mZmZmZmZmZddlmZmZejZejZejZejZd/dd ZGd d d eZGd d d ejZddZeedZGdddej Z ddZ!ddZ"Gdddej#Z$Gdddej Z%Gddde$Z&erGdddej Z'Gdd d ej(ej)Z)Gd!d"d"ej*Z*Gd#d$d$ej+Z+Gd%d&d&ej,Z-gZ.    '  (d0d)d*Z/d1d+d,Z0d-d.Z1dS)2)absolute_importN)_)getattr)encodingerrorhttpconnection keepalivepycompatsslutil urllibcompatutil) stringutilurlutilcCs8|dd}|dd}|dd}|r|dd}|S) a1Replace special characters "&", "<" and ">" to HTML-safe sequences. If the optional flag quote is true, the quotation mark character (") is also translated. This is the same as cgi.escape in Python, but always operates on bytes, whereas cgi.escape in Python 3 only works on unicodes. &s&s>"s")replace)squoter//usr/lib/python3/dist-packages/mercurial/url.pyescape's    rc@s4eZdZddZddZddZddZd d Zd S) passwordmgrcCs||_||_dSN)uipasswddb)selfrrrrr__init__8s zpasswordmgr.__init__cCs|j||||Sr)r add_password)rrealmuriuserpasswdrrrr <szpasswordmgr.add_passwordc Cst|tdtfs Jt|tsJ|j||}|\}}t|t|}}|r<|r<|||t|t|fS|r@|sat |j ||}|ra|\}}| d| d}}|j d||re|stt|} d| _|j sttdtt| |j tdtt| |j tdt||r|j td|n |j jtddd }|s|j }|durd S|j|||||||t|t|fS) Nsusernamespassword#using auth.%s.* for authentication s"http authorization required for %ss#http authorization required for %s s realm: %s s user: %s suser:)default)NN) isinstancetypestrrfind_user_passwordr bytesurl _writedebugstrurlhttpconnectionmodreadauthforurirgetdebugrurlquery interactiverAbortr hidepasswordbyteswritepromptgetpassr ) rr!authuriauthinfor#r$resgroupauthurrrr*?sN      zpasswordmgr.find_user_passwordcCs0td}|j|||rdt|pdfdS)N http auth: user %s, password %s *not set)rrr1len)rr#r$msgrrrr,ps(zpasswordmgr._writedebugcCs|jd|Sr)rr*)rr;rrrfind_stored_passwordtz passwordmgr.find_stored_passwordN)__name__ __module__ __qualname__rr r*r,rFrrrrr7s 1 rc@eZdZddZddZdS) proxyhandlercCs|ddp tjd}|r{|ds|dsd|d}t|}|js4|dd|_|dd|_d d g}| d d | dd D| dd tjdd dD| ddrbg|_ n||_ t|}||d}|dtt|ni}tj||||_dS)Ns http_proxyshostshttp:shttps:shttp:///suserspasswds localhosts 127.0.0.1cSsg|]}|qSr)lower.0prrr sz)proxyhandler.__init__..snocSs g|] }|r|qSr)striprNrOrrrrRs  sno_proxy,salways)httphttpssproxying through %s )configrenvironr0 startswithrr2r#r$extend configlistsplit configboolno_listr)r1r6r7urlreqrLrr)rrproxyurlproxyr_proxiesrrrrysB     zproxyhandler.__init__cCstt|dd}|jD]+}||krdS|dr*||ddr*dS|dr;||ddr;dSqtj ||||S)N:rs*..r) r r+r gethostr]r_rZendswithr`rL proxy_open)rreqrbtype_hosterrrris zproxyhandler.proxy_openN)rHrIrJrrirrrrrLxs 0rLcsfdd}|S)NcsBt|tjr|dt|D]}||qdS||dSNr)r'r. httpsendfileseekr filechunkiter)rdatachunkorgsendrr _sendfiles   z _gen_sendfile.._sendfiler)rurvrrtr _gen_sendfiles  rws httpshandlerc@s eZdZeejjZddZdS)rcCs4t|dd}|r|jr|d|_|Stj|S)Nproxyres)r will_closecloserxr HTTPConnection getresponse)rrxrrrr|s  zhttpconnection.getresponseN)rHrIrJrwr r{sendr|rrrrrs  rcCs|j}|r|dddvrd|}d}nt|}d}|s$|t|krOtt|}|s3|jdkrOd |j |j p;dg|_ |j |_ |j |jjdSd|_ d|_ dS) N)zhttp://zhttps:/zhttps://TFshttpsrds443) _tunnel_hostr getselector getfullurlrr2r r+schemejoinrlport realhostportheaderscopyupdateparent addheaders)handlerhrj tunnel_host new_tunnelr@rrr_generic_start_transactions    rc s^fddjD}djt|D] }d|qdi}tjs0j|d<jjfdj i| \}}}|t j krKn t tfd d d q>|_|_jd krot tjjddS|d krwd_n|drd_n |dkrd_nt |jdkrd_d_d_t t_dSt j_dj_jd}|r|dkrd_d_nd_ _jd}|rjszt!|_Wn t"yd_Yn wjdkrd_nd_|t j#ks|t j$ksd|krd ksnj dkrd_js*js*jdur*d__%dS)Ncs2i|]}|drt|tj|qS)zproxy-)rNrZr bytestrr)rPxrrr s  z(_generic_proxytunnel..sCONNECT %s HTTP/1.0 s%s: %s s sstrictmethodTcsjSr)fpreadlinerSr)r=rrsz&_generic_proxytunnel..rTsHTTP/1.0 sHTTP/1. sHTTP/0.9 rrFstransfer-encodingschunkedscontent-lengthdsHEAD)&rr}rr iteritemsispy3strictresponse_classsock_method _read_statushttplibCONTINUElistiterstatusrSreasonrrversionrZUnknownProtocollengthchunkedry HTTPMessagestringiorE getheaderrN chunk_left _check_closeint ValueError NO_CONTENT NOT_MODIFIEDrx) r proxyheadersheaderkwargsrrrtrencrr)r=rr_generic_proxytunnels~                   rc@rK) httphandlercCs |t|Sr)do_openrrrjrrr http_openRs zhttphandler.http_opencCt|||tj|||Sr)rr HTTPHandler_start_transactionrrrjrrrrU zhttphandler._start_transactionN)rHrIrJrrrrrrrQs rc@s*eZdZddZejdkrddZdSdS)logginghttpconnectioncOs$tjj|g|Ri|||_dSr)r r{r_create_connection)r createconnargsrrrrr[ zlogginghttpconnection.__init__)rer~r~cCs2||j|jf|j|j|_|jr|dSdSr)rrlrtimeoutsource_addressrr_tunnelrrrrconnectbs  zlogginghttpconnection.connectN)rHrIrJrsys version_inforrrrrrZs   rcs2eZdZdZd fdd ZddZddZZS) logginghttphandlerz"HTTP handler that logs socket I/O.Ncs(tt|j|d||_||_||_dSNr)superrr_logfh_logname _observeropts)rlogfhname observeroptsr __class__rrrms zlogginghttphandler.__init__cCs||j|Sr)r_makeconnectionrrrrrwrGzlogginghttphandler.http_opencs"fdd}t|g|Ri|S)Ncs,tj|i|}tjj|jfijSr)socketcreate_connectionr makeloggingsocketrrr)rrrrrrcreateconnection{s  z.createconnection)r)rrrrrrrrzs z"logginghttphandler._makeconnectionr)rHrIrJ__doc__rrr __classcell__rrrrrjs  rc@sFeZdZejZejZe ej Z e ej Z   dddZddZdS)httpsconnectionNcOs.tjj|||g|Ri|||_||_dSr)r r{rkey_file cert_file)rrlrrrrrrrrrs  zhttpsconnection.__init__cCsht|j|jf|_|j}|jrt||jddd}tj |j|j |j |j |d|_t |jdS)Nrdrr)rserverhostname)rrrlrrrrrsplitr wrapsocketrrrvalidatesocket)rrlrrrrszhttpsconnection.connect)NNN)rHrIrJr HTTPResponserr HTTPS_PORT default_portrwsafesendr}wrapgetresponser{r|rrrrrrrs   rc@s0eZdZd ddZddZddZd dd ZdS) httpshandlerNcCs8tjj||dtj|||_t|j|jj|_dSr) r KeepAliveHandlerrr`rrrhttppasswordmgrdbpwmgr)rrrrrrrs zhttpshandler.__init__cCrr)rr rrrrrrrrzhttpshandler._start_transactioncCsbt|}|j|\}}t|j||}|r'|\}}||_|jd|nd|_| |j |S)Nr%) r rrrFr.r/rr?r1rr)rrjr2r#passwordr=r>r?rrr https_opens zhttpshandler.https_opencOsd}d}t|dkr|d}t|dkr|d}|dd}|jr5d|jvr5d|jvr5|jd}|jd}t||||g|Ri|}|j|_|S)Nrrreskeyscert)rDr?rr)rrlrrrkeyfilecertfileconnrrrrs&     zhttpshandler._makeconnectionr)rHrIrJrrrrrrrrrs  rc@$eZdZddZddZddZdS)httpdigestauthhandlercOs$tjj|g|Ri|d|_dSr)r`rr retried_reqrrrrrrrrzhttpdigestauthhandler.__init__cCdSrrrrrrreset_retry_countz'httpdigestauthhandler.reset_retry_countcC*||jur ||_d|_tj|||||Srn)rretriedr`rhttp_error_auth_reqedr auth_headerrlrjrrrrr  z+httpdigestauthhandler.http_error_auth_reqedN)rHrIrJrrrrrrrrs rc@s<eZdZddZddZddZddZd d Zd d Zd S)httpbasicauthhandlercOs*d|_tjj|g|Ri|d|_dSr)r?r`rrrrrrrrs zhttpbasicauthhandler.__init__cC|jr ||j|j|Srr?add_unredirected_headerrrrequestrrr http_requestz!httpbasicauthhandler.http_requestcCrrrr rrr https_requestr z"httpbasicauthhandler.https_requestcCrrrrrrrrrz&httpbasicauthhandler.reset_retry_countcCrrn)rrr`rrrrrrrrz*httpbasicauthhandler.http_error_auth_reqedcCs|j|t|\}}|durCdt|t|f}dtt| }| |j d|kr3dS||_ | |j ||j|SdS)Ns%s:%szBasic %s)r$r*r rr r+r-base64 b64encoderS get_headerrr?rropen)rrlrjr!r#pwrawr?rrrretry_http_basic_auths  z*httpbasicauthhandler.retry_http_basic_authN) rHrIrJrr r rrrrrrrrs rc@r) cookiehandlerc Csd|_|dd}|s dSt|}ztjt|}|||_WdStjj yH}z| t d|t |fWYd}~dSd}~ww)Nsauths cookiefiles?(error loading cookie file %s: %s; continuing without cookies) ) cookiejarrXr expandpath cookielibMozillaCookieJarr fsdecodeload LoadErrorwarnrr forcebytestr)rr cookiefilerrmrrrr!s*    zcookiehandler.__init__cC|jr |j||Srradd_cookie_headerr rrrr 8 zcookiehandler.http_requestcCr rr!r rrrr >r#zcookiehandler.https_requestN)rHrIrJrr r rrrrr s rsTcsztdd}g}|r|t|||pi|dn|t|dtr,|t|d|ttj |durk|\} } } } | d\} }| | ksR| rZ | | | |  d| | rfdt | pgdf|ttf|fd d tD|ttj|}d|_d|_d|_|sd t}d t|fg|_n d t|fg|_|r|jd |S)a@ construct an opener suitable for urllib2 authinfo will be added to the password manager The opener can be configured to log socket events if the various ``logging*`` arguments are specified. ``loggingfh`` denotes a file object to log events to. ``loggingname`` denotes the name of the to print when logging. ``loggingopts`` is a dict of keyword arguments to pass to the constructed ``util.socketobserver`` instance. ``sendaccept`` allows controlling whether the ``Accept`` request header is sent. The header is sent by default. shttpstimeoutrNrrArBrCcsg|]}|qSrr)rPrpassmgrrrrrRszopener..s"mercurial/proto-1.0 (Mercurial %s)z User-agent)Acceptzapplication/mercurial-0.1) configwithfloatappendrr has_httpsrrLrrrFr r1rDr[rr handlerfuncsrr` buildopener requestscountsentbytescountreceivedbytescountr rr sysstrr)rr< useragent loggingfh loggingname loggingopts sendacceptrhandlersr!urisr#r$ saveduser savedpassopeneragentrr%rr;HsN        r;cCstt|}|jr|j|_|\}}ntt|}dt t t |}d}t |||dt||S)Nsfile://)r6)rr2rrNr<r normpathabspathr r+r` pathname2urlrr;rr-)rurl_rrr6r@r<pathrrrrs   rcs&|jGfddd|j}||_dS)zWrap a response object with common error handlers. This ensures that any I/O from any consumer raises the appropriate error and messaging. cseZdZdfdd ZdS)z!wrapresponse..readerproxyNc sz|WStjy5}z#|jr$t|j}|j|}td||f}ntd}tj|tddd}~wtjyN}z tjtd|tddd}~ww)NsBHTTP request error (incomplete response; expected %d bytes got %d)s(HTTP request error (incomplete response)svthis may be an intermittent network failure; if the error persists, consider contacting the network or server operator)hintsHTTP request error (%s)) rIncompleteReadexpectedrDpartialrrPeerTransportError HTTPException)rsizermgottotalrEorigreadrrreads8    z&wrapresponse..readerproxy.readr)rHrIrJrMrrKrr readerproxysrNN)rMr)resprNrrKr wrapresponses "rPr)NNNr$NT)NT)2 __future__rrrri18nrr rrrrr.r r r r utilsrrrrurlerrr`robjectrrLrw safehasattrr+r{rrrrrrrrrrr basehandlerrr,r;rrPrrrrsN   (  A> d $01% a