Plan 9 from Bell Labs’s /usr/web/sources/contrib/jas/root/sys/lib/python2.7/site-packages/mercurial/keepalive.pyc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�t�c@s�dZddlZddlZddlZddlZddlZdaddlZej	dkrlda
nda
defd��YZd	efd
��YZ
de
ejfd��YZd
ejfd��YZd�Zd�Zdejfd��YZd�Zd�ad�Zd�Zdd�Zd�Zdd�Zedkr�ddlZddlZy$eejd�ZejdZWn'e e!fk
r�dejdGHq�Xeee�ndS(s 
An HTTP handler for urllib2 that supports HTTP 1.1 and keepalive.

>>> import urllib2
>>> from keepalive import HTTPHandler
>>> keepalive_handler = HTTPHandler()
>>> opener = urllib2.build_opener(keepalive_handler)
>>> urllib2.install_opener(opener)
>>>
>>> fo = urllib2.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.

  For python versions earlier than 2.4, you can avoid this fancy error
  handling by setting the module-level global HANDLE_ERRORS to zero.
  You see, prior to 2.4, it's the HTTP Handler's job to determine what
  to handle specially, and what to just pass up.  HANDLE_ERRORS == 0
  means "pass everything up".  In python 2.4, however, this job no
  longer belongs to the HTTP Handler and is now done by a NEW handler,
  HTTPErrorProcessor.  Here's the bottom line:

    python version < 2.4
        HANDLE_ERRORS == 1  (default) pass up 200, treat the rest as
                            errors
        HANDLE_ERRORS == 0  pass everything up, error processing is
                            left to the calling code
    python version >= 2.4
        HANDLE_ERRORS == 1  pass up 200, treat the rest as errors
        HANDLE_ERRORS == 0  (default) pass everything up, let the
                            other handlers (specifically,
                            HTTPErrorProcessor) decide what to do

  In practice, setting the variable either way makes little difference
  in python 2.4, so for the most consistent behavior across versions,
  you probably just want to use the defaults, which will give you
  exceptions on errors.

i�NiiiitConnectionManagercBsGeZdZd�Zd�Zd�Zd�Zd�Zdd�Z	RS(sV
    The connection manager must be able to:
      * keep track of all existing
      cCs.tj�|_i|_i|_i|_dS(N(tthreadt
allocate_lockt_lockt_hostmapt_connmapt	_readymap(tself((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt__init__�s		cCss|jj�zQ||jkr/g|j|<n|j|j|�||j|<||j|<Wd|jj�XdS(N(RtacquireRtappendRRtrelease(Rthostt
connectiontready((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytadd�s

cCs�|jj�zky|j|}Wntk
r4nCX|j|=|j|=|j|j|�|j|sw|j|=nWd|jj�XdS(N(RR	RtKeyErrorRRtremoveR(RR
R((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR�s




cCs)y||j|<Wntk
r$nXdS(N(RR(RR
R((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt	set_ready�s
cCszd}|jj�zR||jkrdx<|j|D]*}|j|r3d|j|<|}Pq3q3WnWd|jj�X|S(Ni(tNoneRR	RRR(RRtconntc((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytget_ready_conn�s


cCs0|rt|jj|g��St|j�SdS(N(tlistRtgettdict(RR((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytget_all�sN(
t__name__t
__module__t__doc__RRRRRRR(((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR}s					tKeepAliveHandlercBseeZd�Zd�Zd�Zd�Zd�Zdd�Zd�Zd�Z	d	�Z
d
�ZRS(cCst�|_dS(N(Rt_cm(R((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR�scCs8g|jj�j�D]\}}|t|�f^qS(streturn a list of connected hosts and the number of connections
        to each.  [('foo.com:80', 2), ('bar.org', 1)](RRtitemstlen(RRtli((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytopen_connections�scCs;x4|jj|�D] }|jj|�|j�qWdS(s�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(RRRtclose(RRth((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytclose_connection�scCsUxN|jj�j�D]7\}}x(|D] }|jj|�|j�q)WqWdS(sclose all open connectionsN(RRt	iteritemsRR$(RRtconnsR%((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt	close_all�s"
cCs|jj|d�dS(setells us that this request is now closed and that the
        connection is ready for another requestiN(RR(RtrequestRR
((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt_request_closed�sicCs'|r|j�n|jj|�dS(N(R$RR(RRR
R$((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt_remove_connection�s
cCs|jt|�S(N(tdo_opentHTTPConnection(Rtreq((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt	http_open�scCs�|j�}|s$tjd��ny�|jj|�}x�|r�|j|||�}|raPn|j�|jj|�|jj|�}q<W||�}tr�tj	d|t
|��n|jj||d�|j||�|j
�}Wn.tjtjfk
r"}tj|��nX|jr?|jj|�ntratj	d|j|j�n||_||_|j�|_||_|j|_|j|_|j|_|jdks�tr�|S|jjd|||j|j|j�SdS(Ns
no host givens"creating new connection to %s (%d)isSTATUS: %s, %si�thttp(tget_hostturllib2tURLErrorRRt_reuse_connectionR$RtDEBUGtinfotidRt_start_transactiontgetresponsetsocketterrorthttplibt
HTTPExceptiont
will_closetstatustreasont_handlert_hosttget_full_urlt_urlt_connectiontcodetmsgtheaderst
HANDLE_ERRORStparent(Rt
http_classR/RR%trterr((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR-�sH	
					cCs�y |j||�|j�}Wnhtjtjfk
rEd}nFtrjtjd|t|��n|j	j
|�|j��nX|dks�|jdkr�tr�tj
d|t|��nd}n"tr�j
d|t|��n|S(sGstart 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.
        s4unexpected exception - closing connection to %s (%d)i	s&failed to re-use connection to %s (%d)sre-using connection to %s (%d)N(R9R:R;R<R=R>RR6R8RRR$tversionR7(RR%R/RRM((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR5s(	
	
		c
Cs�|jj�}tjdkr1|j|j�n|j|jj�td�|j	�D��}i}x7dD]/}||krmd|d|j
dd	�<qmqmWy�|j�r|j�}|j
d
|j�|�d|kr�jdd
�nd|kr8|jddt|��q8n|j
d|j�|�Wn%tjk
r`}tj|��nXx*|j	�D]\}}	|j||	�qnW|j�|j�r�|j|�ndS(Niicss'|]\}}|j�|fVqdS(N(tlower(t.0tntv((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pys	<genexpr>LsRsaccept-encodingitskip_t-t_tPOSTscontent-typesContent-types!application/x-www-form-urlencodedscontent-lengthsContent-lengths%dtGET(ii(shostsaccept-encoding(RItcopytsystversion_infotupdatetunredirected_hdrsRKt
addheadersRR treplacethas_datatget_datat
putrequesttget_selectort	putheaderR!R;R<R3R4t
endheaderstsend(
RR%R/RItskipheadersRRtdataRNtkRS((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR9Es4
!	
 
(RRRR#R&R)R+R,R0R-R5R9(((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR�s							4	.tHTTPHandlercBseZRS((RR(((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyRjdstHTTPResponsecBsweZdddd�ZejjZd�Zd�Z	d�Z
d�Zdd�Zd�Zdd	�Z
dd
�ZRS(icCshtjj||||�|j|_d|_d|_d|_d|_d|_	d|_
d|_dS(Nti�(R=RkRtfilenoRRGt_rbuft	_rbufsizeRBRCRERF(Rtsockt
debugleveltstricttmethod((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR{s						cCsN|jrJ|jj�d|_|jrJ|jj||j|j�qJndS(N(tfpR$RRBR+RCRF(R((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR$�s	
		cCs-|jj|j|jdd�|j�dS(NR$i(RBR,RCRFR$(R((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR&�scCs|jS(N(RI(R((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR7�scCs|jS(N(RE(R((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytgeturl�scCs�|jra|dk	rat|j�}||kr=||8}qa|j| }|j||_|Sn|j|j|�}d|_|S(NRl(RnRR!t	_raw_read(RtamttLts((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytread�s

	cCs�|j}d}x[trl|dkr�|jj�}|jd�}|dkr[|| }nyt|d�}Wn*tk
r�|j�t	j
|��nX|dkr�Pq�n|dkr�||j|�7}n�||kr||j|�7}|||_|S||kr9||j|�7}|jd�d|_|S||j|�7}||8}|jd�d}qWx3tr�|jj�}|s�Pn|dkrpPqpqpW|j�|S(NRlt;iiis
(t
chunk_lefttTrueRRttreadlinetfindtintt
ValueErrorR$R=tIncompleteReadt
_safe_read(RRwR|tvaluetlineti((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt
_read_chunked�sL		




	


	
i�cCs |jjd�}x�|dkr�d|koAt|j�knr�|j|j�}|scPn|jd�}|dkr�|t|j�}n|j||_qW|dkr�t|j�}n
|d}d|ko�|j�knr�}n|j| |j|}|_|S(Ns
ii(RnRR!RvRo(RtlimitR�tnewRh((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR~�s 5
%	cCsfd}g}xStra|j�}|s+Pn|j|�|t|�7}|r||krPqqW|S(Ni(R}R~R
R!(RtsizehintttotalRR�((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt	readlines�s	
N(RRRRR=RkRzRvR$R&R7RuR�R~R�(((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyRkgs					8cCswt|dd�dk	rdS|jdkrM|jrA|j�qMtj�n|jdkrndGt|�GHny�d}t|dd�}|dk	r�jdkr�dGHn||�}x9|r�jj	|�||�}q�Wn|jj	|�Wn{t
jk
rr}t}|dt
jkrc|jtjkrVd|_|j�|_t}n|j�n|rs�qsnXdS(s`Send `str' to the server.

    Shamelessly ripped off from httplib to patch a bad behavior.
    t_broken_pipe_respNissend:i Rzssending a read()able(tgetattrRRpt	auto_opentconnectR=tNotConnectedRqtreprtsendallR;R<R}terrnotEPIPEt_HTTPConnection__statet_CS_REQ_SENTR�R:tFalseR$(Rtstrt	blocksizeRzRhRStreraise((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytsafesends:	
			
cs"�fd�}�jj|_|S(s>Wraps getresponse in cls with a broken-pipe sane version.
    cs/t|dd�}|dk	r"|S�j|�S(NR�(R�RR:(RRM(tcls(s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytsafegetresponse9s(R:R(R�R�((R�s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytwrapgetresponse6s	R.cBs#eZeZeZeej�Z	RS((
RRRktresponse_classR�RfR�R=R.R:(((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR.EscCs!t}t�}tj|�}tj|�idd6dd6}x�d	D]�}d|||fGH|ay^tj|�}|j�|j�y|j|j	}}Wnt
k
r�d
\}}nXWntk
r�}	d|	GH�qFXd||fGHqFW|a|j
�}
dG|
GH|j�dS(Ntoffitonis.  fancy error handling %s (HANDLE_ERRORS = %i)s  EXCEPTION: %ss  status = %s, reason = %ssopen connections:(ii(NN(RJRjR3tbuild_openertinstall_openerturlopenRzR$R@RAtAttributeErrorRtIOErrorR#R)(turltorigtkeepalive_handlertopenertposR�tfoR@RAtethosts((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt
error_handlerPs0	




		cCsHyddlm}Wn!tk
r7ddlm}nX|a||�S(Ni�(tmd5(thashlibR�tImportError(Ryt_md5((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR�ls
cCsDd}tj�}tj|�tj|�}|j�}|j�tj|�}|d|j�fGHtjt	��}tj|�tj|�}|j�}|j�tj|�}|d|j�fGHtj|�}d}x*t
r|j�}|r
||}q�q�|j�tj|�}|d|j�fGHdS(Ns%25s: %ss
normal urllibskeepalive readRlskeepalive readline(R3R�R�R�RzR$R�R�t	hexdigestRjR}R~(R�tformatR�R�tfootmtf((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt
continuityus2



	

cCs�d||fGHtjjd�tj�}tj|�t||�}d|GHtjjd�tjt��}tj|�t||�}d|GHd||GHdS(Ns   making %i connections to:
  %ss(  first using the normal urllib handlerss  TIME: %.3f ss(  now using the keepalive handler       s  improvement factor: %.2f(RZtstdouttwriteR3R�R�tfetchRj(tNR�R�tt1tt2((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytcomp�s
	
	cCs�ddl}g}|j�}xnt|�D]`}|rS|dkrS|j|�ntj|�}|j�}|j�|jt|��q+W|j�|}	d}
x>|dD]2}|
d}
||dks�d|
|fGHq�q�W|	S(Ni�iis+WARNING: inconsistent length on read %i: %i(	ttimetrangetsleepR3R�RzR$R
R!(R�R�tdelayR�tlenst	starttimeR�R�R�tdifftj((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR��s"

cCst}dtfd��Y}|�adGHtj|�}|j�}|j�d}d|GHxH|dkr�tjjd|�tjj	�t
jd�|d8}qaWtjjd	�d
GHtj|�}|j�}|j�||kr�GHndGH|adS(
Nt
FakeLoggercBseZd�ZeZZZRS(cWs
||GHdS(N((RRHtargs((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pytdebug�s(RRR�R7twarningR<(((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyR��s	s-  fetching the file to establish a connectionis;  waiting %i seconds for the server to close the connectionis
  %2iis
s!  fetching the file a second times  data are identicals  ERROR: DATA DIFFER(
R6tobjectR3R�RzR$RZR�R�tflushR�R�tstderr(R�tdbbackupR�R�tdata1R�tdata2((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyttest_timeout�s.	
	


i
cCsmdGHyt|�Wn tk
r5dGHtj�nXHdGHt|�HdGHt||�HdGHt|�dS(Ns-checking error handler (do this on a non-200)s.exiting - exception will prevent further testss>performing continuity test (making sure stuff isn't corrupted)sperforming speed comparisons#performing dropped-connection check(R�R�RZtexitR�R�R�(R�R�((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyttest�s


t__main__s%s <integer> <url>(ii("RR�R=R;RR3RR6RZR[RJR�RRRjRkR�R�R.R�R�R�R�R�R�R�RR�R�targvR�R�t
IndexErrorR�(((s7/sys/lib/python2.7/site-packages/mercurial/keepalive.pyt<module>lsB	?��3					!		#

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].