wsgi.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. """WSGI support for the Tornado web framework.
  16. WSGI is the Python standard for web servers, and allows for interoperability
  17. between Tornado and other Python web frameworks and servers.
  18. This module provides WSGI support via the `WSGIContainer` class, which
  19. makes it possible to run applications using other WSGI frameworks on
  20. the Tornado HTTP server. The reverse is not supported; the Tornado
  21. `.Application` and `.RequestHandler` classes are designed for use with
  22. the Tornado `.HTTPServer` and cannot be used in a generic WSGI
  23. container.
  24. """
  25. import concurrent.futures
  26. from io import BytesIO
  27. import tornado
  28. import sys
  29. from tornado.concurrent import dummy_executor
  30. from tornado import escape
  31. from tornado import httputil
  32. from tornado.ioloop import IOLoop
  33. from tornado.log import access_log
  34. from typing import List, Tuple, Optional, Callable, Any, Dict
  35. from types import TracebackType
  36. import typing
  37. if typing.TYPE_CHECKING:
  38. from typing import Type # noqa: F401
  39. from _typeshed.wsgi import WSGIApplication as WSGIAppType # noqa: F401
  40. # PEP 3333 specifies that WSGI on python 3 generally deals with byte strings
  41. # that are smuggled inside objects of type unicode (via the latin1 encoding).
  42. # This function is like those in the tornado.escape module, but defined
  43. # here to minimize the temptation to use it in non-wsgi contexts.
  44. def to_wsgi_str(s: bytes) -> str:
  45. assert isinstance(s, bytes)
  46. return s.decode("latin1")
  47. class WSGIContainer:
  48. r"""Makes a WSGI-compatible application runnable on Tornado's HTTP server.
  49. .. warning::
  50. WSGI is a *synchronous* interface, while Tornado's concurrency model
  51. is based on single-threaded *asynchronous* execution. Many of Tornado's
  52. distinguishing features are not available in WSGI mode, including efficient
  53. long-polling and websockets. The primary purpose of `WSGIContainer` is
  54. to support both WSGI applications and native Tornado ``RequestHandlers`` in
  55. a single process. WSGI-only applications are likely to be better off
  56. with a dedicated WSGI server such as ``gunicorn`` or ``uwsgi``.
  57. Wrap a WSGI application in a `WSGIContainer` to make it implement the Tornado
  58. `.HTTPServer` ``request_callback`` interface. The `WSGIContainer` object can
  59. then be passed to classes from the `tornado.routing` module,
  60. `tornado.web.FallbackHandler`, or to `.HTTPServer` directly.
  61. This class is intended to let other frameworks (Django, Flask, etc)
  62. run on the Tornado HTTP server and I/O loop.
  63. Realistic usage will be more complicated, but the simplest possible example uses a
  64. hand-written WSGI application with `.HTTPServer`::
  65. def simple_app(environ, start_response):
  66. status = "200 OK"
  67. response_headers = [("Content-type", "text/plain")]
  68. start_response(status, response_headers)
  69. return [b"Hello world!\n"]
  70. async def main():
  71. container = tornado.wsgi.WSGIContainer(simple_app)
  72. http_server = tornado.httpserver.HTTPServer(container)
  73. http_server.listen(8888)
  74. await asyncio.Event().wait()
  75. asyncio.run(main())
  76. The recommended pattern is to use the `tornado.routing` module to set up routing
  77. rules between your WSGI application and, typically, a `tornado.web.Application`.
  78. Alternatively, `tornado.web.Application` can be used as the top-level router
  79. and `tornado.web.FallbackHandler` can embed a `WSGIContainer` within it.
  80. If the ``executor`` argument is provided, the WSGI application will be executed
  81. on that executor. This must be an instance of `concurrent.futures.Executor`,
  82. typically a ``ThreadPoolExecutor`` (``ProcessPoolExecutor`` is not supported).
  83. If no ``executor`` is given, the application will run on the event loop thread in
  84. Tornado 6.3; this will change to use an internal thread pool by default in
  85. Tornado 7.0.
  86. .. warning::
  87. By default, the WSGI application is executed on the event loop's thread. This
  88. limits the server to one request at a time (per process), making it less scalable
  89. than most other WSGI servers. It is therefore highly recommended that you pass
  90. a ``ThreadPoolExecutor`` when constructing the `WSGIContainer`, after verifying
  91. that your application is thread-safe. The default will change to use a
  92. ``ThreadPoolExecutor`` in Tornado 7.0.
  93. .. versionadded:: 6.3
  94. The ``executor`` parameter.
  95. .. deprecated:: 6.3
  96. The default behavior of running the WSGI application on the event loop thread
  97. is deprecated and will change in Tornado 7.0 to use a thread pool by default.
  98. """
  99. def __init__(
  100. self,
  101. wsgi_application: "WSGIAppType",
  102. executor: Optional[concurrent.futures.Executor] = None,
  103. ) -> None:
  104. self.wsgi_application = wsgi_application
  105. self.executor = dummy_executor if executor is None else executor
  106. def __call__(self, request: httputil.HTTPServerRequest) -> None:
  107. IOLoop.current().spawn_callback(self.handle_request, request)
  108. async def handle_request(self, request: httputil.HTTPServerRequest) -> None:
  109. data = {} # type: Dict[str, Any]
  110. response = [] # type: List[bytes]
  111. def start_response(
  112. status: str,
  113. headers: List[Tuple[str, str]],
  114. exc_info: Optional[
  115. Tuple[
  116. "Optional[Type[BaseException]]",
  117. Optional[BaseException],
  118. Optional[TracebackType],
  119. ]
  120. ] = None,
  121. ) -> Callable[[bytes], Any]:
  122. data["status"] = status
  123. data["headers"] = headers
  124. return response.append
  125. loop = IOLoop.current()
  126. app_response = await loop.run_in_executor(
  127. self.executor,
  128. self.wsgi_application,
  129. self.environ(request),
  130. start_response,
  131. )
  132. try:
  133. app_response_iter = iter(app_response)
  134. def next_chunk() -> Optional[bytes]:
  135. try:
  136. return next(app_response_iter)
  137. except StopIteration:
  138. # StopIteration is special and is not allowed to pass through
  139. # coroutines normally.
  140. return None
  141. while True:
  142. chunk = await loop.run_in_executor(self.executor, next_chunk)
  143. if chunk is None:
  144. break
  145. response.append(chunk)
  146. finally:
  147. if hasattr(app_response, "close"):
  148. app_response.close() # type: ignore
  149. body = b"".join(response)
  150. if not data:
  151. raise Exception("WSGI app did not call start_response")
  152. status_code_str, reason = data["status"].split(" ", 1)
  153. status_code = int(status_code_str)
  154. headers = data["headers"] # type: List[Tuple[str, str]]
  155. header_set = {k.lower() for (k, v) in headers}
  156. body = escape.utf8(body)
  157. if status_code != 304:
  158. if "content-length" not in header_set:
  159. headers.append(("Content-Length", str(len(body))))
  160. if "content-type" not in header_set:
  161. headers.append(("Content-Type", "text/html; charset=UTF-8"))
  162. if "server" not in header_set:
  163. headers.append(("Server", "TornadoServer/%s" % tornado.version))
  164. start_line = httputil.ResponseStartLine("HTTP/1.1", status_code, reason)
  165. header_obj = httputil.HTTPHeaders()
  166. for key, value in headers:
  167. header_obj.add(key, value)
  168. assert request.connection is not None
  169. request.connection.write_headers(start_line, header_obj, chunk=body)
  170. request.connection.finish()
  171. self._log(status_code, request)
  172. def environ(self, request: httputil.HTTPServerRequest) -> Dict[str, Any]:
  173. """Converts a `tornado.httputil.HTTPServerRequest` to a WSGI environment.
  174. .. versionchanged:: 6.3
  175. No longer a static method.
  176. """
  177. hostport = request.host.split(":")
  178. if len(hostport) == 2:
  179. host = hostport[0]
  180. port = int(hostport[1])
  181. else:
  182. host = request.host
  183. port = 443 if request.protocol == "https" else 80
  184. environ = {
  185. "REQUEST_METHOD": request.method,
  186. "SCRIPT_NAME": "",
  187. "PATH_INFO": to_wsgi_str(
  188. escape.url_unescape(request.path, encoding=None, plus=False)
  189. ),
  190. "QUERY_STRING": request.query,
  191. "REMOTE_ADDR": request.remote_ip,
  192. "SERVER_NAME": host,
  193. "SERVER_PORT": str(port),
  194. "SERVER_PROTOCOL": request.version,
  195. "wsgi.version": (1, 0),
  196. "wsgi.url_scheme": request.protocol,
  197. "wsgi.input": BytesIO(escape.utf8(request.body)),
  198. "wsgi.errors": sys.stderr,
  199. "wsgi.multithread": self.executor is not dummy_executor,
  200. "wsgi.multiprocess": True,
  201. "wsgi.run_once": False,
  202. }
  203. if "Content-Type" in request.headers:
  204. environ["CONTENT_TYPE"] = request.headers.pop("Content-Type")
  205. if "Content-Length" in request.headers:
  206. environ["CONTENT_LENGTH"] = request.headers.pop("Content-Length")
  207. for key, value in request.headers.items():
  208. environ["HTTP_" + key.replace("-", "_").upper()] = value
  209. return environ
  210. def _log(self, status_code: int, request: httputil.HTTPServerRequest) -> None:
  211. if status_code < 400:
  212. log_method = access_log.info
  213. elif status_code < 500:
  214. log_method = access_log.warning
  215. else:
  216. log_method = access_log.error
  217. request_time = 1000.0 * request.request_time()
  218. assert request.method is not None
  219. assert request.uri is not None
  220. summary = (
  221. request.method # type: ignore[operator]
  222. + " "
  223. + request.uri
  224. + " ("
  225. + request.remote_ip
  226. + ")"
  227. )
  228. log_method("%d %s %.2fms", status_code, summary, request_time)
  229. HTTPRequest = httputil.HTTPServerRequest