{r Qc@sdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZmZddlmZmZddlmZddlmZddl m!Z!e"Z#y8ddl$Z$dd lm%Z%e&e$d re'Z#nWne(k rnXyddl)Z)Wne(k rddl*Z)nXe+d d d dfDZ,ide'6de"6Z-ej.j/ej.j0ej.j1ej2j3dZ4de5fdYZ6de5fdYZ7de5fdYZ8dej9fdYZ9de5fdYZ:de:fdYZ;dS(s" Handles basic connections to AWS i(twith_statementN(tconfigt UserAgent(tAWSConnectionErrortBotoClientError(tBotoServerError(tProvider(t ResultSet(thttps_connectiontSSLErrorccs|]}|tjkVqdS(N(tostenviron(t.0tkey((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pys Zst USER_IS_ADMINtCURRENT_VERSION_IDtAPPLICATION_IDiiPs cacerts.txttHostConnectionPoolcBsMeZdZdZdZdZdZdZdZdZ RS(s A pool of connections for one remote (host,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 fram ConnectionPool while it's mutex is held. cCs g|_dS(N(tqueue(tself((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyt__init__}scCs 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((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pytsizescCs |jj|tjfdS(sZ Adds a connection to the pool, along with the time it was added. N(Rtappendttime(Rtconn((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/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 of there aren't any. iN(tcleantrangeRRtpopt _conn_readyRtNone(Rt_R((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pytgets cCs6tr tSt|dd}|dkp1|jSdS(sV There is a nice state diagram at the top of httplib.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_ENGINEtFalsetgetattrRtisclosed(RRtresponse((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRs cCsFx?t|jdkrA|j|jdrA|jjdqWdS(s/ Get rid of stale connections. iN(RRt _pair_staleR(R((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRs.cCs)|\}}tj}|tj|kS(s[ Returns true of the (connection,time) pair is too old to be used. (RtConnectionPooltSTALE_DURATION(Rtpairt_connt return_timetnow((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyR(s  ( t__name__t __module__t__doc__RRRR!RRR((((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRcs      R)cBsYeZdZdZdZdZdZdZdZdZ 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 threadingtLocktmutexRtgetfloatR)R*(R((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRs    cCs'tj|j}i|d<|d=|S(NR4R8(tcopyt__dict__(Rt pickled_dict((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyt __getstate__s cCs|jdS(N(R(Rtdct((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyt __setstate__scCstd|jjDS(s@ Returns the number of connections in the pool. css|]}|jVqdS(N(R(R tpool((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pys s(tsumR4tvalues(R((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRscCsN|j|j5||f}||jkr3dS|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(RR8R4RR!(Rthostt is_secureR ((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pytget_http_connections    cCsV|jG||f}||jkr8t|j|\w+):(?P.*)@)?(?P[\w\-\.]+)(?::(?P\d+))?RCROtusertpassR2RRRRsFhttp_proxy environment variable does not specify a port, using default(RRRRR R RtcompiletmatchtgroupRRzRROt use_proxy(RRRRRtpatternR((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyR|~s2         cCs9|jj||}|dk r%|S|j||SdS(N(RRERtnew_http_connection(RRCRDR((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyREs cCs|jr/| r/d|jt|jf}n|dkrJ|j}n|rtjjd||j |jr|j ||rdpd}qC|j r|j |}qC|j rt rtj|d|j|j }qCtj||j }nMtjjd|j |j r.|j ||j }ntj||j }|jdkre|j|jn|jdd |jkr||jkr||f|_nt|_|S( Ns%s:%ds1establishing HTTPS connection: host=%s, kwargs=%siiPtca_certss'establishing HTTP connection: kwargs=%sit:i(RRRRRRtbototlogRpRt proxy_sslRRlRyRtCertValidatingHTTPSConnectionRmRdtHTTPSConnectiontHTTPConnectiontset_debuglevelRRCRDRRbtresponse_class(RRCRDR^((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRs>    !         + cCs|jj|||dS(N(RRF(RRCRDR^((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRFsc Cs|r|rd||f}nd|j|jf}tjtjtj}y#|j|jt|jfWn nXt j j d||j d||j dt |jr&|jr&x7|jjD]#\}}|j d||fqWtjddtr3|j dq3n |j dtj|d td |j }|j|jd krtjd d |j|j|j|jfn|jtj|}|jrtrt j j d|j |j!j"dd}|j!j"dd} t$j%|d|d| dt$j&d|j } | j'} |jj(ddd} t)j*| | st)j+| | dqnKt,tdrtj$j-|} n'tj$|dd} tj.|| } | |_/|S(Ns%s:%ds'Proxy connection: CONNECT %s HTTP/1.0 sCONNECT %s HTTP/1.0 sUser-Agent: %s s%s: %s R2t"send_crlf_after_proxy_auth_headerss tstrictt debugleveliis*Error talking to HTTP proxy %s:%s: %s (%s)sBwrapping ssl socket for proxied connection; CA certificate file=%stkey_filet cert_filetkeyfiletcertfilet cert_reqsRRishostname mismatchtssl(0RCROR~tAF_INETt SOCK_STREAMtconnectRRRRRRptsendallRRRtget_proxy_auth_headerRHRRxR$RdRbRtbegintstatusRtreasontcloseRRlRyRmRR!RRt wrap_sockett CERT_REQUIREDt getpeercertRRtValidateCertificateHostnameRthasattrt SSLSockett FakeSockettsock( RRCRORtktvtrespthRRtsslSocktcertthostname((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRs\ #           cCs%|jd|p|j|}|S(Ns://(RNR(RRPRC((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pytprefix_proxy_to_paths!cCs,tj|jd|j}id|d6S(NRsBasic %ssProxy-Authorization(tbase64t encodestringRR(RR((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRsc CsEtjjd|jtjjd|jtjjd|jtjjd|jtjjd|jd}d}d}|dkrt j dd|j }n|}d} |j |j|j } x,| |krtjd | } y`tjjd |jj|jd |t|rS|| |j|j|j|j}n.| j|j|j|j|j| j}|jd } |jd krt|dtrd|_nt|r||| | } | r| \}} } |r tjj|ntj| wqn|jdks=|jdkrwd|j}|d| 7}tjj||j}n|jdks|jdks| r|j|j|j | |Stj| \}|_|_}}}|r|jd|7_nd|d}||j|j7}tjj||j |j|dk} d}wWn|jk r}xC|j D]8}t!||rrtjjd|j"j#|qrqrWtjjd|j"j#|j$|j|j } nXtj| | d7} qW|r t%|j|j&|n!|r/|nd}t'|dS(s> 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: %sR2Ruiis Token: %sR^tlocationtHEADRKiisReceived %d response. sRetrying in %3.1f secondsi,iRsRedirecting: %ss://Rns0encountered unretryable %s exception, re-raisings&encountered %s exception, reconnectingis-Please report this exception as a Boto Issue!N((RRRpRMRPRTRSRCRRRRuRERDtrandomRRRatcallabletrequestt getresponset getheaderR%R$RKRtsleepRRgRFturlparseRRRVRR/RRRR(RRtsendertoverride_num_retriest retry_handlerR'RTteRutiR^t next_sleepRRtmsgtschemeRRtquerytfragmentt unretryable((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyt_mexes          '       Rcc Cs|j|}|dk r-|j|}n|dkrBi}n |j}|dkrci}n |j}|p{|j}|jr|s|}n|j||}|jr|jr|j r|j |j qnt ||j ||j ||||| S(N(RRR:RCRRRRRDtupdateRRJRNRO(RRMRPRQRRRStdataRC((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pytbuild_base_http_requests$         c CsI| dkri} n|j|||| |||} |j| ||S(s?Makes a request to the server, with stock multiple-retry logic.N(RR R( RRMRPRSR RCRQRRRRt http_request((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyt make_requests   cCstjjdd|_dS(s(Optional) Close any open HTTP connections. This is non-destructive, and making a new request will open a connection again.sclosing all HTTP connectionsN(RRRpRR(R((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRsN("R/R0RRRRRRRtpropertyRRRtauth_region_nameR^Rtgs_access_key_idRRtgs_secret_access_keyRRRR|RERRFRRRRR R R(((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRisT                 "  + A   e   tAWSQueryConnectioncBseZdZeZd d ed d d d d d dd dd edZdZdZ d dddZ dZ d Z dd dd Z dd dd Zdd dd ZRS(RciRjcCsAtj|| ||||||||| | | d| d| dS(NRR(RiR(RRRRDRORRRRRCRpRRPRR((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRs   cCsgS(N((R((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRscCstjj|S(N(Rtutilstget_utf8_value(RR((s2/tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/connection.pyRstGETcCsf|j||d|id|j}|r=||jd,sf                           3jeP