
    6hbX                    P   S r SSKJr  SSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  SSKJrJrJr  SSKJr  \R"                  r\R$                  r\R&                  rSq " S	 S
5      r " S S5      r " S S\\R.                  5      r " S S\R2                  5      rS rS r " S S\R8                  5      rS rS rSS jrS r SS jr!\"S:X  a3  SSK#r# \$" \RJ                  S   5      r&\RJ                  S   r'\!" \'\&5        gg! \(\)4 a    \*" S\RJ                  S   -  5         gf = f)aK  An HTTP handler for urllib2 that supports HTTP 1.1 and keepalive.

>>> import urllib2
>>> from keepalive import HTTPHandler
>>> keepalive_handler = HTTPHandler()
>>> opener = urlreq.buildopener(keepalive_handler)
>>> urlreq.installopener(opener)
>>>
>>> fo = urlreq.urlopen('http://www.python.org')

If a connection to a given host is requested, and all of the existing
connections are still in use, another connection will be opened.  If
the handler tries to use an existing connection but it fails in some
way, it will be closed and removed from the pool.

To remove the handler, simply re-run build_opener with no arguments, and
install that opener.

You can explicitly close connections by using the close_connection()
method of the returned file-like object (described below) or you can
use the handler methods:

  close_connection(host)
  close_all()
  open_connections()

NOTE: using the close_connection and close_all methods of the handler
should be done with care when using multiple threads.
  * there is nothing that prevents another thread from creating new
    connections immediately after connections are closed
  * no checks are done to prevent in-use connections from being closed

>>> keepalive_handler.close_all()

EXTRA ATTRIBUTES AND METHODS

  Upon a status of 200, the object returned has a few additional
  attributes and methods, which should not be used if you want to
  remain consistent with the normal urllib2-returned objects:

    close_connection()  -  close the connection to the host
    readlines()         -  you know, readlines()
    status              -  the return status (i.e. 404)
    reason              -  english translation of status (i.e. 'File not found')

  If you want the best of both worlds, use this inside an
  AttributeError-catching try:

  >>> try: status = fo.status
  >>> except AttributeError: status = None

  Unfortunately, these are ONLY there if status == 200, so it's not
  easy to distinguish between non-200 responses.  The reason is that
  urllib2 tries to do clever things with error codes 301, 302, 401,
  and 407, and it wraps the object upon return.
    )annotationsN   )_)hex)pycompaturllibcompatutil)procutilc                  @    \ rS rSrSrS rS rS rS rS r	SS	 jr
S
rg)ConnectionManagerl   zH
The connection manager must be able to:
  * keep track of all existing
c                    [         R                  " 5       U l        [        R                  " [
        5      U l        0 U l        0 U l        g N)		threadingLock_lockcollectionsdefaultdictlist_hostmap_connmap	_readymapselfs    5/usr/lib/python3/dist-packages/mercurial/keepalive.py__init__ConnectionManager.__init__r   s0    ^^%
#//5    c                    U R                   R                  5          U R                  U   R                  U5        XR                  U'   X0R
                  U'   U R                   R                  5         g ! U R                   R                  5         f = fr   )r   acquirer   appendr   r   release)r   host
connectionreadys       r   addConnectionManager.addx   sg    

	!MM$&&z2(,MM*%).NN:&JJ DJJ s   :A1 1Bc                   U R                   R                  5           U R                  U   nU R                  U	 U R                  U	 U R                  U   R                  U5        U R                  U   (       d  U R                  U	 U R                   R                  5         g ! [         a     N'f = f! U R                   R                  5         f = fr   )r   r    r   r   r   removeKeyErrorr"   )r   r$   r#   s      r   r)   ConnectionManager.remove   s    

	!	,}}Z0 MM*-NN:.d#**:6}}T*d+JJ    JJ s)   B  AB0  
B-*B0 ,B--B0 0Cc                B     X R                   U'   g ! [         a     g f = fr   )r   r*   )r   r$   r%   s      r   	set_readyConnectionManager.set_ready   s%    	).NN:& 		s    
c                ,   S nU R                   R                  5          U R                  U    H*  nU R                  U   (       d  M  SU R                  U'   Un  O   U R                   R	                  5         U$ ! U R                   R	                  5         f = f)NF)r   r    r   r   r"   )r   r#   conncs       r   get_ready_conn ConnectionManager.get_ready_conn   s    

	!]]4(>>!$$(-DNN1%D	 ) JJ  JJ s   $A7 A7 7BNc           
         U(       a  [        U R                  U   5      $ [        U R                  R                  5        VVs0 s H  u  p#U[        U5      _M     snn5      $ s  snnf r   )r   r   dictitems)r   r#   hconnss       r   get_allConnectionManager.get_all   sT    d+,,26--2E2E2GH2GJQDK2GH Hs   A"
)r   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r&   r)   r-   r2   r9   __static_attributes__ r   r   r   r   l   s%    
!! r   r   c                  X    \ rS rSrSS jrS rS rS rS rSS jr	S	 r
S
 rS rS rSrg)KeepAliveHandler   Nc                J    [        5       U l        Xl        SU l        SU l        g Nr   )r   _cm_timeoutrequestscountsentbytescount)r   timeouts     r   r   KeepAliveHandler.__init__   s!    $&r   c                    U R                   R                  5       R                  5        VVs/ s H  u  pU[        U5      4PM     snn$ s  snnf )zlreturn a list of connected hosts and the number of connections
