
    q&fZ                        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ed      Z G d de	j6                        Zd Z G d de	j:                        Z G d de	j6                        Z G d de      Z er9d$dZ! G d de	j6                        Z" G d de	jF                  ejH                        Z$ G d dejJ                        Z% G d dejL                        Z& G d dejN                        Z(g Z)	 	 	 	 	 	 d%d Z*d&d!Z+d" Z,y)'    N   )_)encodingerrorhttpconnection	keepalivepycompatsslutilurllibcompatutil)
stringutilurlutilc                     | j                  dd      } | j                  dd      } | j                  dd      } |r| j                  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   $   sL     	
		$!A			$ A			$ AIIdI&H    c                   *    e Zd Zd Zd Zd Zd Zd Zy)passwordmgrc                      || _         || _        y N)uipasswddb)selfr   r   s      r   __init__zpasswordmgr.__init__5   s     r   c                 >    | j                   j                  ||||      S r   )r   add_password)r    realmuriuserpasswds        r   r#   zpasswordmgr.add_password9   s    }}))%dFCCr   c                    t        |t        d       t        f      sJ t        |t              sJ | j                  j	                  ||      }|\  }}t        j                  |      t        j                  |      }}|r>|r<| j                  ||       t        j                  |      t        j                  |      fS |r|sht        j                  | j                  ||      }|rE|\  }}|j                  d      |j                  d      }}| j                  j                  d|z         |r|sot        j                  t        j                  |            }	d |	_        | j                  j#                         s=t%        j&                  t)        d      t        j*                  t-        |	            z        | j                  j/                  t)        d      t        j*                  t-        |	            z         | j                  j/                  t)        d      t        j                  |      z         |r(| j                  j/                  t)        d      |z         n&| j                  j1                  t)        d      d 	      }|s| j                  j3                         }|y
| j                  j5                  ||||       | 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<   sM   %$t*c!2333'3'''==33E7Cf((.0A0A&0IfFT6*OOD)8??6+BCC6#22477GTJC!t#xx4dhh{6KfEMN6H--g67AAG77&&(kk;<**5845 
 GGMM9:&&uQx01 GGMM!N+h.?.?.FFGa.56ww~~ak4~@* <""5'4@v&%xv'>??r   c                     t        d      }| j                  j                  |||xr dt        |      z  xs dfz         y )N    http auth: user %s, password %s
   *   not set)r   r   r5   len)r    r&   r'   msgs       r   r0   zpasswordmgr._writedebugm   s:    45cT6#@dS[.@#NJOOPr   c                 :    | j                   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   s    !D/@bQ?r   r   c                       e Zd Zd Zd Zy)proxyhandlerc                    |j                  dd      xs t        j                  j                  d      }|r|j	                  d      s|j	                  d      sd|z   dz   }t        j                  |      }|j                  s.|j                  dd      |_        |j                  dd      |_        d	d
g}|j                  |j                  dd      D cg c]  }|j                          c}       |j                  t        j                  j                  dd      j                  d      D cg c]0  }|j                         r|j                         j                         2 c}       |j                  dd      rg | _        n|| _        t!        |      }||d}|j#                  dt        j$                  t'        |            z         ni }t(        j*                  j-                  | |       || _        y c c}w c c}w )Ns
   http_proxys   hosts   http:s   https:s   http://   /s   users   passwds	   localhosts	   127.0.0.1s   nos   no_proxyr      ,s   always)httphttpss   proxying through %s
)configr   environr4   
startswithr   r6   r&   r'   extend
configlistlowersplitstrip
configboolno_listr-   r5   r:   r;   urlreqrR   r!   r   )r    r   proxyurlproxyra   pproxiess          r   r!   zproxyhandler.__init__v   s   99]G4 
8H8H8L8L9

  ##H-1D1DY1O%047KK)E::YY}g>
!yy	B $\2GNN$&MM-$GHqH NN &--11+sCII$Owwy GGIOO% }}]I6!& 5zH'(;GHH(7+?+?e+MM G$$T735 Is   G5-5G:c                    t        j                  t        j                  |            j	                  d      d   }| j
                  D ]X  }||k(  r y |j                  d      r|j                  |dd        r y |j                  d      sC|j                  |dd        sX y  t        j                  j                  | |||      S )N   :r   s   *.      .r   )r	   r/   r   gethostr^   ra   rZ   endswithrb   rR   
