
    	^cSY                        d dl Z d dlZddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZ ddlmZmZ ej        Zej        Zej        Zej        Zd'dZ G d d          Z G d	 d
ej                  Zd Z ej        ed          Z G d de	j                  Zd Zd(dZ G d de	j                  Z G d de	j                  Z  G d de          Z!er, G d de	j                  Z" G d de	j#        ej$                  Z$ G d dej%                  Z% G d dej&                  Z& G d  d!ej'                  Z(g Z)	 	 	 	 	 	 d)d$Z*d*d%Z+d& Z,dS )+    N   )_)encodingerrorhttpconnection	keepalivepycompatsslutilurllibcompatutil)
stringutilurlutilc                     |                      dd          } |                      dd          } |                      dd          } |r|                      dd          } | S )	a1  Replace 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   &amp;   <s   &lt;   >s   &gt;   "s   &quot;)replace)squotes     //usr/lib/python3/dist-packages/mercurial/url.pyescaper   $   s]     	
		$!!A			$  A			$  A 'IIdI&&H    c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )passwordmgrc                 "    || _         || _        d S N)uipasswddb)selfr   r   s      r   __init__zpasswordmgr.__init__5   s     r   c                 <    | j                             ||||          S r   )r   add_password)r    realmuriuserpasswds        r   r#   zpasswordmgr.add_password9   s    }))%dFCCCr   c                 0   t          |t          d           t          f          sJ t          |t                    sJ | j                            ||          }|\  }}t          j        |          t          j        |          }}|r@|r>|                     ||           t          j        |          t          j        |          fS |r|sit          j
        | j        ||          }|rL|\  }}|                    d          |                    d          }}| j                            d|z             |r|st          j        t          j        |                    }	d |	_        | j                                        sCt%          j        t)          d          t          j        t-          |	                    z            | j                            t)          d          t          j        t-          |	                    z             | j                            t)          d          t          j        |          z             |r+| j                            t)          d          |z             n)| j                            t)          d          d 	          }|s| j                                        }|d
S | j                            ||||           |                     ||           t          j        |          t          j        |          fS )Ns   usernames   password#   using auth.%s.* for authentication
s"   http authorization required for %ss#   http authorization required for %s
s
   realm: %s