to each.  [('foo.com:80', 2), ('bar.org', 1)])rG   r9   r6   len)r   r#   lis      r   open_connections!KeepAliveHandler.open_connections   s>     37((2B2B2D2J2J2LM2LJTs2w2LMMMs   A	c                    U R                   R                  U5       H.  nU R                   R                  U5        UR                  5         M0     g)zclose connection(s) to <host>
host is the host:port spec, as in 'www.cnn.com:8080' as passed in.
no error occurs if there is no connection to that host.N)rG   r9   r)   close)r   r#   r7   s      r   close_connection!KeepAliveHandler.close_connection   s6     !!$'AHHOOAGGI (r   c                    U R                   R                  5       R                  5        H9  u  pU H.  nU R                   R                  U5        UR	                  5         M0     M;     g)zclose all open connectionsN)rG   r9   r6   r)   rS   )r   r#   r8   r7   s       r   	close_allKeepAliveHandler.close_all   sG    88++-335KD"	  6r   c                <    U R                   R                  US5        g)z]tells us that this request is now closed and that the
connection is ready for another requestTN)rG   r-   )r   requestr#   r$   s       r   _request_closed KeepAliveHandler._request_closed   s     	:t,r   c                h    U(       a  UR                  5         U R                  R                  U5        g r   )rS   rG   r)   )r   r#   r$   rS   s       r   _remove_connection#KeepAliveHandler._remove_connection   s!    
#r   c                .    U R                  [        U5      $ r   )do_openHTTPConnection)r   reqs     r   	http_openKeepAliveHandler.http_open   s    ||NC00r   c                   [         R                  " U5      nU(       d  [        R                  S5      e U R                  R                  U5      nU(       ai  U R                  XBU5      nU(       a  OUR                  5         U R                  R                  U5        U R                  R                  U5      nU(       a  Mi  U" X0R                  S9n[        (       a   [        R                  SU[        U5      5        U R                  R                  X4S5        U R                  XB5        UR                  5       n [1        USS5      (       a  U R                  R                  U5        [        (       a+  [        R                  SUR2                  UR4                  5        Xl        X5l        UR;                  5       Ul        XEl        UR2                  Ul         URB                  Ul"        UR4                  Ul!        U$ ! [         R"                   aD  n[        R                  [%        S5      [&        R(                  " UR*                  5      -  5      eS nAf[,        [         R.                  4 a  n[        R                  U5      eS nAff = f)	Ns   no host given)rK   s"   creating new connection to %s (%d)Fs   bad HTTP status line: %s
will_closeTs   STATUS: %s, %s)#r   gethosturlerrurlerrorrG   r2   _reuse_connectionrS   r)   rH   DEBUGinfoidr&   _start_transactiongetresponsehttplibBadStatusLiner   r   sysbyteslineOSErrorHTTPExceptiongetattrstatusreason_handler_hostget_full_url_url_connectioncodemsgheaders)r   
http_classrc   r#   r7   rerrs          r   ra   KeepAliveHandler.do_open   s   ##C(//"233 	'''-A**148  	"HH++D1 ! t]];5JJ=tRU Te,''/MMO 1lD))HHOOA5JJ(!((AHH=
!!#EE	/ $$ 	//-.1B1B3881LL  ../ 	'//#&&	's1   ;G7 0AG7 ?A8G7 7J?I