proxy_open)r    reqrd   type_hostes         r   rm   zproxyhandler.proxy_open   s      !5!5c!:;AA$GJ 	Aqy||E"t}}QqrU';||D!dmmAabE&:	 ""--dCFFr   N)rM   rN   rO   r!   rm   rP   r   r   rR   rR   u   s    .`
Gr   rR   c                       fd}|S )Nc                     t        |t        j                        r5|j                  d       t	        j
                  |      D ]  } | |        y  | |       y Nr   )r+   r2   httpsendfileseekr   filechunkiter)r    datachunkorgsends      r   	_sendfilez _gen_sendfile.<locals>._sendfile   sO    d-::;IIaL++D1 %e$% D$r   rP   )rz   r{   s   ` r   _gen_sendfiler|      s      r   httpshandlerc                   D    e Zd Z eej
                  j                        Zy)r   N)rM   rN   rO   r|   r   HTTPConnectionsendrP   r   r   r   r      s    11667Dr   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(  rdj                  |j                  |j                  xs dg      |_        |j                  j                         |_        |j                  j                  | j                   j"                         y d |_        d |_        y )	N   )zhttp://zhttps:/zhttps://TFs   httpsrh   s   443)_tunnel_hostr   getselector
getfullurlr   r6   r	   r/   schemejoinrp   portrealhostportheaderscopyupdateparent
addheaders)handlerhrn   tunnel_host
new_tunnelrD   s         r   _generic_start_transactionr      s    ""Kr?"88${2K
"..s3
[L$;$;C$@@KK))+67X-!YY1A6'CDAN((*AIIIW^^667ANAIr   c                       e Zd Zd Zd Zy)httphandlerc                 .    | j                  t        |      S r   )do_openr   r    rn   s     r   	http_openzhttphandler.http_open   s    ||NC00r   c                 ^    t        | ||       t        j                  j                  | ||      S r   )r   r   HTTPHandler_start_transactionr    r   rn   s      r   r   zhttphandler._start_transaction   s)    "4C0$$77aEEr   N)rM   rN   rO   r   r   rP   r   r   r   r      s    1Fr   r   c                       e Zd Zd Zy)logginghttpconnectionc                 X    t        j                  j                  | g|i | || _        y r   )r   r   r!   _create_connection)r    
createconnargskwargss       r   r!   zlogginghttpconnection.__init__   s(      ))$@@@",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.c                 X    t         t        |   |       || _        || _        || _        y Ntimeout)superr   r!   _logfh_logname_observeropts)r    logfhnameobserveroptsr   	__class__s        r   r!   zlogginghttphandler.__init__   s,     $00A)r   c                 :    | j                  | j                  |      S r   )r   _makeconnectionr   s     r   r   zlogginghttphandler.http_open   s    ||D00#66r   c                 ,      fd}t        |g|i |S )Nc                      t        j                  | i |}t        j                  j                  |j
                  fi j                  S r   )socketcreate_connectionr   makeloggingsocketr   r   r   )r   r   sockr    s      r   createconnectionz<logginghttphandler._makeconnection.<locals>.createconnection  sH    ++T<V<D))T4==484F4F r   )r   )r    r   r   r   s   `   r   r   z"logginghttphandler._makeconnection   s     	 %%5GGGGr   r   )rM   rN   rO   __doc__r!   r   r   __classcell__)r   s   @r   r   r      s    ,*7Hr   r   httpsconnectionc                 *  	 | j                   }|D ci c]M  }|j                         j                  d      r,t        j                  |      t        j                  ||         O }}| j
                  }| j                  d|z         |j                         D ]  }| j                  d|z          | j                  d       | j                  | j                  | j                        		 	j                         \  }}}|t        j                  k7  rnt        t        	fdd             A|dk(  r*t        t        	j                   j"                  d             y | j%                          t'        j(                  d	||j+                         fz        c c}w )
Nzproxy-s   CONNECT %s HTTP/1.0
s   %s: %s
s   
)methodc                  T     j                   j                         j                         S r   )fpreadliner_   )rA   s   r   <lambda>z&_generic_proxytunnel.<locals>.<lambda>)  s    cffoo/557 r   r      zTunnel connection failed: %d %s)r   r]   rZ   r	   bytestrr   r   itemsresponse_classr   _method_read_statushttplibCONTINUElistiterr   r   closer   r   r_   )
r    r   xproxyheadersr   headerversionstatusreasonrA   s
            @r   _generic_proxytunnelr     sd   ,, 
wwy##H- Q!1!1'!*!==
 

 ((		,|;<"((* 	.FIImf,-	.		' !!$))DLL!A &)&6&6&8#GVV)))7=>  S=cffoow/0JJL,,1VV\\^4LL E
s   AF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y)r   Nc                 j    t        j                  j                  | ||g|i | || _        || _        y r   )r   r   r!   key_file	cert_file)r    rp   r   r   r   r   r   s          r   r!   zhttpsconnection.__init__;  s4     $$--dD$PPP$DM&DNr   c                    t        j                  | j                  | j                  f| j                        | _        | j                  }| j                  }|r t        |        |j                  dd      d   }t        j                  | j
                  | j                  | j                  | j                  |      | _        t        j                  | j
                         y )Nrh   r   r   )r   serverhostname)r   r   rp   r   r   r   r   r   rsplitr
   
