a
    &0É_tl  ã                	   @   s’  d Z ddlmZmZ ddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZ ddlmZmZmZmZ ddlmZ ejZejZejZdaG d	d
„ d
eƒZG dd„ deƒZG dd„ deejƒZG dd„ dejƒZdd„ Zdd„ ZG dd„ dej ƒZ dd„ Z!dd„ Z"d%dd„Z#dd„ Z$d&d d!„Z%e&d"krŽddl'Z'ze(ej)d ƒZ*ej)d# Z+W n* e,e-fy‚   e.d$ej)d  ƒ Y n0 e%e+e*ƒ dS )'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.
é    )Úabsolute_importÚprint_functionNé   )Ú_)Úgetattr)ÚnodeÚpycompatÚurllibcompatÚutil)Úprocutilc                   @   sB   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	dS )ÚConnectionManagerzV
    The connection manager must be able to:
      * keep track of all existing
      c                 C   s&   t  ¡ | _t t¡| _i | _i | _d S ©N)	Ú	threadingZLockÚ_lockÚcollectionsÚdefaultdictÚlistÚ_hostmapÚ_connmapÚ	_readymap©Úself© r   ú5/usr/lib/python3/dist-packages/mercurial/keepalive.pyÚ__init__u   s    
zConnectionManager.__init__c                 C   sN   | j  ¡  z2| j|  |¡ || j|< || j|< W | j  ¡  n| j  ¡  0 d S r   )r   Úacquirer   Úappendr   r   Úrelease)r   ÚhostÚ
connectionÚreadyr   r   r   Úadd{   s    

zConnectionManager.addc                 C   s~   | j  ¡  zbz| j| }W n ty,   Y n40 | j|= | j|= | j|  |¡ | j| s`| j|= W | j  ¡  n| j  ¡  0 d S r   )r   r   r   ÚKeyErrorr   r   Úremover   )r   r   r   r   r   r   r#   „   s    


zConnectionManager.removec                 C   s&   z|| j |< W n ty    Y n0 d S r   )r   r"   )r   r   r    r   r   r   Ú	set_ready”   s    zConnectionManager.set_readyc                 C   sZ   d }| j  ¡  z:| j| D ] }| j| rd| j|< |} q<qW | j  ¡  n| j  ¡  0 |S )NF)r   r   r   r   r   )r   r   ZconnÚcr   r   r   Úget_ready_connš   s    


z ConnectionManager.get_ready_connNc                 C   s    |rt | j| ƒS t| jƒS d S r   )r   r   Údict)r   r   r   r   r   Úget_all§   s    zConnectionManager.get_all)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r#   r$   r&   r(   r   r   r   r   r   o   s   	r   c                   @   s`   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚKeepAliveHandlerNc                 C   s   t ƒ | _|| _d| _d| _d S ©Nr   )r   Ú_cmÚ_timeoutÚrequestscountÚsentbytescount)r   Útimeoutr   r   r   r   ¯   s    zKeepAliveHandler.__init__c                 C   s   dd„ | j  ¡  ¡ D ƒS )ztreturn a list of connected hosts and the number of connections
        to each.  [('foo.com:80', 2), ('bar.org', 1)]c                 S   s   g | ]\}}|t |ƒf‘qS r   )Úlen)Ú.0r   Zlir   r   r   Ú
