Plan 9 from Bell Labs’s /usr/web/sources/contrib/bichued/root/sys/lib/python/mercurial/keepalive.pyc

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


�
�c
@s�dZddkZddkZddkZddkZdaddkZeidjo
da	nda	ddd��YZ
d	dd
��YZdeeifd��YZd
ei
fd��YZ
deifd��YZd�Zd�ad�Zd�Zdd�Zd�Zdd�ZedjogddkZddkZy$eeid�ZeidZWndeidGHq�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 (ie 404)
    reason              -  english translation of status (ie '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.ti�|_h|_h|_h|_dS(N(tthreadt
allocate_lockt_lockt_hostmapt_connmapt	_readymap(tself((s&/sys/lib/python/mercurial/keepalive.pyt__init__s		cCsu|ii�zS||ijog|i|<n|i|i|�||i|<||i|<Wd|ii�XdS(N(RtacquireRtappendRRtrelease(Rthostt
connectiontready((s&/sys/lib/python/mercurial/keepalive.pytadd�s
!
cCs�|ii�zoy|i|}Wntj
onFX|i|=|i|=|i|i|�|i|p|i|=nWd|ii�XdS(N(RR	RtKeyErrorRRtremoveR(RR
R((s&/sys/lib/python/mercurial/keepalive.pyR�s


 cCs+y||i|<Wntj
onXdS(N(RR(RR
R((s&/sys/lib/python/mercurial/keepalive.pyt	set_ready�scCs~d}|ii�zV||ijoBx?|i|D],}|i|od|i|<|}Pq4q4WnWd|ii�X|S(Ni(tNoneRR	RRR(RRtconntc((s&/sys/lib/python/mercurial/keepalive.pytget_ready_conn�s

cCs5|ot|ii|g��Snt|i�SdS(N(tlistRtgettdict(RR((s&/sys/lib/python/mercurial/keepalive.pytget_all�sN(
t__name__t
__module__t__doc__RRRRRRR(((s&/sys/lib/python/mercurial/keepalive.pyRzs		
			tKeepAliveHandlercBseeZd�Zd�Zd�Zd�Zd�Zdd�Zd�Zd�Z	d	�Z
d
�ZRS(cCst�|_dS(N(Rt_cm(R((s&/sys/lib/python/mercurial/keepalive.pyR�scCs@g}|ii�i�D]\}}||t|�fq~S(streturn a list of connected hosts and the number of connections
        to each.  [('foo.com:80', 2), ('bar.org', 1)](RRtitemstlen(Rt_[1]Rtli((s&/sys/lib/python/mercurial/keepalive.pytopen_connections�scCs;x4|ii|�D] }|ii|�|i�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((s&/sys/lib/python/mercurial/keepalive.pytclose_connection�scCsUxN|ii�i�D]7\}}x(|D] }|ii|�|i�q)WqWdS(sclose all open connectionsN(RRt	iteritemsRR%(RRtconnsR&((s&/sys/lib/python/mercurial/keepalive.pyt	close_all�scCs|ii|d�dS(sdtells us that this request is now closed and the the
        connection is ready for another requestiN(RR(RtrequestRR
((s&/sys/lib/python/mercurial/keepalive.pyt_request_closed�sicCs)|o|i�n|ii|�dS(N(R%RR(RRR
R%((s&/sys/lib/python/mercurial/keepalive.pyt_remove_connection�scCs|it|�S(N(tdo_opentHTTPConnection(Rtreq((s&/sys/lib/python/mercurial/keepalive.pyt	http_open�sc
Cs	|i�}|ptid��ny�|ii|�}x�|oQ|i|||�}|oPn|i�|ii|�|ii|�}q>W||�}toti	d|t
|��n|ii||d�|i||�|i
�}Wn0titifj
o}ti|��nX|io|ii|�ntoti	d|i|i�n||_||_|i�|_||_|i|_|i|_|i|_|idjpto|Sn)|iid|||i|i|i�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_classR0RR&trterr((s&/sys/lib/python/mercurial/keepalive.pyR.�s@

$			cCsy |i||�|i�}Wnptitifj
o
d}nMto!tidd|t|��n|i	i
|�|i��nX|djp|idjo.toti
d|t|��nd}n%toti
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.
        sunexpected exception - closing sconnection to %s (%d)i	s&failed to re-use connection to %s (%d)sre-using connection to %s (%d)N(R:R;R<R=R>R?RR7R9RRR%tversionR8(RR&R0RRN((s&/sys/lib/python/mercurial/keepalive.pyR6s"



$c

Cs�|ii�}tidjo|i|i�n|i|ii�td�|i	�D��}h}x9dD]1}||jod|d|i
dd	�<qoqoWy�|i�ou|i�}|i
d
|i�|�d|jo|idd
�nd|jo|iddt|��qBn|i
d|i�|�Wn'tij
o}ti|��nXx*|i	�D]\}}	|i||	�qzW|i�|i�o|i|�ndS(Niicss+x$|]\}}|i�|fVqWdS(N(tlower(t.0tntv((s&/sys/lib/python/mercurial/keepalive.pys	<genexpr>>sRsaccept-encodingitskip_t-t_tPOSTscontent-typesContent-types!application/x-www-form-urlencodedscontent-lengthsContent-lengths%dtGET(ii(shostsaccept-encoding(RJtcopytsystversion_infotupdatetunredirected_hdrsRLt
addheadersRR treplacethas_datatget_datat
putrequesttget_selectort	putheaderR!R<R=R4R5t
endheaderstsend(
RR&R0RJtskipheadersRStdataROtkRT((s&/sys/lib/python/mercurial/keepalive.pyR:7s8
"

	
"


(RRRR$R'R*R,R-R1R.R6R:(((s&/sys/lib/python/mercurial/keepalive.pyR�s							0	+tHTTPHandlercBseZRS((RR(((s&/sys/lib/python/mercurial/keepalive.pyRkVstHTTPResponsecBsweZdddd�ZeiiZd�Zd�Z	d�Z
d�Zdd�Zd�Zdd	�Z
dd
�ZRS(icCs�|otii||||�ntii|||�|i|_d|_d|_d|_d|_d|_	d|_
d|_dS(Nti�(R>RlRtfilenoRRHt_rbuft	_rbufsizeRCRDRFRG(Rtsockt
debugleveltstricttmethod((s&/sys/lib/python/mercurial/keepalive.pyRms						cCsR|ioD|ii�d|_|io |ii||i|i�qNndS(N(tfpR%RRCR,RDRG(R((s&/sys/lib/python/mercurial/keepalive.pyR%}s

	
cCs-|ii|i|idd�|i�dS(NR%i(RCR-RDRGR%(R((s&/sys/lib/python/mercurial/keepalive.pyR'�scCs|iS(N(RJ(R((s&/sys/lib/python/mercurial/keepalive.pyR8�scCs|iS(N(RF(R((s&/sys/lib/python/mercurial/keepalive.pytgeturl�scCs�|io\|dj	oOt|i�}||jo||8}qf|i| }|i||_|Sn|i|i|�}d|_|S(NRm(RoRR!t	_raw_read(RtamttLts((s&/sys/lib/python/mercurial/keepalive.pytread�s

	cCs�|i}d}xqtoi|djo�|ii�}|id�}|djo|| }nyt|d�}Wn,tj
o |i�t	i
|��nX|djoPq�n|djo||i|�7}n�||jo(||i|�7}|||_|Sn\||jo1||i|�7}|id�d|_|Sn||i|�7}||8}|id�d}qWx9to1|ii�}|pPn|djoPq�q�W|i�|S(NRmt;iiis
(t
chunk_lefttTrueRRutreadlinetfindtintt
ValueErrorR%R>tIncompleteReadt
_safe_read(RRxR}tvaluetlineti((s&/sys/lib/python/mercurial/keepalive.pyt
_read_chunked�sL	




	




	





i�cCs-|iid�}x�|djo�d|jot|i�jnoe|i|i�}|pPn|id�}|djo|t|i�}n|i||_qW|djot|i�}n|d}d|jot|i�jno
|}n|i| |i|}|_|S(Ns
ii(RoR�R!RwRp(RtlimitR�tnewRi((s&/sys/lib/python/mercurial/keepalive.pyR�s8$ 
1cCsdd}g}xQ|i�}|pPn|i|�|t|�7}|o||joPqq|S(Ni(RR
R!(RtsizehintttotalRR�((s&/sys/lib/python/mercurial/keepalive.pyt	readlines�s
N(RRRRR>RlR{RwR%R'R8RvR�RR�(((s&/sys/lib/python/mercurial/keepalive.pyRlYs					8R/cBseZeZRS((RRRltresponse_class(((s&/sys/lib/python/mercurial/keepalive.pyR/�sc
Cs)t}t�}ti|�}ti|�hdd<dd<}x�d	D]�}d|||fGH|ay`ti|�}|i�|i�y|i|i	}}Wnt
j
od
\}}nXWn!tj
o}	d|	GH�qJXd||fGHqJW|a|i
�}
dG|
GH|i�dS(Ntoffitonis.  fancy error handling %s (HANDLE_ERRORS = %i)s  EXCEPTION: %ss  status = %s, reason = %ssopen connections:(ii(NN(RKRkR4tbuild_openertinstall_openerturlopenR{R%RARBtAttributeErrorRtIOErrorR$R*(turltorigtkeepalive_handlertopenertposR�tfoRARBtethosts((s&/sys/lib/python/mercurial/keepalive.pyt
error_handler�s.	


#		cCsJyddkl}Wn#tj
oddkl}nX|a||�S(Ni�(tmd5(thashlibR�tImportError(Rzt_md5((s&/sys/lib/python/mercurial/keepalive.pyR�scCs?d}ti�}ti|�ti|�}|i�}|i�ti|�}|d|i�fGHtit	��}ti|�ti|�}|i�}|i�ti|�}|d|i�fGHti|�}d}x%|i
�}|o||}q�Pq�i�ti|�}|d|i�fGHdS(Ns%25s: %ss
normal urllibskeepalive readRmskeepalive readline(R4R�R�R�R{R%R�R�t	hexdigestRkR(R�tformatR�R�tfootmtf((s&/sys/lib/python/mercurial/keepalive.pyt
continuitys0




cCs�d||fGHtiid�ti�}ti|�t||�}d|GHtiid�tit��}ti|�t||�}d|GHd||fGHdS(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[tstdouttwriteR4R�R�tfetchRk(tNR�R�tt1tt2((s&/sys/lib/python/mercurial/keepalive.pytcomp>s
	
	cCs�ddk}g}|i�}xqt|�D]c}|o|djo|i|�nti|�}|i�}|i�|it|��q+W|i�|}	d}
x@|dD]4}|
d}
||djpd|
|fGHq�q�W|	S(Ni�iis+WARNING: inconsistent length on read %i: %i(	ttimetrangetsleepR4R�R{R%R
R!(R�R�tdelayR�tlenst	starttimeR�R�R�tdifftj((s&/sys/lib/python/mercurial/keepalive.pyR�Ps$
%

cCst}dd
d��Y}|�adGHti|�}|i�}|i�d}d|GHxJ|djo<tiid|�tii�t	i
d�|d8}q^Wtiid	�d
GHti|�}|i�}|i�||jo	dGHndGH|adS(Nt
FakeLoggercBseZd�ZeZZZRS(cWs
||GHdS(N((RRItargs((s&/sys/lib/python/mercurial/keepalive.pytdebughs(RRR�R8twarningR=(((s&/sys/lib/python/mercurial/keepalive.pyR�gs	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((R7R4R�R{R%R[R�R�tflushR�R�tstderr(R�tdbbackupR�R�tdata1R�tdata2((s&/sys/lib/python/mercurial/keepalive.pyttest_timeoutds.	
	



	i
cCsodGHyt|�Wn"tj
odGHti�nXHdGHt|�HdGHt||�HdGHt|�dS(Ns,checking error hander (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�R[texitR�R�R�(R�R�((s&/sys/lib/python/mercurial/keepalive.pyttest�s

t__main__s%s <integer> <url>(ii(((RR4R>R<RRR7R[R\RKRRRkRlR/R�R�R�R�R�R�R�RR�R�targvR�R�(((s&/sys/lib/python/mercurial/keepalive.pys<module>ks:<��				 		"


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.