wrapsocketr   r   r   validatesocket)r    rp   r   s      r   connectzhttpsconnection.connectH  s    00DII&DI 99D,,L$T*#**43A6**		77#DI ""499-r   )NNN)rM   rN   rO   r   HTTPResponser   r   
HTTPS_PORTdefault_portr|   safesendr   wrapgetresponser   getresponser!   r   rP   r   r   r   r   4  sU    "//))Y//0/i//0F0FG
 	'	.r   c                   (    e Zd ZddZd Zd ZddZy)r}   Nc                     t         j                  j                  | |       t        j                  j                  |        || _        t        | j
                  | j
                  j                        | _        y r   )	r   KeepAliveHandlerr!   rb   r}   r   r   httppasswordmgrdbpwmgr)r    r   r   s      r   r!   zhttpshandler.__init__\  sR    &&//g/F((.DG$TWWdgg.G.GHDJr   c                 ^    t        | ||       t        j                  j                  | ||      S r   )r   r   r   r   r   s      r   r   zhttpshandler._start_transactionb  s)    &tQ4--@@q#NNr   c                 J   t        j                  |      }| j                  j                  |      \  }}t	        j
                  | j                  ||      }|r+|\  }}|| _        | j                  j                  d|z         nd | _        | j                  | j                  |      S )Nr)   )r   r   r   rL   r2   r3   r   rC   r5   r   r   )r    rn   r6   r&   passwordrA   rB   rC   s           r   
https_openzhttpshandler.https_openf  s     ))#.C!ZZ<<SAND(#22477CFC!t 	EMN 	<< 4 4c::r   c                 6   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|i |}| j                  |_        |S )Nr   r   ri   s   keys   cert)rI   rC   r   r   )r    rp   r   r   r   keyfilecertfileconns           r   r   zhttpshandler._makeconnectiont  s    GH4yA~q'4yA~78D yyVtyy0W		5I))F+99W-"dGX048>D ggDGKr   r   )rM   rN   rO   r!   r   r   r   rP   r   r   r}   r}   [  s    	I	O	;	r   c                       e Zd Zd Zd Zd Zy)httpdigestauthhandlerc                 X    t        j                  j                  | g|i | d | _        y r   )rb   r   r!   retried_reqr    r   r   s      r   r!   zhttpdigestauthhandler.__init__  s(    $$--dDTDVDr   c                      y r   rP   r    s    r   reset_retry_countz'httpdigestauthhandler.reset_retry_count       	r   c                     || j                   ur|| _         d| _        t        j                  j	                  | ||||      S rt   )r   retriedrb   r   http_error_auth_reqedr    auth_headerrp   rn   r   s        r   r   z+httpdigestauthhandler.http_error_auth_reqed  sC    d&&&"DDL++AA+tS'
 	
r   N)rM   rN   rO   r!   r   r   rP   r   r   r   r     s     
r   r   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)httpbasicauthhandlerc                 f    d | _         t        j                  j                  | g|i | d | _        y r   )rC   rb   r  r!   r   r   s      r   r!   zhttpbasicauthhandler.__init__  s/    	##,,TCDCFCr   c                 j    | j                   r&|j                  | j                  | j                          |S r   rC   add_unredirected_headerr  r    requests     r   http_requestz!httpbasicauthhandler.http_request  (    99++D,<,<diiHr   c                 j    | j                   r&|j                  | j                  | j                          |S r   r  r  s     r   https_requestz"httpbasicauthhandler.https_request  r  r   c                      y r   rP   r   s    r   r   z&httpbasicauthhandler.reset_retry_count  r   r   c                     || j                   ur|| _         d| _        t        j                  j	                  | ||||      S rt   )r   r   rb   r  r   r   s        r   r   z*httpbasicauthhandler.http_error_auth_reqed  sC    d&&&"DDL**@@+tS'
 	
r   c                    | j                   j                  |t        j                  |            \  }}|dt	        j
                  |      t	        j
                  |      fz  }dt	        j                  t        j                  |      j                               z  }|j                  | j                  d       |k(  ry || _        |j                  | j                  |       | j                  j                  |      S y )Ns   %s:%szBasic %s)r'   r.   r   r   r	   r/   r1   base64	b64encoder_   