J'I<<Jc                    U R                  X5        UR                  5       nUb  UR                  S:X  a/  [
        (       a   [
        R                  SU[        U5      5        SnU$ [
        (       a   [
        R                  SU[        U5      5        U$ ! [        [        R                  4 a    Sn N  [
        (       a   [
        R                  SU[        U5      5        U R                  R                  U5        UR                  5         e = f)a  start the transaction with a re-used connection
return a response object (r) upon success or None on failure.
This DOES not close or remove bad connections in cases where
it returns.  However, if an unexpected exception occurs, it
will close and remove the connection before re-raising.
Ns4   unexpected exception - closing connection to %s (%d)	   s&   failed to re-use connection to %s (%d)s   re-using connection to %s (%d))ro   rp   ru   rq   rv   rl   errorrn   rG   r)   rS   versionrm   )r   r7   rc   r#   r   s        r   rk   "KeepAliveHandler._reuse_connection  s    	##A+A2 9		Q
 u

=tRU A
  u

<dBqEJI ../ 	A	 uKqE
 HHOOAGGIs   !B D0ADc                   [        USS5      n[        R                  " U R                  R                  5      nUR                  [        UR                  R                  5       5      5        UR                  [        UR                  R                  5       5      5        [        R                  " S UR                  5        5       5      n0 nS H"  nXd;   d  M
  SUSUR                  SS5      -   '   M$      [        R                  " U5      (       a  [        R                  " U5      nUR                  " UR                  5       [        R                   " U5      40 UD6  S	U;  a  UR#                  S
S5        SU;  a  UR#                  SS[%        U5      -  5        O7UR                  " UR                  5       [        R                   " U5      40 UD6   UR                  5        H  u  pUR#                  X5        M     UR-                  5         [        R                  " U5      (       a  UR/                  W5        U =R0                  S-  sl        U =R2                  [        USS5      U-
  -  sl         U R                  =R0                  S-  sl        U R                  =R2                  [        USS5      U-
  -  sl        g ! [&         a  n[(        R+                  U5      eS nAff = f! [4         a     g f = f)NrJ   r   c              3  J   #    U  H  u  pUR                  5       U4v   M     g 7fr   )lower).0nvs      r   	<genexpr>6KeepAliveHandler._start_transaction.<locals>.<genexpr>K  s     K?41A?s   !#)r#   zaccept-encodingr   skip_-r   zcontent-typezContent-typez!application/x-www-form-urlencodedzcontent-lengthzContent-lengthz%d)rw   r	   sortdictparent
addheadersupdatesortedr   r6   unredirected_hdrsreplacer   hasdatagetdata
putrequest
get_methodgetselector	putheaderrN   ru   ri   rj   
endheaderssendrI   rJ   AttributeError)r   r7   rc   oldbytescountr   skipheadersr   datar   kr   s              r   ro   #KeepAliveHandler._start_transactionB  s_   #3Q7
 -- 6 67vckk//123vc3399;<=--K7==?KK,A|=>GaiiS&99: -	'##C((#++C0NN$ ,,S1 "
 "0KK&(K $72KK 0$T2BCNN$ ,,S1 " MMODAKK $	$$FF4L 	awq*:A>NN	KK%%*%KK&&+Q/-?&  	'//#&&	'"  		s2   2B#J9 6J9 ,AK  9
KKK 
K-,K-)rG   rH   rI   rJ   r   r   )r;   r<   r=   r>   r   rP   rT   rW   r[   r^   rd   ra   rk   ro   r@   rA   r   r   rC   rC      s7     N
-
$17r0d4r   rC   c                      \ rS rSrSrg)HTTPHandleriy  rA   N)r;   r<   r=   r>   r@   rA   r   r   r   r   y  s    r   r   c                  n   ^  \ rS rSrSS jrS rS rS rS rSU 4S jjr	SSU 4S jjjr
U 4S	 jrS
rU =r$ )HTTPResponsei}  c                    [         R                  R                  XX$S9  UR                  U l        S U l        SU l        SU l        SU l        S U l        S U l	        S U l
        S U l        g )N)
debuglevelmethodr   r   i  )rq   r   r   filenor   receivedbytescount_rbuf	_rbufsizerz   r{   r}   r~   )r   sockr   strictr   s        r   r   HTTPResponse.__init__  sg    %%: 	& 	
 kk	"#

	r   c                    U R                   (       ad  U R                   R                  5         S U l         U R                  (       a1  U R                  R                  X R                  U R
                  5        g g g r   )fprS   rz   r[   r{   r~   r   s    r   rS   HTTPResponse.close  sO    77GGMMODG}}--**d&6&6  r   c                $    U R                  5         g r   rS   r   s    r   _close_connHTTPResponse._close_conn  s    

r   c                    U R                   R                  U R                  U R                  SS9  U R	                  5         g )Nr   r   )rz   r^   r{   r~   rS   r   s    r   rT   HTTPResponse.close_connection  s.    ((T5E5EQ(O

r   c                    U R                   $ r   )r}   r   s    r   geturlHTTPResponse.geturl  s    yyr   c                J  > [         TU ]  U5      nU =R                  [        U5      -  sl        U R                  b(  U R                  =R                  [        U5      -  sl        U R
                  b2  U R
                  R                  =R                  [        U5      -  sl        U$ r   )superreadr   rN   r~   rz   r   )r   amtr   	__class__s      r   r   HTTPResponse.read  s{    w|C 3t9,'//3t9</==$MM  33s4y@3r   c                F  > [         TU ]  US9nU =R                  [        U5      -  sl        U R                  b(  U R                  =R                  [        U5      -  sl        U R
                  b2  U R
                  R                  =R                  [        U5      -  sl        U$ )N)limit)r   readliner   rN   r~   rz   r   )r   r   r   r   s      r   r   HTTPResponse.readline  s    we,3t9,'//3t9</==$MM  33s4y@3r   c                  > [         TU ]  U5      nU =R                  U-  sl        U R                  b  U R                  =R                  U-  sl        U R                  b)  U R                  R
                  =R                  U-  sl        U$ r   )r   readintor   r~   rz   r   )r   destgotr   s      r   r   HTTPResponse.readinto  sp    gt$3&'//36/==$MM  33s:3
r   )
r~   rz   r{   r   r   r}   r   r   r   r   )r   r   Nr   ))r   int)r;   r<   r=   r>   r   rS   r   rT   r   r   r   r   r@   __classcell__r   s   @r   r   r   }  s5     &  r   r   c                <   [        U SS5      b  gU R                  c2  U R                  (       a  U R                  5         O[        R
                  eU R                  S:  a  [        S[        U5      5         Sn[        USS5      nUbv  U R                  S:  a  [        S5        U" U5      nU(       aK  U R                  R                  U5        U =R                  [        U5      -  sl
        U" U5      nU(       a  MJ  ggU R                  R                  U5        U =R                  [        U5      -  sl
        g! [         aZ    U R                  [        R                  :X  a  SU l        U R!                  5       U l        SnOS	nU R#                  5         U(       a  e  gf = f)
zXSend `str' to the server.

Shamelessly ripped off from httplib to patch a bad behavior.
_broken_pipe_respNr   s   send:i    r   s   sending a read()ableFT)rw   r   	auto_openconnectrq   NotConnectedr   printreprsendallrJ   rN   BrokenPipeError_HTTPConnection__state_CS_REQ_SENTr   rp   rS   )r   str	blocksizer   r   reraises         r   safesendr     sR    t($/;yy>>LLN&&& hS	"	sFD)"-.	?D		!!$'##s4y0#I $
 IIc"3s8+ 	&&'*>*>>%)D"%)%5%5%7D"GG

 	s   5BD7 =9D7 7A!FFc                J   ^  U 4S jnT R                   R                  Ul        U$ )z9Wraps getresponse in cls with a broken-pipe sane version.c                J   > [        U SS 5      nUb  U$ TR                  U 5      $ )Nr   )rw   rp   )r   r   clss     r   safegetresponse(wrapgetresponse.<locals>.safegetresponse  s-    
 D-t4=Ht$$r   )rp   r?   )r   r   s   ` r   wrapgetresponser     s"    % "oo55Or   c                  Z   ^  \ rS rSr\r\r\" \	R                  5      rS rU 4S jrSrU =r$ )rb   i  c                h    [         R                  R                  " U /UQ70 UD6  SU l        SU l        g rF   )rq   rb   r   rJ   r   )r   argskwargss      r   r   HTTPConnection.__init__  s0    ''>t>v>"#r   c                   > [         TU ]  5       nSnU R                  nU(       a   SUR                  5       -  nSXU R
                  U R                  4-  $ ! [         a     N(f = f)Nz(unconnected)z%s:%dz<%s: %s <--> %s:%d>)r   __repr__r   getsocknameru   r#   port)r   baselocalsr   s       r   r   HTTPConnection.__repr__  sf    w!II!--/1 %TYY		'JJJ  s   A 
A"!A")r   rJ   )r;   r<   r=   r>   r   response_classr   r   r   rq   rb   rp   r   r   r@   r   r   s   @r   rb   rb     s0     "ND!'"8"89K$
	K 	Kr   rb   c                Z   [         R                  nSn[        R                  5       n[        R	                  U5        [        R                  U 5      nUR                  5       nUR                  5         U" U5      n[        US[        UR                  5       5      4-  5        [        R                  [        5       5      n[        R	                  U5        [        R                  U 5      nUR                  5       nUR                  5         U" U5      n[        US[        UR                  5       5      4-  5        [        R                  U 5      nSn UR                  5       nU(       a  XW-   nOOM  UR                  5         U" U5      n[        US[        UR                  5       5      4-  5        g )Ns   %25s: %ss   normal urllibs   keepalive readr   s   keepalive readline)hashlibmd5urlreqbuildopenerinstallopenerurlopenr   rS   r   r   digestr   r   )urlr   formatopenerfofoomfs           r   
continuityr
  ,  sD   
++CF !F
 		B
'')CHHJCA	&$c!((*o6
67 .F
 		B
'')CHHJCA	&%s188:7
78		B
C
KKM'C  HHJCA	&)3qxxz?;
;<r   c                   [        SX4-  5        [        R                  R                  S5        [        R                  5       n[        R                  U5        [        X5      n[        SU-  5        [        R                  R                  S5        [        R                  [        5       5      n[        R                  U5        [        X5      n[        SU-  5        [        SX4-  -  5        g )Ns      making %i connections to:
  %ss(     first using the normal urllib handlerss     TIME: %.3f ss(     now using the keepalive handler       s     improvement factor: %.2f)	r   r
   stdoutwriter   r   r   fetchr   )Nr  r  t1t2s        r   compr  P  s    	
.!
9:OOEF!F
 	qB	
b
 !OOEF.F
 	qB	
b
 !	
'27
34r   c                   SS K n/ nUR                   " 5       n[        U 5       Hq  nU(       a  US:  a  UR                  " U5        [        R	                  U5      nUR                  5       nUR                  5         UR                  [        U5      5        Ms     UR                   " 5       U-
  n	Sn
USS   H!  nU
S-   n
XdS   :X  a  M  [        SX4-  5        M#     U	$ )Nr   r   s+   WARNING: inconsistent length on read %i: %i)
timerangesleepr   r  r   rS   r!   rN   r   )r  r  delayr  lens	starttimeir  r  diffjs              r   r  r  c  s    D		I1XQUJJu^^C ggi

CH  99;"D	A!"XEG|@A6IJ 
 Kr   c                   [         n " S S5      nU" 5       q [        S5        [        R                  U 5      nUR	                  5       nUR                  5         Sn[        SU-  5        US:  ac  [        R                  R                  SU-  5        [        R                  R                  5         [        R                  " S5        US-  nUS:  a  Mc  [        R                  R                  S	5        [        S
5        [        R                  U 5      nUR	                  5       nUR                  5         XF:X  a  [        S5        Uq g [        S5        Uq g )Nc                  &    \ rS rSrS r\=r=rrSrg) test_timeout.<locals>.FakeLoggeri~  c                    [        X-  5        g r   )r   )r   r   r   s      r   debug&test_timeout.<locals>.FakeLogger.debug  s    #*r   rA   N)	r;   r<   r=   r>   r!  rm   warningr   r@   rA   r   r   
FakeLoggerr  ~  s    	 "'&&wr   r$  s-     fetching the file to establish a connection   s;     waiting %i seconds for the server to close the connectionr   s     %2ir      s!     fetching the file a second times     data are identicals     ERROR: DATA DIFFER)rl   r   r   r  r   rS   r
   r  r  flushr  r  stderr)r  dbbackupr$  r  data1r  data2s          r   test_timeoutr,  z  s   H' ' LE	
:;		BGGIEHHJ
A	
H1
LM
a%j1n-

1	Q	 a%
 OO% 	
./		BGGIEHHJ~%& E 	%&Er   c                    [        S5        [        U 5        [        S5        [        S5        [        X5        [        S5        [        S5        [        U 5        g )Ns>   performing continuity test (making sure stuff isn't corrupted)r   s   performing speed comparisons#   performing dropped-connection check)r   r
  r  r,  )r  r  s     r   testr.    sA    	
KLsO	#J	
()L	#J	
01r   __main__   z%s <integer> <url>r   )
   )+r?   
__future__r   r   r   sysr   i18nr   noder    r   r   r	   utilsr
   rq   ri   r   rl   r   rC   httphandlerr   r   r   r   rb   r
  r  r  r,  r.  r;   r  r   argvr  r  
IndexError
ValueErrorr   rA   r   r   <module>r<     s;  47v #   
    
 
,,		> >BI IX	"F$6$6 	E7'' EP3l"KW++ K@!=H5&.#L zhhqk 	S!  
# 2"SXXa[012s   $D  D%$D%