You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1568 lines
57KB

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