r"""HTTP/1.1 client library<intro stuff goes here><other stuff, too>HTTPConnection goes through a number of "states", which define when a clientmay legally make another request or fetch the response for a particularrequest. This diagram details these state transitions:(null)|| HTTPConnection()vIdle|| putrequest()vRequest-started|| ( putheader() )* endheaders()vRequest-sent|\_____________________________| | getresponse() raises| response = getresponse() | ConnectionErrorv vUnread-response Idle[Response-headers-read]|\____________________| || response.read() | putrequest()v vIdle Req-started-unread-response______/|/ |response.read() | | ( putheader() )* endheaders()v vRequest-started Req-sent-unread-response|| response.read()vRequest-sentThis 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 apartially read response bodyNote: this enforcement is applied by the HTTPConnection class. TheHTTPResponse class does not enforce this state machine, whichimplies sophisticated clients may accelerate the request/responsepipeline. Caution should be taken, though: accelerating the statesbeyond the above pattern may imply knowledge of the server'sconnection-close behavior for certain requests. For example, itis impossible to tell whether the server will close the connectionUNTIL the response headers have been read; this means that furtherrequests cannot be placed into the pipeline until it is known thatthe server will NOT be closing the connection.Logical State __state __response------------- ------- ----------Idle _CS_IDLE NoneRequest-started _CS_REQ_STARTED NoneRequest-sent _CS_REQ_SENT NoneUnread-response _CS_IDLE <response_class>Req-started-unread-response _CS_REQ_STARTED <response_class>Req-sent-unread-response _CS_REQ_SENT <response_class>"""import email.parserimport email.messageimport httpimport ioimport reimport socketimport collections.abcfrom urllib.parse import urlsplit# HTTPMessage, parse_headers(), and the HTTP status code constants are# intentionally omitted for simplicity__all__ = ["HTTPResponse", "HTTPConnection","HTTPException", "NotConnected", "UnknownProtocol","UnknownTransferEncoding", "UnimplementedFileMode","IncompleteRead", "InvalidURL", "ImproperConnectionState","CannotSendRequest", "CannotSendHeader", "ResponseNotReady","BadStatusLine", "LineTooLong", "RemoteDisconnected", "error","responses"]HTTP_PORT = 80HTTPS_PORT = 443_UNKNOWN = 'UNKNOWN'# connection states_CS_IDLE = 'Idle'_CS_REQ_STARTED = 'Request-started'_CS_REQ_SENT = 'Request-sent'# hack to maintain backwards compatibilityglobals().update(http.HTTPStatus.__members__)# another hack to maintain backwards compatibility# Mapping status codes to official W3C namesresponses = {v: v.phrase for v in http.HTTPStatus.__members__.values()}# maximal amount of data to read at one time in _safe_readMAXAMOUNT = 1048576# maximal line length when calling readline()._MAXLINE = 65536_MAXHEADERS = 100# Header name/value ABNF (http://tools.ietf.org/html/rfc7230#section-3.2)## VCHAR = %x21-7E# obs-text = %x80-FF# header-field = field-name ":" OWS field-value OWS# field-name = token# field-value = *( field-content / obs-fold )# field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]# field-vchar = VCHAR / obs-text## obs-fold = CRLF 1*( SP / HTAB )# ; obsolete line folding# ; see Section 3.2.4# token = 1*tchar## tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"# / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"# / DIGIT / ALPHA# ; any VCHAR, except delimiters## VCHAR defined in http://tools.ietf.org/html/rfc5234#appendix-B.1# the patterns for both name and value are more lenient than RFC# definitions to allow for backwards compatibility_is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch_is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search# These characters are not allowed within HTTP URL paths.# See https://tools.ietf.org/html/rfc3986#section-3.3 and the# https://tools.ietf.org/html/rfc3986#appendix-A pchar definition.# Prevents CVE-2019-9740. Includes control characters such as \r\n.# We don't restrict chars above \x7f as putrequest() limits us to ASCII._contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f]')# Arguably only these _should_ allowed:# _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")# We are more lenient for assumed real world compatibility purposes.# We always set the Content-Length header for these methods because some# servers will otherwise respond with a 411_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}def _encode(data, name='data'):"""Call data.encode("latin-1") but show a better error message."""try:return data.encode("latin-1")except UnicodeEncodeError as err:raise UnicodeEncodeError(err.encoding,err.object,err.start,err.end,"%s (%.20r) is not valid Latin-1. Use %s.encode('utf-8') ""if you want to send it encoded in UTF-8." %(name.title(), data[err.start:err.end], name)) from Noneclass HTTPMessage(email.message.Message):# XXX The only usage of this method is in# http.server.CGIHTTPRequestHandler. Maybe move the code there so# that it doesn't need to be part of the public API. The API has# never been defined so this could cause backwards compatibility# issues.def getallmatchingheaders(self, name):"""Find all header lines matching a given header name.Look through the list of headers and find all lines matching a givenheader name (and their continuation lines). A list of the lines isreturned, without interpretation. If the header does not occur, anempty list is returned. If the header occurs multiple times, alloccurrences are returned. Case is not important in the header name."""name = name.lower() + ':'n = len(name)lst = []hit = 0for line in self.keys():if line[:n].lower() == name:hit = 1elif not line[:1].isspace():hit = 0if hit:lst.append(line)return lstdef parse_headers(fp, _class=HTTPMessage):"""Parses 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 bytesfrom the stream, bytes which we later need to read as bytes.So we read the correct bytes here, as bytes, for email Parserto parse."""headers = []while True:line = fp.readline(_MAXLINE + 1)if len(line) > _MAXLINE:raise LineTooLong("header line")headers.append(line)if len(headers) > _MAXHEADERS:raise HTTPException("got more than %d headers" % _MAXHEADERS)if line in (b'\r\n', b'\n', b''):breakhstring = b''.join(headers).decode('iso-8859-1')return email.parser.Parser(_class=_class).parsestr(hstring)class HTTPResponse(io.BufferedIOBase):# See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.# The bytes from the socket object are iso-8859-1 strings.# See RFC 2616 sec 2.2 which notes an exception for MIME-encoded# text following RFC 2047. The basic status line parsing only# accepts iso-8859-1.def __init__(self, sock, debuglevel=0, method=None, url=None):# If the response includes a content-length header, we need to# make sure that the client doesn't read more than the# specified number of bytes. If it does, it will block until# the server times out and closes the connection. This will# happen if a self.fp.read() is done (without a size) whether# self.fp is buffered or not. So, no self.fp.read() by# clients unless they know what they are doing.self.fp = sock.makefile("rb")self.debuglevel = debuglevelself._method = method# The HTTPResponse object is returned via urllib. The clients# of http and urllib expect different attributes for the# headers. headers is used here and supports urllib. msg is# provided as a backwards compatibility layer for http# clients.self.headers = self.msg = None# from the Status-Line of the responseself.version = _UNKNOWN # HTTP-Versionself.status = _UNKNOWN # Status-Codeself.reason = _UNKNOWN # Reason-Phraseself.chunked = _UNKNOWN # is "chunked" being used?self.chunk_left = _UNKNOWN # bytes left to read in current chunkself.length = _UNKNOWN # number of bytes left in responseself.will_close = _UNKNOWN # conn will close at end of responsedef _read_status(self):line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")if len(line) > _MAXLINE:raise LineTooLong("status line")if self.debuglevel > 0:print("reply:", repr(line))if not line:# Presumably, the server closed the connection before# sending a valid response.raise RemoteDisconnected("Remote end closed connection without"" response")try:version, status, reason = line.split(None, 2)except ValueError:try:version, status = line.split(None, 1)reason = ""except ValueError:# empty version will cause next test to fail.version = ""if not version.startswith("HTTP/"):self._close_conn()raise BadStatusLine(line)# The status code is a three-digit numbertry:status = int(status)if status < 100 or status > 999:raise BadStatusLine(line)except ValueError:raise BadStatusLine(line)return version, status, reasondef begin(self):if self.headers is not None:# we've already started reading the responsereturn# read until we get a non-100 responsewhile True:version, status, reason = self._read_status()if status != CONTINUE:break# skip the header from the 100 responsewhile True:skip = self.fp.readline(_MAXLINE + 1)if len(skip) > _MAXLINE:raise LineTooLong("header line")skip = skip.strip()if not skip:breakif self.debuglevel > 0:print("header:", skip)self.code = self.status = statusself.reason = reason.strip()if version in ("HTTP/1.0", "HTTP/0.9"):# Some servers might still return "0.9", treat it as 1.0 anywayself.version = 10elif version.startswith("HTTP/1."):self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1else:raise UnknownProtocol(version)self.headers = self.msg = parse_headers(self.fp)if self.debuglevel > 0:for hdr, val in self.headers.items():print("header:", hdr + ":", val)# are we using the chunked-style of transfer encoding?tr_enc = self.headers.get("transfer-encoding")if tr_enc and tr_enc.lower() == "chunked":self.chunked = Trueself.chunk_left = Noneelse:self.chunked = False# will the connection close at the end of the response?self.will_close = self._check_close()# do we have a Content-Length?# NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"self.length = Nonelength = self.headers.get("content-length")# are we using the chunked-style of transfer encoding?tr_enc = self.headers.get("transfer-encoding")if length and not self.chunked:try:self.length = int(length)except ValueError:self.length = Noneelse:if self.length < 0: # ignore nonsensical negative lengthsself.length = Noneelse:self.length = None# does the body have a fixed length? (of zero)if (status == NO_CONTENT or status == NOT_MODIFIED or100 <= status < 200 or # 1xx codesself._method == "HEAD"):self.length = 0# if the connection remains open, and we aren't using chunked, and# a content-length was not provided, then assume that the connection# WILL close.if (not self.will_close andnot self.chunked andself.length is None):self.will_close = Truedef _check_close(self):conn = self.headers.get("connection")if self.version == 11:# An HTTP/1.1 proxy is assumed to stay open unless# explicitly closed.if conn and "close" in conn.lower():return Truereturn False# Some HTTP/1.0 implementations have support for persistent# connections, using rules different than HTTP/1.1.# For older HTTP, Keep-Alive indicates persistent connection.if self.headers.get("keep-alive"):return False# At least Akamai returns a "Connection: Keep-Alive" header,# which was supposed to be sent by the client.if conn and "keep-alive" in conn.lower():return False# Proxy-Connection is a netscape hack.pconn = self.headers.get("proxy-connection")if pconn and "keep-alive" in pconn.lower():return False# otherwise, assume it will closereturn Truedef _close_conn(self):fp = self.fpself.fp = Nonefp.close()def close(self):try:super().close() # set "closed" flagfinally:if self.fp:self._close_conn()# These implementations are for the benefit of io.BufferedReader.# XXX This class should probably be revised to act more like# the "raw stream" that BufferedReader expects.def flush(self):super().flush()if self.fp:self.fp.flush()def readable(self):"""Always returns True"""return True# End of "raw stream" methodsdef isclosed(self):"""True if the connection is closed."""# NOTE: it is possible that we will not ever call self.close(). This# case occurs when will_close is TRUE, length is None, and we# read up to the last byte, but NOT past it.## IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be# called, meaning self.isclosed() is meaningful.return self.fp is Nonedef read(self, amt=None):if self.fp is None:return b""if self._method == "HEAD":self._close_conn()return b""if amt is not None:# Amount is given, implement using readintob = bytearray(amt)n = self.readinto(b)return memoryview(b)[:n].tobytes()else:# Amount is not given (unbounded read) so we must check self.length# and self.chunkedif self.chunked:return self._readall_chunked()if self.length is None:s = self.fp.read()else:try:s = self._safe_read(self.length)except IncompleteRead:self._close_conn()raiseself.length = 0self._close_conn() # we read everythingreturn sdef readinto(self, b):"""Read up to len(b) bytes into bytearray b and return the numberof bytes read."""if self.fp is None:return 0if self._method == "HEAD":self._close_conn()return 0if self.chunked:return self._readinto_chunked(b)if self.length is not None:if len(b) > self.length:# clip the read to the "end of response"b = memoryview(b)[0:self.length]# we do not use _safe_read() here because this may be a .will_close# connection, and the user is reading more bytes than will be provided# (for example, reading in 1k chunks)n = self.fp.readinto(b)if not n and b:# Ideally, we would raise IncompleteRead if the content-length# wasn't satisfied, but it might break compatibility.self._close_conn()elif self.length is not None:self.length -= nif not self.length:self._close_conn()return ndef _read_next_chunk_size(self):# Read the next chunk size from the fileline = self.fp.readline(_MAXLINE + 1)if len(line) > _MAXLINE:raise LineTooLong("chunk size")i = line.find(b";")if i >= 0:line = line[:i] # strip chunk-extensionstry:return int(line, 16)except ValueError:# close the connection as protocol synchronisation is# probably lostself._close_conn()raisedef _read_and_discard_trailer(self):# read and discard trailer up to the CRLF terminator### note: we shouldn't have any trailers!while True:line = self.fp.readline(_MAXLINE + 1)if len(line) > _MAXLINE:raise LineTooLong("trailer line")if not line:# a vanishingly small number of sites EOF without# sending the trailerbreakif line in (b'\r\n', b'\n', b''):breakdef _get_chunk_left(self):# return self.chunk_left, reading a new chunk if necessary.# chunk_left == 0: at the end of the current chunk, need to close it# chunk_left == None: No current chunk, should read next.# This function returns non-zero or None if the last chunk has# been read.chunk_left = self.chunk_leftif not chunk_left: # Can be 0 or Noneif chunk_left is not None:# We are at the end of chunk, discard chunk endself._safe_read(2) # toss the CRLF at the end of the chunktry:chunk_left = self._read_next_chunk_size()except ValueError:raise IncompleteRead(b'')if chunk_left == 0:# last chunk: 1*("0") [ chunk-extension ] CRLFself._read_and_discard_trailer()# we read everything; close the "file"self._close_conn()chunk_left = Noneself.chunk_left = chunk_leftreturn chunk_leftdef _readall_chunked(self):assert self.chunked != _UNKNOWNvalue = []try:while True:chunk_left = self._get_chunk_left()if chunk_left is None:breakvalue.append(self._safe_read(chunk_left))self.chunk_left = 0return b''.join(value)except IncompleteRead:raise IncompleteRead(b''.join(value))def _readinto_chunked(self, b):assert self.chunked != _UNKNOWNtotal_bytes = 0mvb = memoryview(b)try:while True:chunk_left = self._get_chunk_left()if chunk_left is None:return total_bytesif len(mvb) <= chunk_left:n = self._safe_readinto(mvb)self.chunk_left = chunk_left - nreturn total_bytes + ntemp_mvb = mvb[:chunk_left]n = self._safe_readinto(temp_mvb)mvb = mvb[n:]total_bytes += nself.chunk_left = 0except IncompleteRead:raise IncompleteRead(bytes(b[0:total_bytes]))def _safe_read(self, amt):"""Read the number of bytes requested, compensating for partial reads.Normally, we have a blocking socket, but a read() can be interruptedby a signal (resulting in a partial read).Note that we cannot distinguish between EOF and an interrupt when zerobytes have been read. IncompleteRead() will be raised in thissituation.This function should be used when <amt> bytes "should" be present forreading. If the bytes are truly not available (due to EOF), then theIncompleteRead exception can be used to detect the problem."""s = []while amt > 0:chunk = self.fp.read(min(amt, MAXAMOUNT))if not chunk:raise IncompleteRead(b''.join(s), amt)s.append(chunk)amt -= len(chunk)return b"".join(s)def _safe_readinto(self, b):"""Same as _safe_read, but for reading into a buffer."""total_bytes = 0mvb = memoryview(b)while total_bytes < len(b):if MAXAMOUNT < len(mvb):temp_mvb = mvb[0:MAXAMOUNT]n = self.fp.readinto(temp_mvb)else:n = self.fp.readinto(mvb)if not n:raise IncompleteRead(bytes(mvb[0:total_bytes]), len(b))mvb = mvb[n:]total_bytes += nreturn total_bytesdef read1(self, n=-1):"""Read with at most one underlying system call. If at least onebyte is buffered, return that instead."""if self.fp is None or self._method == "HEAD":return b""if self.chunked:return self._read1_chunked(n)if self.length is not None and (n < 0 or n > self.length):n = self.lengthresult = self.fp.read1(n)if not result and n:self._close_conn()elif self.length is not None:self.length -= len(result)return resultdef peek(self, n=-1):# Having this enables IOBase.readline() to read more than one# byte at a timeif self.fp is None or self._method == "HEAD":return b""if self.chunked:return self._peek_chunked(n)return self.fp.peek(n)def readline(self, limit=-1):if self.fp is None or self._method == "HEAD":return b""if self.chunked:# Fallback to IOBase readline which uses peek() and read()return super().readline(limit)if self.length is not None and (limit < 0 or limit > self.length):limit = self.lengthresult = self.fp.readline(limit)if not result and limit:self._close_conn()elif self.length is not None:self.length -= len(result)return resultdef _read1_chunked(self, n):# Strictly speaking, _get_chunk_left() may cause more than one read,# but that is ok, since that is to satisfy the chunked protocol.chunk_left = self._get_chunk_left()if chunk_left is None or n == 0:return b''if not (0 <= n <= chunk_left):n = chunk_left # if n is negative or larger than chunk_leftread = self.fp.read1(n)self.chunk_left -= len(read)if not read:raise IncompleteRead(b"")return readdef _peek_chunked(self, n):# Strictly speaking, _get_chunk_left() may cause more than one read,# but that is ok, since that is to satisfy the chunked protocol.try:chunk_left = self._get_chunk_left()except IncompleteRead:return b'' # peek doesn't worry about protocolif chunk_left is None:return b'' # eof# peek is allowed to return more than requested. Just request the# entire chunk, and truncate what we get.return self.fp.peek(chunk_left)[:chunk_left]def fileno(self):return self.fp.fileno()def getheader(self, name, default=None):'''Returns the value of the header matching *name*.If there are multiple matching headers, the values arecombined into a single string separated by commas and spaces.If no matching header is found, returns *default* or None ifthe *default* is not specified.If the headers are unknown, raises http.client.ResponseNotReady.'''if self.headers is None:raise ResponseNotReady()headers = self.headers.get_all(name) or defaultif isinstance(headers, str) or not hasattr(headers, '__iter__'):return headerselse:return ', '.join(headers)def getheaders(self):"""Return list of (header, value) tuples."""if self.headers is None:raise ResponseNotReady()return list(self.headers.items())# We override IOBase.__iter__ so that it doesn't check for closed-nessdef __iter__(self):return self# For compatibility with old-style urllib responses.def info(self):'''Returns an instance of the class mimetools.Message containingmeta-information associated with the URL.When the method is HTTP, these headers are those returned bythe server at the head of the retrieved HTML page (includingContent-Length and Content-Type).When the method is FTP, a Content-Length header will bepresent if (as is now usual) the server passed back a filelength in response to the FTP retrieval request. AContent-Type header will be present if the MIME type can beguessed.When the method is local-file, returned headers will includea Date representing the file's last-modified time, aContent-Length giving file size, and a Content-Typecontaining a guess at the file's type. See also thedescription of the mimetools module.'''return self.headersdef geturl(self):'''Return the real URL of the page.In some cases, the HTTP server redirects a client to anotherURL. The urlopen() function handles this transparently, but insome cases the caller needs to know which URL the client wasredirected to. The geturl() method can be used to get at thisredirected URL.'''return self.urldef getcode(self):'''Return the HTTP status code that was sent with the response,or None if the URL is not an HTTP URL.'''return self.statusclass HTTPConnection:_http_vsn = 11_http_vsn_str = 'HTTP/1.1'response_class = HTTPResponsedefault_port = HTTP_PORTauto_open = 1debuglevel = 0@staticmethoddef _is_textIO(stream):"""Test whether a file-like object is a text or a binary stream."""return isinstance(stream, io.TextIOBase)@staticmethoddef _get_content_length(body, method):"""Get the content-length based on the body.If the body is None, we set Content-Length: 0 for methods that expecta body (RFC 7230, Section 3.3.2). We also set the Content-Length forany method if the body is a str or bytes-like object and not a file."""if body is None:# do an explicit check for not None here to distinguish# between unset and set but emptyif method.upper() in _METHODS_EXPECTING_BODY:return 0else:return Noneif hasattr(body, 'read'):# file-like object.return Nonetry:# does it implement the buffer protocol (bytes, bytearray, array)?mv = memoryview(body)return mv.nbytesexcept TypeError:passif isinstance(body, str):return len(body)return Nonedef __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,source_address=None, blocksize=8192):self.timeout = timeoutself.source_address = source_addressself.blocksize = blocksizeself.sock = Noneself._buffer = []self.__response = Noneself.__state = _CS_IDLEself._method = Noneself._tunnel_host = Noneself._tunnel_port = Noneself._tunnel_headers = {}(self.host, self.port) = self._get_hostport(host, port)# This is stored as an instance variable to allow unit# tests to replace it with a suitable mockupself._create_connection = socket.create_connectiondef set_tunnel(self, host, port=None, headers=None):"""Set up host and port for HTTP CONNECT tunnelling.In a connection that uses HTTP CONNECT tunneling, the host passed to theconstructor is used as a proxy server that relays all communication tothe endpoint passed to `set_tunnel`. This done by sending an HTTPCONNECT request to the proxy server when the connection is established.This method must be called before the HTML connection has beenestablished.The headers argument should be a mapping of extra HTTP headers to sendwith the CONNECT request."""if self.sock:raise RuntimeError("Can't set up tunnel for established connection")self._tunnel_host, self._tunnel_port = self._get_hostport(host, port)if headers:self._tunnel_headers = headerselse:self._tunnel_headers.clear()def _get_hostport(self, host, port):if port is None:i = host.rfind(':')j = host.rfind(']') # ipv6 addresses have [...]if i > j:try:port = int(host[i+1:])except ValueError:if host[i+1:] == "": # http://foo.com:/ == http://foo.com/port = self.default_portelse:raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])host = host[:i]else:port = self.default_portif host and host[0] == '[' and host[-1] == ']':host = host[1:-1]return (host, port)def set_debuglevel(self, level):self.debuglevel = leveldef _tunnel(self):connect_str = "CONNECT %s:%d HTTP/1.0\r\n" % (self._tunnel_host,self._tunnel_port)connect_bytes = connect_str.encode("ascii")self.send(connect_bytes)for header, value in self._tunnel_headers.items():header_str = "%s: %s\r\n" % (header, value)header_bytes = header_str.encode("latin-1")self.send(header_bytes)self.send(b'\r\n')response = self.response_class(self.sock, method=self._method)(version, code, message) = response._read_status()if code != http.HTTPStatus.OK:self.close()raise OSError("Tunnel connection failed: %d %s" % (code,message.strip()))while True:line = response.fp.readline(_MAXLINE + 1)if len(line) > _MAXLINE:raise LineTooLong("header line")if not line:# for sites which EOF without sending a trailerbreakif line in (b'\r\n', b'\n', b''):breakif self.debuglevel > 0:print('header:', line.decode())def connect(self):"""Connect to the host and port specified in __init__."""self.sock = self._create_connection((self.host,self.port), self.timeout, self.source_address)self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)if self._tunnel_host:self._tunnel()def close(self):"""Close the connection to the HTTP server."""self.__state = _CS_IDLEtry:sock = self.sockif sock:self.sock = Nonesock.close() # close it manually... there may be other refsfinally:response = self.__responseif response:self.__response = Noneresponse.close()def send(self, data):"""Send `data' to the server.``data`` can be a string object, a bytes object, an array object, afile-like object that supports a .read() method, or an iterable object."""if self.sock is None:if self.auto_open:self.connect()else:raise NotConnected()if self.debuglevel > 0:print("send:", repr(data))if hasattr(data, "read") :if self.debuglevel > 0:print("sendIng a read()able")encode = self._is_textIO(data)if encode and self.debuglevel > 0:print("encoding file using iso-8859-1")while 1:datablock = data.read(self.blocksize)if not datablock:breakif encode:datablock = datablock.encode("iso-8859-1")self.sock.sendall(datablock)returntry:self.sock.sendall(data)except TypeError:if isinstance(data, collections.abc.Iterable):for d in data:self.sock.sendall(d)else:raise TypeError("data should be a bytes-like object ""or an iterable, got %r" % type(data))def _output(self, s):"""Add a line of output to the current request buffer.Assumes that the line does *not* end with \\r\\n."""self._buffer.append(s)def _read_readable(self, readable):if self.debuglevel > 0:print("sendIng a read()able")encode = self._is_textIO(readable)if encode and self.debuglevel > 0:print("encoding file using iso-8859-1")while True:datablock = readable.read(self.blocksize)if not datablock:breakif encode:datablock = datablock.encode("iso-8859-1")yield datablockdef _send_output(self, message_body=None, encode_chunked=False):"""Send 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."""self._buffer.extend((b"", b""))msg = b"\r\n".join(self._buffer)del self._buffer[:]self.send(msg)if message_body is not None:# create a consistent interface to message_bodyif hasattr(message_body, 'read'):# Let file-like take precedence over byte-like. This# is needed to allow the current position of mmap'ed# files to be taken into account.chunks = self._read_readable(message_body)else:try:# this is solely to check to see if message_body# implements the buffer API. it /would/ be easier# to capture if PyObject_CheckBuffer was exposed# to Python.memoryview(message_body)except TypeError:try:chunks = iter(message_body)except TypeError:raise TypeError("message_body should be a bytes-like ""object or an iterable, got %r"% type(message_body))else:# the object implements the buffer interface and# can be passed directly into socket methodschunks = (message_body,)for chunk in chunks:if not chunk:if self.debuglevel > 0:print('Zero length chunk ignored')continueif encode_chunked and self._http_vsn == 11:# chunked encodingchunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \+ b'\r\n'self.send(chunk)if encode_chunked and self._http_vsn == 11:# end chunked transferself.send(b'0\r\n\r\n')def putrequest(self, method, url, skip_host=False,skip_accept_encoding=False):"""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"""# if a prior response has been completed, then forget about it.if self.__response and self.__response.isclosed():self.__response = None# in certain cases, we cannot issue another request on this connection.# this occurs when:# 1) we are in the process of sending a request. (_CS_REQ_STARTED)# 2) a response to a previous request has signalled that it is going# to close the connection upon completion.# 3) the headers for the previous response have not been read, thus# we cannot determine whether point (2) is true. (_CS_REQ_SENT)## if there is no prior response, then we can request at will.## if point (2) is true, then we will have passed the socket to the# response (effectively meaning, "there is no prior response"), and# will open a new one when a new request is made.## Note: if a prior response exists, then we *can* start a new request.# We are not allowed to begin fetching the response to this new# request, however, until that prior response is complete.#if self.__state == _CS_IDLE:self.__state = _CS_REQ_STARTEDelse:raise CannotSendRequest(self.__state)# Save the method we use, we need it later in the response phaseself._method = methodif not url:url = '/'# Prevent CVE-2019-9740.match = _contains_disallowed_url_pchar_re.search(url)if match:raise InvalidURL(f"URL can't contain control characters. {url!r} "f"(found at least {match.group()!r})")request = '%s %s %s' % (method, url, self._http_vsn_str)# Non-ASCII characters should have been eliminated earlierself._output(request.encode('ascii'))if self._http_vsn == 11:# Issue some standard headers for better HTTP/1.1 complianceif not skip_host:# this header is issued *only* for HTTP/1.1# connections. more specifically, this means it is# only issued when the client uses the new# HTTPConnection() class. backwards-compat clients# will be using HTTP/1.0 and those clients may be# issuing this header themselves. we should NOT issue# it twice; some web servers (such as Apache) barf# when they see two Host: headers# If we need a non-standard port,include it in the# header. If the request is going through a proxy,# but the host of the actual URL, not the host of the# proxy.netloc = ''if url.startswith('http'):nil, netloc, nil, nil, nil = urlsplit(url)if netloc:try:netloc_enc = netloc.encode("ascii")except UnicodeEncodeError:netloc_enc = netloc.encode("idna")self.putheader('Host', netloc_enc)else:if self._tunnel_host:host = self._tunnel_hostport = self._tunnel_portelse:host = self.hostport = self.porttry:host_enc = host.encode("ascii")except UnicodeEncodeError:host_enc = host.encode("idna")# As per RFC 273, IPv6 address should be wrapped with []# when used as Host headerif host.find(':') >= 0:host_enc = b'[' + host_enc + b']'if port == self.default_port:self.putheader('Host', host_enc)else:host_enc = host_enc.decode("ascii")self.putheader('Host', "%s:%s" % (host_enc, port))# note: we are assuming that clients will not attempt to set these# headers since *this* library must deal with the# consequences. this also means that when the supporting# libraries are updated to recognize other forms, then this# code should be changed (removed or updated).# we only want a Content-Encoding of "identity" since we don't# support encodings such as x-gzip or x-deflate.if not skip_accept_encoding:self.putheader('Accept-Encoding', 'identity')# we can accept "chunked" Transfer-Encodings, but no others# NOTE: no TE header implies *only* "chunked"#self.putheader('TE', 'chunked')# if TE is supplied in the header, then it must appear in a# Connection header.#self.putheader('Connection', 'TE')else:# For HTTP/1.0, the server will assume "not chunked"passdef putheader(self, header, *values):"""Send a request header line to the server.For example: h.putheader('Accept', 'text/html')"""if self.__state != _CS_REQ_STARTED:raise CannotSendHeader()if hasattr(header, 'encode'):header = header.encode('ascii')if not _is_legal_header_name(header):raise ValueError('Invalid header name %r' % (header,))values = list(values)for i, one_value in enumerate(values):if hasattr(one_value, 'encode'):values[i] = one_value.encode('latin-1')elif isinstance(one_value, int):values[i] = str(one_value).encode('ascii')if _is_illegal_header_value(values[i]):raise ValueError('Invalid header value %r' % (values[i],))value = b'\r\n\t'.join(values)header = header + b': ' + valueself._output(header)def endheaders(self, message_body=None, *, encode_chunked=False):"""Indicate that the last header line has been sent to the server.This method sends the request to the server. The optional message_bodyargument can be used to pass a message body associated with therequest."""if self.__state == _CS_REQ_STARTED:self.__state = _CS_REQ_SENTelse:raise CannotSendHeader()self._send_output(message_body, encode_chunked=encode_chunked)def request(self, method, url, body=None, headers={}, *,encode_chunked=False):"""Send a complete request to the server."""self._send_request(method, url, body, headers, encode_chunked)def _send_request(self, method, url, body, headers, encode_chunked):# Honor explicitly requested Host: and Accept-Encoding: headers.header_names = frozenset(k.lower() for k in headers)skips = {}if 'host' in header_names:skips['skip_host'] = 1if 'accept-encoding' in header_names:skips['skip_accept_encoding'] = 1self.putrequest(method, url, **skips)# chunked encoding will happen if HTTP/1.1 is used and either# the caller passes encode_chunked=True or the following# conditions hold:# 1. content-length has not been explicitly set# 2. the body is a file or iterable, but not a str or bytes-like# 3. Transfer-Encoding has NOT been explicitly set by the callerif 'content-length' not in header_names:# only chunk body if not explicitly set for backwards# compatibility, assuming the client code is already handling the# chunkingif 'transfer-encoding' not in header_names:# if content-length cannot be automatically determined, fall# back to chunked encodingencode_chunked = Falsecontent_length = self._get_content_length(body, method)if content_length is None:if body is not None:if self.debuglevel > 0:print('Unable to determine size of %r' % body)encode_chunked = Trueself.putheader('Transfer-Encoding', 'chunked')else:self.putheader('Content-Length', str(content_length))else:encode_chunked = Falsefor hdr, value in headers.items():self.putheader(hdr, value)if isinstance(body, str):# RFC 2616 Section 3.7.1 says that text default has a# default charset of iso-8859-1.body = _encode(body, 'body')self.endheaders(body, encode_chunked=encode_chunked)def getresponse(self):"""Get the response from the server.If the HTTPConnection is in the correct state, returns aninstance of HTTPResponse or of whatever object is returned bythe response_class variable.If a request has not been sent or if a previous response hasnot be handled, ResponseNotReady is raised. If the HTTPresponse indicates that the connection should be closed, thenit will be closed before the response is returned. When theconnection is closed, the underlying socket is closed."""# if a prior response has been completed, then forget about it.if self.__response and self.__response.isclosed():self.__response = None# if a prior response exists, then it must be completed (otherwise, we# cannot read this response's header to determine the connection-close# behavior)## note: if a prior response existed, but was connection-close, then the# socket and response were made independent of this HTTPConnection# object since a new request requires that we open a whole new# connection## this means the prior response had one of two states:# 1) will_close: this connection was reset and the prior socket and# response operate independently# 2) persistent: the response was retained and we await its# isclosed() status to become true.#if self.__state != _CS_REQ_SENT or self.__response:raise ResponseNotReady(self.__state)if self.debuglevel > 0:response = self.response_class(self.sock, self.debuglevel,method=self._method)else:response = self.response_class(self.sock, method=self._method)try:try:response.begin()except ConnectionError:self.close()raiseassert response.will_close != _UNKNOWNself.__state = _CS_IDLEif response.will_close:# this effectively passes the connection to the responseself.close()else:# remember this, so we can tell when it is completeself.__response = responsereturn responseexcept:response.close()raisetry:import sslexcept ImportError:passelse:class HTTPSConnection(HTTPConnection):"This class allows communication via SSL."default_port = HTTPS_PORT# XXX Should key_file and cert_file be deprecated in favour of context?def __init__(self, host, port=None, key_file=None, cert_file=None,timeout=socket._GLOBAL_DEFAULT_TIMEOUT,source_address=None, *, context=None,check_hostname=None, blocksize=8192):super(HTTPSConnection, self).__init__(host, port, timeout,source_address,blocksize=blocksize)if (key_file is not None or cert_file is not None orcheck_hostname is not None):import warningswarnings.warn("key_file, cert_file and check_hostname are ""deprecated, use a custom context instead.",DeprecationWarning, 2)self.key_file = key_fileself.cert_file = cert_fileif context is None:context = ssl._create_default_https_context()# enable PHA for TLS 1.3 connections if availableif context.post_handshake_auth is not None:context.post_handshake_auth = Truewill_verify = context.verify_mode != ssl.CERT_NONEif check_hostname is None:check_hostname = context.check_hostnameif check_hostname and not will_verify:raise ValueError("check_hostname needs a SSL context with ""either CERT_OPTIONAL or CERT_REQUIRED")if key_file or cert_file:context.load_cert_chain(cert_file, key_file)# cert and key file means the user wants to authenticate.# enable TLS 1.3 PHA implicitly even for custom contexts.if context.post_handshake_auth is not None:context.post_handshake_auth = Trueself._context = contextif check_hostname is not None:self._context.check_hostname = check_hostnamedef connect(self):"Connect to a host on a given (SSL) port."super().connect()if self._tunnel_host:server_hostname = self._tunnel_hostelse:server_hostname = self.hostself.sock = self._context.wrap_socket(self.sock,server_hostname=server_hostname)__all__.append("HTTPSConnection")class HTTPException(Exception):# Subclasses that define an __init__ must call Exception.__init__# or define self.args. Otherwise, str() will fail.passclass NotConnected(HTTPException):passclass InvalidURL(HTTPException):passclass UnknownProtocol(HTTPException):def __init__(self, version):self.args = version,self.version = versionclass UnknownTransferEncoding(HTTPException):passclass UnimplementedFileMode(HTTPException):passclass IncompleteRead(HTTPException):def __init__(self, partial, expected=None):self.args = partial,self.partial = partialself.expected = expecteddef __repr__(self):if self.expected is not None:e = ', %i more expected' % self.expectedelse:e = ''return '%s(%i bytes read%s)' % (self.__class__.__name__,len(self.partial), e)def __str__(self):return repr(self)class ImproperConnectionState(HTTPException):passclass CannotSendRequest(ImproperConnectionState):passclass CannotSendHeader(ImproperConnectionState):passclass ResponseNotReady(ImproperConnectionState):passclass BadStatusLine(HTTPException):def __init__(self, line):if not line:line = repr(line)self.args = line,self.line = lineclass LineTooLong(HTTPException):def __init__(self, line_type):HTTPException.__init__(self, "got more than %d bytes when reading %s"% (_MAXLINE, line_type))class RemoteDisconnected(ConnectionResetError, BadStatusLine):def __init__(self, *pos, **kw):BadStatusLine.__init__(self, "")ConnectionResetError.__init__(self, *pos, **kw)# for backwards compatibilityerror = HTTPException
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。