FRTc@sdZddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZddl Z ddlZ ddlZ ddlZ ddl mZmZddlmZmZmZmZmZddlmZdd lmZdd lmZdd lmZdd lm Z dd l!m"Z"e#Z$y8ddl%Z%ddl m&Z&e'e%dre(Z$nWne)k rnXyddl*Z*Wne)k rddl+Z*nXe,ddddfDZ-ide(6de#6Z.ej/j0ej/j1ej/j2e j3j4dZ5de6fdYZ7de6fdYZ8de6fdYZ9dej:fdYZ:de6fd YZ;d!e;fd"YZ<dS(#s" Handles basic connections to AWS i(tdatetimeN(tauth(t auth_handler(tconfigt UserAgent(tsixt http_clientturlparsetquotet encodebytes(tAWSConnectionError(tBotoClientError(tBotoServerError(tPleaseRetryException(tProvider(t ResultSet(thttps_connectiontSSLErrorccs|]}|tjkVqdS(N(tostenviron(t.0tkey((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pys Wst USER_IS_ADMINtCURRENT_VERSION_IDtAPPLICATION_IDiiPs cacerts.txttHostConnectionPoolcBsMeZdZdZdZdZdZdZdZdZ RS(s A pool of connections for one remote (host,port,is_secure). When connections are added to the pool, they are put into a pending queue. The _mexe method returns connections to the pool before the response body has been read, so they connections aren't ready to send another request yet. They stay in the pending queue until they are ready for another request, at which point they are returned to the pool of ready connections. The pool of ready connections is an ordered list of (connection,time) pairs, where the time is the time the connection was returned from _mexe. After a certain period of time, connections are considered stale, and discarded rather than being reused. This saves having to wait for the connection to time out if AWS has decided to close it on the other end because of inactivity. Thread Safety: This class is used only from ConnectionPool while it's mutex is held. cCs g|_dS(N(tqueue(tself((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt__init__zscCs t|jS(s Returns the number of connections in the pool for this host. Some of the connections may still be in use, and may not be ready to be returned by get(). (tlenR(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pytsize}scCs |jj|tjfdS(sZ Adds a connection to the pool, along with the time it was added. N(Rtappendttime(Rtconn((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pytputscCsf|jxUtt|jD]>}|jjd\}}|j|rQ|S|j|q WdS(s Returns the next connection in this pool that is ready to be reused. Returns None if there aren't any. iN(tcleantrangeRRtpopt _conn_readyR"tNone(Rt_R!((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pytgets cCs6tr tSt|dd}|dkp1|jSdS(sZ There is a nice state diagram at the top of http_client.py. It indicates that once the response headers have been read (which _mexe does before adding the connection to the pool), a response is attached to the connection, and it stays there until it's done reading. This isn't entirely true: even after the client is done reading, the response may be closed, but not removed from the connection yet. This is ugly, reading a private instance variable, but the state we care about isn't available in any public methods. t_HTTPConnection__responseN(t ON_APP_ENGINEtFalsetgetattrR'tisclosed(RR!tresponse((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyR&s cCsFx?t|jdkrA|j|jdrA|jjdqWdS(s/ Get rid of stale connections. iN(RRt _pair_staleR%(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyR#s.cCs)|\}}tj}|tj|kS(s[ Returns true of the (connection,time) pair is too old to be used. (R tConnectionPooltSTALE_DURATION(Rtpairt_connt return_timetnow((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyR0s  ( t__name__t __module__t__doc__RRR"R)R&R#R0(((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyR`s      R1cBsYeZdZdZdZdZdZdZdZdZ dZ d Z RS( s A connection pool that expires connections after a fixed period of time. This saves time spent waiting for a connection that AWS has timed out on the other end. This class is thread-safe. g@gN@cCs@i|_d|_tj|_tjddtjt_dS(NgtBototconnection_stale_duration( t host_to_pooltlast_clean_timet threadingtLocktmutexRtgetfloatR1R2(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRs    cCs'tj|j}i|d<|d=|S(NR<R@(tcopyt__dict__(Rt pickled_dict((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt __getstate__s cCs|jdS(N(R(Rtdct((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt __setstate__scCstd|jjDS(s@ Returns the number of connections in the pool. css|]}|jVqdS(N(R(Rtpool((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pys s(tsumR<tvalues(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRscCsQ|j|j8|||f}||jkr6dS|j|jSWdQXdS(s Gets a connection from the pool for the named host. Returns None if there is no connection that can be reused. It's the caller's responsibility to call close() on the connection when it's no longer needed. N(R#R@R<R'R)(Rthosttportt is_secureR((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pytget_http_connections   cCsY|jJ|||f}||jkr;t|j|?@[\]^`{|}~sutf-8s User-AgentsContent-LengthsTransfer-EncodingRT(R-R,R[t isinstanceRt text_typeRtencodetsetattrtTrueRt _auth_handlertadd_authRR\(Rt connectiontkwargsRtvaltsafe((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt authorizens & (R7R8RR]Rj(((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRS/s 7 t HTTPResponsecBseZdZddZRS(cOs#tjj|||d|_dS(Nt(RRkRt_cached_response(RtargsRg((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRscCsK|dkr4|js-tjj||_n|jStjj||SdS(s<Read the response. This method does not have the same behavior as http_client.HTTPResponse.read. Instead, if this method is called with no ``amt`` arg, then the response body will be cached. Subsequent calls to ``read()`` with no args **will return the cached response**. N(R'RmRRktread(Rtamt((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRos  N(R7R8RR'Ro(((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRks tAWSAuthConnectioncBseZd d ed d d d d dd ddd eed dZdZdZdZdZe eeZ dZ d Z e e e Z d Ze eZd Ze eZeZeZd Ze eZeZeZd Ze eZddZd dZdZdZdZdZdZd d dZd dZdZdZ dZ!dZ"d d d dZ#d d dd dZ$d dd d d d d d dZ%dZ&RS(!it/tawscCsC||_d|_tjddr9tjdd}n||_tjdd||_|jryt ryt dntj ddt }|dkrd}n||_ |r||_n t||_|j|||| tjtjtjtjf|_g|_tr'|jjtjntjf|_| dk re| d|_|j| d 7_n d|_|rd |_ n d |_ ||_!| |_"t#| t$j%sd} ntj&dd | |_'d|_(i|_)t*j+dt*j+d fdkrtj&ddd|j)dscCsgS(N((R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRAscCst|jdS(Nt service_name(R-Rd(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt_get_auth_service_nameDscCs||j_dS(N(RdR(Rtvalue((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt_set_auth_service_nameJscCst|jdS(Nt region_name(R-Rd(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt_get_auth_region_nameNscCs||j_dS(N(RdR(RR((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt_set_auth_region_nameQscCs|j|jS(N(RNR(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRfUscCs |jjS(N(Rt access_key(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRYscCs |jjS(N(Rt secret_key(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyR_scCs |jjS(N(RR(R((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRescCs|js#|jtjdd|S|jd}|dkrU||}|| }nd}|ddkrtt}nt}|jjd}|j |jdg|D]}|r|^q}ddj |}|ddkr|r|d7}n|r ||}n|S(Ns^(/*)/s\1t?iiRr( R}RXtretsubtfindR'RcR,tsplittextendtjoin(RRXtposRZt need_trailingt path_elementstp((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pytget_pathis&       cCs|s|j}n|dkr*|j}nWtrCtjd dksVtjd dkrn|dkrn|j}nd|j|f}|S( NiPis2.5s2.6s2.7is%s:%d(s2.6s2.7(RLRKR+Rtversion(RRLtsignature_host((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt server_names    cCs||_||_||_||_dtjkr|j rtjd}|jtjd}|rF|j d|_|j d|_|j d|_|j d|_qFn|jst j ddd|_n|jst j dd d|_n|js"t j dd d|_n|jsFt j dd d|_n|j rm|jrmd GH|j |_ntjjd dptjjdd|_|jdk |_dS(Nt http_proxysX(?:http://)?(?:(?P[\w\-\.]+):(?P.*)@)?(?P[\w\-\.]+)(?::(?P\d+))?RKRLtusertpassR:RRRRsFhttp_proxy environment variable does not specify a port, using defaulttno_proxyRltNO_PROXY(RRRRRRRtcompiletmatchtgroupRRR'RLR)Rt use_proxy(RRRRRtpatternR((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRs4         -cCs?|jj|||}|dk r(|S|j|||SdS(N(RRNR'tnew_http_connection(RRKRLRMR!((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyRNs cCs|js tS|jdkr tS|}|jdd}xB|jjdD].}|rL|j|sv|j|rLtSqLWtS(Nt*t:it,(RR,RcRtendswith(RRKthostonlytname((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt skip_proxys $c Cs|dkr|j}n|jddd}|jj}||d<|jr| r|j| r|j}t|j |d mexe - Multi-execute inside a loop, retrying multiple times to handle transient Internet errors by simply trying again. Also handles redirects. This code was inspired by the S3Utils classes posted to the boto-users Google group by Larry Bates. Thanks! s Method: %ssPath: %ssData: %ss Headers: %ssHost: %ssPort: %ss Params: %sR:R~iRasutf-8itmax_retry_delayi<s Token: %sRfts3tanonsFinal headers: %ssResponse headers: %stlocationtHEADRTiiiisReceived %d response. sRetrying in %3.1f secondsi,iRRRisRedirecting: %ss://Rws!encountered a retry exception: %ss0encountered unretryable %s exception, re-raisings&encountered %s exception, reconnectingRs-Please report this exception as a Boto Issue!N(iiii(=RRRyRVRXR\R[RKRLRZR'RRR~RNRMR_tbytesRRatmintrandomR)RRRjRR-R,R RR6t start_timetcallableRt getresponset getheaderst getheaderRTR tsleepRRotdecodeRRORthandle_request_dataRRR RR/RRRR7RcR RR (RRtsendertoverride_num_retriest retry_handlerR/R\teR~tiRft next_sleepRRRtconn_header_valuetschemeRZtquerytfragmentt unretryable((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt_mexevs                $$       RlcCs4|j|}|dk r-|j|}n|dkrBi}n |j}|dkrci}n |j}|jrtjjd| r|j|dRERORRRS(((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyR36s"       *(=R9RRRRRRRR txml.saxRIRBRRRt boto.utilst boto.handlert boto.cacertsRRt boto.compatRRRRR tboto.exceptionR R R R t boto.providerRtboto.resultsetRR,RRRRRct ImportErrorR>tdummy_threadingtallR+RRXRtdirnametabspathtcacertst__file__RtobjectRR1RSRkRqR3(((s</opt/freeware/lib/python2.7/site-packages/boto/connection.pyt,sb             (       3jeT