<listcomp>¹   ó    z5KeepAliveHandler.open_connections.<locals>.<listcomp>)r/   r(   Úitemsr   r   r   r   Úopen_connections¶   s    z!KeepAliveHandler.open_connectionsc                 C   s*   | j  |¡D ]}| j  |¡ | ¡  qdS )z¨close 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)r/   r(   r#   Úclose)r   r   Úhr   r   r   Úclose_connection»   s    z!KeepAliveHandler.close_connectionc                 C   s<   t  | j ¡ ¡D ]&\}}|D ]}| j |¡ | ¡  qqdS )zclose all open connectionsN)r   Z	iteritemsr/   r(   r#   r:   )r   r   Zconnsr;   r   r   r   Ú	close_allÃ   s    zKeepAliveHandler.close_allc                 C   s   | j  |d¡ dS )zetells us that this request is now closed and that the
        connection is ready for another requestTN)r/   r$   )r   Zrequestr   r   r   r   r   Ú_request_closedÊ   s    z KeepAliveHandler._request_closedr   c                 C   s   |r|  ¡  | j |¡ d S r   )r:   r/   r#   )r   r   r   r:   r   r   r   Ú_remove_connectionÏ   s    z#KeepAliveHandler._remove_connectionc                 C   s   |   t|¡S r   )Údo_openÚHTTPConnection)r   Úreqr   r   r   Ú	http_openÕ   s    zKeepAliveHandler.http_openc              
   C   sŒ  t  |¡}|st d¡‚z’| j |¡}|r`|  |||¡}|r>q¨| ¡  | j |¡ | j |¡}q&||| j	d}t
r„t
 d|t|ƒ¡ | j ||d¡ |  ||¡ | ¡ }W nv tjyê } z&t tdƒt |j¡ ¡‚W Y d }~n@d }~0  tjtjfy  } zt |¡‚W Y d }~n
d }~0 0 t|ddƒr<| j |¡ t
rTt
 d|j|j¡ | |_||_| ¡ |_||_ |j|_!|j"|_#|j|_"|S )	Ns   no host given)r3   s"   creating new connection to %s (%d)Fs   bad HTTP status line: %sZ
will_closeTs   STATUS: %s, %s)$r	   ZgethostÚurlerrÚurlerrorr/   r&   Ú_reuse_connectionr:   r#   r0   ÚDEBUGÚinfoÚidr!   Ú_start_transactionÚgetresponseÚhttplibZBadStatusLiner   r   ZsysbytesÚlineÚsocketÚerrorÚHTTPExceptionr   ZstatusÚreasonÚ_handlerÚ_hostZget_full_urlÚ_urlÚ_connectionÚcodeÚmsgÚheaders)r   Z
http_classrB   r   r;   ÚrÚerrr   r   r   r@   Ø   sL    


ÿÿ 
zKeepAliveHandler.do_openc              	   C   s¶   z|   ||¡ | ¡ }W nT tjtjfy6   d}Y n8   trRt d|t|ƒ¡ | j 	|¡ | 
¡  ‚ Y n0 |du s€|jdkrœtr–t d|t|ƒ¡ d}ntr²t d|t|ƒ¡ |S )aG  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))rJ   rK   rN   rO   rL   rP   rG   rI   r/   r#   r:   ÚversionrH   )r   r;   rB   r   rY   r   r   r   rF     s0    
ý
ÿz"KeepAliveHandler._reuse_connectionc              
   C   sî  t |ddƒ}t | jj¡}| t|j ¡ ƒ¡ | t|j	 ¡ ƒ¡ t dd„ | ¡ D ƒ¡}i }dD ] }||v rbd|d| 
dd	¡ < qbz†t |¡rêt |¡}|j| ¡ t |¡fi |¤Ž d
|vrÌ| dd¡ d|vrè| ddt|ƒ ¡ n|j| ¡ t |¡fi |¤Ž W n2 tjy< } zt |¡‚W Y d }~n
d }~0 0 | ¡ D ]\}	}
| |	|
¡ qF| ¡  t |¡r|| |¡ |  jd7  _|  jt |ddƒ| 7  _z0| j jd7  _| j jt |ddƒ| 7  _W n tyè   Y n0 d S )Nr2   r   c                 s   s   | ]\}}|  ¡ |fV  qd S r   )Úlower)r5   ÚnÚvr   r   r   Ú	<genexpr>L  r7   z6KeepAliveHandler._start_transaction.<locals>.<genexpr>)r   zaccept-encodingr   Zskip_ú-r   zcontent-typezContent-typez!application/x-www-form-urlencodedzcontent-lengthzContent-lengthz%d)r   r
   ZsortdictÚparentZ
addheadersÚupdateÚsortedrX   r8   Zunredirected_hdrsÚreplacer	   ZhasdataZgetdataZ
putrequestZ
get_methodZgetselectorZ	putheaderr4   rN   rO   rD   rE   Z
endheadersÚsendr1   r2   ÚAttributeError)r   r;   rB   ZoldbytescountrX   Zskipheadersr^   ÚdatarZ   Úkr_   r   r   r   rJ   C  s\    

þýÿþý
 
ÿ
z#KeepAliveHandler._start_transaction)N)r   )r)   r*   r+   r   r9   r<   r=   r>   r?   rC   r@   rF   rJ   r   r   r   r   r-   ®   s   

92r-   c                   @   s   e Zd ZdS )ÚHTTPHandlerN)r)   r*   r+   r   r   r   r   rj   z  s   rj   c                   @   s€   e Zd Zddd„ZejjZeejddƒZ	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ Zddd„Zdd„ Zdd„ Zddd„Zdd„ ZdS )ÚHTTPResponser   Nc                 C   sr   i }t jsd|d< d|d< tjj| |f||dœ|¤Ž |j| _d | _d| _d| _d| _	d | _
d | _d | _d | _d S )NTÚstrictÚ	buffering)Ú
debuglevelÚmethodr   r7   i   )r   Zispy3rL   rk   r   ÚfilenorV   ÚreceivedbytescountÚ_rbufÚ	_rbufsizerR   rS   rT   rU   )r   Úsockrn   rl   ro   Zextrakwr   r   r   r   ‘  s(    ÿÿÿzHTTPResponse.__init__Úreadintoc                 C   s4   | j r0| j  ¡  d | _ | jr0| j | | j| j¡ d S r   )Úfpr:   rR   r>   rS   rU   r   r   r   r   r:   «  s    

ÿzHTTPResponse.closec                 C   s   |   ¡  d S r   ©r:   r   r   r   r   Ú_close_conn´  s    zHTTPResponse._close_connc                 C   s"   | j j| j| jdd |  ¡  d S )Nr   rw   )rR   r?   rS   rU   r:   r   r   r   r   r<   ·  s    zHTTPResponse.close_connectionc                 C   s   | j S r   )rX   r   r   r   r   rH   »  s    zHTTPResponse.infoc                 C   s   | j S r   )rT   r   r   r   r   Úgeturl¾  s    zHTTPResponse.geturlc                 C   sÚ   | j rL|d urLt| j ƒ}||kr*||8 }n"| j d |… }| j |d … | _ |S | j }d| _ |  |¡}|  jt|ƒ7  _z| j jt|ƒ7  _W n tyž   Y n0 z| jj jt|ƒ7  _W n tyÌ   Y n0 ||7 }|S )Nr7   )rr   r4   Ú	_raw_readrq   rU   rg   rR   rb   )r   ÚamtÚLÚsrh   r   r   r   ÚreadÁ  s*    


zHTTPResponse.readc                 C   sX  | j }g }|d u r€| j ¡ }| d¡}|dkr:|d |… }zt|dƒ}W n* tyr   |  ¡  t d 	|¡¡‚Y n0 |dkr€q |d u rš| 
|  |¡¡ nv||k rÆ| 
|  |¡¡ || | _ d 	|¡S ||krø| 
|  |¡¡ |  d¡ d | _ d 	|¡S | 
|  |¡¡ ||8 }|  d¡ d }q
| j ¡ }|s4qF|dkr qFq |  ¡  d 	|¡S )Nó   ;r   é   r7   é   s   
)Ú
chunk_leftrv   ÚreadlineÚfindÚintÚ
ValueErrorr:   rL   ZIncompleteReadÚjoinr   Z
_safe_read)r   r{   r‚   ÚpartsrM   Úir   r   r   Ú_read_chunkedá  sH    








zHTTPResponse._read_chunkedc                 C   s4  | j  d¡}|dkr>|d7 }| j d |… }| j |d … | _ |S | jd u rR| j g}ng }d}| j}|  |¡}|spqä|  jt|ƒ7  _| j jt|ƒ7  _z| jj	 jt|ƒ7  _W n t
yÂ   Y n0 | |¡ | d¡}|dkr`qäq`|dkrüd| _ d |¡S |d7 }|d |d … | _ |d d |… |d< d |¡S )Nó   
r   r   éÿÿÿÿr7   )rr   r„   Ú_raw_readintors   rz   rq   r4   rU   rR   rb   rg   r   r‡   )r   r‰   rM   ZchunksÚreadsizeÚnewr   r   r   rƒ     s>    





zHTTPResponse.readlinec                 C   s@   d}g }|   ¡ }|sq<| |¡ |t|ƒ7 }|r||krq<q|S r.   )rƒ   r   r4   )r   ÚsizehintÚtotalr   rM   r   r   r   Ú	readlinesE  s    
zHTTPResponse.readlinesc                 C   s  | j d u r8|  t|ƒ¡}|s dS ||dt|ƒ…< t|ƒS t|ƒ}t| jƒ}||kr|| jd |… |d|…< | j|d … | _|S t|ƒ}|   |||… ¡}|  j|7  _| j j|7  _z| j j|7  _W n tyÚ   Y n0 | j|d|…< |t| jƒ7 }d| _|S )Nr   r7   )	r   r~   r4   rr   Ú
memoryviewrq   rU   rR   rg   )r   ÚdestÚresr‘   ZhaveZmvZgotr   r   r   ru   R  s0    

zHTTPResponse.readinto)r   r   N)N)r   )r)   r*   r+   r   rL   rk   r~   rz   r   r   r:   rx   r<   rH   ry   rŠ   rƒ   r’   ru   r   r   r   r   rk   ~  s   
	
 6.
rk   c              
   C   sD  t | ddƒdurdS | jdu r4| jr.|  ¡  ntj‚| jdkrLtdt|ƒƒ z‚d}t |ddƒ}|dur®| jdkrxtdƒ ||ƒ}|rÌ| j 	|¡ |  j
t|ƒ7  _
||ƒ}q€n| j 	|¡ |  j
t|ƒ7  _
W np tjy> } zTd}|jd tjkr"| jtjkrd| _|  ¡ | _d	}|  ¡  |r*‚ W Y d}~n
d}~0 0 dS )
z`Send `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()ableTF)r   rt   Z	auto_openZconnectrL   ZNotConnectedrn   ÚprintÚreprZsendallr2   r4   rN   rO   ÚargsÚerrnoZEPIPEZ_HTTPConnection__stateZ_CS_REQ_SENTr–   rK   r:   )r   ÚstrZ	blocksizer~   rh   r_   Zreraiser   r   r   Úsafesendo  s>    




rœ   c                    s   ‡ fdd„}ˆ j j|_|S )z>Wraps getresponse in cls with a broken-pipe sane version.
    c                    s"   t | dd ƒ}|d ur|S ˆ  | ¡S )Nr–   )r   rK   )r   rY   ©Úclsr   r   Úsafegetresponse©  s    z(wrapgetresponse.<locals>.safegetresponse)rK   r,   )rž   rŸ   r   r   r   Úwrapgetresponse¥  s    

r    c                   @   s&   e Zd ZeZeZeej	ƒZ
dd„ ZdS )rA   c                 O   s*   t jj| g|¢R i |¤Ž d| _d| _d S r.   )rL   rA   r   r2   rq   )r   r™   Úkwargsr   r   r   r   Á  s    zHTTPConnection.__init__N)r)   r*   r+   rk   Zresponse_classrœ   rf   r    rL   rA   rK   r   r   r   r   r   rA   ·  s   
rA   c                 C   s   t j}d}t ¡ }t |¡ t | ¡}| ¡ }| ¡  ||ƒ}t|dt	 
| ¡ ¡f ƒ t tƒ ¡}t |¡ t | ¡}| ¡ }| ¡  ||ƒ}t|dt	 
| ¡ ¡f ƒ t | ¡}d}| ¡ }|rÒ|| }q¸qÒq¸| ¡  ||ƒ}t|dt	 
| ¡ ¡f ƒ d S )Ns   %25s: %ss   normal urllibs   keepalive readr7   s   keepalive readline)ÚhashlibÚmd5ÚurlreqÚbuildopenerÚinstallopenerÚurlopenr~   r:   r—   r   ÚhexZdigestrj   rƒ   )Úurlr£   ÚformatÚopenerÚfoÚfooÚmÚfr   r   r   Ú
continuityÌ  s2    





r°   c                 C   s   t d| |f ƒ tj d¡ t ¡ }t |¡ t| |ƒ}t d| ƒ tj d¡ t tƒ ¡}t |¡ t| |ƒ}t d| ƒ t d||  ƒ d S )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¦   Úfetchrj   )ÚNr©   r«   Út1Út2r   r   r   Úcompð  s    



r·   c                 C   sª   dd l }g }|  ¡ }t| ƒD ]B}|r6|dkr6| |¡ t |¡}| ¡ }| ¡  | t|ƒ¡ q|  ¡ | }	d}
|dd … D ](}|
d }
||d ks|t	d|
|f ƒ q||	S )Nr   r   s+   WARNING: inconsistent length on read %i: %i)
ÚtimeÚrangeÚsleepr¤   r§   r~   r:   r   r4   r—   )r´   r©   Zdelayr¸   ZlensZ	starttimer‰   r¬   r­   ZdiffÚjr   r   r   r³     s"    

r³   c                 C   sÒ   t }G dd„ dtƒ}|ƒ a tdƒ t | ¡}| ¡ }| ¡  d}td| ƒ |dkr‚tj 	d| ¡ tj 
¡  t d¡ |d8 }qLtj 	d	¡ td
ƒ t | ¡}| ¡ }| ¡  ||krÂtdƒ ntdƒ |a d S )Nc                   @   s    e Zd Zdd„ Ze Z ZZdS )z test_timeout.<locals>.FakeLoggerc                 W   s   t || ƒ d S r   )r—   )r   rW   r™   r   r   r   Údebug  s    z&test_timeout.<locals>.FakeLogger.debugN)r)   r*   r+   r¼   rH   ZwarningrO   r   r   r   r   Ú
FakeLogger  s   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)rG   Úobjectr—   r¤   r§   r~   r:   r   r±   r²   Úflushr¸   rº   Ústderr)r©   Zdbbackupr½   r¬   Zdata1r‰   Zdata2r   r   r   Útest_timeout  s.    





rÃ   é
   c                 C   sF   t dƒ t| ƒ t dƒ t dƒ t|| ƒ t dƒ t dƒ t| ƒ d S )Ns>   performing continuity test (making sure stuff isn't corrupted)r7   s   performing speed comparisons#   performing dropped-connection check)r—   r°   r·   rÃ   )r©   r´   r   r   r   Útest@  s    
rÅ   Ú__main__r   s   %s <integer> <url>)r   )rÄ   )/r,   Z
__future__r   r   r   rš   r¢   rN   Úsysr   Zi18nr   r   r   Ú r   r	   r
   Zutilsr   rL   rD   r¤   rG   rÀ   r   r-   Zhttphandlerrj   rk   rœ   r    rA   r°   r·   r³   rÃ   rÅ   r)   r¸   r…   Úargvr´   r©   Ú
IndexErrorr†   r—   r   r   r   r   Ú<module>   sJ   ;? M r6$
&

