a DOg@sNdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z gdZ dZdZdZdZd Zd Zeejjd d ejjDZd ZdZdZedjZedj Z!edZ"edZ#hdZ$d@ddZ%Gdddej&j'Z(ddZ)e(fddZ*Gdddej+Z,Gd d!d!Z-z ddl.Z.Wne/yHYn0Gd"d#d#e-Z0e 1d#Gd$d%d%e2Z3Gd&d'd'e3Z4Gd(d)d)e3Z5Gd*d+d+e3Z6Gd,d-d-e3Z7Gd.d/d/e3Z8Gd0d1d1e3Z9Gd2d3d3e3Z:Gd4d5d5e:Z;Gd6d7d7e:ZGdd?d?e@e>ZAe3ZBdS)Aa 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 |\_____________________________ | | 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 responsesPiZUNKNOWNZIdlezRequest-startedz Request-sentcCsi|] }||jqS)phrase).0vrr#/usr/lib64/python3.9/http/client.py kriids[^:\s][^:\r\n]*s\n(?![ \t])|\r(?![ \t\n])z[- ]z[-]>ZPOSTPUTZPATCHdatac Csjz |dWStyd}z@t|j|j|j|jd|||j|j|fdWYd}~n d}~00dS)zd?Z Z!S)FrrNcCsR|d|_||_||_d|_|_t|_t|_t|_ t|_ t|_ t|_ t|_ dS)Nrb)makefilerA debuglevel_methodrBmsg_UNKNOWNversionstatusreasonchunked chunk_leftlength will_close)r3sockrMmethodurlrrr__init__s  zHTTPResponse.__init__c Cst|jtdd}t|tkr*td|jdkrBtdt||sNt dz| dd\}}}WnBt yz| dd\}}d}Wnt yd}Yn0Yn0| d s| t|z$t|}|d ks|d krt|Wnt yt|Yn0|||fS) Nr-rDz status linerzreply:z-Remote end closed connection without responsezHTTP/ri)strrAr>r?r/rrMprintreprrsplit ValueError startswith _close_connrint)r3r5rQrRrSrrr _read_statuss2      zHTTPResponse._read_statusc Cs|jdurdS|\}}}|tkr&qHt|j}|jdkrDtd|~q||_|_| |_ |dvrnd|_ n| drd|_ nt |t|j|_|_|jdkr|jD]\}}td|d|q|jd }|r|d krd |_d|_nd |_||_d|_|jd }|r`|js`zt||_WntyJd|_Yn0|jdkrfd|_nd|_|tks|tksd|krdksn|jdkrd|_|js|js|jdurd |_dS)Nrzheaders:)zHTTP/1.0zHTTP/0.9 zHTTP/1. header:r,transfer-encodingrTTFcontent-lengthrHEAD)rBrfZCONTINUErCrArMr_coderRstriprSrQrcrrJrOitemsgetr.rTrU _check_closerWrVrerbZ NO_CONTENTZ NOT_MODIFIEDrN) r3rQrRrSZskipped_headershdrvalZtr_encrVrrrbegin9sd               zHTTPResponse.begincCsv|jd}|jdkr.|r*d|vr*dSdS|jdr>dS|rRd|vrRdS|jd}|rrd|vrrdSdS)N connectionrhcloseTFz keep-alivezproxy-connection)rBrqrQr.)r3connZpconnrrrrrs    zHTTPResponse._check_closecCs|j}d|_|dSN)rArw)r3rArrrrdszHTTPResponse._close_conncs2ztW|jr.|n|jr,|0dSry)superrwrArdr3 __class__rrrws   zHTTPResponse.closecst|jr|jdSry)rzflushrAr{r|rrr~s zHTTPResponse.flushcCsdS)zAlways returns TrueTrr{rrrreadableszHTTPResponse.readablecCs |jduS)z!True if the connection is closed.N)rAr{rrrisclosedszHTTPResponse.isclosedcCs|jdurdS|jdkr$|dS|durRt|}||}t|d|S|jr`|S|j durv|j }n4z| |j }Wnt y|Yn0d|_ ||SdS)Nrrmr) rArNrd bytearrayreadinto memoryviewtobytesrT_readall_chunkedrVread _safe_readr )r3amtbr4srrrrs*      zHTTPResponse.readcCs|jdurdS|jdkr$|dS|jr4||S|jdur^t||jkr^t|d|j}|j|}|s||r||n&|jdur|j|8_|js||S)z^Read up to len(b) bytes into bytearray b and return the number of bytes read. Nrrm) rArNrdrT_readinto_chunkedrVr/rr)r3rr4rrrrs$       zHTTPResponse.readintocCsp|jtd}t|tkr$td|d}|dkrB|d|}z t|dWStyj|Yn0dS)Nr-z chunk size;r) rAr>r?r/rfindrerbrd)r3r5irrr_read_next_chunk_sizes     z"HTTPResponse._read_next_chunk_sizecCs:|jtd}t|tkr$td|s*q6|dvrq6qdS)Nr-z trailer liner;)rAr>r?r/rr3r5rrr_read_and_discard_trailers z&HTTPResponse._read_and_discard_trailercCsj|j}|sf|dur|dz |}WntyBtdYn0|dkr`||d}||_|S)Nr\rr)rUrrrbr rrd)r3rUrrr_get_chunk_left$s   zHTTPResponse._get_chunk_leftcCsn|jtksJg}z6|}|dur&q>|||d|_qd|WStyhtd|Yn0dSNrr)rTrPrr2rrUrFr )r3valuerUrrrr<s  zHTTPResponse._readall_chunkedcCs|jtksJd}t|}zv|}|dur2|WSt||kr\||}|||_||WS|d|}||}||d}||7}d|_qWn&tytt|d|Yn0dS)Nr) rTrPrrr/_safe_readintorUr bytes)r3r total_bytesmvbrUr4temp_mvbrrrrJs$         zHTTPResponse._readinto_chunkedcCsTg}|dkrJ|jt|t}|s2td|||||t|8}qd|S)aVRead 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. rr)rArmin MAXAMOUNTr rFr2r/)r3rrchunkrrrrbs zHTTPResponse._safe_readcCsd}t|}|t|kr~tt|kr>|dt}|j|}n |j|}|shtt|d|t|||d}||7}q |S)z2Same as _safe_read, but for reading into a buffer.rN)rr/rrArr r)r3rrrrr4rrrrys      zHTTPResponse._safe_readintocCs|jdus|jdkrdS|jr(||S|jdurJ|dksD||jkrJ|j}|j|}|sh|rh|n|jdur|jt|8_|S)zvRead with at most one underlying system call. If at least one byte is buffered, return that instead. Nrmrr)rArNrT_read1_chunkedrVread1rdr/)r3r4resultrrrrs    zHTTPResponse.read1cCs4|jdus|jdkrdS|jr(||S|j|S)Nrmr)rArNrT _peek_chunkedpeek)r3r4rrrrs  zHTTPResponse.peekcs|jdus|jdkrdS|jr*t|S|jdurL|dksF||jkrL|j}|j|}|sj|rj|n|jdur|jt|8_|S)Nrmrr)rArNrTrzr>rVrdr/)r3limitrr|rrr>s    zHTTPResponse.readlinecCsd|}|dus|dkrdSd|kr0|ks6n|}|j|}|jt|8_|s`td|Sr)rrArrUr/r )r3r4rUrrrrrs zHTTPResponse._read1_chunkedcCsBz |}Wnty YdS0|dur.dS|j|d|S)Nr)rr rAr)r3r4rUrrrrs  zHTTPResponse._peek_chunkedcCs |jSry)rAfilenor{rrrrszHTTPResponse.filenocCsF|jdurt|j|p|}t|ts4t|ds8|Sd|SdS)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, )rBrZget_all isinstancer^hasattrrF)r3r(defaultrBrrr getheaders zHTTPResponse.getheadercCs|jdurtt|jS)z&Return list of (header, value) tuples.N)rBrlistrpr{rrr getheaderss zHTTPResponse.getheaderscCs|Sryrr{rrrrszHTTPResponse.__iter__cCs|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. )rBr{rrrinfoszHTTPResponse.infocCs|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. )rZr{rrrgeturls zHTTPResponse.geturlcCs|jS)zuReturn the HTTP status code that was sent with the response, or None if the URL is not an HTTP URL. )rRr{rrrgetcodeszHTTPResponse.getcode)rNN)N)r)r)r)N)"r7r8r9r[rfrurrrdrwr~rrrrrrrrrrrrrr>rrrrrrrrr __classcell__rrr|rrs< !H  "     rc@s eZdZdZdZeZeZdZ dZ e ddZ e ddZ d ejd d fd d Zd7d dZddZddZddZddZddZddZddZddZd8d d!Zd9d"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Z d:dd.d/d0Z!d ifdd.d1d2Z"d3d4Z#d5d6Z$d S);rrhzHTTP/1.1r-rcCs t|tjS)zFTest whether a file-like object is a text or a binary stream. )rio TextIOBase)streamrrr _is_textIO%szHTTPConnection._is_textIOcCsd|dur|tvrdSdSt|dr*dSzt|}|jWStyLYn0t|tr`t|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 TypeErrorrr^r/)bodyrYmvrrr_get_content_length+s    z"HTTPConnection._get_content_lengthN cCsn||_||_||_d|_g|_d|_t|_d|_d|_ d|_ i|_ | ||\|_ |_||j tj|_dSry)timeoutsource_address blocksizerX_buffer_HTTPConnection__response_CS_IDLE_HTTPConnection__staterN _tunnel_host _tunnel_port_tunnel_headers _get_hostporthostport_validate_hostsocketcreate_connection_create_connection)r3rrrrrrrrr[Ks zHTTPConnection.__init__cCs<|jrtd|||\|_|_|r.||_n |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)rX RuntimeErrorrrrrclear)r3rrrBrrr set_tunnelas zHTTPConnection.set_tunnelcCs|dur|d}|d}||krzt||dd}WnFty||dddkrf|j}ntd||ddYn0|d|}n|j}|r|ddkr|ddkr|dd}||fS) Nr,]r-r]znonnumeric port: '%s'r[r)rfindrerb default_portr )r3rrrjrrrrys    zHTTPConnection._get_hostportcCs ||_dSry)rM)r3levelrrrset_debuglevelszHTTPConnection.set_debuglevelc Cs d|jd|jf}|g}|jD]$\}}||d|ddq&|d|d|~|j|j |j d}| \}}}|t j jkr|td |d ||jtd } t| tkrtd | sq| d vrq|jdkrtd| qdS)NsCONNECT %s:%d HTTP/1.0 asciiz:  r r<rrYzTunnel connection failed:  r-r:r;rri)rr!rrrpr2sendrFresponse_classrXrNrfhttp HTTPStatusZOKrwOSErrorrorAr>r?r/rrMr_rG) r3connectrBheaderrresponserQrnmessager5rrr_tunnels.    zHTTPConnection._tunnelc Csz||j|jf|j|j|_z|jtjtj dWn2t yf}z|j t j krRWYd}~n d}~00|j rv|dS)z3Connect to the host and port specified in __init__.r-N)rrrrrrX setsockoptr IPPROTO_TCP TCP_NODELAYrerrnoZ ENOPROTOOPTrrr3errrrs zHTTPConnection.connectcCsZt|_z4|j}|r d|_|W|j}|rVd|_|n|j}|rTd|_|0dS)z(Close the connection to the HTTP server.N)rrrXrwr)r3rXrrrrrws  zHTTPConnection.closecCs|jdur |jr|nt|jdkr8tdt|t|dr|jdkrTtd||}|rt|jdkrttd| |j }|sq|r| d}|j |qtdSz|j |WnHt yt|tjjr|D]}|j |qnt dt|Yn0dS) 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:rsendIng a read()ableencoding file using iso-8859-1rDz9data should be a bytes-like object or an iterable, got %r)rX auto_openrrrMr_r`rrrrr!sendallrr collectionsabcIterabletype)r3rr! datablockdrrrrs8         zHTTPConnection.sendcCs|j|dS)zuAdd a line of output to the current request buffer. Assumes that the line does *not* end with \r\n. N)rr2)r3rrrr_outputszHTTPConnection._outputccs^|jdkrtd||}|r2|jdkr2td||j}|sDqZ|rR|d}|Vq2dS)NrrrrD)rMr_rrrr!)r3rr!rrrr_read_readables    zHTTPConnection._read_readableFc Cs|jdd|j}|jdd=|||durt|drN||}nVz t|WnBtyz t|}Wn"tytdt |Yn0Yn0|f}|D]R}|s|j dkrt dq|r|j dkrt |d d d |d}||q|r|j dkr|d dS) 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. )rrr<NrzAmessage_body should be a bytes-like object or an iterable, got %rrzZero length chunk ignoredrhXrrs0 )rextendrFrrrrriterrrMr_ _http_vsnr/r!)r3 message_bodyencode_chunkedrOchunksrrrr _send_outputs:             zHTTPConnection._send_outputc Cs|jr|jrd|_|jtkr(t|_n t|j||||_|pHd}||d|||j f}| | ||j dkr|sd}| drt|\}}}}}|rz|d}Wnty|d}Yn0|d |n|jr|j} |j} n |j} |j} z| d} Wnty0| d} Yn0| d d krNd | d } | |jkrh|d | n| d} |d d| | f|s|ddndS)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/z%s %s %srhr]rrZidnaZHostr,r[]z%s:%szAccept-Encodingidentity)rrrr_CS_REQ_STARTEDr _validate_methodrN_validate_path _http_vsn_strr_encode_requestrrcrr!r" putheaderrrrrrrrG) r3rYrZ skip_hostskip_accept_encodingrequestnetlocnilZ netloc_encrrZhost_encrrr putrequest=sP             zHTTPConnection.putrequestcCs |dS)Nr)r!)r3r rrrrszHTTPConnection._encode_requestcCs,t|}|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_researchrbgroup)r3rYmatchrrrrs zHTTPConnection._validate_methodcCs,t|}|r(td|d|ddS)zValidate a url for putrequest.&URL can't contain control characters. rrN!_contains_disallowed_url_pchar_rerr r)r3rZrrrrrs   zHTTPConnection._validate_pathcCs,t|}|r(td|d|ddS)z9Validate a host so it doesn't contain control characters.rrrNr)r3rrrrrrs   zHTTPConnection._validate_hostcGs|jtkrtt|dr$|d}t|s:td|ft|}t|D]\\}}t|drl|d||<nt |t rt |d||<t ||rJtd||fqJd |}|d|}||dS) zkSend a request header line to the server. For example: h.putheader('Accept', 'text/html') r!rzInvalid header name %rr zInvalid header value %rs s: N)rrrrr!_is_legal_header_namerbr enumeraterrer^_is_illegal_header_valuerFr)r3rvaluesrZ one_valuerrrrr s"        zHTTPConnection.putheaderrcCs*|jtkrt|_nt|j||ddS)zIndicate 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. rN)rr _CS_REQ_SENTrr)r3rrrrr endheaderss zHTTPConnection.endheaderscCs||||||dS)z&Send a complete request to the server.N) _send_request)r3rYrZrrBrrrrr szHTTPConnection.requestc Cstdd|D}i}d|vr&d|d<d|vr6d|d<|j||fi|d|vrd |vrd }|||}|dur|dur|jd krtd |d }|ddq|dt|nd }|D]\} } || | qt|trt |d}|j ||ddS)Ncss|]}|VqdSry)r.)rkrrr rz/HTTPConnection._send_request..rr-r zaccept-encodingr rkrjFrzUnable to determine size of %rTzTransfer-EncodingrTzContent-Lengthrr) frozensetrrrMr_r r^rprr*r) r3rYrZrrBrZ header_namesZskipsZcontent_lengthrsrrrrr s0      zHTTPConnection._send_requestcCs|jr|jrd|_|jtks&|jr0t|j|jdkrR|j|j|j|jd}n|j|j|jd}zZz | Wnt y| Yn0|j t ksJt|_|j r| n||_|WS| Yn0dS)a)Get the response from the server. If the HTTPConnection is in the correct state, returns an instance of HTTPResponse or of whatever object is returned by the response_class variable. If a request has not been sent or if a previous response has not be handled, ResponseNotReady is raised. If the HTTP response indicates that the connection should be closed, then it will be closed before the response is returned. When the connection is closed, the underlying socket is closed. Nrr)rrrrrrMrrXrNruConnectionErrorrwrWrPr)r3rrrr getresponse5s0      zHTTPConnection.getresponse)NN)NF)FF)N)%r7r8r9rrrr HTTP_PORTrrrM staticmethodrrr_GLOBAL_DEFAULT_TIMEOUTr[rrrrrrwrrrrrrrrrr rr r r%rrrrrsJ     & 6    .rcsHeZdZdZeZdddejdfddddfdd ZfddZ Z S) HTTPSConnectionz(This class allows communication via SSL.Nr)contextcheck_hostnamerc stt|j||||| d|dus2|dus2|durHddl} | dtd||_||_|durtt }|j durtd|_ |j tj k} |dur|j }|r| std|s|r||||j durd|_ ||_|dur||j_ dS)N)rrzTkey_file, cert_file and check_hostname are deprecated, use a custom context instead.r\TzMcheck_hostname needs a SSL context with either CERT_OPTIONAL or CERT_REQUIRED)rzr)r[warningswarnDeprecationWarningkey_file cert_filesslZ_create_default_https_contextZpost_handshake_authZ verify_modeZ CERT_NONEr+rbZload_cert_chain_context) r3rrr/r0rrr*r+rr,Z will_verifyr|rrr[s:    zHTTPSConnection.__init__cs6t|jr|j}n|j}|jj|j|d|_dS)z(Connect to a host on a given (SSL) port.)server_hostnameN)rzrrrr2Z wrap_socketrX)r3r3r|rrrs  zHTTPSConnection.connect) r7r8r9__doc__ HTTPS_PORTrrr(r[rrrrr|rr)ys$r)c@s eZdZdS)rNr7r8r9rrrrrsrc@s eZdZdS)rNr6rrrrrsrc@s eZdZdS)r Nr6rrrrr sr c@seZdZddZdS)rcCs|f|_||_dSry)argsrQ)r3rQrrrr[szUnknownProtocol.__init__Nr7r8r9r[rrrrrsrc@s eZdZdS)rNr6rrrrrsrc@s eZdZdS)r Nr6rrrrr sr c@s$eZdZdddZddZejZdS)r NcCs|f|_||_||_dSry)r7partialexpected)r3r9r:rrrr[szIncompleteRead.__init__cCs2|jdurd|j}nd}d|jjt|j|fS)Nz, %i more expectedr]z%s(%i bytes read%s))r:r}r7r/r9rrrr__repr__s    zIncompleteRead.__repr__)N)r7r8r9r[r;r$__str__rrrrr s r c@s eZdZdS)r Nr6rrrrr sr c@s eZdZdS)r Nr6rrrrr sr c@s eZdZdS)rNr6rrrrrsrc@s eZdZdS)rNr6rrrrrsrc@seZdZddZdS)rcCs|s t|}|f|_||_dSry)r`r7r5rrrrr[szBadStatusLine.__init__Nr8rrrrrsrc@seZdZddZdS)rcCst|dt|fdS)Nz&got more than %d bytes when reading %s)rr[r?)r3Z line_typerrrr[szLineTooLong.__init__Nr8rrrrrsrc@seZdZddZdS)rcOs(t|dtj|g|Ri|dS)Nr])rr[ConnectionResetError)r3poskwrrrr[s zRemoteDisconnected.__init__Nr8rrrrrsr)r)Cr4Z email.parserrHZ email.messagerrrrercollections.abcr urllib.parser__all__r&r5rPrrrglobalsupdater __members__rrrr?r@compile fullmatchrrrrrrr*rZMessager+rCrJBufferedIOBaserrr1 ImportErrorr)r2 Exceptionrrr rrr r r r rrrrr=rrrrrrszF       .] 8