httpserver.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #
  2. # Copyright 2009 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. """A non-blocking, single-threaded HTTP server.
  16. Typical applications have little direct interaction with the `HTTPServer`
  17. class except to start a server at the beginning of the process
  18. (and even that is often done indirectly via `tornado.web.Application.listen`).
  19. .. versionchanged:: 4.0
  20. The ``HTTPRequest`` class that used to live in this module has been moved
  21. to `tornado.httputil.HTTPServerRequest`. The old name remains as an alias.
  22. """
  23. import socket
  24. import ssl
  25. from tornado.escape import native_str
  26. from tornado.http1connection import HTTP1ServerConnection, HTTP1ConnectionParameters
  27. from tornado import httputil
  28. from tornado import iostream
  29. from tornado import netutil
  30. from tornado.tcpserver import TCPServer
  31. from tornado.util import Configurable
  32. import typing
  33. from typing import Union, Any, Dict, Callable, List, Type, Tuple, Optional, Awaitable
  34. if typing.TYPE_CHECKING:
  35. from typing import Set # noqa: F401
  36. class HTTPServer(TCPServer, Configurable, httputil.HTTPServerConnectionDelegate):
  37. r"""A non-blocking, single-threaded HTTP server.
  38. A server is defined by a subclass of `.HTTPServerConnectionDelegate`,
  39. or, for backwards compatibility, a callback that takes an
  40. `.HTTPServerRequest` as an argument. The delegate is usually a
  41. `tornado.web.Application`.
  42. `HTTPServer` supports keep-alive connections by default
  43. (automatically for HTTP/1.1, or for HTTP/1.0 when the client
  44. requests ``Connection: keep-alive``).
  45. If ``xheaders`` is ``True``, we support the
  46. ``X-Real-Ip``/``X-Forwarded-For`` and
  47. ``X-Scheme``/``X-Forwarded-Proto`` headers, which override the
  48. remote IP and URI scheme/protocol for all requests. These headers
  49. are useful when running Tornado behind a reverse proxy or load
  50. balancer. The ``protocol`` argument can also be set to ``https``
  51. if Tornado is run behind an SSL-decoding proxy that does not set one of
  52. the supported ``xheaders``.
  53. By default, when parsing the ``X-Forwarded-For`` header, Tornado will
  54. select the last (i.e., the closest) address on the list of hosts as the
  55. remote host IP address. To select the next server in the chain, a list of
  56. trusted downstream hosts may be passed as the ``trusted_downstream``
  57. argument. These hosts will be skipped when parsing the ``X-Forwarded-For``
  58. header.
  59. To make this server serve SSL traffic, send the ``ssl_options`` keyword
  60. argument with an `ssl.SSLContext` object. For compatibility with older
  61. versions of Python ``ssl_options`` may also be a dictionary of keyword
  62. arguments for the `ssl.SSLContext.wrap_socket` method.::
  63. ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
  64. ssl_ctx.load_cert_chain(os.path.join(data_dir, "mydomain.crt"),
  65. os.path.join(data_dir, "mydomain.key"))
  66. HTTPServer(application, ssl_options=ssl_ctx)
  67. `HTTPServer` initialization follows one of three patterns (the
  68. initialization methods are defined on `tornado.tcpserver.TCPServer`):
  69. 1. `~tornado.tcpserver.TCPServer.listen`: single-process::
  70. async def main():
  71. server = HTTPServer()
  72. server.listen(8888)
  73. await asyncio.Event().wait()
  74. asyncio.run(main())
  75. In many cases, `tornado.web.Application.listen` can be used to avoid
  76. the need to explicitly create the `HTTPServer`.
  77. While this example does not create multiple processes on its own, when
  78. the ``reuse_port=True`` argument is passed to ``listen()`` you can run
  79. the program multiple times to create a multi-process service.
  80. 2. `~tornado.tcpserver.TCPServer.add_sockets`: multi-process::
  81. sockets = bind_sockets(8888)
  82. tornado.process.fork_processes(0)
  83. async def post_fork_main():
  84. server = HTTPServer()
  85. server.add_sockets(sockets)
  86. await asyncio.Event().wait()
  87. asyncio.run(post_fork_main())
  88. The ``add_sockets`` interface is more complicated, but it can be used with
  89. `tornado.process.fork_processes` to run a multi-process service with all
  90. worker processes forked from a single parent. ``add_sockets`` can also be
  91. used in single-process servers if you want to create your listening
  92. sockets in some way other than `~tornado.netutil.bind_sockets`.
  93. Note that when using this pattern, nothing that touches the event loop
  94. can be run before ``fork_processes``.
  95. 3. `~tornado.tcpserver.TCPServer.bind`/`~tornado.tcpserver.TCPServer.start`:
  96. simple **deprecated** multi-process::
  97. server = HTTPServer()
  98. server.bind(8888)
  99. server.start(0) # Forks multiple sub-processes
  100. IOLoop.current().start()
  101. This pattern is deprecated because it requires interfaces in the
  102. `asyncio` module that have been deprecated since Python 3.10. Support for
  103. creating multiple processes in the ``start`` method will be removed in a
  104. future version of Tornado.
  105. .. versionchanged:: 4.0
  106. Added ``decompress_request``, ``chunk_size``, ``max_header_size``,
  107. ``idle_connection_timeout``, ``body_timeout``, ``max_body_size``
  108. arguments. Added support for `.HTTPServerConnectionDelegate`
  109. instances as ``request_callback``.
  110. .. versionchanged:: 4.1
  111. `.HTTPServerConnectionDelegate.start_request` is now called with
  112. two arguments ``(server_conn, request_conn)`` (in accordance with the
  113. documentation) instead of one ``(request_conn)``.
  114. .. versionchanged:: 4.2
  115. `HTTPServer` is now a subclass of `tornado.util.Configurable`.
  116. .. versionchanged:: 4.5
  117. Added the ``trusted_downstream`` argument.
  118. .. versionchanged:: 5.0
  119. The ``io_loop`` argument has been removed.
  120. """
  121. def __init__(self, *args: Any, **kwargs: Any) -> None:
  122. # Ignore args to __init__; real initialization belongs in
  123. # initialize since we're Configurable. (there's something
  124. # weird in initialization order between this class,
  125. # Configurable, and TCPServer so we can't leave __init__ out
  126. # completely)
  127. pass
  128. def initialize(
  129. self,
  130. request_callback: Union[
  131. httputil.HTTPServerConnectionDelegate,
  132. Callable[[httputil.HTTPServerRequest], None],
  133. ],
  134. no_keep_alive: bool = False,
  135. xheaders: bool = False,
  136. ssl_options: Optional[Union[Dict[str, Any], ssl.SSLContext]] = None,
  137. protocol: Optional[str] = None,
  138. decompress_request: bool = False,
  139. chunk_size: Optional[int] = None,
  140. max_header_size: Optional[int] = None,
  141. idle_connection_timeout: Optional[float] = None,
  142. body_timeout: Optional[float] = None,
  143. max_body_size: Optional[int] = None,
  144. max_buffer_size: Optional[int] = None,
  145. trusted_downstream: Optional[List[str]] = None,
  146. ) -> None:
  147. # This method's signature is not extracted with autodoc
  148. # because we want its arguments to appear on the class
  149. # constructor. When changing this signature, also update the
  150. # copy in httpserver.rst.
  151. self.request_callback = request_callback
  152. self.xheaders = xheaders
  153. self.protocol = protocol
  154. self.conn_params = HTTP1ConnectionParameters(
  155. decompress=decompress_request,
  156. chunk_size=chunk_size,
  157. max_header_size=max_header_size,
  158. header_timeout=idle_connection_timeout or 3600,
  159. max_body_size=max_body_size,
  160. body_timeout=body_timeout,
  161. no_keep_alive=no_keep_alive,
  162. )
  163. TCPServer.__init__(
  164. self,
  165. ssl_options=ssl_options,
  166. max_buffer_size=max_buffer_size,
  167. read_chunk_size=chunk_size,
  168. )
  169. self._connections = set() # type: Set[HTTP1ServerConnection]
  170. self.trusted_downstream = trusted_downstream
  171. @classmethod
  172. def configurable_base(cls) -> Type[Configurable]:
  173. return HTTPServer
  174. @classmethod
  175. def configurable_default(cls) -> Type[Configurable]:
  176. return HTTPServer
  177. async def close_all_connections(self) -> None:
  178. """Close all open connections and asynchronously wait for them to finish.
  179. This method is used in combination with `~.TCPServer.stop` to
  180. support clean shutdowns (especially for unittests). Typical
  181. usage would call ``stop()`` first to stop accepting new
  182. connections, then ``await close_all_connections()`` to wait for
  183. existing connections to finish.
  184. This method does not currently close open websocket connections.
  185. Note that this method is a coroutine and must be called with ``await``.
  186. """
  187. while self._connections:
  188. # Peek at an arbitrary element of the set
  189. conn = next(iter(self._connections))
  190. await conn.close()
  191. def handle_stream(self, stream: iostream.IOStream, address: Tuple) -> None:
  192. context = _HTTPRequestContext(
  193. stream, address, self.protocol, self.trusted_downstream
  194. )
  195. conn = HTTP1ServerConnection(stream, self.conn_params, context)
  196. self._connections.add(conn)
  197. conn.start_serving(self)
  198. def start_request(
  199. self, server_conn: object, request_conn: httputil.HTTPConnection
  200. ) -> httputil.HTTPMessageDelegate:
  201. if isinstance(self.request_callback, httputil.HTTPServerConnectionDelegate):
  202. delegate = self.request_callback.start_request(server_conn, request_conn)
  203. else:
  204. delegate = _CallableAdapter(self.request_callback, request_conn)
  205. if self.xheaders:
  206. delegate = _ProxyAdapter(delegate, request_conn)
  207. return delegate
  208. def on_close(self, server_conn: object) -> None:
  209. self._connections.remove(typing.cast(HTTP1ServerConnection, server_conn))
  210. class _CallableAdapter(httputil.HTTPMessageDelegate):
  211. def __init__(
  212. self,
  213. request_callback: Callable[[httputil.HTTPServerRequest], None],
  214. request_conn: httputil.HTTPConnection,
  215. ) -> None:
  216. self.connection = request_conn
  217. self.request_callback = request_callback
  218. self.request = None # type: Optional[httputil.HTTPServerRequest]
  219. self.delegate = None
  220. self._chunks = [] # type: List[bytes]
  221. def headers_received(
  222. self,
  223. start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
  224. headers: httputil.HTTPHeaders,
  225. ) -> Optional[Awaitable[None]]:
  226. self.request = httputil.HTTPServerRequest(
  227. connection=self.connection,
  228. start_line=typing.cast(httputil.RequestStartLine, start_line),
  229. headers=headers,
  230. )
  231. return None
  232. def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
  233. self._chunks.append(chunk)
  234. return None
  235. def finish(self) -> None:
  236. assert self.request is not None
  237. self.request.body = b"".join(self._chunks)
  238. self.request._parse_body()
  239. self.request_callback(self.request)
  240. def on_connection_close(self) -> None:
  241. del self._chunks
  242. class _HTTPRequestContext:
  243. def __init__(
  244. self,
  245. stream: iostream.IOStream,
  246. address: Tuple,
  247. protocol: Optional[str],
  248. trusted_downstream: Optional[List[str]] = None,
  249. ) -> None:
  250. self.address = address
  251. # Save the socket's address family now so we know how to
  252. # interpret self.address even after the stream is closed
  253. # and its socket attribute replaced with None.
  254. if stream.socket is not None:
  255. self.address_family = stream.socket.family
  256. else:
  257. self.address_family = None
  258. # In HTTPServerRequest we want an IP, not a full socket address.
  259. if (
  260. self.address_family in (socket.AF_INET, socket.AF_INET6)
  261. and address is not None
  262. ):
  263. self.remote_ip = address[0]
  264. else:
  265. # Unix (or other) socket; fake the remote address.
  266. self.remote_ip = "0.0.0.0"
  267. if protocol:
  268. self.protocol = protocol
  269. elif isinstance(stream, iostream.SSLIOStream):
  270. self.protocol = "https"
  271. else:
  272. self.protocol = "http"
  273. self._orig_remote_ip = self.remote_ip
  274. self._orig_protocol = self.protocol
  275. self.trusted_downstream = set(trusted_downstream or [])
  276. def __str__(self) -> str:
  277. if self.address_family in (socket.AF_INET, socket.AF_INET6):
  278. return self.remote_ip
  279. elif isinstance(self.address, bytes):
  280. # Python 3 with the -bb option warns about str(bytes),
  281. # so convert it explicitly.
  282. # Unix socket addresses are str on mac but bytes on linux.
  283. return native_str(self.address)
  284. else:
  285. return str(self.address)
  286. def _apply_xheaders(self, headers: httputil.HTTPHeaders) -> None:
  287. """Rewrite the ``remote_ip`` and ``protocol`` fields."""
  288. # Squid uses X-Forwarded-For, others use X-Real-Ip
  289. ip = headers.get("X-Forwarded-For", self.remote_ip)
  290. # Skip trusted downstream hosts in X-Forwarded-For list
  291. for ip in (cand.strip() for cand in reversed(ip.split(","))):
  292. if ip not in self.trusted_downstream:
  293. break
  294. ip = headers.get("X-Real-Ip", ip)
  295. if netutil.is_valid_ip(ip):
  296. self.remote_ip = ip
  297. # AWS uses X-Forwarded-Proto
  298. proto_header = headers.get(
  299. "X-Scheme", headers.get("X-Forwarded-Proto", self.protocol)
  300. )
  301. if proto_header:
  302. # use only the last proto entry if there is more than one
  303. # TODO: support trusting multiple layers of proxied protocol
  304. proto_header = proto_header.split(",")[-1].strip()
  305. if proto_header in ("http", "https"):
  306. self.protocol = proto_header
  307. def _unapply_xheaders(self) -> None:
  308. """Undo changes from `_apply_xheaders`.
  309. Xheaders are per-request so they should not leak to the next
  310. request on the same connection.
  311. """
  312. self.remote_ip = self._orig_remote_ip
  313. self.protocol = self._orig_protocol
  314. class _ProxyAdapter(httputil.HTTPMessageDelegate):
  315. def __init__(
  316. self,
  317. delegate: httputil.HTTPMessageDelegate,
  318. request_conn: httputil.HTTPConnection,
  319. ) -> None:
  320. self.connection = request_conn
  321. self.delegate = delegate
  322. def headers_received(
  323. self,
  324. start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
  325. headers: httputil.HTTPHeaders,
  326. ) -> Optional[Awaitable[None]]:
  327. # TODO: either make context an official part of the
  328. # HTTPConnection interface or figure out some other way to do this.
  329. self.connection.context._apply_xheaders(headers) # type: ignore
  330. return self.delegate.headers_received(start_line, headers)
  331. def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
  332. return self.delegate.data_received(chunk)
  333. def finish(self) -> None:
  334. self.delegate.finish()
  335. self._cleanup()
  336. def on_connection_close(self) -> None:
  337. self.delegate.on_connection_close()
  338. self._cleanup()
  339. def _cleanup(self) -> None:
  340. self.connection.context._unapply_xheaders() # type: ignore
  341. HTTPRequest = httputil.HTTPServerRequest