o ]Lbjl @sdZddlmZmZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddlmZddlm Z mZmZdd lmZejZejZejZdaGd d d eZGd d d eZGdddeejZGdddejZddZddZ Gdddej!Z!ddZ"ddZ#d&ddZ$ddZ%d'd!d"Z&e'd#krddl(Z(ze)ej*dZ+ej*d$Z,Wne-e.fye/d%ej*dYdSwe&e,e+dSdS)(aKAn 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_importprint_functionN)_)getattr)hex)pycompat urllibcompatutil)procutilc@sBeZdZdZddZddZddZdd Zd d Zdd dZ d S)ConnectionManagerzT The connection manager must be able to: * keep track of all existing cCs&t|_tt|_i|_i|_dSN) threadingLock_lock collections defaultdictlist_hostmap_connmap _readymapselfr5/usr/lib/python3/dist-packages/mercurial/keepalive.py__init__us   zConnectionManager.__init__cCsL|jz|j||||j|<||j|<W|jdS|jwr )racquirerappendrrrelease)rhost connectionreadyrrradd{s   zConnectionManager.addcCs|jzBz|j|}Wn tyYn*w|j|=|j|=|j|||j|s8|j|=W|jdSW|jdSW|jdS|jwr )rrrKeyErrorrrremover)rr rrrrr$s"    zConnectionManager.removecCs&z||j|<WdStyYdSwr )rr#)rr r!rrr set_readys  zConnectionManager.set_readycCshd}|jz&|j|D]}|j|rd|j|<|}n q W|j|SW|j|S|jw)NF)rrrrr)rrconncrrrget_ready_conns       z ConnectionManager.get_ready_connNcCs|r t|j|St|jSr )rrdict)rrrrrget_alls zConnectionManager.get_allr ) __name__ __module__ __qualname____doc__rr"r$r%r(r*rrrrr os  r c@s`eZdZdddZddZddZdd Zd d Zdd dZddZ ddZ ddZ ddZ dS)KeepAliveHandlerNcCst|_||_d|_d|_dSNr)r _cm_timeout requestscountsentbytescount)rtimeoutrrrrs zKeepAliveHandler.__init__cCsdd|jDS)ztreturn a list of connected hosts and the number of connections to each. [('foo.com:80', 2), ('bar.org', 1)]cSsg|] \}}|t|fqSr)len).0rlirrr sz5KeepAliveHandler.open_connections..)r1r*itemsrrrropen_connectionssz!KeepAliveHandler.open_connectionscCs*|j|D] }|j||qdS)zclose connection(s) to 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)r1r*r$close)rrhrrrclose_connections  z!KeepAliveHandler.close_connectioncCs<t|jD]\}}|D] }|j||qqdS)zclose all open connectionsN)r iteritemsr1r*r$r<)rrconnsr=rrr close_alls   zKeepAliveHandler.close_allcCs|j|ddS)zetells us that this request is now closed and that the connection is ready for another requestTN)r1r%)rrequestrr rrr_request_closedsz KeepAliveHandler._request_closedrcCs|r||j|dSr )r<r1r$)rrr r<rrr_remove_connectionsz#KeepAliveHandler._remove_connectioncCs |t|Sr )do_openHTTPConnection)rreqrrr http_open zKeepAliveHandler.http_openc Cspt|}|s tdzJ|j|}|r1||||}|rn6||j||j|}|s|||j d}t rCt d|t ||j ||d||||}Wn.tjyp}z ttdt|jd}~wtjtjfy}zt|d}~wwt|ddr|j|t rt d|j|j||_||_||_||_ |j|_!|j"|_#|j|_"|S) Ns no host given)r5s"creating new connection to %s (%d)Fsbad HTTP status line: %s will_closeTsSTATUS: %s, %s)$r gethosturlerrurlerrorr1r(_reuse_connectionr<r$r2DEBUGinfoidr"_start_transaction getresponsehttplib BadStatusLinerrsysbyteslinesocketerror HTTPExceptionrstatusreason_handler_host get_full_url_url _connectioncodemsgheaders)r http_classrGrr=rerrrrrrEsV           zKeepAliveHandler.do_openc Csz ||||}Wn'tjtjfyd}Yntr)td|t||j || |dus=|j dkrLtrHt d|t|d}|StrWt d|t||S)aGstart 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. Ns4unexpected exception - closing connection to %s (%d) s&failed to re-use connection to %s (%d)sre-using connection to %s (%d)) rRrSrXrYrTrZrOrQr1r$r<versionrP)rr=rGrrfrrrrNs2     z"KeepAliveHandler._reuse_connectionc Cst|dd}t|jj}|t|j|t|j tdd|D}i}dD]}||vrAd|d| dd <q1zCt |rut |}|j|t |fi|d |vrf|d d d |vrt|ddt|n|j|t |fi|Wntjy}zt|d}~ww|D] \} } || | q|t |r|||jd7_|jt|dd|7_z|jjd7_|jjt|dd|7_WdStyYdSw)Nr4rcss |] \}}||fVqdSr )lower)r7nvrrr Lsz6KeepAliveHandler._start_transaction..)rzaccept-encodingrskip_-rz content-typez Content-typez!application/x-www-form-urlencodedzcontent-lengthzContent-lengthz%d)rr sortdictparent addheadersupdatesortedrdr:unredirected_hdrsreplacer hasdatagetdata putrequest get_method getselector putheaderr6rXrYrLrM endheaderssendr3r4AttributeError) rr=rG oldbytescountrd skipheadersrkdatargkrlrrrrRCsh       z#KeepAliveHandler._start_transactionr r) r+r,r-rr;r>rArCrDrHrErNrRrrrrr/s  9 2r/c@s eZdZdS) HTTPHandlerN)r+r,r-rrrrrzsrc@seZdZdddZejjZeejddZ ddZ dd Z d d Z d d Z ddZdddZddZddZdddZddZdS) HTTPResponserNcCsri}tjs d|d<d|d<tjj||f||d||j|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ dS)NTstrict buffering) debuglevelmethodri)rispy3rTrrfilenorbreceivedbytescount_rbuf _rbufsizer]r^r`ra)rsockrrrextrakwrrrrs( zHTTPResponse.__init__readintocCs<|jr|jd|_|jr|j||j|jdSdSdSr )fpr<r]rCr^rarrrrr<s  zHTTPResponse.closecCs |dSr r<rrrr _close_connrIzHTTPResponse._close_conncCs"|jj|j|jdd|dS)Nrr)r]rDr^rar<rrrrr>s zHTTPResponse.close_connectioncC|jSr )rdrrrrrPzHTTPResponse.infocCrr )r`rrrrgeturlrzHTTPResponse.geturlcCs|jr&|dur&t|j}||kr||8}n|jd|}|j|d|_|S|j}d|_||}|jt|7_z |jjt|7_Wn tyOYnwz |jjjt|7_Wn tyfYnw||7}|S)Nr)rr6 _raw_readrrarr]rq)ramtLsrrrrreads.     zHTTPResponse.readcCsL|j}g} |dur>|j}|d}|dkr|d|}zt|d}Wnty8|td |w|dkr>nP|durK| | |n;||kra| | ||||_d |S||krz| | || dd|_d |S| | |||8}| dd}q |j}|sn|dkrnq|d |S)NT;rrs ) chunk_leftrreadlinefindint ValueErrorr<rTIncompleteReadjoinr _safe_read)rrrpartsrWirrr _read_chunkedsR         $  zHTTPResponse._read_chunkedcCs6|jd}|dkr|d7}|jd|}|j|d|_|S|jdur)|jg}ng}d}|j} ||}|s9n:|jt|7_|jjt|7_z |jj jt|7_Wn t ybYnw| ||d}|dkrrnq1|dkrd|_d |S|d7}|d|d|_|dd||d<d |S)N rrTr) rr _raw_readintorrrr6rar]rqrrr)rrrWchunksreadsizenewrrrrsD         zHTTPResponse.readlinecCsFd}g} |}|s |S|||t|7}|r"||kr" |Sqr0)rrr6)rsizehinttotalrrWrrr readlinesEs  zHTTPResponse.readlinescCs|jdur|t|}|sdS||dt|<t|St|}t|j}||kr>|jd||d|<|j|d|_|St|}||||}|j|7_|jj|7_z |jj|7_Wn tymYnw|j|d|<|t|j7}d|_|S)Nrr) rrr6r memoryviewrrar]r)rdestresrhavemvgotrrrrRs2   zHTTPResponse.readinto)rrNr r)r+r,r-rrTrrrrrr<rr>rPrrrrrrrrrr~s    6 . rc CsHt|dddur dS|jdur|jr|ntj|jdkr&tdt|zHd}t|dd}|dur]|jdkr.safegetresponse)rSr.)rrrrrwrapgetresponses  rc@s&eZdZeZeZeej Z ddZ dS)rFcOs*tjj|g|Ri|d|_d|_dSr0)rTrFrr4r)rrkwargsrrrrs zHTTPConnection.__init__N) r+r,r-rresponse_classrr~rrTrFrSrrrrrrFs   rFcCstj}d}t}t|t|}|}|||}t|dt | ftt }t|t|}|}|||}t|dt | ft|}d} | }|rf||}nnq[|||}t|dt | fdS)Ns%25s: %ss normal urllibskeepalive readrTskeepalive readline) hashlibmd5urlreq buildopener installopenerurlopenrr<rrdigestrr)urlrformatopenerfofoomfrrr continuitys6       rcCstd||ftjdt}t|t||}td|tjdtt}t|t||}td|td||dS)Ns making %i connections to: %ss( first using the normal urllib handlerss TIME: %.3f ss( now using the keepalive handler s improvement factor: %.2f) rr stdoutwriterrrfetchr)Nrrt1t2rrrcomps         rc Csddl}g}|}t|D]!}|r|dkr||t|}|}||t|q||} d} |ddD]}| d} ||dksRt d| |fq>| S)Nrrs+WARNING: inconsistent length on read %i: %i) timerangesleeprrrr<rr6r) rrdelayrlens starttimerrrdiffjrrrrs$      rcCst}Gdddt}|atdt|}|}|d}td||dkrDtj d|tj t d|d8}|dks*tj d td t|}|}|||krgtd |adStd |adS) Nc@s eZdZddZeZZZdS)z test_timeout..FakeLoggercWst||dSr )r)rrcrrrrdebugsz&test_timeout..FakeLogger.debugN)r+r,r-rrPwarningrYrrrr FakeLoggersrs- fetching the file to establish a connections; waiting %i seconds for the server to close the connectionrs %2ir s! fetching the file a second times data are identicals ERROR: DATA DIFFER)rOobjectrrrrr<r rrflushrrstderr)rdbbackuprrdata1rdata2rrr test_timeouts2      r cCsFtdt|tdtdt||tdtdt|dS)Ns>performing continuity test (making sure stuff isn't corrupted)rsperforming speed comparisons#performing dropped-connection check)rrrr)rrrrrtest?s  r__main__rs%s r)r)0r. __future__rrrrrrXsysri18nrrrnoderr r utilsr rTrLrrOrr r/ httphandlerrrrrrFrrrrrr+rrargvrr IndexErrorrrrrrrsR;    ?Mr6$  &