[@s@dZddlZddlZddlZddlZddlZddlZddlZddl m Z dddddd d d d d ddddddgZ dZ dZ dZdZdZdZdZdZdZdZdZdZd 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@dIZAdJZBdKZCdLZDdMZEdNZFi-dOd6dPd6dQd6dRd6dSd6dTd 6dUd!6dVd"6dWd#6dXd&6dYd'6dZd(6d[d)6d\d*6d]d+6d^d_6d`d,6dad-6dbd.6dcd/6ddd06ded16dfd26dgd36dhd46did56djd66dkd76dld86dmd96dnd:6dod;6dpd<6dqd=6drd>6dsdC6dtdD6dudE6dvdF6dwdG6dxdH6dydI6dzdJ6d{dK6d|dN6ZGd}ZHd~ZIdZJejKdjLZMejKdjNZOGdddejPjQZReRddZSGdddejTZUGdddZVyddlWZWWneXk rYn$XGdddeVZYe jZdGddde[Z\Gddde\Z]Gdd d e\Z^Gddde\Z_Gdd d e\Z`Gdd d e\ZaGdd d e\ZbGdd d e\ZcGdddecZdGdddecZeGdddecZfGddde\ZgGddde\Zhe\ZidS)a 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 N)urlsplit HTTPResponseHTTPConnection HTTPException NotConnectedUnknownProtocolUnknownTransferEncodingUnimplementedFileModeIncompleteRead InvalidURLImproperConnectionStateCannotSendRequestCannotSendHeaderResponseNotReady BadStatusLineerror responsesPiZUNKNOWNZIdlezRequest-startedz Request-sentdefi,i-i.i/i0i1i3iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiZContinuezSwitching ProtocolsOKZCreatedZAcceptedzNon-Authoritative Informationz No Contentz Reset ContentzPartial ContentzMultiple ChoiceszMoved PermanentlyZFoundz See Otherz Not Modifiedz Use Proxyz(Unused)i2zTemporary Redirectz Bad RequestZ UnauthorizedzPayment RequiredZ Forbiddenz Not FoundzMethod Not AllowedzNot AcceptablezProxy Authentication RequiredzRequest TimeoutZConflictZGonezLength RequiredzPrecondition FailedzRequest Entity Too LargezRequest-URI Too LongzUnsupported Media TypezRequested Range Not SatisfiablezExpectation FailedzPrecondition RequiredzToo Many RequestszRequest Header Fields Too LargezInternal Server ErrorzNot Implementedz Bad GatewayzService UnavailablezGateway TimeoutzHTTP Version Not SupportedzNetwork Authentication Requirediis[^:\s][^:\r\n]*s\n(?![ \t])|\r(?![ \t\n])c@seZdZddZdS) HTTPMessagecCs|jd}t|}g}d}xn|jD]`}|d|j|kr`d}n|ddjsd}n|r5|j|q5q5W|S)aFind all header lines matching a given header name. Look through the list of headers and find all lines matching a given header name (and their continuation lines). A list of the lines is returned, without interpretation. If the header does not occur, an empty list is returned. If the header occurs multiple times, all occurrences are returned. Case is not important in the header name. :rN)lowerlenkeysisspaceappend)selfnamenZlstZhitliner-!/usr/lib/python3.4/http/client.pygetallmatchingheaderss    z!HTTPMessage.getallmatchingheadersN)__name__ __module__ __qualname__r/r-r-r-r.r!s r!cCsg}xz|jtd}t|tkr=tdn|j|t|tkrotdtn|d kr Pq q Wdj|jd}t j j d|j |S) aGParses only RFC2822 headers from a file pointer. email Parser wants to see strings rather than bytes. But a TextIOWrapper around self.rfile would buffer too many bytes from the stream, bytes which we later need to read as bytes. So we read the correct bytes here, as bytes, for email Parser to parse. r#z header linezgot more than %d headers  z iso-8859-1_class)r3r4r5) readline_MAXLINEr% LineTooLongr( _MAXHEADERSrjoindecodeemailparserZParserZparsestr)fpr6headersr,Zhstringr-r-r. parse_headerss   rAcsQeZdZdddddZddZddZd d Zd d Zfd dZfddZ ddZ ddZ dfddZ ddZ ddZddZddZdd Zd!d"Zd#d$Zd%d&Zdd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2ZS)3rrNcCsw|jd|_||_||_d|_|_t|_t|_t|_ t|_ t|_ t|_ t|_ dS)Nrb)Zmakefiler? debuglevel_methodr@msg_UNKNOWNversionstatusreasonchunked chunk_leftlength will_close)r)sockrCmethodurlr-r-r.__init__7s        zHTTPResponse.__init__cCsxt|jjtdd}t|tkr@tdn|jdkretdt|n|szt |ny|j dd\}}}WnOt k ry"|j dd\}}d}Wnt k rd}YnXYnX|j ds|j t |ny7t|}|d ks:|d krIt |nWnt k rjt |YnX|||fS) Nr#z iso-8859-1z status linerzreply:zHTTP/ri)strr?r7r8r%r9rCprintreprrsplit ValueError startswith _close_connint)r)r,rGrHrIr-r-r. _read_statusUs2      zHTTPResponse._read_statusc Cs|jdk rdSx|j\}}}|tkr;Pnxp|jjtd}t|tkrutdn|j}|sPn|j dkr>t d|q>q>WqW||_ |_ |j|_ |dkrd|_n'|jdrd |_n t|t|j|_|_|j dkrbx'|jD]}t d|d d qBWn|jjd }|r|jd krd|_d|_n d|_|j|_d|_|jjd}|jjd }|rE|j rEyt||_Wntk r&d|_YqNX|jdkrNd|_qNn d|_|tks|tksd|ko}dkns|jdkrd|_n|j r|j r|jdkrd|_ndS)Nr#z header linerzheader:HTTP/1.0HTTP/0.9 zHTTP/1. end ztransfer-encodingrJTFzcontent-lengthrrHEAD)r]r^)r@r\CONTINUEr?r7r8r%r9striprCrUcoderHrIrGrYrrArEgetr$rJrK _check_closerMrLr[rX NO_CONTENT NOT_MODIFIEDrD)r)rGrHrIskiphdrZtr_encrLr-r-r.beginusf               zHTTPResponse.begincCs|jjd}|jdkrS|jjd}|rOd|jkrOdSdS|jjdridS|rd|jkrdS|jjd}|rd|jkrdSdS)NZ connectionr`closeTFz keep-alivezproxy-connection)r@rgrGr$)r)ZconnZpconnr-r-r.rhszHTTPResponse._check_closecCs |j}d|_|jdS)N)r?rn)r)r?r-r-r.rZs  zHTTPResponse._close_conncs'tj|jr#|jndS)N)superrnr?rZ)r)) __class__r-r.rns  zHTTPResponse.closecs*tj|jr&|jjndS)N)roflushr?)r))rpr-r.rqs  zHTTPResponse.flushcCsdS)NTr-)r)r-r-r.readableszHTTPResponse.readablecCs |jdkS)z!True if the connection is closed.N)r?)r)r-r-r.isclosedszHTTPResponse.isclosedc s|jdkrdS|jdkr0|jdS|dk rRtt|j|S|jre|jS|jdkr|jj}nAy|j |j}Wnt k r|jYnXd|_|j|SdS)Nr5rcr) r?rDrZrorreadrJ_readall_chunkedrL _safe_readr )r)amts)rpr-r.rts&        zHTTPResponse.readcCs|jdkrdS|jdkr0|jdS|jrF|j|S|jdk rt||jkrt|d|j}qn|jj|}| r|r|jn7|jdk r|j|8_|js|jqn|S)Nrrc) r?rDrZrJ_readinto_chunkedrLr% memoryviewreadinto)r)br+r-r-r.r{%s$      zHTTPResponse.readintoc Cs|jjtd}t|tkr7tdn|jd}|dkre|d|}nyt|dSWntk r|jYnXdS)Nr#z chunk size;r) r?r7r8r%r9findr[rXrZ)r)r,ir-r-r._read_next_chunk_sizeCs   z"HTTPResponse._read_next_chunk_sizecCs\xU|jjtd}t|tkr:tdn|sDPn|dkrPqqWdS)Nr#z trailer line  r5)rrr5)r?r7r8r%r9)r)r,r-r-r._read_and_discard_trailerSs z&HTTPResponse._read_and_discard_trailerc Cs|jtkst|j}g}x|dkry |j}|dkrRPnWqtk r|tdj|YqXn|j|j ||j dd}q'W|j |j dj|S)Nrr5rR) rJrFAssertionErrorrKrrXr r;r(rvrrZ)r)rKvaluer-r-r.ruas"         zHTTPResponse._readall_chunkedc Csp|jtkst|j}d}t|}x%|dkry |j}|dkr^PnWqtk rtt|d|YqXnt ||kr|j |}|||_||St ||kr|j |}|j dd|_||S|d|}|j |}||d}||7}|j dd}q3W|j |j |S)NrrR)rJrFrrKrzrrXr bytesr%_safe_readintorvrrZ)r)r|rK total_bytesmvbr+temp_mvbr-r-r.ryzs<      $        zHTTPResponse._readinto_chunkedcCsg}xi|dkrq|jjt|t}|sQtdj||n|j||t|8}q Wdj|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. rr5)r?rtmin MAXAMOUNTr r;r(r%)r)rwrxchunkr-r-r.rvs zHTTPResponse._safe_readcCsd}t|}x|t|krtt|kr^|dt}|jj|}n|jj|}|stt|d|t|n||d}||7}qW|S)z2Same as _safe_read, but for reading into a buffer.rN)rzr%rr?r{r r)r)r|rrrr+r-r-r.rs (zHTTPResponse._safe_readintocCs |jjS)N)r?fileno)r)r-r-r.rszHTTPResponse.filenocCsg|jdkrtn|jj|p0|}t|tsRt|d rV|Sdj|SdS)N__iter__z, )r@rZget_all isinstancerThasattrr;)r)r*defaultr@r-r-r. getheaders  zHTTPResponse.getheadercCs.|jdkrtnt|jjS)z&Return list of (header, value) tuples.N)r@rlistitems)r)r-r-r. getheaderss zHTTPResponse.getheaderscCs|S)Nr-)r)r-r-r.rszHTTPResponse.__iter__cCs|jS)N)r@)r)r-r-r.infoszHTTPResponse.infocCs|jS)N)rP)r)r-r-r.geturlszHTTPResponse.geturlcCs|jS)N)rH)r)r-r-r.getcodeszHTTPResponse.getcode)r0r1r2rQr\rmrhrZrnrqrrrsrtr{rrruryrvrrrrrrrrr-r-)rpr.r.s0  P          )       c@s*eZdZdZdZeZeZdZ dZ dZ de j dddZddd d Zd d Zd dZddZddZddZddZddZdddZddddZddZddd Zdid!d"Zd#d$Zd%d&Zd'd(ZdS))rr`zHTTP/1.1r#ri@NcCs||_||_d|_g|_d|_t|_d|_d|_d|_ i|_ |j ||\|_ |_ tj|_dS)N)timeoutsource_addressrN_buffer_HTTPConnection__response_CS_IDLE_HTTPConnection__staterD _tunnel_host _tunnel_port_tunnel_headers _get_hostporthostportsocketZcreate_connection_create_connection)r)rrrrr-r-r.rQs          zHTTPConnection.__init__cCsY|jrtdn|j||\|_|_|rH||_n |jjdS)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 HTML 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)rN RuntimeErrorrrrrclear)r)rrr@r-r-r. set_tunnels   zHTTPConnection.set_tunnelc Cs|dkr|jd}|jd}||kryt||dd}WnVtk r||dddkr|j}ntd||ddYnX|d|}n |j}|r|ddkr|ddkr|dd }qn||fS) Nr"]r#rSznonnumeric port: '%s'r[r)rfindr[rX default_portr )r)rrrjr-r-r.r)s    # &zHTTPConnection._get_hostportcCs ||_dS)N)rC)r)levelr-r-r.set_debuglevel=szHTTPConnection.set_debuglevelc CsLd|j|jf}|jd}|j|xI|jjD]8\}}d||f}|jd}|j|qBW|jd|j|jd|j}|j \}} } | dkr|j t d| | j fnxU|j jtd } t| tkr*td n| s4Pn| d krPqqWdS)NzCONNECT %s:%d HTTP/1.0 asciiz%s: %s zlatin-1 rOrzTunnel connection failed: %d %sr#z header line r5)rrr5)rrencodesendrrresponse_classrNrDr\rnOSErrorrer?r7r8r%r9) r)Z connect_strZ connect_bytesheaderrZ header_strZ header_bytesresponserGrfmessager,r-r-r._tunnel@s.        zHTTPConnection._tunnelcCsD|j|j|jf|j|j|_|jr@|jndS)z3Connect to the host and port specified in __init__.N)rrrrrrNrr)r)r-r-r.connect\s zHTTPConnection.connectcCsQ|jr"|jjd|_n|jrD|jjd|_nt|_dS)z(Close the connection to the HTTP server.N)rNrnrrr)r)r-r-r.rnds      zHTTPConnection.closecCs|jdkr1|jr%|jq1tn|jdkrVtdt|nd}t|dr,|jdkrtdnd}y |j}Wnt k rYn2Xd|krd }|jdkrtd qnxE|j |}|sPn|r|j d }n|jj |qWdSy|jj |Wn^t k rt|tjrx7|D]}|jj |qiWnt d t|YnXdS) 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:i rtzsendIng a read()ableFr|Tzencoding file using iso-8859-1z iso-8859-1z9data should be a bytes-like object or an iterable, got %r)rN auto_openrrrCrUrVrmodeAttributeErrorrtrZsendall TypeErrorr collectionsIterabletype)r)dataZ blocksizerrZ datablockdr-r-r.rnsF         zHTTPConnection.sendcCs|jj|dS)zuAdd a line of output to the current request buffer. Assumes that the line does *not* end with \r\n. N)rr()r)rxr-r-r._outputszHTTPConnection._outputcCs|jjddj|j}|jdd=t|trit||jkri||7}d}n|j||dk r|j|ndS)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. r5s N)r5r5)rextendr;rrr%mssr)r) message_bodyrEr-r-r. _send_outputs$    zHTTPConnection._send_outputc Cs%|jr$|jjr$d|_n|jtkr?t|_nt|j||_|sfd}nd|||jf}|j|j d|j dkr!|sd}|j drt |\}}}}}n|r*y|j d}Wn!t k r|j d}YnX|jd |q|jrH|j} |j} n|j} |j} y| j d} Wn!t k r| j d} YnX| jd d krd | d } n| |jkr|jd | q| jd} |jd d| | fn|s!|jddq!ndS)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 %srr`rSZhttpZidnaZHostr"r[]z%s:%szAccept-EncodingZidentity)rrsrr_CS_REQ_STARTEDr rD _http_vsn_strrr _http_vsnrYrUnicodeEncodeError putheaderrrrrrrr<) r)rOrP skip_hostskip_accept_encodingrequestZnetlocZnilZ netloc_encrrZhost_encr-r-r. putrequestsN              zHTTPConnection.putrequestcGs/|jtkrtnt|dr<|jd}nt|s^td|fnt|}xt|D]\}}t|dr|jd||zs z0HTTPConnection._send_request..rr#rzaccept-encodingrzcontent-lengthz iso-8859-1) dictfromkeysrrrrrrTrr) r)rOrPrr@Z header_namesZskipsrlrr-r-r.rxs    zHTTPConnection._send_requestc Cs|jr$|jjr$d|_n|jtks<|jrNt|jn|jdkr|j|j|jd|j}n|j|jd|j}yO|j |j t kst t |_|j r|jn ||_|SWn|jYnXdS)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 class 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. NrrO)rrsrrrrCrrNrDrmrMrFrrrn)r)rr-r-r. getresponses&       zHTTPConnection.getresponse)r0r1r2rrrr HTTP_PORTrrrCrr_GLOBAL_DEFAULT_TIMEOUTrQrrrrrrnrrrrrrrrrrr-r-r-r.rs2       0 {   c s^eZdZdZeZdddejdddddfddZfddZ S) HTTPSConnectionz(This class allows communication via SSL.Ncontextcheck_hostnamec stt|j||||||_||_|dkrLtj}n|jtjk} |dkrv|j }n|r| rt dn|s|r|j ||n||_ ||_ dS)NzMcheck_hostname needs a SSL context with either CERT_OPTIONAL or CERT_REQUIRED)rorrQkey_file cert_filesslZ_create_default_https_contextZ verify_modeZ CERT_NONErrXZload_cert_chain_context_check_hostname) r)rrrrrrrrZ will_verify)rpr-r.rQs        zHTTPSConnection.__init__c stj|jr"|j}n |j}|jj|jd||_|jj r|jryt j |jj |Wqt k r|jj tj|jjYqXndS)z(Connect to a host on a given (SSL) port.server_hostnameN)rorrrrZ wrap_socketrNrrrZmatch_hostnameZ getpeercert ExceptionZshutdownrZ SHUT_RDWRrn)r)r)rpr-r.rs       zHTTPSConnection.connect) r0r1r2__doc__ HTTPS_PORTrrrrQrr-r-)rpr.rs   rc@seZdZdS)rN)r0r1r2r-r-r-r.rs c@seZdZdS)rN)r0r1r2r-r-r-r.rs c@seZdZdS)r N)r0r1r2r-r-r-r.r s c@seZdZddZdS)rcCs|f|_||_dS)N)argsrG)r)rGr-r-r.rQ s zUnknownProtocol.__init__N)r0r1r2rQr-r-r-r.r s c@seZdZdS)rN)r0r1r2r-r-r-r.rs c@seZdZdS)r N)r0r1r2r-r-r-r.r s c@s7eZdZdddZddZddZdS)r NcCs"|f|_||_||_dS)N)rpartialexpected)r)rrr-r-r.rQs  zIncompleteRead.__init__cCs<|jdk rd|j}nd}dt|j|fS)Nz, %i more expectedrSzIncompleteRead(%i bytes read%s))rr%r)r)er-r-r.__repr__szIncompleteRead.__repr__cCs t|S)N)rV)r)r-r-r.__str__ szIncompleteRead.__str__)r0r1r2rQrrr-r-r-r.r s  c@seZdZdS)r N)r0r1r2r-r-r-r.r #s c@seZdZdS)r N)r0r1r2r-r-r-r.r &s c@seZdZdS)rN)r0r1r2r-r-r-r.r)s c@seZdZdS)rN)r0r1r2r-r-r-r.r,s c@seZdZddZdS)rcCs.|st|}n|f|_||_dS)N)rVrr,)r)r,r-r-r.rQ0s zBadStatusLine.__init__N)r0r1r2rQr-r-r-r.r/s c@seZdZddZdS)r9cCstj|dt|fdS)Nz&got more than %d bytes when reading %s)rrQr8)r)Z line_typer-r-r.rQ7s zLineTooLong.__init__N)r0r1r2rQr-r-r-r.r96s r9)jrZ email.parserr=Z email.messageiorrerrZ urllib.parser__all__rrrFrrrrdZSWITCHING_PROTOCOLSZ PROCESSINGr ZCREATEDZACCEPTEDZNON_AUTHORITATIVE_INFORMATIONriZ RESET_CONTENTZPARTIAL_CONTENTZ MULTI_STATUSZIM_USEDZMULTIPLE_CHOICESZMOVED_PERMANENTLYZFOUNDZ SEE_OTHERrjZ USE_PROXYZTEMPORARY_REDIRECTZ BAD_REQUESTZ UNAUTHORIZEDZPAYMENT_REQUIREDZ FORBIDDENZ NOT_FOUNDZMETHOD_NOT_ALLOWEDZNOT_ACCEPTABLEZPROXY_AUTHENTICATION_REQUIREDZREQUEST_TIMEOUTZCONFLICTZGONEZLENGTH_REQUIREDZPRECONDITION_FAILEDZREQUEST_ENTITY_TOO_LARGEZREQUEST_URI_TOO_LONGZUNSUPPORTED_MEDIA_TYPEZREQUESTED_RANGE_NOT_SATISFIABLEZEXPECTATION_FAILEDZUNPROCESSABLE_ENTITYZLOCKEDZFAILED_DEPENDENCYZUPGRADE_REQUIREDZPRECONDITION_REQUIREDZTOO_MANY_REQUESTSZREQUEST_HEADER_FIELDS_TOO_LARGEZINTERNAL_SERVER_ERRORZNOT_IMPLEMENTEDZ BAD_GATEWAYZSERVICE_UNAVAILABLEZGATEWAY_TIMEOUTZHTTP_VERSION_NOT_SUPPORTEDZINSUFFICIENT_STORAGEZ NOT_EXTENDEDZNETWORK_AUTHENTICATION_REQUIREDrrr8r:compile fullmatchrsearchrrZMessager!rA RawIOBaserrr ImportErrorrr(rrrr rrr r r r rrrr9rr-r-r-r.Cs,            0