http1connection.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. #
  2. # Copyright 2014 Facebook
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """Client and server implementations of HTTP/1.x.
  16. .. versionadded:: 4.0
  17. """
  18. import asyncio
  19. import logging
  20. import re
  21. import types
  22. from tornado.concurrent import (
  23. Future,
  24. future_add_done_callback,
  25. future_set_result_unless_cancelled,
  26. )
  27. from tornado.escape import native_str, utf8
  28. from tornado import gen
  29. from tornado import httputil
  30. from tornado import iostream
  31. from tornado.log import gen_log, app_log
  32. from tornado.util import GzipDecompressor
  33. from typing import cast, Optional, Type, Awaitable, Callable, Union, Tuple
  34. CR_OR_LF_RE = re.compile(b"\r|\n")
  35. class _QuietException(Exception):
  36. def __init__(self) -> None:
  37. pass
  38. class _ExceptionLoggingContext:
  39. """Used with the ``with`` statement when calling delegate methods to
  40. log any exceptions with the given logger. Any exceptions caught are
  41. converted to _QuietException
  42. """
  43. def __init__(self, logger: logging.Logger) -> None:
  44. self.logger = logger
  45. def __enter__(self) -> None:
  46. pass
  47. def __exit__(
  48. self,
  49. typ: "Optional[Type[BaseException]]",
  50. value: Optional[BaseException],
  51. tb: types.TracebackType,
  52. ) -> None:
  53. if value is not None:
  54. assert typ is not None
  55. # Let HTTPInputError pass through to higher-level handler
  56. if isinstance(value, httputil.HTTPInputError):
  57. return None
  58. self.logger.error("Uncaught exception", exc_info=(typ, value, tb))
  59. raise _QuietException
  60. class HTTP1ConnectionParameters:
  61. """Parameters for `.HTTP1Connection` and `.HTTP1ServerConnection`."""
  62. def __init__(
  63. self,
  64. no_keep_alive: bool = False,
  65. chunk_size: Optional[int] = None,
  66. max_header_size: Optional[int] = None,
  67. header_timeout: Optional[float] = None,
  68. max_body_size: Optional[int] = None,
  69. body_timeout: Optional[float] = None,
  70. decompress: bool = False,
  71. ) -> None:
  72. """
  73. :arg bool no_keep_alive: If true, always close the connection after
  74. one request.
  75. :arg int chunk_size: how much data to read into memory at once
  76. :arg int max_header_size: maximum amount of data for HTTP headers
  77. :arg float header_timeout: how long to wait for all headers (seconds)
  78. :arg int max_body_size: maximum amount of data for body
  79. :arg float body_timeout: how long to wait while reading body (seconds)
  80. :arg bool decompress: if true, decode incoming
  81. ``Content-Encoding: gzip``
  82. """
  83. self.no_keep_alive = no_keep_alive
  84. self.chunk_size = chunk_size or 65536
  85. self.max_header_size = max_header_size or 65536
  86. self.header_timeout = header_timeout
  87. self.max_body_size = max_body_size
  88. self.body_timeout = body_timeout
  89. self.decompress = decompress
  90. class HTTP1Connection(httputil.HTTPConnection):
  91. """Implements the HTTP/1.x protocol.
  92. This class can be on its own for clients, or via `HTTP1ServerConnection`
  93. for servers.
  94. """
  95. def __init__(
  96. self,
  97. stream: iostream.IOStream,
  98. is_client: bool,
  99. params: Optional[HTTP1ConnectionParameters] = None,
  100. context: Optional[object] = None,
  101. ) -> None:
  102. """
  103. :arg stream: an `.IOStream`
  104. :arg bool is_client: client or server
  105. :arg params: a `.HTTP1ConnectionParameters` instance or ``None``
  106. :arg context: an opaque application-defined object that can be accessed
  107. as ``connection.context``.
  108. """
  109. self.is_client = is_client
  110. self.stream = stream
  111. if params is None:
  112. params = HTTP1ConnectionParameters()
  113. self.params = params
  114. self.context = context
  115. self.no_keep_alive = params.no_keep_alive
  116. # The body limits can be altered by the delegate, so save them
  117. # here instead of just referencing self.params later.
  118. self._max_body_size = (
  119. self.params.max_body_size
  120. if self.params.max_body_size is not None
  121. else self.stream.max_buffer_size
  122. )
  123. self._body_timeout = self.params.body_timeout
  124. # _write_finished is set to True when finish() has been called,
  125. # i.e. there will be no more data sent. Data may still be in the
  126. # stream's write buffer.
  127. self._write_finished = False
  128. # True when we have read the entire incoming body.
  129. self._read_finished = False
  130. # _finish_future resolves when all data has been written and flushed
  131. # to the IOStream.
  132. self._finish_future = Future() # type: Future[None]
  133. # If true, the connection should be closed after this request
  134. # (after the response has been written in the server side,
  135. # and after it has been read in the client)
  136. self._disconnect_on_finish = False
  137. self._clear_callbacks()
  138. # Save the start lines after we read or write them; they
  139. # affect later processing (e.g. 304 responses and HEAD methods
  140. # have content-length but no bodies)
  141. self._request_start_line = None # type: Optional[httputil.RequestStartLine]
  142. self._response_start_line = None # type: Optional[httputil.ResponseStartLine]
  143. self._request_headers = None # type: Optional[httputil.HTTPHeaders]
  144. # True if we are writing output with chunked encoding.
  145. self._chunking_output = False
  146. # While reading a body with a content-length, this is the
  147. # amount left to read.
  148. self._expected_content_remaining = None # type: Optional[int]
  149. # A Future for our outgoing writes, returned by IOStream.write.
  150. self._pending_write = None # type: Optional[Future[None]]
  151. def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[bool]:
  152. """Read a single HTTP response.
  153. Typical client-mode usage is to write a request using `write_headers`,
  154. `write`, and `finish`, and then call ``read_response``.
  155. :arg delegate: a `.HTTPMessageDelegate`
  156. Returns a `.Future` that resolves to a bool after the full response has
  157. been read. The result is true if the stream is still open.
  158. """
  159. if self.params.decompress:
  160. delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)
  161. return self._read_message(delegate)
  162. async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:
  163. need_delegate_close = False
  164. try:
  165. header_future = self.stream.read_until_regex(
  166. b"\r?\n\r?\n", max_bytes=self.params.max_header_size
  167. )
  168. if self.params.header_timeout is None:
  169. header_data = await header_future
  170. else:
  171. try:
  172. header_data = await gen.with_timeout(
  173. self.stream.io_loop.time() + self.params.header_timeout,
  174. header_future,
  175. quiet_exceptions=iostream.StreamClosedError,
  176. )
  177. except gen.TimeoutError:
  178. self.close()
  179. return False
  180. start_line_str, headers = self._parse_headers(header_data)
  181. if self.is_client:
  182. resp_start_line = httputil.parse_response_start_line(start_line_str)
  183. self._response_start_line = resp_start_line
  184. start_line = (
  185. resp_start_line
  186. ) # type: Union[httputil.RequestStartLine, httputil.ResponseStartLine]
  187. # TODO: this will need to change to support client-side keepalive
  188. self._disconnect_on_finish = False
  189. else:
  190. req_start_line = httputil.parse_request_start_line(start_line_str)
  191. self._request_start_line = req_start_line
  192. self._request_headers = headers
  193. start_line = req_start_line
  194. self._disconnect_on_finish = not self._can_keep_alive(
  195. req_start_line, headers
  196. )
  197. need_delegate_close = True
  198. with _ExceptionLoggingContext(app_log):
  199. header_recv_future = delegate.headers_received(start_line, headers)
  200. if header_recv_future is not None:
  201. await header_recv_future
  202. if self.stream is None:
  203. # We've been detached.
  204. need_delegate_close = False
  205. return False
  206. skip_body = False
  207. if self.is_client:
  208. assert isinstance(start_line, httputil.ResponseStartLine)
  209. if (
  210. self._request_start_line is not None
  211. and self._request_start_line.method == "HEAD"
  212. ):
  213. skip_body = True
  214. code = start_line.code
  215. if code == 304:
  216. # 304 responses may include the content-length header
  217. # but do not actually have a body.
  218. # http://tools.ietf.org/html/rfc7230#section-3.3
  219. skip_body = True
  220. if 100 <= code < 200:
  221. # 1xx responses should never indicate the presence of
  222. # a body.
  223. if "Content-Length" in headers or "Transfer-Encoding" in headers:
  224. raise httputil.HTTPInputError(
  225. "Response code %d cannot have body" % code
  226. )
  227. # TODO: client delegates will get headers_received twice
  228. # in the case of a 100-continue. Document or change?
  229. await self._read_message(delegate)
  230. else:
  231. if headers.get("Expect") == "100-continue" and not self._write_finished:
  232. self.stream.write(b"HTTP/1.1 100 (Continue)\r\n\r\n")
  233. if not skip_body:
  234. body_future = self._read_body(
  235. resp_start_line.code if self.is_client else 0, headers, delegate
  236. )
  237. if body_future is not None:
  238. if self._body_timeout is None:
  239. await body_future
  240. else:
  241. try:
  242. await gen.with_timeout(
  243. self.stream.io_loop.time() + self._body_timeout,
  244. body_future,
  245. quiet_exceptions=iostream.StreamClosedError,
  246. )
  247. except gen.TimeoutError:
  248. gen_log.info("Timeout reading body from %s", self.context)
  249. self.stream.close()
  250. return False
  251. self._read_finished = True
  252. if not self._write_finished or self.is_client:
  253. need_delegate_close = False
  254. with _ExceptionLoggingContext(app_log):
  255. delegate.finish()
  256. # If we're waiting for the application to produce an asynchronous
  257. # response, and we're not detached, register a close callback
  258. # on the stream (we didn't need one while we were reading)
  259. if (
  260. not self._finish_future.done()
  261. and self.stream is not None
  262. and not self.stream.closed()
  263. ):
  264. self.stream.set_close_callback(self._on_connection_close)
  265. await self._finish_future
  266. if self.is_client and self._disconnect_on_finish:
  267. self.close()
  268. if self.stream is None:
  269. return False
  270. except httputil.HTTPInputError as e:
  271. gen_log.info("Malformed HTTP message from %s: %s", self.context, e)
  272. if not self.is_client:
  273. await self.stream.write(b"HTTP/1.1 400 Bad Request\r\n\r\n")
  274. self.close()
  275. return False
  276. finally:
  277. if need_delegate_close:
  278. with _ExceptionLoggingContext(app_log):
  279. delegate.on_connection_close()
  280. header_future = None # type: ignore
  281. self._clear_callbacks()
  282. return True
  283. def _clear_callbacks(self) -> None:
  284. """Clears the callback attributes.
  285. This allows the request handler to be garbage collected more
  286. quickly in CPython by breaking up reference cycles.
  287. """
  288. self._write_callback = None
  289. self._write_future = None # type: Optional[Future[None]]
  290. self._close_callback = None # type: Optional[Callable[[], None]]
  291. if self.stream is not None:
  292. self.stream.set_close_callback(None)
  293. def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:
  294. """Sets a callback that will be run when the connection is closed.
  295. Note that this callback is slightly different from
  296. `.HTTPMessageDelegate.on_connection_close`: The
  297. `.HTTPMessageDelegate` method is called when the connection is
  298. closed while receiving a message. This callback is used when
  299. there is not an active delegate (for example, on the server
  300. side this callback is used if the client closes the connection
  301. after sending its request but before receiving all the
  302. response.
  303. """
  304. self._close_callback = callback
  305. def _on_connection_close(self) -> None:
  306. # Note that this callback is only registered on the IOStream
  307. # when we have finished reading the request and are waiting for
  308. # the application to produce its response.
  309. if self._close_callback is not None:
  310. callback = self._close_callback
  311. self._close_callback = None
  312. callback()
  313. if not self._finish_future.done():
  314. future_set_result_unless_cancelled(self._finish_future, None)
  315. self._clear_callbacks()
  316. def close(self) -> None:
  317. if self.stream is not None:
  318. self.stream.close()
  319. self._clear_callbacks()
  320. if not self._finish_future.done():
  321. future_set_result_unless_cancelled(self._finish_future, None)
  322. def detach(self) -> iostream.IOStream:
  323. """Take control of the underlying stream.
  324. Returns the underlying `.IOStream` object and stops all further
  325. HTTP processing. May only be called during
  326. `.HTTPMessageDelegate.headers_received`. Intended for implementing
  327. protocols like websockets that tunnel over an HTTP handshake.
  328. """
  329. self._clear_callbacks()
  330. stream = self.stream
  331. self.stream = None # type: ignore
  332. if not self._finish_future.done():
  333. future_set_result_unless_cancelled(self._finish_future, None)
  334. return stream
  335. def set_body_timeout(self, timeout: float) -> None:
  336. """Sets the body timeout for a single request.
  337. Overrides the value from `.HTTP1ConnectionParameters`.
  338. """
  339. self._body_timeout = timeout
  340. def set_max_body_size(self, max_body_size: int) -> None:
  341. """Sets the body size limit for a single request.
  342. Overrides the value from `.HTTP1ConnectionParameters`.
  343. """
  344. self._max_body_size = max_body_size
  345. def write_headers(
  346. self,
  347. start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
  348. headers: httputil.HTTPHeaders,
  349. chunk: Optional[bytes] = None,
  350. ) -> "Future[None]":
  351. """Implements `.HTTPConnection.write_headers`."""
  352. lines = []
  353. if self.is_client:
  354. assert isinstance(start_line, httputil.RequestStartLine)
  355. self._request_start_line = start_line
  356. lines.append(utf8(f"{start_line[0]} {start_line[1]} HTTP/1.1"))
  357. # Client requests with a non-empty body must have either a
  358. # Content-Length or a Transfer-Encoding. If Content-Length is not
  359. # present we'll add our Transfer-Encoding below.
  360. self._chunking_output = (
  361. start_line.method in ("POST", "PUT", "PATCH")
  362. and "Content-Length" not in headers
  363. )
  364. else:
  365. assert isinstance(start_line, httputil.ResponseStartLine)
  366. assert self._request_start_line is not None
  367. assert self._request_headers is not None
  368. self._response_start_line = start_line
  369. lines.append(utf8("HTTP/1.1 %d %s" % (start_line[1], start_line[2])))
  370. self._chunking_output = (
  371. # TODO: should this use
  372. # self._request_start_line.version or
  373. # start_line.version?
  374. self._request_start_line.version == "HTTP/1.1"
  375. # Omit payload header field for HEAD request.
  376. and self._request_start_line.method != "HEAD"
  377. # 1xx, 204 and 304 responses have no body (not even a zero-length
  378. # body), and so should not have either Content-Length or
  379. # Transfer-Encoding headers.
  380. and start_line.code not in (204, 304)
  381. and (start_line.code < 100 or start_line.code >= 200)
  382. # No need to chunk the output if a Content-Length is specified.
  383. and "Content-Length" not in headers
  384. )
  385. # If connection to a 1.1 client will be closed, inform client
  386. if (
  387. self._request_start_line.version == "HTTP/1.1"
  388. and self._disconnect_on_finish
  389. ):
  390. headers["Connection"] = "close"
  391. # If a 1.0 client asked for keep-alive, add the header.
  392. if (
  393. self._request_start_line.version == "HTTP/1.0"
  394. and self._request_headers.get("Connection", "").lower() == "keep-alive"
  395. ):
  396. headers["Connection"] = "Keep-Alive"
  397. if self._chunking_output:
  398. headers["Transfer-Encoding"] = "chunked"
  399. if not self.is_client and (
  400. self._request_start_line.method == "HEAD"
  401. or cast(httputil.ResponseStartLine, start_line).code == 304
  402. ):
  403. self._expected_content_remaining = 0
  404. elif "Content-Length" in headers:
  405. self._expected_content_remaining = parse_int(headers["Content-Length"])
  406. else:
  407. self._expected_content_remaining = None
  408. # TODO: headers are supposed to be of type str, but we still have some
  409. # cases that let bytes slip through. Remove these native_str calls when those
  410. # are fixed.
  411. header_lines = (
  412. native_str(n) + ": " + native_str(v) for n, v in headers.get_all()
  413. )
  414. lines.extend(line.encode("latin1") for line in header_lines)
  415. for line in lines:
  416. if CR_OR_LF_RE.search(line):
  417. raise ValueError("Illegal characters (CR or LF) in header: %r" % line)
  418. future = None
  419. if self.stream.closed():
  420. future = self._write_future = Future()
  421. future.set_exception(iostream.StreamClosedError())
  422. future.exception()
  423. else:
  424. future = self._write_future = Future()
  425. data = b"\r\n".join(lines) + b"\r\n\r\n"
  426. if chunk:
  427. data += self._format_chunk(chunk)
  428. self._pending_write = self.stream.write(data)
  429. future_add_done_callback(self._pending_write, self._on_write_complete)
  430. return future
  431. def _format_chunk(self, chunk: bytes) -> bytes:
  432. if self._expected_content_remaining is not None:
  433. self._expected_content_remaining -= len(chunk)
  434. if self._expected_content_remaining < 0:
  435. # Close the stream now to stop further framing errors.
  436. self.stream.close()
  437. raise httputil.HTTPOutputError(
  438. "Tried to write more data than Content-Length"
  439. )
  440. if self._chunking_output and chunk:
  441. # Don't write out empty chunks because that means END-OF-STREAM
  442. # with chunked encoding
  443. return utf8("%x" % len(chunk)) + b"\r\n" + chunk + b"\r\n"
  444. else:
  445. return chunk
  446. def write(self, chunk: bytes) -> "Future[None]":
  447. """Implements `.HTTPConnection.write`.
  448. For backwards compatibility it is allowed but deprecated to
  449. skip `write_headers` and instead call `write()` with a
  450. pre-encoded header block.
  451. """
  452. future = None
  453. if self.stream.closed():
  454. future = self._write_future = Future()
  455. self._write_future.set_exception(iostream.StreamClosedError())
  456. self._write_future.exception()
  457. else:
  458. future = self._write_future = Future()
  459. self._pending_write = self.stream.write(self._format_chunk(chunk))
  460. future_add_done_callback(self._pending_write, self._on_write_complete)
  461. return future
  462. def finish(self) -> None:
  463. """Implements `.HTTPConnection.finish`."""
  464. if (
  465. self._expected_content_remaining is not None
  466. and self._expected_content_remaining != 0
  467. and not self.stream.closed()
  468. ):
  469. self.stream.close()
  470. raise httputil.HTTPOutputError(
  471. "Tried to write %d bytes less than Content-Length"
  472. % self._expected_content_remaining
  473. )
  474. if self._chunking_output:
  475. if not self.stream.closed():
  476. self._pending_write = self.stream.write(b"0\r\n\r\n")
  477. self._pending_write.add_done_callback(self._on_write_complete)
  478. self._write_finished = True
  479. # If the app finished the request while we're still reading,
  480. # divert any remaining data away from the delegate and
  481. # close the connection when we're done sending our response.
  482. # Closing the connection is the only way to avoid reading the
  483. # whole input body.
  484. if not self._read_finished:
  485. self._disconnect_on_finish = True
  486. # No more data is coming, so instruct TCP to send any remaining
  487. # data immediately instead of waiting for a full packet or ack.
  488. self.stream.set_nodelay(True)
  489. if self._pending_write is None:
  490. self._finish_request(None)
  491. else:
  492. future_add_done_callback(self._pending_write, self._finish_request)
  493. def _on_write_complete(self, future: "Future[None]") -> None:
  494. exc = future.exception()
  495. if exc is not None and not isinstance(exc, iostream.StreamClosedError):
  496. future.result()
  497. if self._write_callback is not None:
  498. callback = self._write_callback
  499. self._write_callback = None
  500. self.stream.io_loop.add_callback(callback)
  501. if self._write_future is not None:
  502. future = self._write_future
  503. self._write_future = None
  504. future_set_result_unless_cancelled(future, None)
  505. def _can_keep_alive(
  506. self, start_line: httputil.RequestStartLine, headers: httputil.HTTPHeaders
  507. ) -> bool:
  508. if self.params.no_keep_alive:
  509. return False
  510. connection_header = headers.get("Connection")
  511. if connection_header is not None:
  512. connection_header = connection_header.lower()
  513. if start_line.version == "HTTP/1.1":
  514. return connection_header != "close"
  515. elif (
  516. "Content-Length" in headers
  517. or is_transfer_encoding_chunked(headers)
  518. or getattr(start_line, "method", None) in ("HEAD", "GET")
  519. ):
  520. # start_line may be a request or response start line; only
  521. # the former has a method attribute.
  522. return connection_header == "keep-alive"
  523. return False
  524. def _finish_request(self, future: "Optional[Future[None]]") -> None:
  525. self._clear_callbacks()
  526. if not self.is_client and self._disconnect_on_finish:
  527. self.close()
  528. return
  529. # Turn Nagle's algorithm back on, leaving the stream in its
  530. # default state for the next request.
  531. self.stream.set_nodelay(False)
  532. if not self._finish_future.done():
  533. future_set_result_unless_cancelled(self._finish_future, None)
  534. def _parse_headers(self, data: bytes) -> Tuple[str, httputil.HTTPHeaders]:
  535. # The lstrip removes newlines that some implementations sometimes
  536. # insert between messages of a reused connection. Per RFC 7230,
  537. # we SHOULD ignore at least one empty line before the request.
  538. # http://tools.ietf.org/html/rfc7230#section-3.5
  539. data_str = native_str(data.decode("latin1")).lstrip("\r\n")
  540. # RFC 7230 section allows for both CRLF and bare LF.
  541. eol = data_str.find("\n")
  542. start_line = data_str[:eol].rstrip("\r")
  543. headers = httputil.HTTPHeaders.parse(data_str[eol:])
  544. return start_line, headers
  545. def _read_body(
  546. self,
  547. code: int,
  548. headers: httputil.HTTPHeaders,
  549. delegate: httputil.HTTPMessageDelegate,
  550. ) -> Optional[Awaitable[None]]:
  551. if "Content-Length" in headers:
  552. if "," in headers["Content-Length"]:
  553. # Proxies sometimes cause Content-Length headers to get
  554. # duplicated. If all the values are identical then we can
  555. # use them but if they differ it's an error.
  556. pieces = re.split(r",\s*", headers["Content-Length"])
  557. if any(i != pieces[0] for i in pieces):
  558. raise httputil.HTTPInputError(
  559. "Multiple unequal Content-Lengths: %r"
  560. % headers["Content-Length"]
  561. )
  562. headers["Content-Length"] = pieces[0]
  563. try:
  564. content_length: Optional[int] = parse_int(headers["Content-Length"])
  565. except ValueError:
  566. # Handles non-integer Content-Length value.
  567. raise httputil.HTTPInputError(
  568. "Only integer Content-Length is allowed: %s"
  569. % headers["Content-Length"]
  570. )
  571. if cast(int, content_length) > self._max_body_size:
  572. raise httputil.HTTPInputError("Content-Length too long")
  573. else:
  574. content_length = None
  575. is_chunked = is_transfer_encoding_chunked(headers)
  576. if code == 204:
  577. # This response code is not allowed to have a non-empty body,
  578. # and has an implicit length of zero instead of read-until-close.
  579. # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
  580. if is_chunked or content_length not in (None, 0):
  581. raise httputil.HTTPInputError(
  582. "Response with code %d should not have body" % code
  583. )
  584. content_length = 0
  585. if is_chunked:
  586. return self._read_chunked_body(delegate)
  587. if content_length is not None:
  588. return self._read_fixed_body(content_length, delegate)
  589. if self.is_client:
  590. return self._read_body_until_close(delegate)
  591. return None
  592. async def _read_fixed_body(
  593. self, content_length: int, delegate: httputil.HTTPMessageDelegate
  594. ) -> None:
  595. while content_length > 0:
  596. body = await self.stream.read_bytes(
  597. min(self.params.chunk_size, content_length), partial=True
  598. )
  599. content_length -= len(body)
  600. if not self._write_finished or self.is_client:
  601. with _ExceptionLoggingContext(app_log):
  602. ret = delegate.data_received(body)
  603. if ret is not None:
  604. await ret
  605. async def _read_chunked_body(self, delegate: httputil.HTTPMessageDelegate) -> None:
  606. # TODO: "chunk extensions" http://tools.ietf.org/html/rfc2616#section-3.6.1
  607. total_size = 0
  608. while True:
  609. chunk_len_str = await self.stream.read_until(b"\r\n", max_bytes=64)
  610. try:
  611. chunk_len = parse_hex_int(native_str(chunk_len_str[:-2]))
  612. except ValueError:
  613. raise httputil.HTTPInputError("invalid chunk size")
  614. if chunk_len == 0:
  615. crlf = await self.stream.read_bytes(2)
  616. if crlf != b"\r\n":
  617. raise httputil.HTTPInputError(
  618. "improperly terminated chunked request"
  619. )
  620. return
  621. total_size += chunk_len
  622. if total_size > self._max_body_size:
  623. raise httputil.HTTPInputError("chunked body too large")
  624. bytes_to_read = chunk_len
  625. while bytes_to_read:
  626. chunk = await self.stream.read_bytes(
  627. min(bytes_to_read, self.params.chunk_size), partial=True
  628. )
  629. bytes_to_read -= len(chunk)
  630. if not self._write_finished or self.is_client:
  631. with _ExceptionLoggingContext(app_log):
  632. ret = delegate.data_received(chunk)
  633. if ret is not None:
  634. await ret
  635. # chunk ends with \r\n
  636. crlf = await self.stream.read_bytes(2)
  637. assert crlf == b"\r\n"
  638. async def _read_body_until_close(
  639. self, delegate: httputil.HTTPMessageDelegate
  640. ) -> None:
  641. body = await self.stream.read_until_close()
  642. if not self._write_finished or self.is_client:
  643. with _ExceptionLoggingContext(app_log):
  644. ret = delegate.data_received(body)
  645. if ret is not None:
  646. await ret
  647. class _GzipMessageDelegate(httputil.HTTPMessageDelegate):
  648. """Wraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``."""
  649. def __init__(self, delegate: httputil.HTTPMessageDelegate, chunk_size: int) -> None:
  650. self._delegate = delegate
  651. self._chunk_size = chunk_size
  652. self._decompressor = None # type: Optional[GzipDecompressor]
  653. def headers_received(
  654. self,
  655. start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
  656. headers: httputil.HTTPHeaders,
  657. ) -> Optional[Awaitable[None]]:
  658. if headers.get("Content-Encoding", "").lower() == "gzip":
  659. self._decompressor = GzipDecompressor()
  660. # Downstream delegates will only see uncompressed data,
  661. # so rename the content-encoding header.
  662. # (but note that curl_httpclient doesn't do this).
  663. headers.add("X-Consumed-Content-Encoding", headers["Content-Encoding"])
  664. del headers["Content-Encoding"]
  665. return self._delegate.headers_received(start_line, headers)
  666. async def data_received(self, chunk: bytes) -> None:
  667. if self._decompressor:
  668. compressed_data = chunk
  669. while compressed_data:
  670. decompressed = self._decompressor.decompress(
  671. compressed_data, self._chunk_size
  672. )
  673. if decompressed:
  674. ret = self._delegate.data_received(decompressed)
  675. if ret is not None:
  676. await ret
  677. compressed_data = self._decompressor.unconsumed_tail
  678. if compressed_data and not decompressed:
  679. raise httputil.HTTPInputError(
  680. "encountered unconsumed gzip data without making progress"
  681. )
  682. else:
  683. ret = self._delegate.data_received(chunk)
  684. if ret is not None:
  685. await ret
  686. def finish(self) -> None:
  687. if self._decompressor is not None:
  688. tail = self._decompressor.flush()
  689. if tail:
  690. # The tail should always be empty: decompress returned
  691. # all that it can in data_received and the only
  692. # purpose of the flush call is to detect errors such
  693. # as truncated input. If we did legitimately get a new
  694. # chunk at this point we'd need to change the
  695. # interface to make finish() a coroutine.
  696. raise ValueError(
  697. "decompressor.flush returned data; possible truncated input"
  698. )
  699. return self._delegate.finish()
  700. def on_connection_close(self) -> None:
  701. return self._delegate.on_connection_close()
  702. class HTTP1ServerConnection:
  703. """An HTTP/1.x server."""
  704. def __init__(
  705. self,
  706. stream: iostream.IOStream,
  707. params: Optional[HTTP1ConnectionParameters] = None,
  708. context: Optional[object] = None,
  709. ) -> None:
  710. """
  711. :arg stream: an `.IOStream`
  712. :arg params: a `.HTTP1ConnectionParameters` or None
  713. :arg context: an opaque application-defined object that is accessible
  714. as ``connection.context``
  715. """
  716. self.stream = stream
  717. if params is None:
  718. params = HTTP1ConnectionParameters()
  719. self.params = params
  720. self.context = context
  721. self._serving_future = None # type: Optional[Future[None]]
  722. async def close(self) -> None:
  723. """Closes the connection.
  724. Returns a `.Future` that resolves after the serving loop has exited.
  725. """
  726. self.stream.close()
  727. # Block until the serving loop is done, but ignore any exceptions
  728. # (start_serving is already responsible for logging them).
  729. assert self._serving_future is not None
  730. try:
  731. await self._serving_future
  732. except Exception:
  733. pass
  734. def start_serving(self, delegate: httputil.HTTPServerConnectionDelegate) -> None:
  735. """Starts serving requests on this connection.
  736. :arg delegate: a `.HTTPServerConnectionDelegate`
  737. """
  738. assert isinstance(delegate, httputil.HTTPServerConnectionDelegate)
  739. fut = gen.convert_yielded(self._server_request_loop(delegate))
  740. self._serving_future = fut
  741. # Register the future on the IOLoop so its errors get logged.
  742. self.stream.io_loop.add_future(fut, lambda f: f.result())
  743. async def _server_request_loop(
  744. self, delegate: httputil.HTTPServerConnectionDelegate
  745. ) -> None:
  746. try:
  747. while True:
  748. conn = HTTP1Connection(self.stream, False, self.params, self.context)
  749. request_delegate = delegate.start_request(self, conn)
  750. try:
  751. ret = await conn.read_response(request_delegate)
  752. except (
  753. iostream.StreamClosedError,
  754. iostream.UnsatisfiableReadError,
  755. asyncio.CancelledError,
  756. ):
  757. return
  758. except _QuietException:
  759. # This exception was already logged.
  760. conn.close()
  761. return
  762. except Exception:
  763. gen_log.error("Uncaught exception", exc_info=True)
  764. conn.close()
  765. return
  766. if not ret:
  767. return
  768. await asyncio.sleep(0)
  769. finally:
  770. delegate.on_close(self)
  771. DIGITS = re.compile(r"[0-9]+")
  772. HEXDIGITS = re.compile(r"[0-9a-fA-F]+")
  773. def parse_int(s: str) -> int:
  774. """Parse a non-negative integer from a string."""
  775. if DIGITS.fullmatch(s) is None:
  776. raise ValueError("not an integer: %r" % s)
  777. return int(s)
  778. def parse_hex_int(s: str) -> int:
  779. """Parse a non-negative hexadecimal integer from a string."""
  780. if HEXDIGITS.fullmatch(s) is None:
  781. raise ValueError("not a hexadecimal integer: %r" % s)
  782. return int(s, 16)
  783. def is_transfer_encoding_chunked(headers: httputil.HTTPHeaders) -> bool:
  784. """Returns true if the headers specify Transfer-Encoding: chunked.
  785. Raise httputil.HTTPInputError if any other transfer encoding is used.
  786. """
  787. # Note that transfer-encoding is an area in which postel's law can lead
  788. # us astray. If a proxy and a backend server are liberal in what they accept,
  789. # but accept slightly different things, this can lead to mismatched framing
  790. # and request smuggling issues. Therefore we are as strict as possible here
  791. # (even technically going beyond the requirements of the RFCs: a value of
  792. # ",chunked" is legal but doesn't appear in practice for legitimate traffic)
  793. if "Transfer-Encoding" not in headers:
  794. return False
  795. if "Content-Length" in headers:
  796. # Message cannot contain both Content-Length and
  797. # Transfer-Encoding headers.
  798. # http://tools.ietf.org/html/rfc7230#section-3.3.3
  799. raise httputil.HTTPInputError(
  800. "Message with both Transfer-Encoding and Content-Length"
  801. )
  802. if headers["Transfer-Encoding"].lower() == "chunked":
  803. return True
  804. # We do not support any transfer-encodings other than chunked, and we do not
  805. # expect to add any support because the concept of transfer-encoding has
  806. # been removed in HTTP/2.
  807. raise httputil.HTTPInputError(
  808. "Unsupported Transfer-Encoding %s" % headers["Transfer-Encoding"]
  809. )