s	   user: %s
s   user:)default)NN)
isinstancetypestrr   find_user_passwordr	   bytesurl_writedebugstrurlhttpconnectionmodreadauthforurir   getdebugr   urlqueryinteractiver   Abortr   hidepasswordbyteswritepromptgetpassr#   )
r    r$   authuriauthinfor&   r'   resgroupauthus
             r   r.   zpasswordmgr.find_user_password<   s   %$t**c!233333'3'''''=33E7CCf(..0A&0I0If 	DF 	DT6***OD))8?6+B+BCC 	O6 	O#247GTJJC O!t#xx44dhh{6K6KfEMNNN 	+6 	+H-g6677AAG7&&(( k;<<*588445  
 GMM9::&uQxx001   GMM!N++h.?.F.FFGGG Aa..56666w~~akk4~@@ +** <:""5'4@@@v&&&%%xv'>'>??r   c                     t          d          }| j                            |||rdt          |          z  pdfz             d S )N    http auth: user %s, password %s
   *   not set)r   r   r5   len)r    r&   r'   msgs       r   r0   zpasswordmgr._writedebugm   sI    455cT6#@dS[[.@#NJOOPPPPPr   c                 8    | j                             d |          S r   )r   r.   )r    r?   s     r   find_stored_passwordz passwordmgr.find_stored_passwordq   s    }//g>>>r   N)__name__
__module____qualname__r!   r#   r.   r0   rL    r   r   r   r   4   si        ! ! !D D D/@ /@ /@bQ Q Q? ? ? ? ?r   r   c                       e Zd Zd Zd ZdS )proxyhandlerc                    |                     dd          pt          j                            d          }|r{|                    d          s|                    d          sd|z   dz   }t          j        |          }|j        s6|                     dd          |_        |                     dd          |_        d	d
g}|	                    d |
                    dd          D                        |	                    d t          j                            dd                              d          D                        |                    dd          rg | _        n|| _        t          |          }||d}|                    dt          j        t#          |                    z             ni }t$          j                            | |           || _        d S )Ns
   http_proxys   hosts   http:s   https:s   http://   /s   users   passwds	   localhosts	   127.0.0.1c                 6    g | ]}|                                 S rP   )lower.0ps     r   
<listcomp>z)proxyhandler.__init__.<locals>.<listcomp>   s     HHHqHHHr   s   noc                     g | ]<}|                                 |                                                                 =S rP   )striprV   rW   s     r   rZ   z)proxyhandler.__init__.<locals>.<listcomp>   sH       wwyyGGIIOO%%  r   s   no_proxyr      ,s   always)httphttpss   proxying through %s
)configr   environr4   
startswithr   r6   r&   r'   extend
configlistsplit
configboolno_listr-   r5   r:   r;   urlreqrR   r!   r   )r    r   proxyurlproxyrg   proxiess         r   r!   zproxyhandler.__init__v   s   99]G44 
8H8L8L9
 9

  %	 ##H--8191D1DY1O1O8 &047K))E: CYY}g>>
!yy	BB $\2GNNHHBMM-$G$GHHH   NN %-11+sCCII$OO     }}]I66 '!& 5zzH'(;;GHH(7+?e+M+MM    G$$T7333r   c                    t          j        t          j        |                                        d          d         }| j        D ]u}||k    r d S |                    d          r |                    |dd                    r d S |                    d          r |                    |dd                    r d S vt          j	        
                    | |||          S )N   :r   s   *.      .r   )r	   r/   r   gethostre   rg   rb   endswithrh   rR   
proxy_open)r    reqrj   type_hostes         r   rr   zproxyhandler.proxy_open   s     !5c!:!:;;AA$GGJ 	 	Aqyytt||E"" t}}QqrrU';'; tt||D!! dmmAabbE&:&: tt"--dCFFFr   N)rM   rN   rO   r!   rr   rP   r   r   rR   rR   u   s8        . . .`
G 
G 
G 
G 
Gr   rR   c                       fd}|S )Nc                     t          |t          j                  r:|                    d           t	          j        |          D ]} | |           d S  | |           d S Nr   )r+   r2   httpsendfileseekr   filechunkiter)r    datachunkorgsends      r   	_sendfilez _gen_sendfile.<locals>._sendfile   s{    d-:;; 	 IIaLLL+D11 % %e$$$$% % GD$r   rP   )r   r   s   ` r   _gen_sendfiler      s$              r   s   httpshandlerc                   8    e Zd Z eej        j                  ZdS )r   N)rM   rN   rO   r   r   HTTPConnectionsendrP   r   r   r   r      s#        =1677DDDr   r   c                    |j         }|r|d d         dvrd|z   }d}nt          j        |          }d}|s|t          j        |          k    rt	          j        t          j        |                    }|s|j        dk    rld	                    |j
        |j        pdg          |_        |j                                        |_        |j                            | j        j                   d S d |_        d |_        d S )	N   )zhttp://zhttps:/zhttps://TFs   httpsrm   s   443)_tunnel_hostr   getselector
getfullurlr   r6   r	   r/   schemejoinru   portrealhostportheaderscopyupdateparent
addheaders)handlerhrs   tunnel_host
new_tunnelrD   s         r   _generic_start_transactionr      s    "K rr?"888${2K

".s33
 [L$;C$@$@@@K)+6677 	X--!YY1A6'CDDAN((**AIIW^6777FANAIIIr   r    httpsconnectionc                    | j         fdD             }| j        }|                     d|z             |                                D ]}|                     d|z             |                     d           |                     | j        | j                  	                                 \  }}}|t          j	        k    rn!t          t          fdd                     J|d	k    r)t          t          j        j        d                     d S |                                  t          j        d
||                                fz            )Nc                     i | ]V}|                                                     d           )t          j        |          t          j        |                   WS )zproxy-)rV   rb   r	   bytestr)rX   xr   s     r   
<dictcomp>z(_generic_proxytunnel.<locals>.<dictcomp>   sa       7799))X-gaj99  r   s   CONNECT %s HTTP/1.0
s   %s: %s
s   
)methodTc                  Z     j                                                                         S r   )fpreadliner\   )rA   s   r   <lambda>z&_generic_proxytunnel.<locals>.<lambda>   s    #&//++1133 r   r      zTunnel connection failed: %d %s)r   r   r   itemsresponse_classsock_method_read_statushttplibCONTINUElistiterr   r   closesocketr   r\   )	r    proxyheadersr   headerversionstatusreasonr   rA   s	          @@r   _generic_proxytunnelr      sr   lG     L
 $LII(<7888$$&& * *		-&())))IIg 

di

=
=C;"%"2"2"4"4W%%%T3333S99:::; }}T#&/7++,,,,,

l-0HH
 
 	
r   c                       e Zd Zd Zd ZdS )httphandlerc                 8    |                      t          |          S r   )do_openr   r    rs   s     r   	http_openzhttphandler.http_open  s    ||NC000r   c                 f    t          | ||           t          j                            | ||          S r   )r   r   HTTPHandler_start_transactionr    r   rs   s      r   r   zhttphandler._start_transaction  s/    "4C000$77aEEEr   N)rM   rN   rO   r   r   rP   r   r   r   r   
  s7        1 1 1F F F F Fr   r   c                       e Zd Zd ZdS )logginghttpconnectionc                 H    t          j        j        | g|R i | || _        d S r   )r   r   r!   _create_connection)r    
createconnargskwargss       r   r!   zlogginghttpconnection.__init__  s4     )$@@@@@@@",r   N)rM   rN   rO   r!   rP   r   r   r   r     s#        - - - - -r   r   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )logginghttphandlerz"HTTP handler that logs socket I/O.Nc                     t          t          |                               |           || _        || _        || _        d S Ntimeout)superr   r!   _logfh_logname_observeropts)r    logfhnameobserveroptsr   	__class__s        r   r!   zlogginghttphandler.__init__  sA     $''000AAA)r   c                 8    |                      | j        |          S r   )r   _makeconnectionr   s     r   r   zlogginghttphandler.http_open&  s    ||D0#666r   c                 .      fd}t          |g|R i |S )Nc                  l    t          j        | i |}t          j        j        |j        fi j        S r   )r   create_connectionr   makeloggingsocketr   r   r   )r   r   r   r    s      r   createconnectionz<logginghttphandler._makeconnection.<locals>.createconnection*  sH    +T<V<<D)T4= 484F  r   )r   )r    r   r   r   s   `   r   r   z"logginghttphandler._makeconnection)  s=    	 	 	 	 	 %%5GGGGGGGr   r   )rM   rN   rO   __doc__r!   r   r   __classcell__)r   s   @r   r   r     sh        ,,* * * * * *7 7 7H H H H H H Hr   r   c                       e Zd Zej        Zej        Z e	ej
                  Z ej        ej                  Z	 	 	 ddZd ZdS )r   Nc                 Z    t          j        j        | ||g|R i | || _        || _        d S r   )r   r   r!   key_file	cert_file)r    ru   r   r   r   r   r   s          r   r!   zhttpsconnection.__init__<  s>     $-dD$PPPPPPP$DM&DNNNr   c                 \   t          j        | j        | j        f          | _        | j        }| j        }|r+t          |            |                    dd          d         }t          j	        | j        | j
        | j        | j        |          | _        t          j        | j                   d S )Nrm   r   r   )r   serverhostname)r   r   ru   r   r   r   r   rsplitr
   
wrapsocketr   r   r   validatesocket)r    ru   r   s      r   connectzhttpsconnection.connectI  s    0$)TY1GHHDI9D,L 7$T***#**433A6*	7#  DI "49-----r   )NNN)rM   rN   rO   r   HTTPResponser   r   
HTTPS_PORTdefault_portr   safesendr   wrapgetresponser   getresponser!   r   rP   r   r   r   r   5  sy        "/)}Y/00/i/0FGG
 	' 	' 	' 	'	. 	. 	. 	. 	.r   c                   *    e Zd ZddZd Zd ZddZdS )httpshandlerNc                     t           j                            | |           t          j                            |            || _        t          | j        | j        j                  | _        d S r   )	r   KeepAliveHandlerr!   rh   r   r   r   httppasswordmgrdbpwmgr)r    r   r   s      r   r!   zhttpshandler.__init__[  sX    &//g/FFF((...DG$TWdg.GHHDJJJr   c                 f    t          | ||           t          j                            | ||          S r   )r   r   r   r   r   s      r   r   zhttpshandler._start_transactiona  s/    &tQ444-@@q#NNNr   c                 6   t          j        |          }| j                            |          \  }}t	          j        | j        ||          }|r*|\  }}|| _        | j                            d|z             nd | _        | 	                    | j
        |          S )Nr)   )r   r   r   rL   r2   r3   r   rC   r5   r   r   )r    rs   r6   r&   passwordrA   rB   rC   s           r   
https_openzhttpshandler.https_opene  s     )#..C!Z<<SAAND(#247CFFC !!t 	EMNNNN 	<< 4c:::r   c                 2   d }d }t          |          dk    r|d         }t          |          dk    r|d         }|dd          }| j        r,d| j        v r#d| j        v r| j        d         }| j        d         }t          ||||g|R i |}| j        |_        |S )Nr   r   rn   s   keys   cert)rI   rC   r   r   )r    ru   r   r   r   keyfilecertfileconns           r   r   zhttpshandler._makeconnections  s    GH4yyA~~q'4yyA~~78D y .Vty00W	5I5I)F+9W-"dGX04  8> D gDGKr   r   )rM   rN   rO   r!   r   r   r   rP   r   r   r   r   Z  sa        	I 	I 	I 	I	O 	O 	O	; 	; 	;	 	 	 	 	 	r   r   c                        e Zd Zd Zd Zd ZdS )httpdigestauthhandlerc                 H    t          j        j        | g|R i | d | _        d S r   )rh   r   r!   retried_reqr    r   r   s      r   r!   zhttpdigestauthhandler.__init__  s4    $-dDTDDDVDDDr   c                     d S r   rP   r    s    r   reset_retry_countz'httpdigestauthhandler.reset_retry_count  	     	r   c                 v    || j         ur|| _         d| _        t          j                            | ||||          S ry   )r   retriedrh   r   http_error_auth_reqedr    auth_headerru   rs   r   s        r   r  z+httpdigestauthhandler.http_error_auth_reqed  sE    d&&&"DDL+AA+tS'
 
 	
r   N)rM   rN   rO   r!   r  r  rP   r   r   r   r     sA               
 
 
 
 
r   r   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )httpbasicauthhandlerc                 V    d | _         t          j        j        | g|R i | d | _        d S r   )rC   rh   r
  r!   r   r   s      r   r!   zhttpbasicauthhandler.__init__  s;    	#,TCDCCCFCCCr   c                 T    | j         r |                    | j        | j                    |S r   rC   add_unredirected_headerr  r    requests     r   http_requestz!httpbasicauthhandler.http_request  -    9 	I++D,<diHHHr   c                 T    | j         r |                    | j        | j                    |S r   r  r  s     r   https_requestz"httpbasicauthhandler.https_request  r  r   c                     d S r   rP   r  s    r   r  z&httpbasicauthhandler.reset_retry_count  r  r   c                 v    || j         ur|| _         d| _        t          j                            | ||||          S ry   )r   r  rh   r
  r  r  s        r   r  z*httpbasicauthhandler.http_error_auth_reqed  sE    d&&&"DDL*@@+tS'
 
 	
r   c                    | j                             |t          j        |                    \  }}|dt	          j        |          t	          j        |          fz  }dt	          j        t          j        |          	                                          z  }|
                    | j        d           |k    rd S || _        |                    | j        |           | j                            |          S d S )Ns   %s:%szBasic %s)r'   r.   r   r   r	   r/   r1   base64	b64encoder\   
get_headerr  rC   r  r   open)r    ru   rs   r$   r&   pwrawrC   s           r   retry_http_basic_authz*httpbasicauthhandler.retry_http_basic_auth  s    ;11<*3//
 
b >h/55x7H7L7LMMC0@0E0E0K0K0M0M N NND~~d.55==tDI''(8$???;##C(((4r   N)	rM   rN   rO   r!   r  r  r  r  r  rP   r   r   r
  r
    sn             
      
 
 
    r   r
  c                        e Zd Zd Zd Zd ZdS )cookiehandlerc                    d | _         |                    dd          }|sd S t          j        |          }	 t          j                            t          j        |                    }|                                 || _         d S # t          j        j	        $ rD}|
                    t          d          |t          j        |          fz             Y d }~d S d }~ww xY w)Ns   auths
   cookiefiles?   (error loading cookie file %s: %s; continuing without cookies)
)	cookiejarr`   r   
expandpath	cookielibMozillaCookieJarr	   fsdecodeload	LoadErrorwarnr   r   forcebytestr)r    r   
cookiefiler"  rv   s        r   r!   zcookiehandler.__init__  s   YYw66
 	F_Z00
	77!*-- I NN&DNNN~' 	 	 	GG"  z6q99:	;        	s   AB C9CCc                 H    | j         r| j                             |           |S r   r"  add_cookie_headerr  s     r   r  zcookiehandler.http_request  '    > 	6N,,W555r   c                 H    | j         r| j                             |           |S r   r-  r  s     r   r  zcookiehandler.https_request  r/  r   N)rM   rN   rO   r!   r  r  rP   r   r   r   r     sA          .      r   r      sTc                                           t          dd          }g }|r)|                    t          |||pi |                     nN|                    t	          |                     t
          r$|                    t           |                     |                    t                                t            j	                  |s|\  }	}
}}
                    |
d                   \  }}||k    s|r                    |	|
||                                d||rdt          |          z  pdfz             |                    t                    t!                    f           |                     fd	t"          D                        |                    t%                                t'          j        | }d|_        d|_        d|_        |s3d
t1          j                    z  }dt5          j        |          fg|_        ndt5          j        |          fg|_        |r|j                            d           |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.
    s   https   timeoutr   Nr   rF   rG   rH   c                 (    g | ]} |          S rP   rP   )rX   r   passmgrr   s     r   rZ   zopener.<locals>.<listcomp>1  s#    :::QQr7^^:::r   s"   mercurial/proto-1.0 (Mercurial %s)z
User-agent)Acceptzapplication/mercurial-0.1)
configwithfloatappendr   r   	has_httpsr   rR   r   r   rL   r#   r5   rI   rc   r
  r   handlerfuncsr   rh   buildopenerrequestscountsentbytescountreceivedbytescountr   r   r	   sysstrr   )r   r@   	useragent	loggingfhloggingnameloggingopts
sendacceptr   handlersr$   urisr&   r'   	saveduser	savedpassopeneragentr4  s   `                @r   rI  rI    s   0 mmE7J77GH ?;(9r7  	
 	
 	
 	
 	G444555 	?OOLW===>>>OOL$$%%%"b233G$,!tT6&;;DGDD	99  dF;;;
0V2s6{{ 2@jAB	
 	
 	

 OO	g	&	&(=g(F(FG   OO:::::\:::;;;OOM"%%&&&*F FF !F"  I5F*HOE,B,BCD*HOI,F,FGH  J  !HIIIMr   c                    t          j        |          }|j        r6|j                                        |_        |                                \  }}nit          j        t          j        |                    }dt          j	        t                              t          j        |                              z   }d }t          | ||                              t          j        |          |          S )Ns   file://)rD  )r   r6   r   rV   r@   r   normpathabspathr	   r/   rh   pathname2urlr&  rI  r  r1   )r   url_r}   rD  rD   r@   paths          r   r  r  Y  s    DAx 8>>##hh}T\$//00H- 1$ 7 788
 
 
 "h:666;;t  r   c                 N    | j          G fdd| j                  }|| _        dS )zWrap a response object with common error handlers.

    This ensures that any I/O from any consumer raises the appropriate
    error and messaging.
    c                       e Zd Zd fd	ZdS )!wrapresponse.<locals>.readerproxyNc                    	  |          S # t           j        $ rq}|j        r3t          |j                  }|j        |z   }t          d          ||fz  }nt          d          }t          j        |t          d                    d }~wt           j        $ r8}t          j        t          d          |z  t          d                    d }~ww xY w)NsB   HTTP request error (incomplete response; expected %d bytes got %d)s(   HTTP request error (incomplete response)sv   this may be an intermittent network failure; if the error persists, consider contacting the network or server operator)hints   HTTP request error (%s))	r   IncompleteReadexpectedrI   partialr   r   PeerTransportErrorHTTPException)r    sizerv   gottotalrJ   origreads         r   readz&wrapresponse.<locals>.readerproxy.readr  s   x~~%)   : Iai..CJ,E5  %CC
 GHHC.6     (   .011A56    s!   
 CA,B		C3CCr   )rM   rN   rO   r_  )r^  s   r   readerproxyrS  q  s3        	 	 	 	 	 	 	 	r   r`  N)r_  r   )respr`  r^  s     @r   wrapresponserb  i  sP     yH             dn      D !DNNNr   r   )r    r   )NNNr1  NT)NT)-r  r   i18nr    r   r   r   r2   r   r	   r
   r   r   utilsr   r   r   stringiourlerrrh   r   r   rR   r   safehasattrr9  r   r   r   r   r   r   r   r   r   r   r   r
  basehandlerr   r:  rI  r  rb  rP   r   r   <module>rj     s          	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	       
 ,=		    >? >? >? >? >? >? >? >?B;G ;G ;G ;G ;G6& ;G ;G ;G|   DV_55	8 8 8 8 8Y- 8 8 8  ,&
 &
 &
 &
RF F F F F)' F F F- - - - -I4 - - -H H H H H H H H4  T#. #. #. #. #.)2 #. #. #.J- - - - -y163F - - -`
 
 
 
 
F8 
 
 
*. . . . .66 . . .b" " " " "F& " " "J 
 ^ ^ ^ ^B    *! *! *! *! *!r   