logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

client.py (56530B)


  1. r"""HTTP/1.1 client library
  2. <intro stuff goes here>
  3. <other stuff, too>
  4. HTTPConnection goes through a number of "states", which define when a client
  5. may legally make another request or fetch the response for a particular
  6. request. This diagram details these state transitions:
  7. (null)
  8. |
  9. | HTTPConnection()
  10. v
  11. Idle
  12. |
  13. | putrequest()
  14. v
  15. Request-started
  16. |
  17. | ( putheader() )* endheaders()
  18. v
  19. Request-sent
  20. |\_____________________________
  21. | | getresponse() raises
  22. | response = getresponse() | ConnectionError
  23. v v
  24. Unread-response Idle
  25. [Response-headers-read]
  26. |\____________________
  27. | |
  28. | response.read() | putrequest()
  29. v v
  30. Idle Req-started-unread-response
  31. ______/|
  32. / |
  33. response.read() | | ( putheader() )* endheaders()
  34. v v
  35. Request-started Req-sent-unread-response
  36. |
  37. | response.read()
  38. v
  39. Request-sent
  40. This diagram presents the following rules:
  41. -- a second request may not be started until {response-headers-read}
  42. -- a response [object] cannot be retrieved until {request-sent}
  43. -- there is no differentiation between an unread response body and a
  44. partially read response body
  45. Note: this enforcement is applied by the HTTPConnection class. The
  46. HTTPResponse class does not enforce this state machine, which
  47. implies sophisticated clients may accelerate the request/response
  48. pipeline. Caution should be taken, though: accelerating the states
  49. beyond the above pattern may imply knowledge of the server's
  50. connection-close behavior for certain requests. For example, it
  51. is impossible to tell whether the server will close the connection
  52. UNTIL the response headers have been read; this means that further
  53. requests cannot be placed into the pipeline until it is known that
  54. the server will NOT be closing the connection.
  55. Logical State __state __response
  56. ------------- ------- ----------
  57. Idle _CS_IDLE None
  58. Request-started _CS_REQ_STARTED None
  59. Request-sent _CS_REQ_SENT None
  60. Unread-response _CS_IDLE <response_class>
  61. Req-started-unread-response _CS_REQ_STARTED <response_class>
  62. Req-sent-unread-response _CS_REQ_SENT <response_class>
  63. """
  64. import email.parser
  65. import email.message
  66. import http
  67. import io
  68. import re
  69. import socket
  70. import sys
  71. import collections.abc
  72. from urllib.parse import urlsplit
  73. # HTTPMessage, parse_headers(), and the HTTP status code constants are
  74. # intentionally omitted for simplicity
  75. __all__ = ["HTTPResponse", "HTTPConnection",
  76. "HTTPException", "NotConnected", "UnknownProtocol",
  77. "UnknownTransferEncoding", "UnimplementedFileMode",
  78. "IncompleteRead", "InvalidURL", "ImproperConnectionState",
  79. "CannotSendRequest", "CannotSendHeader", "ResponseNotReady",
  80. "BadStatusLine", "LineTooLong", "RemoteDisconnected", "error",
  81. "responses"]
  82. HTTP_PORT = 80
  83. HTTPS_PORT = 443
  84. _UNKNOWN = 'UNKNOWN'
  85. # connection states
  86. _CS_IDLE = 'Idle'
  87. _CS_REQ_STARTED = 'Request-started'
  88. _CS_REQ_SENT = 'Request-sent'
  89. # hack to maintain backwards compatibility
  90. globals().update(http.HTTPStatus.__members__)
  91. # another hack to maintain backwards compatibility
  92. # Mapping status codes to official W3C names
  93. responses = {v: v.phrase for v in http.HTTPStatus.__members__.values()}
  94. # maximal line length when calling readline().
  95. _MAXLINE = 65536
  96. _MAXHEADERS = 100
  97. # Header name/value ABNF (http://tools.ietf.org/html/rfc7230#section-3.2)
  98. #
  99. # VCHAR = %x21-7E
  100. # obs-text = %x80-FF
  101. # header-field = field-name ":" OWS field-value OWS
  102. # field-name = token
  103. # field-value = *( field-content / obs-fold )
  104. # field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
  105. # field-vchar = VCHAR / obs-text
  106. #
  107. # obs-fold = CRLF 1*( SP / HTAB )
  108. # ; obsolete line folding
  109. # ; see Section 3.2.4
  110. # token = 1*tchar
  111. #
  112. # tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
  113. # / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
  114. # / DIGIT / ALPHA
  115. # ; any VCHAR, except delimiters
  116. #
  117. # VCHAR defined in http://tools.ietf.org/html/rfc5234#appendix-B.1
  118. # the patterns for both name and value are more lenient than RFC
  119. # definitions to allow for backwards compatibility
  120. _is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch
  121. _is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search
  122. # These characters are not allowed within HTTP URL paths.
  123. # See https://tools.ietf.org/html/rfc3986#section-3.3 and the
  124. # https://tools.ietf.org/html/rfc3986#appendix-A pchar definition.
  125. # Prevents CVE-2019-9740. Includes control characters such as \r\n.
  126. # We don't restrict chars above \x7f as putrequest() limits us to ASCII.
  127. _contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f]')
  128. # Arguably only these _should_ allowed:
  129. # _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
  130. # We are more lenient for assumed real world compatibility purposes.
  131. # These characters are not allowed within HTTP method names
  132. # to prevent http header injection.
  133. _contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
  134. # We always set the Content-Length header for these methods because some
  135. # servers will otherwise respond with a 411
  136. _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
  137. def _encode(data, name='data'):
  138. """Call data.encode("latin-1") but show a better error message."""
  139. try:
  140. return data.encode("latin-1")
  141. except UnicodeEncodeError as err:
  142. raise UnicodeEncodeError(
  143. err.encoding,
  144. err.object,
  145. err.start,
  146. err.end,
  147. "%s (%.20r) is not valid Latin-1. Use %s.encode('utf-8') "
  148. "if you want to send it encoded in UTF-8." %
  149. (name.title(), data[err.start:err.end], name)) from None
  150. class HTTPMessage(email.message.Message):
  151. # XXX The only usage of this method is in
  152. # http.server.CGIHTTPRequestHandler. Maybe move the code there so
  153. # that it doesn't need to be part of the public API. The API has
  154. # never been defined so this could cause backwards compatibility
  155. # issues.
  156. def getallmatchingheaders(self, name):
  157. """Find all header lines matching a given header name.
  158. Look through the list of headers and find all lines matching a given
  159. header name (and their continuation lines). A list of the lines is
  160. returned, without interpretation. If the header does not occur, an
  161. empty list is returned. If the header occurs multiple times, all
  162. occurrences are returned. Case is not important in the header name.
  163. """
  164. name = name.lower() + ':'
  165. n = len(name)
  166. lst = []
  167. hit = 0
  168. for line in self.keys():
  169. if line[:n].lower() == name:
  170. hit = 1
  171. elif not line[:1].isspace():
  172. hit = 0
  173. if hit:
  174. lst.append(line)
  175. return lst
  176. def _read_headers(fp):
  177. """Reads potential header lines into a list from a file pointer.
  178. Length of line is limited by _MAXLINE, and number of
  179. headers is limited by _MAXHEADERS.
  180. """
  181. headers = []
  182. while True:
  183. line = fp.readline(_MAXLINE + 1)
  184. if len(line) > _MAXLINE:
  185. raise LineTooLong("header line")
  186. headers.append(line)
  187. if len(headers) > _MAXHEADERS:
  188. raise HTTPException("got more than %d headers" % _MAXHEADERS)
  189. if line in (b'\r\n', b'\n', b''):
  190. break
  191. return headers
  192. def parse_headers(fp, _class=HTTPMessage):
  193. """Parses only RFC2822 headers from a file pointer.
  194. email Parser wants to see strings rather than bytes.
  195. But a TextIOWrapper around self.rfile would buffer too many bytes
  196. from the stream, bytes which we later need to read as bytes.
  197. So we read the correct bytes here, as bytes, for email Parser
  198. to parse.
  199. """
  200. headers = _read_headers(fp)
  201. hstring = b''.join(headers).decode('iso-8859-1')
  202. return email.parser.Parser(_class=_class).parsestr(hstring)
  203. class HTTPResponse(io.BufferedIOBase):
  204. # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.
  205. # The bytes from the socket object are iso-8859-1 strings.
  206. # See RFC 2616 sec 2.2 which notes an exception for MIME-encoded
  207. # text following RFC 2047. The basic status line parsing only
  208. # accepts iso-8859-1.
  209. def __init__(self, sock, debuglevel=0, method=None, url=None):
  210. # If the response includes a content-length header, we need to
  211. # make sure that the client doesn't read more than the
  212. # specified number of bytes. If it does, it will block until
  213. # the server times out and closes the connection. This will
  214. # happen if a self.fp.read() is done (without a size) whether
  215. # self.fp is buffered or not. So, no self.fp.read() by
  216. # clients unless they know what they are doing.
  217. self.fp = sock.makefile("rb")
  218. self.debuglevel = debuglevel
  219. self._method = method
  220. # The HTTPResponse object is returned via urllib. The clients
  221. # of http and urllib expect different attributes for the
  222. # headers. headers is used here and supports urllib. msg is
  223. # provided as a backwards compatibility layer for http
  224. # clients.
  225. self.headers = self.msg = None
  226. # from the Status-Line of the response
  227. self.version = _UNKNOWN # HTTP-Version
  228. self.status = _UNKNOWN # Status-Code
  229. self.reason = _UNKNOWN # Reason-Phrase
  230. self.chunked = _UNKNOWN # is "chunked" being used?
  231. self.chunk_left = _UNKNOWN # bytes left to read in current chunk
  232. self.length = _UNKNOWN # number of bytes left in response
  233. self.will_close = _UNKNOWN # conn will close at end of response
  234. def _read_status(self):
  235. line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  236. if len(line) > _MAXLINE:
  237. raise LineTooLong("status line")
  238. if self.debuglevel > 0:
  239. print("reply:", repr(line))
  240. if not line:
  241. # Presumably, the server closed the connection before
  242. # sending a valid response.
  243. raise RemoteDisconnected("Remote end closed connection without"
  244. " response")
  245. try:
  246. version, status, reason = line.split(None, 2)
  247. except ValueError:
  248. try:
  249. version, status = line.split(None, 1)
  250. reason = ""
  251. except ValueError:
  252. # empty version will cause next test to fail.
  253. version = ""
  254. if not version.startswith("HTTP/"):
  255. self._close_conn()
  256. raise BadStatusLine(line)
  257. # The status code is a three-digit number
  258. try:
  259. status = int(status)
  260. if status < 100 or status > 999:
  261. raise BadStatusLine(line)
  262. except ValueError:
  263. raise BadStatusLine(line)
  264. return version, status, reason
  265. def begin(self):
  266. if self.headers is not None:
  267. # we've already started reading the response
  268. return
  269. # read until we get a non-100 response
  270. while True:
  271. version, status, reason = self._read_status()
  272. if status != CONTINUE:
  273. break
  274. # skip the header from the 100 response
  275. skipped_headers = _read_headers(self.fp)
  276. if self.debuglevel > 0:
  277. print("headers:", skipped_headers)
  278. del skipped_headers
  279. self.code = self.status = status
  280. self.reason = reason.strip()
  281. if version in ("HTTP/1.0", "HTTP/0.9"):
  282. # Some servers might still return "0.9", treat it as 1.0 anyway
  283. self.version = 10
  284. elif version.startswith("HTTP/1."):
  285. self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1
  286. else:
  287. raise UnknownProtocol(version)
  288. self.headers = self.msg = parse_headers(self.fp)
  289. if self.debuglevel > 0:
  290. for hdr, val in self.headers.items():
  291. print("header:", hdr + ":", val)
  292. # are we using the chunked-style of transfer encoding?
  293. tr_enc = self.headers.get("transfer-encoding")
  294. if tr_enc and tr_enc.lower() == "chunked":
  295. self.chunked = True
  296. self.chunk_left = None
  297. else:
  298. self.chunked = False
  299. # will the connection close at the end of the response?
  300. self.will_close = self._check_close()
  301. # do we have a Content-Length?
  302. # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
  303. self.length = None
  304. length = self.headers.get("content-length")
  305. if length and not self.chunked:
  306. try:
  307. self.length = int(length)
  308. except ValueError:
  309. self.length = None
  310. else:
  311. if self.length < 0: # ignore nonsensical negative lengths
  312. self.length = None
  313. else:
  314. self.length = None
  315. # does the body have a fixed length? (of zero)
  316. if (status == NO_CONTENT or status == NOT_MODIFIED or
  317. 100 <= status < 200 or # 1xx codes
  318. self._method == "HEAD"):
  319. self.length = 0
  320. # if the connection remains open, and we aren't using chunked, and
  321. # a content-length was not provided, then assume that the connection
  322. # WILL close.
  323. if (not self.will_close and
  324. not self.chunked and
  325. self.length is None):
  326. self.will_close = True
  327. def _check_close(self):
  328. conn = self.headers.get("connection")
  329. if self.version == 11:
  330. # An HTTP/1.1 proxy is assumed to stay open unless
  331. # explicitly closed.
  332. if conn and "close" in conn.lower():
  333. return True
  334. return False
  335. # Some HTTP/1.0 implementations have support for persistent
  336. # connections, using rules different than HTTP/1.1.
  337. # For older HTTP, Keep-Alive indicates persistent connection.
  338. if self.headers.get("keep-alive"):
  339. return False
  340. # At least Akamai returns a "Connection: Keep-Alive" header,
  341. # which was supposed to be sent by the client.
  342. if conn and "keep-alive" in conn.lower():
  343. return False
  344. # Proxy-Connection is a netscape hack.
  345. pconn = self.headers.get("proxy-connection")
  346. if pconn and "keep-alive" in pconn.lower():
  347. return False
  348. # otherwise, assume it will close
  349. return True
  350. def _close_conn(self):
  351. fp = self.fp
  352. self.fp = None
  353. fp.close()
  354. def close(self):
  355. try:
  356. super().close() # set "closed" flag
  357. finally:
  358. if self.fp:
  359. self._close_conn()
  360. # These implementations are for the benefit of io.BufferedReader.
  361. # XXX This class should probably be revised to act more like
  362. # the "raw stream" that BufferedReader expects.
  363. def flush(self):
  364. super().flush()
  365. if self.fp:
  366. self.fp.flush()
  367. def readable(self):
  368. """Always returns True"""
  369. return True
  370. # End of "raw stream" methods
  371. def isclosed(self):
  372. """True if the connection is closed."""
  373. # NOTE: it is possible that we will not ever call self.close(). This
  374. # case occurs when will_close is TRUE, length is None, and we
  375. # read up to the last byte, but NOT past it.
  376. #
  377. # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be
  378. # called, meaning self.isclosed() is meaningful.
  379. return self.fp is None
  380. def read(self, amt=None):
  381. if self.fp is None:
  382. return b""
  383. if self._method == "HEAD":
  384. self._close_conn()
  385. return b""
  386. if self.chunked:
  387. return self._read_chunked(amt)
  388. if amt is not None:
  389. if self.length is not None and amt > self.length:
  390. # clip the read to the "end of response"
  391. amt = self.length
  392. s = self.fp.read(amt)
  393. if not s and amt:
  394. # Ideally, we would raise IncompleteRead if the content-length
  395. # wasn't satisfied, but it might break compatibility.
  396. self._close_conn()
  397. elif self.length is not None:
  398. self.length -= len(s)
  399. if not self.length:
  400. self._close_conn()
  401. return s
  402. else:
  403. # Amount is not given (unbounded read) so we must check self.length
  404. if self.length is None:
  405. s = self.fp.read()
  406. else:
  407. try:
  408. s = self._safe_read(self.length)
  409. except IncompleteRead:
  410. self._close_conn()
  411. raise
  412. self.length = 0
  413. self._close_conn() # we read everything
  414. return s
  415. def readinto(self, b):
  416. """Read up to len(b) bytes into bytearray b and return the number
  417. of bytes read.
  418. """
  419. if self.fp is None:
  420. return 0
  421. if self._method == "HEAD":
  422. self._close_conn()
  423. return 0
  424. if self.chunked:
  425. return self._readinto_chunked(b)
  426. if self.length is not None:
  427. if len(b) > self.length:
  428. # clip the read to the "end of response"
  429. b = memoryview(b)[0:self.length]
  430. # we do not use _safe_read() here because this may be a .will_close
  431. # connection, and the user is reading more bytes than will be provided
  432. # (for example, reading in 1k chunks)
  433. n = self.fp.readinto(b)
  434. if not n and b:
  435. # Ideally, we would raise IncompleteRead if the content-length
  436. # wasn't satisfied, but it might break compatibility.
  437. self._close_conn()
  438. elif self.length is not None:
  439. self.length -= n
  440. if not self.length:
  441. self._close_conn()
  442. return n
  443. def _read_next_chunk_size(self):
  444. # Read the next chunk size from the file
  445. line = self.fp.readline(_MAXLINE + 1)
  446. if len(line) > _MAXLINE:
  447. raise LineTooLong("chunk size")
  448. i = line.find(b";")
  449. if i >= 0:
  450. line = line[:i] # strip chunk-extensions
  451. try:
  452. return int(line, 16)
  453. except ValueError:
  454. # close the connection as protocol synchronisation is
  455. # probably lost
  456. self._close_conn()
  457. raise
  458. def _read_and_discard_trailer(self):
  459. # read and discard trailer up to the CRLF terminator
  460. ### note: we shouldn't have any trailers!
  461. while True:
  462. line = self.fp.readline(_MAXLINE + 1)
  463. if len(line) > _MAXLINE:
  464. raise LineTooLong("trailer line")
  465. if not line:
  466. # a vanishingly small number of sites EOF without
  467. # sending the trailer
  468. break
  469. if line in (b'\r\n', b'\n', b''):
  470. break
  471. def _get_chunk_left(self):
  472. # return self.chunk_left, reading a new chunk if necessary.
  473. # chunk_left == 0: at the end of the current chunk, need to close it
  474. # chunk_left == None: No current chunk, should read next.
  475. # This function returns non-zero or None if the last chunk has
  476. # been read.
  477. chunk_left = self.chunk_left
  478. if not chunk_left: # Can be 0 or None
  479. if chunk_left is not None:
  480. # We are at the end of chunk, discard chunk end
  481. self._safe_read(2) # toss the CRLF at the end of the chunk
  482. try:
  483. chunk_left = self._read_next_chunk_size()
  484. except ValueError:
  485. raise IncompleteRead(b'')
  486. if chunk_left == 0:
  487. # last chunk: 1*("0") [ chunk-extension ] CRLF
  488. self._read_and_discard_trailer()
  489. # we read everything; close the "file"
  490. self._close_conn()
  491. chunk_left = None
  492. self.chunk_left = chunk_left
  493. return chunk_left
  494. def _read_chunked(self, amt=None):
  495. assert self.chunked != _UNKNOWN
  496. value = []
  497. try:
  498. while True:
  499. chunk_left = self._get_chunk_left()
  500. if chunk_left is None:
  501. break
  502. if amt is not None and amt <= chunk_left:
  503. value.append(self._safe_read(amt))
  504. self.chunk_left = chunk_left - amt
  505. break
  506. value.append(self._safe_read(chunk_left))
  507. if amt is not None:
  508. amt -= chunk_left
  509. self.chunk_left = 0
  510. return b''.join(value)
  511. except IncompleteRead:
  512. raise IncompleteRead(b''.join(value))
  513. def _readinto_chunked(self, b):
  514. assert self.chunked != _UNKNOWN
  515. total_bytes = 0
  516. mvb = memoryview(b)
  517. try:
  518. while True:
  519. chunk_left = self._get_chunk_left()
  520. if chunk_left is None:
  521. return total_bytes
  522. if len(mvb) <= chunk_left:
  523. n = self._safe_readinto(mvb)
  524. self.chunk_left = chunk_left - n
  525. return total_bytes + n
  526. temp_mvb = mvb[:chunk_left]
  527. n = self._safe_readinto(temp_mvb)
  528. mvb = mvb[n:]
  529. total_bytes += n
  530. self.chunk_left = 0
  531. except IncompleteRead:
  532. raise IncompleteRead(bytes(b[0:total_bytes]))
  533. def _safe_read(self, amt):
  534. """Read the number of bytes requested.
  535. This function should be used when <amt> bytes "should" be present for
  536. reading. If the bytes are truly not available (due to EOF), then the
  537. IncompleteRead exception can be used to detect the problem.
  538. """
  539. data = self.fp.read(amt)
  540. if len(data) < amt:
  541. raise IncompleteRead(data, amt-len(data))
  542. return data
  543. def _safe_readinto(self, b):
  544. """Same as _safe_read, but for reading into a buffer."""
  545. amt = len(b)
  546. n = self.fp.readinto(b)
  547. if n < amt:
  548. raise IncompleteRead(bytes(b[:n]), amt-n)
  549. return n
  550. def read1(self, n=-1):
  551. """Read with at most one underlying system call. If at least one
  552. byte is buffered, return that instead.
  553. """
  554. if self.fp is None or self._method == "HEAD":
  555. return b""
  556. if self.chunked:
  557. return self._read1_chunked(n)
  558. if self.length is not None and (n < 0 or n > self.length):
  559. n = self.length
  560. result = self.fp.read1(n)
  561. if not result and n:
  562. self._close_conn()
  563. elif self.length is not None:
  564. self.length -= len(result)
  565. return result
  566. def peek(self, n=-1):
  567. # Having this enables IOBase.readline() to read more than one
  568. # byte at a time
  569. if self.fp is None or self._method == "HEAD":
  570. return b""
  571. if self.chunked:
  572. return self._peek_chunked(n)
  573. return self.fp.peek(n)
  574. def readline(self, limit=-1):
  575. if self.fp is None or self._method == "HEAD":
  576. return b""
  577. if self.chunked:
  578. # Fallback to IOBase readline which uses peek() and read()
  579. return super().readline(limit)
  580. if self.length is not None and (limit < 0 or limit > self.length):
  581. limit = self.length
  582. result = self.fp.readline(limit)
  583. if not result and limit:
  584. self._close_conn()
  585. elif self.length is not None:
  586. self.length -= len(result)
  587. return result
  588. def _read1_chunked(self, n):
  589. # Strictly speaking, _get_chunk_left() may cause more than one read,
  590. # but that is ok, since that is to satisfy the chunked protocol.
  591. chunk_left = self._get_chunk_left()
  592. if chunk_left is None or n == 0:
  593. return b''
  594. if not (0 <= n <= chunk_left):
  595. n = chunk_left # if n is negative or larger than chunk_left
  596. read = self.fp.read1(n)
  597. self.chunk_left -= len(read)
  598. if not read:
  599. raise IncompleteRead(b"")
  600. return read
  601. def _peek_chunked(self, n):
  602. # Strictly speaking, _get_chunk_left() may cause more than one read,
  603. # but that is ok, since that is to satisfy the chunked protocol.
  604. try:
  605. chunk_left = self._get_chunk_left()
  606. except IncompleteRead:
  607. return b'' # peek doesn't worry about protocol
  608. if chunk_left is None:
  609. return b'' # eof
  610. # peek is allowed to return more than requested. Just request the
  611. # entire chunk, and truncate what we get.
  612. return self.fp.peek(chunk_left)[:chunk_left]
  613. def fileno(self):
  614. return self.fp.fileno()
  615. def getheader(self, name, default=None):
  616. '''Returns the value of the header matching *name*.
  617. If there are multiple matching headers, the values are
  618. combined into a single string separated by commas and spaces.
  619. If no matching header is found, returns *default* or None if
  620. the *default* is not specified.
  621. If the headers are unknown, raises http.client.ResponseNotReady.
  622. '''
  623. if self.headers is None:
  624. raise ResponseNotReady()
  625. headers = self.headers.get_all(name) or default
  626. if isinstance(headers, str) or not hasattr(headers, '__iter__'):
  627. return headers
  628. else:
  629. return ', '.join(headers)
  630. def getheaders(self):
  631. """Return list of (header, value) tuples."""
  632. if self.headers is None:
  633. raise ResponseNotReady()
  634. return list(self.headers.items())
  635. # We override IOBase.__iter__ so that it doesn't check for closed-ness
  636. def __iter__(self):
  637. return self
  638. # For compatibility with old-style urllib responses.
  639. def info(self):
  640. '''Returns an instance of the class mimetools.Message containing
  641. meta-information associated with the URL.
  642. When the method is HTTP, these headers are those returned by
  643. the server at the head of the retrieved HTML page (including
  644. Content-Length and Content-Type).
  645. When the method is FTP, a Content-Length header will be
  646. present if (as is now usual) the server passed back a file
  647. length in response to the FTP retrieval request. A
  648. Content-Type header will be present if the MIME type can be
  649. guessed.
  650. When the method is local-file, returned headers will include
  651. a Date representing the file's last-modified time, a
  652. Content-Length giving file size, and a Content-Type
  653. containing a guess at the file's type. See also the
  654. description of the mimetools module.
  655. '''
  656. return self.headers
  657. def geturl(self):
  658. '''Return the real URL of the page.
  659. In some cases, the HTTP server redirects a client to another
  660. URL. The urlopen() function handles this transparently, but in
  661. some cases the caller needs to know which URL the client was
  662. redirected to. The geturl() method can be used to get at this
  663. redirected URL.
  664. '''
  665. return self.url
  666. def getcode(self):
  667. '''Return the HTTP status code that was sent with the response,
  668. or None if the URL is not an HTTP URL.
  669. '''
  670. return self.status
  671. class HTTPConnection:
  672. _http_vsn = 11
  673. _http_vsn_str = 'HTTP/1.1'
  674. response_class = HTTPResponse
  675. default_port = HTTP_PORT
  676. auto_open = 1
  677. debuglevel = 0
  678. @staticmethod
  679. def _is_textIO(stream):
  680. """Test whether a file-like object is a text or a binary stream.
  681. """
  682. return isinstance(stream, io.TextIOBase)
  683. @staticmethod
  684. def _get_content_length(body, method):
  685. """Get the content-length based on the body.
  686. If the body is None, we set Content-Length: 0 for methods that expect
  687. a body (RFC 7230, Section 3.3.2). We also set the Content-Length for
  688. any method if the body is a str or bytes-like object and not a file.
  689. """
  690. if body is None:
  691. # do an explicit check for not None here to distinguish
  692. # between unset and set but empty
  693. if method.upper() in _METHODS_EXPECTING_BODY:
  694. return 0
  695. else:
  696. return None
  697. if hasattr(body, 'read'):
  698. # file-like object.
  699. return None
  700. try:
  701. # does it implement the buffer protocol (bytes, bytearray, array)?
  702. mv = memoryview(body)
  703. return mv.nbytes
  704. except TypeError:
  705. pass
  706. if isinstance(body, str):
  707. return len(body)
  708. return None
  709. def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
  710. source_address=None, blocksize=8192):
  711. self.timeout = timeout
  712. self.source_address = source_address
  713. self.blocksize = blocksize
  714. self.sock = None
  715. self._buffer = []
  716. self.__response = None
  717. self.__state = _CS_IDLE
  718. self._method = None
  719. self._tunnel_host = None
  720. self._tunnel_port = None
  721. self._tunnel_headers = {}
  722. (self.host, self.port) = self._get_hostport(host, port)
  723. self._validate_host(self.host)
  724. # This is stored as an instance variable to allow unit
  725. # tests to replace it with a suitable mockup
  726. self._create_connection = socket.create_connection
  727. def set_tunnel(self, host, port=None, headers=None):
  728. """Set up host and port for HTTP CONNECT tunnelling.
  729. In a connection that uses HTTP CONNECT tunneling, the host passed to the
  730. constructor is used as a proxy server that relays all communication to
  731. the endpoint passed to `set_tunnel`. This done by sending an HTTP
  732. CONNECT request to the proxy server when the connection is established.
  733. This method must be called before the HTTP connection has been
  734. established.
  735. The headers argument should be a mapping of extra HTTP headers to send
  736. with the CONNECT request.
  737. """
  738. if self.sock:
  739. raise RuntimeError("Can't set up tunnel for established connection")
  740. self._tunnel_host, self._tunnel_port = self._get_hostport(host, port)
  741. if headers:
  742. self._tunnel_headers = headers
  743. else:
  744. self._tunnel_headers.clear()
  745. def _get_hostport(self, host, port):
  746. if port is None:
  747. i = host.rfind(':')
  748. j = host.rfind(']') # ipv6 addresses have [...]
  749. if i > j:
  750. try:
  751. port = int(host[i+1:])
  752. except ValueError:
  753. if host[i+1:] == "": # http://foo.com:/ == http://foo.com/
  754. port = self.default_port
  755. else:
  756. raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
  757. host = host[:i]
  758. else:
  759. port = self.default_port
  760. if host and host[0] == '[' and host[-1] == ']':
  761. host = host[1:-1]
  762. return (host, port)
  763. def set_debuglevel(self, level):
  764. self.debuglevel = level
  765. def _tunnel(self):
  766. connect = b"CONNECT %s:%d HTTP/1.0\r\n" % (
  767. self._tunnel_host.encode("ascii"), self._tunnel_port)
  768. headers = [connect]
  769. for header, value in self._tunnel_headers.items():
  770. headers.append(f"{header}: {value}\r\n".encode("latin-1"))
  771. headers.append(b"\r\n")
  772. # Making a single send() call instead of one per line encourages
  773. # the host OS to use a more optimal packet size instead of
  774. # potentially emitting a series of small packets.
  775. self.send(b"".join(headers))
  776. del headers
  777. response = self.response_class(self.sock, method=self._method)
  778. (version, code, message) = response._read_status()
  779. if code != http.HTTPStatus.OK:
  780. self.close()
  781. raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
  782. while True:
  783. line = response.fp.readline(_MAXLINE + 1)
  784. if len(line) > _MAXLINE:
  785. raise LineTooLong("header line")
  786. if not line:
  787. # for sites which EOF without sending a trailer
  788. break
  789. if line in (b'\r\n', b'\n', b''):
  790. break
  791. if self.debuglevel > 0:
  792. print('header:', line.decode())
  793. def connect(self):
  794. """Connect to the host and port specified in __init__."""
  795. sys.audit("http.client.connect", self, self.host, self.port)
  796. self.sock = self._create_connection(
  797. (self.host,self.port), self.timeout, self.source_address)
  798. self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  799. if self._tunnel_host:
  800. self._tunnel()
  801. def close(self):
  802. """Close the connection to the HTTP server."""
  803. self.__state = _CS_IDLE
  804. try:
  805. sock = self.sock
  806. if sock:
  807. self.sock = None
  808. sock.close() # close it manually... there may be other refs
  809. finally:
  810. response = self.__response
  811. if response:
  812. self.__response = None
  813. response.close()
  814. def send(self, data):
  815. """Send `data' to the server.
  816. ``data`` can be a string object, a bytes object, an array object, a
  817. file-like object that supports a .read() method, or an iterable object.
  818. """
  819. if self.sock is None:
  820. if self.auto_open:
  821. self.connect()
  822. else:
  823. raise NotConnected()
  824. if self.debuglevel > 0:
  825. print("send:", repr(data))
  826. if hasattr(data, "read") :
  827. if self.debuglevel > 0:
  828. print("sendIng a read()able")
  829. encode = self._is_textIO(data)
  830. if encode and self.debuglevel > 0:
  831. print("encoding file using iso-8859-1")
  832. while 1:
  833. datablock = data.read(self.blocksize)
  834. if not datablock:
  835. break
  836. if encode:
  837. datablock = datablock.encode("iso-8859-1")
  838. sys.audit("http.client.send", self, datablock)
  839. self.sock.sendall(datablock)
  840. return
  841. sys.audit("http.client.send", self, data)
  842. try:
  843. self.sock.sendall(data)
  844. except TypeError:
  845. if isinstance(data, collections.abc.Iterable):
  846. for d in data:
  847. self.sock.sendall(d)
  848. else:
  849. raise TypeError("data should be a bytes-like object "
  850. "or an iterable, got %r" % type(data))
  851. def _output(self, s):
  852. """Add a line of output to the current request buffer.
  853. Assumes that the line does *not* end with \\r\\n.
  854. """
  855. self._buffer.append(s)
  856. def _read_readable(self, readable):
  857. if self.debuglevel > 0:
  858. print("sendIng a read()able")
  859. encode = self._is_textIO(readable)
  860. if encode and self.debuglevel > 0:
  861. print("encoding file using iso-8859-1")
  862. while True:
  863. datablock = readable.read(self.blocksize)
  864. if not datablock:
  865. break
  866. if encode:
  867. datablock = datablock.encode("iso-8859-1")
  868. yield datablock
  869. def _send_output(self, message_body=None, encode_chunked=False):
  870. """Send the currently buffered request and clear the buffer.
  871. Appends an extra \\r\\n to the buffer.
  872. A message_body may be specified, to be appended to the request.
  873. """
  874. self._buffer.extend((b"", b""))
  875. msg = b"\r\n".join(self._buffer)
  876. del self._buffer[:]
  877. self.send(msg)
  878. if message_body is not None:
  879. # create a consistent interface to message_body
  880. if hasattr(message_body, 'read'):
  881. # Let file-like take precedence over byte-like. This
  882. # is needed to allow the current position of mmap'ed
  883. # files to be taken into account.
  884. chunks = self._read_readable(message_body)
  885. else:
  886. try:
  887. # this is solely to check to see if message_body
  888. # implements the buffer API. it /would/ be easier
  889. # to capture if PyObject_CheckBuffer was exposed
  890. # to Python.
  891. memoryview(message_body)
  892. except TypeError:
  893. try:
  894. chunks = iter(message_body)
  895. except TypeError:
  896. raise TypeError("message_body should be a bytes-like "
  897. "object or an iterable, got %r"
  898. % type(message_body))
  899. else:
  900. # the object implements the buffer interface and
  901. # can be passed directly into socket methods
  902. chunks = (message_body,)
  903. for chunk in chunks:
  904. if not chunk:
  905. if self.debuglevel > 0:
  906. print('Zero length chunk ignored')
  907. continue
  908. if encode_chunked and self._http_vsn == 11:
  909. # chunked encoding
  910. chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \
  911. + b'\r\n'
  912. self.send(chunk)
  913. if encode_chunked and self._http_vsn == 11:
  914. # end chunked transfer
  915. self.send(b'0\r\n\r\n')
  916. def putrequest(self, method, url, skip_host=False,
  917. skip_accept_encoding=False):
  918. """Send a request to the server.
  919. `method' specifies an HTTP request method, e.g. 'GET'.
  920. `url' specifies the object being requested, e.g. '/index.html'.
  921. `skip_host' if True does not add automatically a 'Host:' header
  922. `skip_accept_encoding' if True does not add automatically an
  923. 'Accept-Encoding:' header
  924. """
  925. # if a prior response has been completed, then forget about it.
  926. if self.__response and self.__response.isclosed():
  927. self.__response = None
  928. # in certain cases, we cannot issue another request on this connection.
  929. # this occurs when:
  930. # 1) we are in the process of sending a request. (_CS_REQ_STARTED)
  931. # 2) a response to a previous request has signalled that it is going
  932. # to close the connection upon completion.
  933. # 3) the headers for the previous response have not been read, thus
  934. # we cannot determine whether point (2) is true. (_CS_REQ_SENT)
  935. #
  936. # if there is no prior response, then we can request at will.
  937. #
  938. # if point (2) is true, then we will have passed the socket to the
  939. # response (effectively meaning, "there is no prior response"), and
  940. # will open a new one when a new request is made.
  941. #
  942. # Note: if a prior response exists, then we *can* start a new request.
  943. # We are not allowed to begin fetching the response to this new
  944. # request, however, until that prior response is complete.
  945. #
  946. if self.__state == _CS_IDLE:
  947. self.__state = _CS_REQ_STARTED
  948. else:
  949. raise CannotSendRequest(self.__state)
  950. self._validate_method(method)
  951. # Save the method for use later in the response phase
  952. self._method = method
  953. url = url or '/'
  954. self._validate_path(url)
  955. request = '%s %s %s' % (method, url, self._http_vsn_str)
  956. self._output(self._encode_request(request))
  957. if self._http_vsn == 11:
  958. # Issue some standard headers for better HTTP/1.1 compliance
  959. if not skip_host:
  960. # this header is issued *only* for HTTP/1.1
  961. # connections. more specifically, this means it is
  962. # only issued when the client uses the new
  963. # HTTPConnection() class. backwards-compat clients
  964. # will be using HTTP/1.0 and those clients may be
  965. # issuing this header themselves. we should NOT issue
  966. # it twice; some web servers (such as Apache) barf
  967. # when they see two Host: headers
  968. # If we need a non-standard port,include it in the
  969. # header. If the request is going through a proxy,
  970. # but the host of the actual URL, not the host of the
  971. # proxy.
  972. netloc = ''
  973. if url.startswith('http'):
  974. nil, netloc, nil, nil, nil = urlsplit(url)
  975. if netloc:
  976. try:
  977. netloc_enc = netloc.encode("ascii")
  978. except UnicodeEncodeError:
  979. netloc_enc = netloc.encode("idna")
  980. self.putheader('Host', netloc_enc)
  981. else:
  982. if self._tunnel_host:
  983. host = self._tunnel_host
  984. port = self._tunnel_port
  985. else:
  986. host = self.host
  987. port = self.port
  988. try:
  989. host_enc = host.encode("ascii")
  990. except UnicodeEncodeError:
  991. host_enc = host.encode("idna")
  992. # As per RFC 273, IPv6 address should be wrapped with []
  993. # when used as Host header
  994. if host.find(':') >= 0:
  995. host_enc = b'[' + host_enc + b']'
  996. if port == self.default_port:
  997. self.putheader('Host', host_enc)
  998. else:
  999. host_enc = host_enc.decode("ascii")
  1000. self.putheader('Host', "%s:%s" % (host_enc, port))
  1001. # note: we are assuming that clients will not attempt to set these
  1002. # headers since *this* library must deal with the
  1003. # consequences. this also means that when the supporting
  1004. # libraries are updated to recognize other forms, then this
  1005. # code should be changed (removed or updated).
  1006. # we only want a Content-Encoding of "identity" since we don't
  1007. # support encodings such as x-gzip or x-deflate.
  1008. if not skip_accept_encoding:
  1009. self.putheader('Accept-Encoding', 'identity')
  1010. # we can accept "chunked" Transfer-Encodings, but no others
  1011. # NOTE: no TE header implies *only* "chunked"
  1012. #self.putheader('TE', 'chunked')
  1013. # if TE is supplied in the header, then it must appear in a
  1014. # Connection header.
  1015. #self.putheader('Connection', 'TE')
  1016. else:
  1017. # For HTTP/1.0, the server will assume "not chunked"
  1018. pass
  1019. def _encode_request(self, request):
  1020. # ASCII also helps prevent CVE-2019-9740.
  1021. return request.encode('ascii')
  1022. def _validate_method(self, method):
  1023. """Validate a method name for putrequest."""
  1024. # prevent http header injection
  1025. match = _contains_disallowed_method_pchar_re.search(method)
  1026. if match:
  1027. raise ValueError(
  1028. f"method can't contain control characters. {method!r} "
  1029. f"(found at least {match.group()!r})")
  1030. def _validate_path(self, url):
  1031. """Validate a url for putrequest."""
  1032. # Prevent CVE-2019-9740.
  1033. match = _contains_disallowed_url_pchar_re.search(url)
  1034. if match:
  1035. raise InvalidURL(f"URL can't contain control characters. {url!r} "
  1036. f"(found at least {match.group()!r})")
  1037. def _validate_host(self, host):
  1038. """Validate a host so it doesn't contain control characters."""
  1039. # Prevent CVE-2019-18348.
  1040. match = _contains_disallowed_url_pchar_re.search(host)
  1041. if match:
  1042. raise InvalidURL(f"URL can't contain control characters. {host!r} "
  1043. f"(found at least {match.group()!r})")
  1044. def putheader(self, header, *values):
  1045. """Send a request header line to the server.
  1046. For example: h.putheader('Accept', 'text/html')
  1047. """
  1048. if self.__state != _CS_REQ_STARTED:
  1049. raise CannotSendHeader()
  1050. if hasattr(header, 'encode'):
  1051. header = header.encode('ascii')
  1052. if not _is_legal_header_name(header):
  1053. raise ValueError('Invalid header name %r' % (header,))
  1054. values = list(values)
  1055. for i, one_value in enumerate(values):
  1056. if hasattr(one_value, 'encode'):
  1057. values[i] = one_value.encode('latin-1')
  1058. elif isinstance(one_value, int):
  1059. values[i] = str(one_value).encode('ascii')
  1060. if _is_illegal_header_value(values[i]):
  1061. raise ValueError('Invalid header value %r' % (values[i],))
  1062. value = b'\r\n\t'.join(values)
  1063. header = header + b': ' + value
  1064. self._output(header)
  1065. def endheaders(self, message_body=None, *, encode_chunked=False):
  1066. """Indicate that the last header line has been sent to the server.
  1067. This method sends the request to the server. The optional message_body
  1068. argument can be used to pass a message body associated with the
  1069. request.
  1070. """
  1071. if self.__state == _CS_REQ_STARTED:
  1072. self.__state = _CS_REQ_SENT
  1073. else:
  1074. raise CannotSendHeader()
  1075. self._send_output(message_body, encode_chunked=encode_chunked)
  1076. def request(self, method, url, body=None, headers={}, *,
  1077. encode_chunked=False):
  1078. """Send a complete request to the server."""
  1079. self._send_request(method, url, body, headers, encode_chunked)
  1080. def _send_request(self, method, url, body, headers, encode_chunked):
  1081. # Honor explicitly requested Host: and Accept-Encoding: headers.
  1082. header_names = frozenset(k.lower() for k in headers)
  1083. skips = {}
  1084. if 'host' in header_names:
  1085. skips['skip_host'] = 1
  1086. if 'accept-encoding' in header_names:
  1087. skips['skip_accept_encoding'] = 1
  1088. self.putrequest(method, url, **skips)
  1089. # chunked encoding will happen if HTTP/1.1 is used and either
  1090. # the caller passes encode_chunked=True or the following
  1091. # conditions hold:
  1092. # 1. content-length has not been explicitly set
  1093. # 2. the body is a file or iterable, but not a str or bytes-like
  1094. # 3. Transfer-Encoding has NOT been explicitly set by the caller
  1095. if 'content-length' not in header_names:
  1096. # only chunk body if not explicitly set for backwards
  1097. # compatibility, assuming the client code is already handling the
  1098. # chunking
  1099. if 'transfer-encoding' not in header_names:
  1100. # if content-length cannot be automatically determined, fall
  1101. # back to chunked encoding
  1102. encode_chunked = False
  1103. content_length = self._get_content_length(body, method)
  1104. if content_length is None:
  1105. if body is not None:
  1106. if self.debuglevel > 0:
  1107. print('Unable to determine size of %r' % body)
  1108. encode_chunked = True
  1109. self.putheader('Transfer-Encoding', 'chunked')
  1110. else:
  1111. self.putheader('Content-Length', str(content_length))
  1112. else:
  1113. encode_chunked = False
  1114. for hdr, value in headers.items():
  1115. self.putheader(hdr, value)
  1116. if isinstance(body, str):
  1117. # RFC 2616 Section 3.7.1 says that text default has a
  1118. # default charset of iso-8859-1.
  1119. body = _encode(body, 'body')
  1120. self.endheaders(body, encode_chunked=encode_chunked)
  1121. def getresponse(self):
  1122. """Get the response from the server.
  1123. If the HTTPConnection is in the correct state, returns an
  1124. instance of HTTPResponse or of whatever object is returned by
  1125. the response_class variable.
  1126. If a request has not been sent or if a previous response has
  1127. not be handled, ResponseNotReady is raised. If the HTTP
  1128. response indicates that the connection should be closed, then
  1129. it will be closed before the response is returned. When the
  1130. connection is closed, the underlying socket is closed.
  1131. """
  1132. # if a prior response has been completed, then forget about it.
  1133. if self.__response and self.__response.isclosed():
  1134. self.__response = None
  1135. # if a prior response exists, then it must be completed (otherwise, we
  1136. # cannot read this response's header to determine the connection-close
  1137. # behavior)
  1138. #
  1139. # note: if a prior response existed, but was connection-close, then the
  1140. # socket and response were made independent of this HTTPConnection
  1141. # object since a new request requires that we open a whole new
  1142. # connection
  1143. #
  1144. # this means the prior response had one of two states:
  1145. # 1) will_close: this connection was reset and the prior socket and
  1146. # response operate independently
  1147. # 2) persistent: the response was retained and we await its
  1148. # isclosed() status to become true.
  1149. #
  1150. if self.__state != _CS_REQ_SENT or self.__response:
  1151. raise ResponseNotReady(self.__state)
  1152. if self.debuglevel > 0:
  1153. response = self.response_class(self.sock, self.debuglevel,
  1154. method=self._method)
  1155. else:
  1156. response = self.response_class(self.sock, method=self._method)
  1157. try:
  1158. try:
  1159. response.begin()
  1160. except ConnectionError:
  1161. self.close()
  1162. raise
  1163. assert response.will_close != _UNKNOWN
  1164. self.__state = _CS_IDLE
  1165. if response.will_close:
  1166. # this effectively passes the connection to the response
  1167. self.close()
  1168. else:
  1169. # remember this, so we can tell when it is complete
  1170. self.__response = response
  1171. return response
  1172. except:
  1173. response.close()
  1174. raise
  1175. try:
  1176. import ssl
  1177. except ImportError:
  1178. pass
  1179. else:
  1180. class HTTPSConnection(HTTPConnection):
  1181. "This class allows communication via SSL."
  1182. default_port = HTTPS_PORT
  1183. # XXX Should key_file and cert_file be deprecated in favour of context?
  1184. def __init__(self, host, port=None, key_file=None, cert_file=None,
  1185. timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
  1186. source_address=None, *, context=None,
  1187. check_hostname=None, blocksize=8192):
  1188. super(HTTPSConnection, self).__init__(host, port, timeout,
  1189. source_address,
  1190. blocksize=blocksize)
  1191. if (key_file is not None or cert_file is not None or
  1192. check_hostname is not None):
  1193. import warnings
  1194. warnings.warn("key_file, cert_file and check_hostname are "
  1195. "deprecated, use a custom context instead.",
  1196. DeprecationWarning, 2)
  1197. self.key_file = key_file
  1198. self.cert_file = cert_file
  1199. if context is None:
  1200. context = ssl._create_default_https_context()
  1201. # send ALPN extension to indicate HTTP/1.1 protocol
  1202. if self._http_vsn == 11:
  1203. context.set_alpn_protocols(['http/1.1'])
  1204. # enable PHA for TLS 1.3 connections if available
  1205. if context.post_handshake_auth is not None:
  1206. context.post_handshake_auth = True
  1207. will_verify = context.verify_mode != ssl.CERT_NONE
  1208. if check_hostname is None:
  1209. check_hostname = context.check_hostname
  1210. if check_hostname and not will_verify:
  1211. raise ValueError("check_hostname needs a SSL context with "
  1212. "either CERT_OPTIONAL or CERT_REQUIRED")
  1213. if key_file or cert_file:
  1214. context.load_cert_chain(cert_file, key_file)
  1215. # cert and key file means the user wants to authenticate.
  1216. # enable TLS 1.3 PHA implicitly even for custom contexts.
  1217. if context.post_handshake_auth is not None:
  1218. context.post_handshake_auth = True
  1219. self._context = context
  1220. if check_hostname is not None:
  1221. self._context.check_hostname = check_hostname
  1222. def connect(self):
  1223. "Connect to a host on a given (SSL) port."
  1224. super().connect()
  1225. if self._tunnel_host:
  1226. server_hostname = self._tunnel_host
  1227. else:
  1228. server_hostname = self.host
  1229. self.sock = self._context.wrap_socket(self.sock,
  1230. server_hostname=server_hostname)
  1231. __all__.append("HTTPSConnection")
  1232. class HTTPException(Exception):
  1233. # Subclasses that define an __init__ must call Exception.__init__
  1234. # or define self.args. Otherwise, str() will fail.
  1235. pass
  1236. class NotConnected(HTTPException):
  1237. pass
  1238. class InvalidURL(HTTPException):
  1239. pass
  1240. class UnknownProtocol(HTTPException):
  1241. def __init__(self, version):
  1242. self.args = version,
  1243. self.version = version
  1244. class UnknownTransferEncoding(HTTPException):
  1245. pass
  1246. class UnimplementedFileMode(HTTPException):
  1247. pass
  1248. class IncompleteRead(HTTPException):
  1249. def __init__(self, partial, expected=None):
  1250. self.args = partial,
  1251. self.partial = partial
  1252. self.expected = expected
  1253. def __repr__(self):
  1254. if self.expected is not None:
  1255. e = ', %i more expected' % self.expected
  1256. else:
  1257. e = ''
  1258. return '%s(%i bytes read%s)' % (self.__class__.__name__,
  1259. len(self.partial), e)
  1260. __str__ = object.__str__
  1261. class ImproperConnectionState(HTTPException):
  1262. pass
  1263. class CannotSendRequest(ImproperConnectionState):
  1264. pass
  1265. class CannotSendHeader(ImproperConnectionState):
  1266. pass
  1267. class ResponseNotReady(ImproperConnectionState):
  1268. pass
  1269. class BadStatusLine(HTTPException):
  1270. def __init__(self, line):
  1271. if not line:
  1272. line = repr(line)
  1273. self.args = line,
  1274. self.line = line
  1275. class LineTooLong(HTTPException):
  1276. def __init__(self, line_type):
  1277. HTTPException.__init__(self, "got more than %d bytes when reading %s"
  1278. % (_MAXLINE, line_type))
  1279. class RemoteDisconnected(ConnectionResetError, BadStatusLine):
  1280. def __init__(self, *pos, **kw):
  1281. BadStatusLine.__init__(self, "")
  1282. ConnectionResetError.__init__(self, *pos, **kw)
  1283. # for backwards compatibility
  1284. error = HTTPException