\d^>dZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z gdZdZdZdZdZd Zd Zeejjd ejjDZd Zd ZejdjZejdj Z!ejdZ"ejdZ#hdZ$d;dZ%Gddej&j'Z(dZ)e(fdZ*Gddej+Z,GddZ- ddl.Z.Gdde-Z/e0dn #e1$rYnwxYwGdd e2Z3Gd!d"e3Z4Gd#d$e3Z5Gd%d&e3Z6Gd'd(e3Z7Gd)d*e3Z8Gd+d,e3Z9Gd-d.e3Z:Gd/d0e:Z;Gd1d2e:Z<Gd3d4e:Z=Gd5d6e3Z>Gd7d8e3Z?Gd9d:e@e>ZAe3ZBdS) 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 |\_____________________________ | | getresponse() raises | response = getresponse() | ConnectionError v v Unread-response Idle [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)urlsplit) HTTPResponseHTTPConnection HTTPException NotConnectedUnknownProtocolUnknownTransferEncodingUnimplementedFileModeIncompleteRead InvalidURLImproperConnectionStateCannotSendRequestCannotSendHeaderResponseNotReady BadStatusLine LineTooLongRemoteDisconnectederror responsesPiUNKNOWNIdlezRequest-startedz Request-sentci|] }||j S)phrase).0vs ..\python\lib\http\client.py rls G G GQQ G G Gids[^:\s][^:\r\n]*s\n(?![ \t])|\r(?![ \t\n])z[- ]z[-]>PUTPOSTPATCHdatac  |dS#t$r[}t|j|j|j|j|d||j|jdd|ddd}~wwxYw)z 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. )rMrr8r )r<rr%s rrzHTTPResponse._safe_readpsEw||C   t99s?? s3t99}55 5 r ct|}|j|}||kr(tt |d|||z |S)z2Same as _safe_read, but for reading into a buffer.N)r8rMrr r)r<rrr=s rrzHTTPResponse._safe_readinto|sR!ff G  Q   s77 q!us1u55 5r cR|j |jdkrdS|jr||S|j|dks ||jkr|j}|j|}|s|r|n$|j|xjt|zc_|S)zvRead with at most one underlying system call. If at least one byte is buffered, return that instead. Nrr r)rMr`rf_read1_chunkedrhread1rwr8)r<r=results rrzHTTPResponse.read1s 7?dlf443 < *&&q)) ) ; "AT[ Aq!! '! '        [ $ KK3v;; &KK r c|j |jdkrdS|jr||S|j|S)Nrr )rMr`rf _peek_chunkedpeek)r<r=s rrzHTTPResponse.peeksL 7?dlf443 < )%%a(( (w||Ar cl|j |jdkrdS|jr!t|S|j|dks ||jkr|j}|j|}|s|r|n$|j|xjt|zc_|S)Nrr r)rMr`rfrrJrhrwr8)r<limitrrs rrJzHTTPResponse.readlines 7?dlf443 < +77##E** * ; " UT[5H5HKE!!%(( '% '        [ $ KK3v;; &KK r c|}||dkrdSd|cxkr|ksn|}|j|}|xjt |zc_|st d|S)Nrr )rrMrrgr8r )r<r=rgrs rrzHTTPResponse._read1_chunkeds))++  a3Q$$$$*$$$$Aw}}Q 3t99$ & %% % r c |}n#t$rYdSwxYw|dS|j|d|S)Nr )rr rMr)r<r=rgs rrzHTTPResponse._peek_chunkedsf --//JJ   33   3w||J''  44s  %%c4|jSr)rMfilenors rrzHTTPResponse.filenosw~~r c|jt|j|p|}t|tst |ds|Sd|S)axReturns the value of the header matching *name*. If there are multiple matching headers, the values are combined into a single string separated by commas and spaces. If no matching header is found, returns *default* or None if the *default* is not specified. If the headers are unknown, raises http.client.ResponseNotReady. N__iter__z, )rNrget_all isinstancerqhasattrrS)r<r/defaultrNs r getheaderzHTTPResponse.getheadersl < "$$ $,&&t,,7 gs # # &77J+G+G &N99W%% %r cx|jtt|jS)z&Return list of (header, value) tuples.)rNrlistrrs r getheaderszHTTPResponse.getheaderss2 < "$$ $DL&&(()))r c|Srrrs rrzHTTPResponse.__iter__s r c|jS)ajReturns an instance of the class mimetools.Message containing meta-information associated with the URL. When the method is HTTP, these headers are those returned by the server at the head of the retrieved HTML page (including Content-Length and Content-Type). When the method is FTP, a Content-Length header will be present if (as is now usual) the server passed back a file length in response to the FTP retrieval request. A Content-Type header will be present if the MIME type can be guessed. When the method is local-file, returned headers will include a Date representing the file's last-modified time, a Content-Length giving file size, and a Content-Type containing a guess at the file's type. See also the description of the mimetools module. )rNrs rinfozHTTPResponse.infos *|r c|jS)aZReturn the real URL of the page. In some cases, the HTTP server redirects a client to another URL. The urlopen() function handles this transparently, but in some cases the caller needs to know which URL the client was redirected to. The geturl() method can be used to get at this redirected URL. )rlrs rgeturlzHTTPResponse.geturls xr c|jS)zuReturn the HTTP status code that was sent with the response, or None if the URL is not an HTTP URL. )rdrs rgetcodezHTTPResponse.getcodes {r )rNNr)r)"rBrCrDrmryrrrwrrrrrrrrrrrrrrrrJrrrrrrrrr __classcell__rs@rrrs)####<'''BF#F#F#P: #####  &&&&P   D   0;;;;,:::0   "          5 5 5   &&&&(*** .   r rceZdZdZdZeZeZdZ dZ e dZ e dZ dejddfd Zd d Zd Zd Zd ZdZdZdZdZdZd!dZ d"dZdZdZdZdZdZ d#dddZ!difdddZ"dZ#dZ$dS)$rr|zHTTP/1.1r6rc6t|tjS)zFTest whether a file-like object is a text or a binary stream. )rio TextIOBase)streams r _is_textIOzHTTPConnection._is_textIO s&"-000r c||tvrdSdSt|drdS t|}|jS#t $rYnwxYwt |trt|SdS)aGet the content-length based on the body. If the body is None, we set Content-Length: 0 for methods that expect a body (RFC 7230, Section 3.3.2). We also set the Content-Length for any method if the body is a str or bytes-like object and not a file. Nrr) upper_METHODS_EXPECTING_BODYrrnbytes TypeErrorrrqr8)bodyrkmvs r_get_content_lengthz"HTTPConnection._get_content_length&s <||~~!888qt 4  4 D!!B9     D  dC  t99 tsA AAN cF||_||_||_d|_g|_d|_t |_d|_d|_ d|_ i|_ | ||\|_ |_||j t j|_dSr)timeoutsource_address blocksizerj_buffer_HTTPConnection__response_CS_IDLE_HTTPConnection__stater` _tunnel_host _tunnel_port_tunnel_headers _get_hostporthostport_validate_hostsocketcreate_connection_create_connection)r<rrrrrs rrmzHTTPConnection.__init__Fs ,"      !!%!3!3D$!?!?DI DI&&&#)":r c|jrtd|||\|_|_|r ||_dS|jdS)aDSet 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 a proxy server that relays all communication to the endpoint passed to `set_tunnel`. This done by sending an 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. z.Can't set up tunnel for established connectionN)rj RuntimeErrorrrrrclear)r<rrrNs r set_tunnelzHTTPConnection.set_tunnel\so 9 QOPP P/3/A/A$/M/M,4,  )#*D  & & ( ( ( ( (r c||d}|d}||krm t||dzd}nF#t$r9||dzddkr|j}nt d||dzdzYnwxYw|d|}n|j}|r"|ddkr|ddkr |dd}||fS) Nr5]r6rpznonnumeric port: '%s'r[r)rfindrxru default_portr )r<rrrjs rrzHTTPConnection._get_hostportts < 3A 3A1uuOtAaCDDz??DD!OOOAaCDDzR''#0()@4!:)MNNNO BQBx( "Q348s??AbDzd|sAABBc||_dSr)r_)r<levels rset_debuglevelzHTTPConnection.set_debuglevels r cd|jd|jfz}|g}|jD]3\}}||d|dd4|d|d|~||j |j } | \}}}|tj jkr;|t!d |d | |jt(d z} t+| t(krt-d | sn3| dvrn.|jdkr"t1d| |dS#|wxYw)NsCONNECT %s:%d HTTP/1.0 asciiz:  r'rHr rkzTunnel connection failed:  Tr6rFrGrr})rr(rrrr;sendrSresponse_classrjr`ryhttp HTTPStatusOKrOSErrorrrMrJrKr8rr_rrrT) r<connectrNheaderrresponsercrmessager@s r_tunnelzHTTPConnection._tunnels/   $ $W - -t/@3BB)!17799 G GMFE NNf33333::9EE F F F Fw #((7##$$$ &&ty&FF '/'<'<'>'> $WdGt))) S4SS'--//SSTTT 4{++HqL99t99x''%m444000?Q&&)T[[]]333 4 NN     HNN    s C(GG)ctjd||j|j||j|jf|j|j|_ |jtj tj dn-#t$r }|j tjkrYd}~nd}~wwxYw|jr|dSdS)z3Connect to the host and port specified in __init__.zhttp.client.connectr6N)sysauditrrrrrrj setsockoptr IPPROTO_TCP TCP_NODELAYrerrno ENOPROTOOPTrr r<es rrzHTTPConnection.connects 'ty$)DDD++ Yty !4<1DFF   I !3V5G K K K K   w%+++,++++     LLNNNNN  s0B B0B++B0ct|_ |j}|rd|_||j}|rd|_|dSdS#|j}|rd|_|wwxYw)z(Close the connection to the HTTP server.N)rrrjrr)r<rjrs rrzHTTPConnection.closes  !9D    H !"&      ! !H !"&     !s $A'Bc||j*|jr|nt|jdkrt dt |t|dr|jdkrt d||}|r|jdkrt d | |j }|snH|r| d}tj d |||j|edStj d || |j|dS#t$rat!|t"jjr"|D]}|j|YdStd t)|zwxYw) zSend `data' to the server. ``data`` can be a string object, a bytes object, an array object, a file-like object that supports a .read() method, or an iterable object. Nrzsend:rzsending a readableencoding file using iso-8859-1r6rQzhttp.client.sendz9data should be a bytes-like object or an iterable, got %r)rj auto_openrrr_rrrsrrrrr(r"r#sendallrr collectionsabcIterabletype)r<r%r( datablockds rrzHTTPConnection.sends 9 ~ % "nn$ ?Q   '4:: & & & 4  ""*+++__T**F 8$/A--6777 - IIdn55  ? ) 0 0 > >I ,dI>>> !!),,, - F $dD111 G I  d # # # # # G G G$  899 G))AI%%a(((())) !9;?::!FGGG  Gs4EAF; F;c:|j|dS)zuAdd a line of output to the current request buffer. Assumes that the line does *not* end with \r\n. N)rr;)r<rs r_outputzHTTPConnection._outputs Ar c#K|jdkrtd||}|r|jdkrtd ||j}|sdS|r|d}|V:)Nrzreading a readabler-TrQ)r_rrrrrr()r<rr(r4s r_read_readablezHTTPConnection._read_readables ?Q   & ' ' '**  4do)) 2 3 3 3  dn55I  ;%,,\:: OOO  r Fc|jdd|j}|jdd=|||t |dr||}nb t ||f}nN#t$rA t|}n-#t$r tdt|zwxYwYnwxYw|D]m}|s|j dkrtd|r7|j dkr,t|d d d |zdz}||n|r$|j dkr|d dSdSdSdS) zSend 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 r rHNrzAmessage_body should be a bytes-like object or an iterable, got %rrzZero length chunk ignoredr|Xrrs0 )rextendrSrrr9rriterr3r_rr _http_vsnr8r()r< message_bodyencode_chunkedrachunkschunks r _send_outputzHTTPConnection._send_outputs J'''ll4<(( LO #  #|V,, -,,\::- |,,,+_FF!>>>>!%l!3!3$>>>')H*.|*<*<)=>>>>> ! !**9:::!"dn&:&:"5zz111188AAEI!"E %     ($.B"6"6 ,'''''S $ #N ( ("6"6s*>B CB,+C,*CCCc|jr |jrd|_|jtkr t|_nt |j||||_|pd}|||d|d|j }| | ||j dkrs|sTd}| drt|\}}}}}|rS |d}n%#t $r|d}YnwxYw|d |n|jr|j} |j} n|j} |j} | d} n%#t $r| d} YnwxYw| d d krd | zd z} | |jkr|d | n0| d} |d | d | |s|dddSdSdS)a`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 N/rr|rprridnaHostr5r[]zAccept-Encodingidentity)rrrr_CS_REQ_STARTEDr_validate_methodr`_validate_path _http_vsn_strr7_encode_requestr>rvrr(r) putheaderrrrrrr rT) r<rkrl skip_hostskip_accept_encodingrequestnetlocnil netloc_encrrhost_encs r putrequestzHTTPConnection.putrequest>s ? #t7799 #"DO* <8 # #*DLL#DL11 1 f%%% jS C   &T-?-?@ T))'22333 >R  0 K>>&))?19#.Cc3K;%+]]7%;%; -;;;%+]]6%:%: ;NN6:6666()#0#0#y#y7#';;w#7#7-777#';;v#6#67 yy~~**#'(?T#9t000vx8888#+??7#;#;v(((DD/IJJJ( >0*===== > > Ds$DD;:D;:FF21F2c,|dS)Nr)r()r<rSs rrOzHTTPConnection._encode_requests~~g&&&r ct|}|r(td|d|ddS)z&Validate a method name for putrequest.z)method can't contain control characters.  (found at least )N)$_contains_disallowed_method_pchar_researchrugroup)r<rkmatchs rrLzHTTPConnection._validate_methodsf5;;FCC  ;:::',{{}}:::;; ; ; ;r ct|}|r(td|d|ddS)zValidate a url for putrequest.&URL can't contain control characters. r[r\N!_contains_disallowed_url_pchar_rer^r r_)r<rlr`s rrMzHTTPConnection._validate_pathsr288==  DCcCC05 CCCDD D D Dr ct|}|r(td|d|ddS)z9Validate a host so it doesn't contain control characters.rbr[r\Nrc)r<rr`s rrzHTTPConnection._validate_hostsr288>>  DCdCC05 CCCDD D D Dr c|jtkrtt|dr|d}t |st d|t|}t|D]\}}t|dr|d||<n:t|tr%t|d||<t||rt d||d |}|dz|z}||dS) zkSend a request header line to the server. For example: h.putheader('Accept', 'text/html') r(rzInvalid header name r'zInvalid header value s s: N)rrKrrr(_is_legal_header_namerur enumeraterrxrq_is_illegal_header_valuerSr7)r<rvaluesr one_valuers rrPzHTTPConnection.putheadersU K|]}|VdSr)r7)rks r z/HTTPConnection._send_request.. s* < =D$> CAD$$D;)NN)NF)FFr)%rBrCrDr>rNrr HTTP_PORTr r.r_ staticmethodrrr_GLOBAL_DEFAULT_TIMEOUTrmrrrr rrrr7r9rCrXrOrLrMrrProrSrqr|rr rrrsIM!NLIJ11\1 \>#'0N $;;;;,))))0(   !!!F    ! ! !&G&G&GP   4(4(4(4(l16(-}}}}~''';;;DDDDDD8 Ge G G G G G)-bG$GGGGG ,=,=,=\=====r rcPeZdZdZeZdddejdfddddfd ZfdZ xZ S)HTTPSConnectionz(This class allows communication via SSL.Nr)contextcheck_hostnamerc2tt|||||| ||| ddl} | dt d||_||_|Btj }|j dkr| dg|j d|_ |j tjk} ||j}|r| st!d|s|r$||||j d|_ ||_|||j_dSdS) N)rrzTkey_file, cert_file and check_hostname are deprecated, use a custom context instead.ror|zhttp/1.1TzMcheck_hostname needs a SSL context with either CERT_OPTIONAL or CERT_REQUIRED)rrrmwarningswarnDeprecationWarningkey_file cert_filessl_create_default_https_contextr>set_alpn_protocolspost_handshake_auth verify_mode CERT_NONErruload_cert_chain_context) r<rrrrrrrrrr will_verifyrs rrmzHTTPSConnection.__init__su /4 ( ( 1 1$g2@R''.. |<<<.:26G/!->K%!(!7 Jk J "IJJJ 79 7'' 8<<<.:26G/#DM)/= ,,,*)r ct|jr|j}n|j}|j|j||_dS)z(Connect to a host on a given (SSL) port.)server_hostnameN)rrrrr wrap_socketrj)r<rrs rrzHTTPSConnection.connects` GGOO     ,"&"3"&) 11$)BQ2SSDIIIr ) rBrCrD__doc__ HTTPS_PORTr rrrmrrrs@rrrzs22! '+TT#;$(% >59$(D% >% >% >% >% >% >% >N S S S S S S S S Sr rceZdZdS)rNrBrCrDrr rrrs Dr rceZdZdS)rNrrr rrrDr rceZdZdS)r Nrrr rr r rr r ceZdZdZdS)rc$|f|_||_dSr)argsrc)r<rcs rrmzUnknownProtocol.__init__sH  r NrBrCrDrmrr rrrs#r rceZdZdS)r Nrrr rr r rr r ceZdZdS)r Nrrr rr r rr r c*eZdZddZdZejZdS)r Nc2|f|_||_||_dSr)rpartialexpected)r<rrs rrmzIncompleteRead.__init__sH    r cr|j d|jz}nd}d|jjt|j|fzS)Nz, %i more expectedrpz%s(%i bytes read%s))rrrBr8rr)s r__repr__zIncompleteRead.__repr__sE = $$t}4AAA$(?(+DL(9(91(>> >r r)rBrCrDrmrr+__str__rr rr r s:!!!!>>>nGGGr r ceZdZdS)r Nrrr rr r rr r ceZdZdS)rNrrr rrrrr rceZdZdS)rNrrr rrrrr rceZdZdS)rNrrr rrrrr rceZdZdZdS)rcF|st|}|f|_||_dSr)rsrr@rs rrmzBadStatusLine.__init__s) ::DE  r Nrrr rrrs#r rceZdZdZdS)rcPt|dt|fzdS)Nz&got more than %d bytes when reading %s)rrmrK)r< line_types rrmzLineTooLong.__init__s6t%M(0)'<&= > > > > >r Nrrr rrrs#>>>>>r rceZdZdZdS)rcft|dtj|g|Ri|dS)Nrp)rrmConnectionResetError)r<poskws rrmzRemoteDisconnected.__init__s?tR(((%d7S777B77777r Nrrr rrrs#88888r r)r%)Cr email.parserrU email.messager'rrrerr"collections.abcr0 urllib.parser__all__r}rrbrrKrnglobalsupdater __members__rjrrKrLcompile fullmatchrgr^rirdr]rr1rMessager3rOrZBufferedIOBaserrrrr; ImportError Exceptionrrr rr r r r rrrrrrrrrr rrs!DDL !!!!!!       #  ,--- H G$/"="D"D"F"F G G G   6# #677A%2:&CDDK%/BJ/@$A$A!(2rz-'@'@$322 E E E E%-'<$) @ @ @ @eeeee2$eeeN] ] ] ] ] ] ] ] ~@&JJJ9S9S9S9S9S.9S9S9Sv NN$%%%%}   D @     I        =           m      m        M        ]        m        /        .        .   M>>>>>->>> 88888-}888 s#E EE