get_headerr  rC   r  r   open)r    rp   rn   r$   r&   pwrawrC   s           r   retry_http_basic_authz*httpbasicauthhandler.retry_http_basic_auth  s    ;;11<**3/
b >h//5x7H7H7LMMC0@0@0E0K0K0M NND~~d..5=DI''(8(8$?;;##C((r   N)	rM   rN   rO   r!   r
  r  r   r   r  rP   r   r   r  r    s      

r   r  c                       e Zd Zd Zd Zd Zy)cookiehandlerc                    d | _         |j                  dd      }|sy t        j                  |      }	 t        j                  j                  t        j                  |            }|j                          || _         y # t        j                  j                  $ r<}|j                  t        d      |t        j                  |      fz         Y d }~y d }~ww xY w)Ns   auths
   cookiefiles?   (error loading cookie file %s: %s; continuing without cookies)
)	cookiejarrX   r   
expandpath	cookielibMozillaCookieJarr	   fsdecodeload	LoadErrorwarnr   r   forcebytestr)r    r   
cookiefiler  rq   s        r   r!   zcookiehandler.__init__  s    YYw6
__Z0
	77!!*-I NN&DN~~'' 	GG" z66q9:	; 	s   A	A= =C2CCc                 T    | j                   r| j                   j                  |       |S r   r  add_cookie_headerr  s     r   r
  zcookiehandler.http_request       >>NN,,W5r   c                 T    | j                   r| j                   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    s    .r   r  c           	      2   | j                  t        dd      }g }|r#|j                  t        |||xs i |             n=|j                  t	        |             t
        r|j                  t        | |             |j                  t        |              t        | | j                        }	|c|\  }
}}}|	j                  |d         \  }}||k7  s|r|	j                  |
|||       | j                  d||xr dt        |      z  xs dfz         |j                  t        |	      t!        |	      f       |j                  t"        D cg c]  } || |	       c}       |j                  t%        |              t'        j(                  | }d|_        d|_        d|_        |s5dt1        j2                         z  }d	t5        j6                  |      fg|_        nd	t5        j6                  |      fg|_        |r|j8                  j                  d
       |S c c}w )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   r   rF   rG   rH   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   r[   r  r   handlerfuncsr  rb   buildopenerrequestscountsentbytescountreceivedbytescountr   r   r	   sysstrr   )r   r@   	useragent	loggingfhloggingnameloggingopts
sendacceptr   handlerspassmgrr$   urisr&   r'   	saveduser	savedpassr   openeragents                      r   r@  r@    s   0 mmE7J7GH;(9r7	
 	G45OOR1 OOL$%"b223G$,!tT6&;;DGD	99  dF;
0V2s6{ 2@jAB	

 OO	g	&(=g(FG OO\:Qr7^:;OOM"%&*F FF !F" 5F*HOOE,BCD*HOOI,FGH   !HIMK ;s   	Hc                    t        j                  |      }|j                  r3|j                  j                         |_        |j	                         \  }}nht        j                  t        j                  |            }dt        j                  t        j                  t        j                  |                  z   }d }t        | ||      j                  t        j                  |      |      S )Ns   file://)r:  )r   r6   r   r]   r@   r   normpathabspathr	   r/   rb   pathname2urlr  r@  r  r1   )r   url_rx   r:  rD   r@   paths          r   r  r  ]  s    DAxx88>>#h}}T\\$/0H-- 1 1$ 78
 
 "h:6;;t r   c                 Z    | j                    G fdd| j                        }|| _        y)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y)!wrapresponse.<locals>.readerproxyNc                    	  |      S # t         j                  $ rq}|j                  r5t        |j                        }|j                  |z   }t        d      ||fz  }nt        d      }t        j                  |t        d            d }~wt         j                  $ r1}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    sizerq   gottotalrJ   origreads         r   readz&wrapresponse.<locals>.readerproxy.readv  s    ~%)) ::aii.CJJ,E5 %C
 GHC..6  (( ..01A56 s!    CA,B

C ,CCr   )rM   rN   rO   rV  )rU  s   r   readerproxyrJ  u  s    	r   rW  N)rV  r   )resprW  rU  s     @r   wrapresponserY  m  s(     yyH dnn  D !DNr   r   )r    r   )NNN   sNT)NT)-r  r   i18nr    r   r   r   r2   r   r	   r
   r   r   utilsr   r   r   stringiourlerrrb   r   r   rR   r|   hasattrr/  r   r   r   r   r   r   r   r   r   r}   r   r  basehandlerr  r0  r@  r  rY  rP   r   r   <module>rb     sj     	 	 	
 ,,==		 >? >?B;G6&& ;G| FN+	8Y-- 8,F)'' F-I44 -H H4 &P%.)22 %.N-y1163F3F -`
F88 
*.666 .b"F&& "J 
 aH *!r   