httpserver.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.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`: simple single-process::
  70. server = HTTPServer(app)
  71. server.listen(8888)
  72. IOLoop.current().start()
  73. In many cases, `tornado.web.Application.listen` can be used to avoid
  74. the need to explicitly create the `HTTPServer`.
  75. 2. `~tornado.tcpserver.TCPServer.bind`/`~tornado.tcpserver.TCPServer.start`:
  76. simple multi-process::
  77. server = HTTPServer(app)
  78. server.bind(8888)
  79. server.start(0) # Forks multiple sub-processes
  80. IOLoop.current().start()
  81. When using this interface, an `.IOLoop` must *not* be passed
  82. to the `HTTPServer` constructor. `~.TCPServer.start` will always start
  83. the server on the default singleton `.IOLoop`.
  84. 3. `~tornado.tcpserver.TCPServer.add_sockets`: advanced multi-process::
  85. sockets = tornado.netutil.bind_sockets(8888)
  86. tornado.process.fork_processes(0)
  87. server = HTTPServer(app)
  88. server.add_sockets(sockets)
  89. IOLoop.current().start()
  90. The `~.TCPServer.add_sockets` interface is more complicated,
  91. but it can be used with `tornado.process.fork_processes` to
  92. give you more flexibility in when the fork happens.
  93. `~.TCPServer.add_sockets` can also be used in single-process
  94. servers if you want to create your listening sockets in some
  95. way other than `tornado.netutil.bind_sockets`.
  96. .. versionchanged:: 4.0
  97. Added ``decompress_request``, ``chunk_size``, ``max_header_size``,
  98. ``idle_connection_timeout``, ``body_timeout``, ``max_body_size``
  99. arguments. Added support for `.HTTPServerConnectionDelegate`
  100. instances as ``request_callback``.
  101. .. versionchanged:: 4.1
  102. `.HTTPServerConnectionDelegate.start_request` is now called with
  103. two arguments ``(server_conn, request_conn)`` (in accordance with the
  104. documentation) instead of one ``(request_conn)``.
  105. .. versionchanged:: 4.2
  106. `HTTPServer` is now a subclass of `tornado.util.Configurable`.
  107. .. versionchanged:: 4.5
  108. Added the ``trusted_downstream`` argument.
  109. .. versionchanged:: 5.0
  110. The ``io_loop`` argument has been removed.
  111. """
  112. def __init__(self, *args: Any, **kwargs: Any) -> None:
  113. # Ignore args to __init__; real initialization belongs in
  114. # initialize since we're Configurable. (there's something
  115. # weird in initialization order between this class,
  116. # Configurable, and TCPServer so we can't leave __init__ out
  117. # completely)
  118. pass
  119. def initialize(
  120. self,
  121. request_callback: Union[
  122. httputil.HTTPServerConnectionDelegate,
  123. Callable[[httputil.HTTPServerRequest], None],
  124. ],
  125. no_keep_alive: bool = False,
  126. xheaders: bool = False,
  127. ssl_options: Union[Dict[str, Any], ssl.SSLContext] = None,
  128. protocol: str = None,
  129. decompress_request: bool = False,
  130. chunk_size: int = None,
  131. max_header_size: int = None,
  132. idle_connection_timeout: float = None,
  133. body_timeout: float = None,
  134. max_body_size: int = None,
  135. max_buffer_size: int = None,
  136. trusted_downstream: List[str] = None,
  137. ) -> None:
  138. # This method's signature is not extracted with autodoc
  139. # because we want its arguments to appear on the class
  140. # constructor. When changing this signature, also update the
  141. # copy in httpserver.rst.
  142. self.request_callback = request_callback
  143. self.xheaders = xheaders
  144. self.protocol = protocol
  145. self.conn_params = HTTP1ConnectionParameters(
  146. decompress=decompress_request,
  147. chunk_size=chunk_size,
  148. max_header_size=max_header_size,
  149. header_timeout=idle_connection_timeout or 3600,
  150. max_body_size=max_body_size,
  151. body_timeout=body_timeout,
  152. no_keep_alive=no_keep_alive,
  153. )
  154. TCPServer.__init__(
  155. self,
  156. ssl_options=ssl_options,
  157. max_buffer_size=max_buffer_size,
  158. read_chunk_size=chunk_size,
  159. )
  160. self._connections = set() # type: Set[HTTP1ServerConnection]
  161. self.trusted_downstream = trusted_downstream
  162. @classmethod
  163. def configurable_base(cls) -> Type[Configurable]:
  164. return HTTPServer
  165. @classmethod
  166. def configurable_default(cls) -> Type[Configurable]:
  167. return HTTPServer
  168. async def close_all_connections(self) -> None:
  169. """Close all open connections and asynchronously wait for them to finish.
  170. This method is used in combination with `~.TCPServer.stop` to
  171. support clean shutdowns (especially for unittests). Typical
  172. usage would call ``stop()`` first to stop accepting new
  173. connections, then ``await close_all_connections()`` to wait for
  174. existing connections to finish.
  175. This method does not currently close open websocket connections.
  176. Note that this method is a coroutine and must be caled with ``await``.
  177. """
  178. while self._connections:
  179. # Peek at an arbitrary element of the set
  180. conn = next(iter(self._connections))
  181. await conn.close()
  182. def handle_stream(self, stream: iostream.IOStream, address: Tuple) -> None:
  183. context = _HTTPRequestContext(
  184. stream, address, self.protocol, self.trusted_downstream
  185. )
  186. conn = HTTP1ServerConnection(stream, self.conn_params, context)
  187. self._connections.add(conn)
  188. conn.start_serving(self)
  189. def start_request(
  190. self, server_conn: object, request_conn: httputil.HTTPConnection
  191. ) -> httputil.HTTPMessageDelegate:
  192. if isinstance(self.request_callback, httputil.HTTPServerConnectionDelegate):
  193. delegate = self.request_callback.start_request(server_conn, request_conn)
  194. else:
  195. delegate = _CallableAdapter(self.request_callback, request_conn)
  196. if self.xheaders:
  197. delegate = _ProxyAdapter(delegate, request_conn)
  198. return delegate
  199. def on_close(self, server_conn: object) -> None:
  200. self._connections.remove(typing.cast(HTTP1ServerConnection, server_conn))
  201. class _CallableAdapter(httputil.HTTPMessageDelegate):
  202. def __init__(
  203. self,
  204. request_callback: Callable[[httputil.HTTPServerRequest], None],
  205. request_conn: httputil.HTTPConnection,
  206. ) -> None:
  207. self.connection = request_conn
  208. self.request_callback = request_callback
  209. self.request = None # type: Optional[httputil.HTTPServerRequest]
  210. self.delegate = None
  211. self._chunks = [] # type: List[bytes]
  212. def headers_received(
  213. self,
  214. start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
  215. headers: httputil.HTTPHeaders,
  216. ) -> Optional[Awaitable[None]]:
  217. self.request = httputil.HTTPServerRequest(
  218. connection=self.connection,
  219. start_line=typing.cast(httputil.RequestStartLine, start_line),
  220. headers=headers,
  221. )
  222. return None
  223. def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
  224. self._chunks.append(chunk)
  225. return None
  226. def finish(self) -> None:
  227. assert self.request is not None
  228. self.request.body = b"".join(self._chunks)
  229. self.request._parse_body()
  230. self.request_callback(self.request)
  231. def on_connection_close(self) -> None:
  232. del self._chunks
  233. class _HTTPRequestContext(object):
  234. def __init__(
  235. self,
  236. stream: iostream.IOStream,
  237. address: Tuple,
  238. protocol: Optional[str],
  239. trusted_downstream: List[str] = None,
  240. ) -> None:
  241. self.address = address
  242. # Save the socket's address family now so we know how to
  243. # interpret self.address even after the stream is closed
  244. # and its socket attribute replaced with None.
  245. if stream.socket is not None:
  246. self.address_family = stream.socket.family
  247. else:
  248. self.address_family = None
  249. # In HTTPServerRequest we want an IP, not a full socket address.
  250. if (
  251. self.address_family in (socket.AF_INET, socket.AF_INET6)
  252. and address is not None
  253. ):
  254. self.remote_ip = address[0]
  255. else:
  256. # Unix (or other) socket; fake the remote address.
  257. self.remote_ip = "0.0.0.0"
  258. if protocol:
  259. self.protocol = protocol
  260. elif isinstance(stream, iostream.SSLIOStream):
  261. self.protocol = "https"
  262. else:
  263. self.protocol = "http"
  264. self._orig_remote_ip = self.remote_ip
  265. self._orig_protocol = self.protocol
  266. self.trusted_downstream = set(trusted_downstream or [])
  267. def __str__(self) -> str:
  268. if self.address_family in (socket.AF_INET, socket.AF_INET6):
  269. return self.remote_ip
  270. elif isinstance(self.address, bytes):
  271. # Python 3 with the -bb option warns about str(bytes),
  272. # so convert it explicitly.
  273. # Unix socket addresses are str on mac but bytes on linux.
  274. return native_str(self.address)
  275. else:
  276. return str(self.address)
  277. def _apply_xheaders(self, headers: httputil.HTTPHeaders) -> None:
  278. """Rewrite the ``remote_ip`` and ``protocol`` fields."""
  279. # Squid uses X-Forwarded-For, others use X-Real-Ip
  280. ip = headers.get("X-Forwarded-For", self.remote_ip)
  281. # Skip trusted downstream hosts in X-Forwarded-For list
  282. for ip in (cand.strip() for cand in reversed(ip.split(","))):
  283. if ip not in self.trusted_downstream:
  284. break
  285. ip = headers.get("X-Real-Ip", ip)
  286. if netutil.is_valid_ip(ip):
  287. self.remote_ip = ip
  288. # AWS uses X-Forwarded-Proto
  289. proto_header = headers.get(
  290. "X-Scheme", headers.get("X-Forwarded-Proto", self.protocol)
  291. )
  292. if proto_header:
  293. # use only the last proto entry if there is more than one
  294. # TODO: support trusting mutiple layers of proxied protocol
  295. proto_header = proto_header.split(",")[-1].strip()
  296. if proto_header in ("http", "https"):
  297. self.protocol = proto_header
  298. def _unapply_xheaders(self) -> None:
  299. """Undo changes from `_apply_xheaders`.
  300. Xheaders are per-request so they should not leak to the next
  301. request on the same connection.
  302. """
  303. self.remote_ip = self._orig_remote_ip
  304. self.protocol = self._orig_protocol
  305. class _ProxyAdapter(httputil.HTTPMessageDelegate):
  306. def __init__(
  307. self,
  308. delegate: httputil.HTTPMessageDelegate,
  309. request_conn: httputil.HTTPConnection,
  310. ) -> None:
  311. self.connection = request_conn
  312. self.delegate = delegate
  313. def headers_received(
  314. self,
  315. start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
  316. headers: httputil.HTTPHeaders,
  317. ) -> Optional[Awaitable[None]]:
  318. # TODO: either make context an official part of the
  319. # HTTPConnection interface or figure out some other way to do this.
  320. self.connection.context._apply_xheaders(headers) # type: ignore
  321. return self.delegate.headers_received(start_line, headers)
  322. def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
  323. return self.delegate.data_received(chunk)
  324. def finish(self) -> None:
  325. self.delegate.finish()
  326. self._cleanup()
  327. def on_connection_close(self) -> None:
  328. self.delegate.on_connection_close()
  329. self._cleanup()
  330. def _cleanup(self) -> None:
  331. self.connection.context._unapply_xheaders() # type: ignore
  332. HTTPRequest = httputil.HTTPServerRequest