m %UIc@sdZdkZdkZdkZdklZydklZWn ej odklZnXdddddd d d d d dddddddgZ dZ dZ dZ dZ dZdZdZdZdZdZdZd Zd!Zd"Zd#Zd$Zd%Zd&Zd'Zd(Zd)Zd*Zd+Z d,Z!d-Z"d.Z#d/Z$d0Z%d1Z&d2Z'd3Z(d4Z)d5Z*d6Z+d7Z,d8Z-d9Z.d:Z/d;Z0d<Z1d=Z2d>Z3d?Z4d@Z5dAZ6dBZ7dCZ8dDZ9dEZ:dFZ;dGZ<dHZ=dIZ>dJZ?dKZ@dLZAdMeiBfdNYZCdfdOYZDdfdPYZEdQfdRYZFdSfdTYZGdUeGfdVYZHdWeGfdXYZIdeEfdYYZJdfdZYZKeLed[od\eKfd]YZMndeNfd^YZOd eOfd_YZPdeOfd`YZQd eOfdaYZRd eOfdbYZSd eOfdcYZTd eOfddYZUdeOfdeYZVdeVfdfYZWdeVfdgYZXdeVfdhYZYdeOfdiYZZeOZ[djfdkYZ\dlZ]e^dmjo e]ndS(ns HTTP/1.1 client library HTTPConnection go through a number of "states", which defines 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 N(surlsplit(sStringIOtHTTPt HTTPResponsetHTTPConnectiontHTTPSConnectiont HTTPExceptiont NotConnectedtUnknownProtocoltUnknownTransferEncodingtUnimplementedFileModetIncompleteReadt InvalidURLtImproperConnectionStatetCannotSendRequesttCannotSendHeadertResponseNotReadyt BadStatusLineterroriPitUNKNOWNtIdlesRequest-starteds Request-sentidieifiiiiiiiiii,i-i.i/i0i1i3iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiit HTTPMessagecBs#tZdZdZdZRS(NcCsV|ii|}|djo||i|R@RARB((Rt__init__s          cCsv|ii}|idjodGt|GHn|pt|ny|idd\}}}WnQt j oEy"|idd\}}d}Wqt j o d}qXnX|i dpA|io|it|q t||i|_d Sny:t|}|d jp |d jot|nWnt j ot|nX|||fS( Nisreply:iiR!sHTTP/sHTTP/0.9iidi(sHTTP/0.9iR!(RR,R0R1R@treprRtsplitRRFR&RGt ValueErrorR2RAtclosetLineAndFileWrappertint(RR&RGRFR1((Rt _read_status$s4    cCs|idj odSnxtox|i\}}}|tjoPnxJtoB|i i i }|pPn|i djo dG|GHqLqLWqW||_|i |_|djo d|_nD|ido d|_n'|djo d|_n t||idjo5d|_d|_d |_tt|_dSnt|i d|_|i djo#x |iiD]}dG|GqWnd|i_ |iid }|o)|id jod |_d|_n d|_|i|_|iid }|oA|i o6yt||_Wqftj od|_qfXn d|_|tjp;|tjp.d |jo djnp|i djo d|_n|i o(|i o|idjo d |_ndS(Nisheader:sHTTP/1.0i sHTTP/1.i sHTTP/0.9i istransfer-encodingRHscontent-lengthiditHEAD(!RRDRR.RSRFR&RGtCONTINUER,R0R4tskipR@R2RRJRHRKRtStringIOR$thdrt getheaderttr_enctlowerRIt _check_closeRRROt NO_CONTENTt NOT_MODIFIEDRC(RR&RXRVRJRGRFRZ((RtbeginIsh                 H &cCs|iid}|idjo<|iid}|od|ijotSntSn|iidotSn|od|ijotSn|iid}|od|ijotSntS(Nt connectioni RPs keep-alivesproxy-connection( RRDRYtconnRFR[R.tFalsetpconn(RRaRc((RR\scCs(|io|iid|_ndS(N(RR,RPR(R((RRPs  cCs |idjS(N(RR,R(R((RtisclosedscCs|idjodSn|io|i|Sn|djoP|idjo|ii}n|i |i}d|_|i |Sn|idj o!||ijo |i}qn|ii|}|idj o|it |8_n|S(NR!i( RR,RRHt _read_chunkedtamtRJtreadtst _safe_readRPR8(RRfRh((RRgs$    cCs|itjpt|i}d}x>to6|djo^|ii }|i d}|djo|| }nt |d}|djoPqn|djo||i|7}n||jo(||i|7}|||_|Sn\||jo1||i|7}|idd|_|Sn||i|7}||8}|idd}q)Wx-to%|ii }|djoPqjqjW|i|S(NR!t;iiis (RRHREtAssertionErrorRIRR.RR,R0R1tfindtiRRRfRiRP(RRfRmRIRR1((RResF                 cCswg}xa|djoS|iit|t}|pt|n|i ||t |8}q Wdi |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!N( RhRfRR,Rgtmint MAXAMOUNTtchunkR R3R8R(RRfRpRh((RRis   cCs0|idjo tn|ii||S(N(RRDRRRYtnametdefault(RRqRr((RRY,s cCs*|idjo tn|iiS(s&Return list of (header, value) tuples.N(RRDRRtitems(R((Rt getheaders1s (R;R<RRLRSR_R\RPRdRgReRiRYRt(((RRs  % P    /  cBstZdZdZeZeZdZdZ dZ dddZ dZ dZdZd Zd Zd Zd Zddd ZdZdZdhdZdZdZRS(Ni sHTTP/1.1iicCs[d|_g|_d|_t|_d|_|i|||dj o ||_ ndS(N( RRR>t_buffert_HTTPConnection__responset_CS_IDLEt_HTTPConnection__stateRCt _set_hostportthosttportRA(RRzR{RA((RRLCs      cCs|djo|id}|id}||joTyt||d}Wn+tj otd||dnX|| }n |i }|o3|ddjo"|ddjo|dd!}qn||_||_dS(Nt:t]isnonnumeric port: '%s'it[i( R{RRztrfindRmtjRRROR Rt default_port(RRzR{RmR((RRyNs   ) cCs ||_dS(N(tlevelRR@(RR((Rtset_debuglevel_scCs&d}xti|i|idtiD]}|\}}}}}yUti||||_ |idjod|i|ifGHn|i i|Wnhtij oY}|idjodG|i|ifGHn|i o|i ind|_ q(nXPq(W|i pti|ndS(s3Connect to the host and port specified in __init__.s!getaddrinfo returns an empty listisconnect: (%s, %s)s connect fail:N(RDtsockett getaddrinfoRRzR{t SOCK_STREAMtrestaftsocktypetprotot canonnametsaR>R@tconnectRRPR(RRRRRRRDR((RRbs*    cCsU|io|iid|_n|io|iid|_nt|_dS(s(Close the connection to the HTTP server.N(RR>RPRRvRwRx(R((RRPxs      cCs|idjo%|io|iq5tn|idjodGt|GHny|ii |Wn:t i j o+}|ddjo|i nnXdS(sSend `str' to the server.issend:i N(RR>Rt auto_openRRR@RMtstrtsendallRRtvRP(RRR((Rtsends  cCs|ii|dS(suAdd a line of output to the current request buffer. Assumes that the line does *not* end with \r\n. N(RRuR3Rh(RRh((Rt_outputscCs:|iiddi|i}|i2|i|dS(spSend the currently buffered request and clear the buffer. Appends an extra \r\n to the buffer. R!s N(R!R!(RRutextendRRDR(RRD((Rt _send_outputs cCsz|io|iio d |_n|itjo t|_n t||_ |p d}nd|||i f}|i ||idjo|pd}|idot|\}}}}}n|o|id|idqW|itjo |id|iidqW|idd|iid|ifn|p|id d qvnd S( 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%s %s %si R!thttptHosttidnas%s:%ssAccept-EncodingtidentityN(RRvRdRRxRwt_CS_REQ_STARTEDR RBRCturlt _http_vsn_strRRt _http_vsnt skip_hosttnetlocR2turlsplittnilt putheadertencodeR{t HTTP_PORTRztskip_accept_encoding(RRBRRRRRR((Rt putrequests.       - cCs>|itjo tnd||f}|i|dS(skSend a request header line to the server. For example: h.putheader('Accept', 'text/html') s%s: %sN(RRxRR theaderRRR(RRRR((RR s  cCs4|itjo t|_n t|idS(s?Indicate that the last header line has been sent to the server.N(RRxRt _CS_REQ_SENTR R(R((Rt endheaderss   cCsry|i||||WnQtij oB}|ddjp |i on|i||||nXdS(s&Send a complete request to the server.ii N( Rt _send_requestRBRtbodyR$RRRR(RRBRRR$R((Rtrequest sc Cstig}|D]} || iq~}h}d|jod|dRARCtresponseR_RKRERkRwRP(RR((Rt getresponse@s$       (R;R<RRRRRRRR@RARRLRyRRRPRRRRRRRRR(((RR8s*      b  t SharedSocketcBs,tZdZdZdZdZRS(NcCs||_d|_dS(Ni(R>Rt_refcnt(RR>((RRLs cCs|id7_dS(Ni(RR(R((RtincrefscCsK|id8_|idjpt|idjo|iindS(Nii(RRRkR>RP(R((RtdecrefscCs|iidS(N(RR>RP(R((Rt__del__s(R;R<RLRRR(((RR}s   tSharedSocketClientcBstZdZdZRS(NcCs/d|_||_|ii|i|_dS(Ni(Rt_closedtsharedt_sharedRR>t_sock(RR((RRLs   cCs1|ip#|iid|_d|_ndS(Ni(RRRRR(R((RRPs   (R;R<RLRP(((RRs tSSLFilecBsetZdZdZedZdZedZdZddZ dZ d Z d Z RS( s(File-like object wrapping an SSL socket.i cCs<ti||||_d|_|p |ii |_ dS(NR!( RRLRR>tsslt_sslt_buftbufsizet __class__tBUFSIZEt_bufsize(RR>RR((RRLs  cCsd}xtoy|ii|i}Wntij oh}|dti jp|dti joq n|dti jp|dti joPnq ti j o@}|dtijoq n|dtijoPnq XPq W|S(NR!i(tbufR.RRRgRRtsslerrorterrtSSL_ERROR_WANT_READtSSL_ERROR_WANT_WRITEtSSL_ERROR_ZERO_RETURNt SSL_ERROR_EOFRterrnotEINTRtEBADF(RRR((Rt_reads&((cCs|ig}t|i}xZ|djp ||jo?|i}|djoPn|i ||t|7}qWdi |}|djod|_|Sn|||_|| SdS(NR!( RRtLR8tavailtsizeRRRhR3Rtall(RRRRRRh((RRgs       cCs|ig}d|_xU|did}|djoPn|i}|djoPn|i|qW|djodi|Sn>di|}|idd}|| }|||_|SdS(NR!iis i( RRRRlRmRRhR3RRR1(RRRmRRhR1((RR0s$        icCsmd}g}xZtoR|i}|pPn|i||t|7}|o||joPqqW|S(Ni( ttotaltlistR.RR0R1R3R8tsizehint(RRRR1R((Rt readliness   cCs |iiS(N(RRtfileno(R((RRscCs|S(N(R(R((Rt__iter__scCs!|i}|p tn|S(N(RR0R1t StopIteration(RR1((Rtnexts  ( R;R<t__doc__RRRLRRgR0RRRR(((RRs       t FakeSocketcBsctZdfdYZdZdZddZddZeZddd Z d Z RS( Nt _closedsocketcBstZdZRS(NcCstdddS(Ni sBad file descriptor(R(RRq((Rt __getattr__s(R;R<R(((RRscCs)t|}ti||||_dS(N(RR>RRLRRR(RR>R((RRLs cCs#ti||ii|_dS(N(RRPRRRR(R((RRP s cCs=|djo|djo tnt|i|i|S(NtrR=(tmodeRRRRRR(RRR((RR?s icCs|ii|S(N(RRtwritetstuff(RRtflags((RRsicCs|ii|S(N(RRRgR8(RR8R((RtrecvscCst|i|S(N(tgetattrRRtattr(RR((RRs( R;R<RRLRPRR?RRRR(((RRs    cBs2tZdZeZeeeedZdZRS(s(This class allows communication via SSL.cCs,ti||||||_||_dS(N(RRLRRzR{RAtkey_filet cert_file(RRzR{RRRA((RRL&s cCsbtititi}|i|i|ifti||i |i }t |||_dS(s(Connect to a host on a given (SSL) port.N( RtAF_INETRR>RRRzR{RRRR(RRR>((RR,s (R;R<Rt HTTPS_PORTRRRLR(((RR!s cBsttZdZdZdZdZeZdeedZ dZ eedZ dZ d Z d Zd ZRS( s-Compatibility class with httplib.py from 1.5.i sHTTP/1.0iR!cCs7|djo d}n|i|i|||dS(s:Provide a default host, since the superclass requires one.iN(R{RRt_setupt_connection_classRzRA(RRzR{RA((RRL?s  cCs^||_|i|_|i|_|i|_|i|_|i|_|i|_d|_ dS(N( RaRt_connRRRRRRRtfile(RRa((RRKs       cCs5|dj o|ii||n|iidS(sDAccept arguments to set the host/port, since the superclass doesn't.N(RzRRRRyR{R(RRzR{((RRYs cCs|iS(sCProvide a getfile, since the superclass' does not use this concept.N(RR(R((Rtgetfile`scGs |ii|di|dS(s.The superclass allows only one value argument.s N(RRRRRtvalues(RRR((RRdscCsy|ii}WnStj oG}|iiidd|_|i d|_ d|i dfSnX|i |_ |i|_|i|i|i fS(sCompat definition since superclass does not define it. Returns a tuple consisting of: - server status code (e.g. '200' if all goes well) - server "reason" corresponding to status code - any RFC822 headers in the response from the server R=iiN(RRRRRteR>R?RRPRR$R1RDR,R&RG(RRR((Rtgetreplyhs    cCs|iid|_dS(N(RRRPRR(R((RRPs (R;R<RRRR@RRRRLRRRRRRP(((RR5s     RtHTTPScBs,tZdZeZdeeeedZRS(sCompatibility with 1.5 httplib interface Python 1.5.2 did not have an HTTPS class, but it defined an interface for sending http requests that is also useful for https. R!cCsO|djo d}n|i|i|||||||_||_dS(Ni( R{RRRRRzRRRA(RRzR{RRRA((RRLs     (R;R<RRRRRL(((RRs cBstZRS(N(R;R<(((RRscBstZRS(N(R;R<(((RRscBstZRS(N(R;R<(((RR scBstZdZRS(NcCs|f|_||_dS(N(RFRtargs(RRF((RRLs (R;R<RL(((RRscBstZRS(N(R;R<(((RRscBstZRS(N(R;R<(((RRscBstZdZRS(NcCs|f|_||_dS(N(tpartialRR(RR((RRLs (R;R<RL(((RR scBstZRS(N(R;R<(((RR scBstZRS(N(R;R<(((RR scBstZRS(N(R;R<(((RR scBstZRS(N(R;R<(((RRscBstZdZRS(NcCs|f|_||_dS(N(R1RR(RR1((RRLs (R;R<RL(((RRsRQcBsJtZdZdZdZdZedZdZedZ RS(s2A limited file-like object for HTTP/0.9 responses.cCs7||_||_d|_d|_t||_dS(Ni( R1Rt_lineRt_filet_line_consumedt _line_offsetR8t _line_left(RR1R((RRLs     cCst|i|S(N(RRRR(RR((RRscCs:d|_|ii|_|ii|_|ii|_dS(Ni(RRRRgR0R(R((Rt_dones cCs"|io|ii|Sn|ipt|djp||ijo^|i|i }|i |djo||iiSq||ii|t |Snu||ijpt|i }||}|i||!}||_ |i|8_|idjo|i n|SdS(Ni(RRRRgRfRRkRRRRhRR8RmR(RRfRmRRh((RRgs$   "   cCsJ|io|iiSn|ipt|i|i}|i |S(N( RRRR0RRkRRRhR(RRh((RR0 s   cCs|io|ii|Sn|ipt|i|ig}|i |djo||iiSn||ii|SdS(N( RRRRRRRkRRRRR(RRR((RRs   ( R;R<RRLRRRRgR0R(((RRQs     cCsdk} dk}|i| idd\}}d}x/|D]'\}}|djo|d}q>q>Wd} d}|do|d} n|do|d}nt }|i ||i | |id||i|i\}}} d G|GHd G|GHd Gt|iiGHH| o#x | iD]}|iGHq8WnHd t fd Y} | d}|idd|i|i|ittdoxdD]\} }d| |fGHt} | i || i | | id|| i| i\}}} d G|GHd G|GHd Gt| iiGHH| o#x | iD]}|iGHqeWnHqWndS(sTest this module. A hodge podge of tests collected here, because they have too many external dependencies for the regular test suite. Nitdis-dswww.python.orgRtGETsstatus =sreason =RgtHTTP11cBstZdZdZRS(Ni sHTTP/1.1(R;R<RR(((RR<sshttp://www.python.org/~jeremy/Rssourceforge.nets/projects/pythons https://%s%s(ssourceforge.nets/projects/python((ssourceforge.nets/projects/python(tsystgetopttargvtoptsRtdltotaRztselectorRthRRRRRR&RGR$R8RRgRR4RRPR+RRths(R R&RRR RRGR R RR$RzRR RR((Rttestsn                         t__main__(_RRt mimetoolsRturlparseRt cStringIORWt ImportErrort__all__RRRERwRRRUtSWITCHING_PROTOCOLSt PROCESSINGtOKtCREATEDtACCEPTEDtNON_AUTHORITATIVE_INFORMATIONR]t RESET_CONTENTtPARTIAL_CONTENTt MULTI_STATUStIM_USEDtMULTIPLE_CHOICEStMOVED_PERMANENTLYtFOUNDt SEE_OTHERR^t USE_PROXYtTEMPORARY_REDIRECTt BAD_REQUESTt UNAUTHORIZEDtPAYMENT_REQUIREDt FORBIDDENt NOT_FOUNDtMETHOD_NOT_ALLOWEDtNOT_ACCEPTABLEtPROXY_AUTHENTICATION_REQUIREDtREQUEST_TIMEOUTtCONFLICTtGONEtLENGTH_REQUIREDtPRECONDITION_FAILEDtREQUEST_ENTITY_TOO_LARGEtREQUEST_URI_TOO_LONGtUNSUPPORTED_MEDIA_TYPEtREQUESTED_RANGE_NOT_SATISFIABLEtEXPECTATION_FAILEDtUNPROCESSABLE_ENTITYtLOCKEDtFAILED_DEPENDENCYtUPGRADE_REQUIREDtINTERNAL_SERVER_ERRORtNOT_IMPLEMENTEDt BAD_GATEWAYtSERVICE_UNAVAILABLEtGATEWAY_TIMEOUTtHTTP_VERSION_NOT_SUPPORTEDtINSUFFICIENT_STORAGEt NOT_EXTENDEDRotMessageRRRRRRRRRR+Rt ExceptionRRR RRRR R R R RRRRQRR;(WRRRERRR R>RRRR1RR+RRRRRRR<R RR=R0RRR6R#RR(RR%RRwRoR^R'RR4R3R8RRWR*R]RURRR2R/RRQR-RR@RRRRR RR:RR R5RRRARR R7R?R9RR.RR$R;RR RR!R)RBR&R"R,((Rt?Cs    9i1Fd YD =