mfc@sdZddlmZddlZddlZddlZddlmZddlmZddl Z e j -ere j dde nddl Z WdQXyddlmZWn!ek rddlmZnXd d d d d ddddddddddddgZdZdZdZdZdZdZd Zd!Zd"Zd#Zd$Zd%Zd&Zd'Zd(Z d)Z!d*Z"d+Z#d,Z$d-Z%d.Z&d/Z'd0Z(d1Z)d2Z*d3Z+d4Z,d5Z-d6Z.d7Z/d8Z0d9Z1d:Z2d;Z3d<Z4d=Z5d>Z6d?Z7d@Z8dAZ9dBZ:dCZ;dDZ<dEZ=dFZ>dGZ?dHZ@dIZAdJZBdKZCdLZDdMZEdNZFdOZGdPZHi)dQd 6dRd!6dSd#6dTd$6dUd%6dVd&6dWd'6dXd(6dYd)6dZd,6d[d-6d\d.6d]d/6d^d06d_d16d`da6dbd26dcd36ddd46ded56dfd66dgd76dhd86did96djd:6dkd;6dld<6dmd=6dnd>6dod?6dpd@6dqdA6drdB6dsdC6dtdD6dudI6dvdJ6dwdK6dxdL6dydM6dzdN6ZId{ZJd|ZKd ZLejMd}jNZOejMd~jPZQejMdZRejMdZSde jTfdYZUd fdYZVd fdYZWd fdYZXyddlYZYWnek rJnCXdeWfdYZZej[ddeXfdYZ\dZ]d e^fdYZ_d e_fdYZ`de_fdYZade_fdYZbde_fdYZcde_fdYZdde_fdYZede_fdYZfdeffdYZgdeffdYZhdeffdYZide_fdYZjde_fdYZke_ZldfdYZmdS(s HTTP/1.1 client library HTTPConnection goes through a number of "states", which define when a client may legally make another request or fetch the response for a particular request. This diagram details these state transitions: (null) | | HTTPConnection() v Idle | | putrequest() v Request-started | | ( putheader() )* endheaders() v Request-sent | | response = getresponse() v Unread-response [Response-headers-read] |\____________________ | | | response.read() | putrequest() v v Idle Req-started-unread-response ______/| / | response.read() | | ( putheader() )* endheaders() v v Request-started Req-sent-unread-response | | response.read() v Request-sent This diagram presents the following rules: -- a second request may not be started until {response-headers-read} -- a response [object] cannot be retrieved until {request-sent} -- there is no differentiation between an unread response body and a partially read response body Note: this enforcement is applied by the HTTPConnection class. The HTTPResponse class does not enforce this state machine, which implies sophisticated clients may accelerate the request/response pipeline. Caution should be taken, though: accelerating the states beyond the above pattern may imply knowledge of the server's connection-close behavior for certain requests. For example, it is impossible to tell whether the server will close the connection UNTIL the response headers have been read; this means that further requests cannot be placed into the pipeline until it is known that the server will NOT be closing the connection. Logical State __state __response ------------- ------- ---------- Idle _CS_IDLE None Request-started _CS_REQ_STARTED None Request-sent _CS_REQ_SENT None Unread-response _CS_IDLE Req-started-unread-response _CS_REQ_STARTED Req-sent-unread-response _CS_REQ_SENT i(tarrayN(t py3kwarning(turlsplittignores.*mimetools has been removed(tStringIOtHTTPt HTTPResponsetHTTPConnectiont HTTPExceptiont NotConnectedtUnknownProtocoltUnknownTransferEncodingtUnimplementedFileModetIncompleteReadt InvalidURLtImproperConnectionStatetCannotSendRequesttCannotSendHeadertResponseNotReadyt BadStatusLineterrort responsesiPitUNKNOWNtIdlesRequest-starteds Request-sentidieifiiiiiiiiii,i-i.i/i0i1i3iiiiiiiiiiiiiiiiiiiiiiiiiiiiiitContinuesSwitching ProtocolstOKtCreatedtAcceptedsNon-Authoritative Informations No Contents Reset ContentsPartial ContentsMultiple ChoicessMoved PermanentlytFounds See Others Not Modifieds Use Proxys(Unused)i2sTemporary Redirects Bad Requestt UnauthorizedsPayment Requiredt Forbiddens Not FoundsMethod Not AllowedsNot AcceptablesProxy Authentication RequiredsRequest TimeouttConflicttGonesLength RequiredsPrecondition FailedsRequest Entity Too LargesRequest-URI Too LongsUnsupported Media TypesRequested Range Not SatisfiablesExpectation FailedsInternal Server ErrorsNot Implementeds Bad GatewaysService UnavailablesGateway TimeoutsHTTP Version Not Supportediis\A[^:\s][^:\r\n]*\Zs\n(?![ \t])|\r(?![ \t\n])s[- -]s[-]t HTTPMessagecBs#eZdZdZdZRS(cCsT|jj|}|dkr.||j|RPtclosetLineAndFileWrappertint(R&RIRTR2RU((s/usr/lib64/python2.7/httplib.pyt _read_statuss8       cCsg}xtr|jjtd}t|tkrFtdn|j|t|tkrxtdtn|dkr Pq q W|S(sReads potential header lines into a list from a file pointer. Length of line is limited by _MAXLINE, and number of headers is limited by _MAXHEADERS. is header linesgot more than %d headerss s R.(s s R.( R7R4R;R<R8R=R?R9R(R&R1RI((s/usr/lib64/python2.7/httplib.pyt _read_headerss   c Cs|jdk rdSx[trp|j\}}}|tkrAPn|j}|jdkrjd|fGHn~qW||_|j|_ |dkrd|_ n?|j drd|_ n$|dkrd|_ n t ||j dkr d|_ d|_d |_tt|_dSt|jd|_|jdkrfx|jjD]}d G|GqQWnd|j_|jjd }|r|jd krd |_d|_n d|_|j|_|jjd }|r9|j r9yt||_ Wntk rd|_ qBX|j dkrBd|_ qBn d|_ |tks|tksd|koqdkns|jdkrd|_ n|j r|j r|j dkrd |_ndS(Nisheaders:sHTTP/1.0i sHTTP/1.i sHTTP/0.9i isheader:stransfer-encodingRVscontent-lengthiditHEAD(RRR$R7RdtCONTINUEReROR2R@RURTR>R RXRVRYR!RR4R1t getheadertlowerRWt _check_closeRcR`t NO_CONTENTt NOT_MODIFIEDRQ(R&RTR2RUtskipped_headersthdrttr_encRX((s/usr/lib64/python2.7/httplib.pytbeginsj                       cCs|jjd}|jdkrS|jjd}|rOd|jkrOtStS|jjdritS|rd|jkrtS|jjd}|rd|jkrtStS(Nt connectioni Ras keep-alivesproxy-connection(RRRhRTRiR7tFalse(R&tconntpconn((s/usr/lib64/python2.7/httplib.pyRj$scCs&|jr"|jjd|_ndS(N(R4RaR$(R&((s/usr/lib64/python2.7/httplib.pyRaBs  cCs |jdkS(N(R4R$(R&((s/usr/lib64/python2.7/httplib.pytisclosedGscCsT|jdkrdS|jdkr0|jdS|jrF|j|S|dkr|jdkrs|jj}n@y|j|j}Wnt k r|jnXd|_|j|S|jdk r||jkr|j}qn|jj|}|s|jn|jdk rP|jt |8_|jsP|jqPn|S(NR.Rfi( R4R$RQRaRVt _read_chunkedRXtreadt _safe_readR R8(R&tamtts((s/usr/lib64/python2.7/httplib.pyRwRs:          cCsC|jtkst|j}g}xtr|dkr|jjtd}t |tkrpt dn|j d}|dkr|| }nyt |d}Wn0t k r|jtdj|nX|dkrPqn|dkr|j|j|n||krR|j|j||||_dj|S||kr|j|j||jdd|_dj|S|j|j|||8}|jdd}q'Wx[tr+|jjtd}t |tkrt dn|sPn|d krPqqW|jdj|S( Nis chunk sizet;iiR.is trailer lines (RVRStAssertionErrorRWR7R$R4R;R<R8R=tfindRcR`RaR R%R?Rx(R&RyRWR(RIti((s/usr/lib64/python2.7/httplib.pyRvsV                      cCsg}xi|dkrq|jjt|t}|sQtdj||n|j||t|8}q Wdj|S(sVRead the number of bytes requested, compensating for partial reads. Normally, we have a blocking socket, but a read() can be interrupted by a signal (resulting in a partial read). Note that we cannot distinguish between EOF and an interrupt when zero bytes have been read. IncompleteRead() will be raised in this situation. This function should be used when bytes "should" be present for reading. If the bytes are truly not available (due to EOF), then the IncompleteRead exception can be used to detect the problem. iR.(R4Rwtmint MAXAMOUNTR R%R?R8(R&RyRztchunk((s/usr/lib64/python2.7/httplib.pyRxs cCs |jjS(N(R4tfileno(R&((s/usr/lib64/python2.7/httplib.pyRscCs.|jdkrtn|jj||S(N(RRR$RRh(R&tnametdefault((s/usr/lib64/python2.7/httplib.pyRhs cCs(|jdkrtn|jjS(s&Return list of (header, value) tuples.N(RRR$Rtitems(R&((s/usr/lib64/python2.7/httplib.pyt getheaderss N(RKRLR$RrR]RdReRpRjRaRuRwRvRxRRhR(((s/usr/lib64/python2.7/httplib.pyRus  '  Q   - :   cBseZdZdZeZeZdZdZ dZ dde j ddZdddZdZdZdZd Zd Zd Zd Zd ZddZdddZdZdZddZdidZdZdZe dZ!RS(i sHTTP/1.1iicCs||_||_d|_g|_d|_t|_d|_d|_ d|_ i|_ |dk rr||_ n|j ||\|_|_tj|_dS(N(ttimeouttsource_addressR$RZt_buffert_HTTPConnection__responset_CS_IDLEt_HTTPConnection__stateRQt _tunnel_hostt _tunnel_portt_tunnel_headersRPt _get_hostportthosttporttsockettcreate_connectiont_create_connection(R&RRRPRR((s/usr/lib64/python2.7/httplib.pyR]s            cCsY|jrtdn|j||\|_|_|rH||_n |jjdS(sC Set up host and port for HTTP CONNECT tunnelling. In a connection that uses HTTP Connect tunneling, the host passed to the constructor is used as proxy server that relays all communication to the endpoint passed to set_tunnel. This is done by sending a HTTP CONNECT request to the proxy server when the connection is established. This method must be called before the HTTP connection has been established. The headers argument should be a mapping of extra HTTP headers to send with the CONNECT request. s.Can't setup tunnel for established connection.N(RZt RuntimeErrorRRRRtclear(R&RRR1((s/usr/lib64/python2.7/httplib.pyt set_tunnels   cCs|dkr|jd}|jd}||kryt||d}WnItk r||ddkr~|j}qtd||dnX|| }n |j}|r|ddkr|ddkr|dd!}qn||fS( Nt:t]iR.snonnumeric port: '%s'it[i(R$trfindRcR`t default_portR(R&RRR~tj((s/usr/lib64/python2.7/httplib.pyRs      &cCs"|j||\|_|_dS(N(RRR(R&RR((s/usr/lib64/python2.7/httplib.pyt _set_hostport/scCs ||_dS(N(RO(R&tlevel((s/usr/lib64/python2.7/httplib.pytset_debuglevel2scCs4|jd|j|jfx4|jjD]#\}}|jd||fq-W|jd|j|jd|jd|j}|j \}}}|dkr|j t j d||j fnx[tr/|jjtd}t|tkrtd n|sPn|dkrPqqWdS( NsCONNECT %s:%d HTTP/1.0 s%s: %s s RPR[isTunnel connection failed: %d %sis header line(tsendRRRt iteritemstresponse_classRZRPRQRdRaRRR@R7R4R;R<R8R=(R&theaderR(tresponseRTtcodetmessageRI((s/usr/lib64/python2.7/httplib.pyt_tunnel5s(       cCsD|j|j|jf|j|j|_|jr@|jndS(s3Connect to the host and port specified in __init__.N(RRRRRRZRR(R&((s/usr/lib64/python2.7/httplib.pytconnectNs cCsQ|jr"|jjd|_n|jrD|jjd|_nt|_dS(s(Close the connection to the HTTP server.N(RZRaR$RRR(R&((s/usr/lib64/python2.7/httplib.pyRaVs      cCs|jdkr1|jr%|jq1tn|jdkrRdGt|GHnd}t|drt|t  r|jdkrdGHn|j |}x<|r|jj ||j |}qWn|jj |dS(sSend `data' to the server.issend:i RwssendIng a read()ableN( RZR$t auto_openRR ROR^R3t isinstanceRRwtsendall(R&tdatat blocksizet datablock((s/usr/lib64/python2.7/httplib.pyR`s    cCs|jj|dS(suAdd a line of output to the current request buffer. Assumes that the line does *not* end with \r\n. N(RR?(R&Rz((s/usr/lib64/python2.7/httplib.pyt_outputtscCsx|jjddj|j}|j2t|trK||7}d}n|j||dk rt|j|ndS(sSend the currently buffered request and clear the buffer. Appends an extra \r\n to the buffer. A message_body may be specified, to be appended to the request. R.s N(R.R.(RtextendR%RtstrR$R(R&t message_bodyRR((s/usr/lib64/python2.7/httplib.pyt _send_output{s    c CsF|jr$|jjr$d|_n|jtkr?t|_n t|j|||_|smd}nt j |}|rt d||j fnd|||j f}|j||jdkrB|s&d}|jdr t|\}}}}}n|r[y|jd} Wn tk rG|jd} nX|jd | q&|jry|j} |j} n|j} |j} y| jd} Wn tk r| jd} nX| jd d krd | d } n| |jkr |jd | q&|jd d| | fn|sB|jddqBndS(s`Send a request to the server. `method' specifies an HTTP request method, e.g. 'GET'. `url' specifies the object being requested, e.g. '/index.html'. `skip_host' if True does not add automatically a 'Host:' header `skip_accept_encoding' if True does not add automatically an 'Accept-Encoding:' header t/s<URL can't contain control characters. %r (found at least %r)s%s %s %si R.thttptasciitidnatHostRiRRs%s:%ssAccept-EncodingtidentityN(RRuR$RRt_CS_REQ_STARTEDRt_validate_methodRQt!_contains_disallowed_url_pchar_retsearchRtgroupt _http_vsn_strRt _http_vsnR>RtencodetUnicodeEncodeErrort putheaderRRRRR}R( R&R[turlt skip_hosttskip_accept_encodingtmatchRntnetloctnilt netloc_encRRthost_enc((s/usr/lib64/python2.7/httplib.pyt putrequestsV                 cCs8tj|}|r4td||jfndS(s&Validate a method name for putrequest.s?method can't contain control characters. %r (found at least %r)N(t$_contains_disallowed_method_pchar_reRR`R(R&R[R((s/usr/lib64/python2.7/httplib.pyR s cGs|jtkrtnd|}t|sGtd|fng|D]}t|^qN}x0|D](}t|rmtd|fqmqmWd|dj|f}|j|dS(skSend a request header line to the server. For example: h.putheader('Accept', 'text/html') s%ssInvalid header name %rsInvalid header value %rs%s: %ss N( RRRt_is_legal_header_nameR`Rt_is_illegal_header_valueR%R(R&Rtvaluestvt one_valueRn((s/usr/lib64/python2.7/httplib.pyRs     cCs5|jtkrt|_n t|j|dS(sIndicate that the last header line has been sent to the server. This method sends the request to the server. The optional message_body argument can be used to pass a message body associated with the request. The message body will be sent in the same packet as the message headers if it is string, otherwise it is sent as a separate packet. N(RRt _CS_REQ_SENTRR(R&R((s/usr/lib64/python2.7/httplib.pyt endheaders+s   cCs|j||||dS(s&Send a complete request to the server.N(t _send_request(R&R[RtbodyR1((s/usr/lib64/python2.7/httplib.pytrequest:scCsd}ytt|}Wnftk r}y"ttj|jj}Wqtt fk r|j dkrdGHqqXnX|dk r|j d|ndS(Nis Cannot stat!!sContent-Length( R$RR8t TypeErrortostfstatRtst_sizetAttributeErrortOSErrorROR(R&Rtthelentte((s/usr/lib64/python2.7/httplib.pyt_set_content_length>s" c Cstjg|D]}|j^q }i}d|krGd|dCs6             mp[ .