wsgi.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. This module
  18. provides WSGI support in two ways:
  19. * `WSGIAdapter` converts a `tornado.web.Application` to the WSGI application
  20. interface. This is useful for running a Tornado app on another
  21. HTTP server, such as Google App Engine. See the `WSGIAdapter` class
  22. documentation for limitations that apply.
  23. * `WSGIContainer` lets you run other WSGI applications and frameworks on the
  24. Tornado HTTP server. For example, with this class you can mix Django
  25. and Tornado handlers in a single server.
  26. """
  27. from __future__ import absolute_import, division, print_function
  28. import sys
  29. from io import BytesIO
  30. import tornado
  31. import warnings
  32. from tornado.concurrent import Future
  33. from tornado import escape
  34. from tornado import httputil
  35. from tornado.log import access_log
  36. from tornado import web
  37. from tornado.escape import native_str
  38. from tornado.util import unicode_type, PY3
  39. if PY3:
  40. import urllib.parse as urllib_parse # py3
  41. else:
  42. import urllib as urllib_parse
  43. # PEP 3333 specifies that WSGI on python 3 generally deals with byte strings
  44. # that are smuggled inside objects of type unicode (via the latin1 encoding).
  45. # These functions are like those in the tornado.escape module, but defined
  46. # here to minimize the temptation to use them in non-wsgi contexts.
  47. if str is unicode_type:
  48. def to_wsgi_str(s):
  49. assert isinstance(s, bytes)
  50. return s.decode('latin1')
  51. def from_wsgi_str(s):
  52. assert isinstance(s, str)
  53. return s.encode('latin1')
  54. else:
  55. def to_wsgi_str(s):
  56. assert isinstance(s, bytes)
  57. return s
  58. def from_wsgi_str(s):
  59. assert isinstance(s, str)
  60. return s
  61. class WSGIApplication(web.Application):
  62. """A WSGI equivalent of `tornado.web.Application`.
  63. .. deprecated:: 4.0
  64. Use a regular `.Application` and wrap it in `WSGIAdapter` instead.
  65. This class will be removed in Tornado 6.0.
  66. """
  67. def __call__(self, environ, start_response):
  68. return WSGIAdapter(self)(environ, start_response)
  69. # WSGI has no facilities for flow control, so just return an already-done
  70. # Future when the interface requires it.
  71. def _dummy_future():
  72. f = Future()
  73. f.set_result(None)
  74. return f
  75. class _WSGIConnection(httputil.HTTPConnection):
  76. def __init__(self, method, start_response, context):
  77. self.method = method
  78. self.start_response = start_response
  79. self.context = context
  80. self._write_buffer = []
  81. self._finished = False
  82. self._expected_content_remaining = None
  83. self._error = None
  84. def set_close_callback(self, callback):
  85. # WSGI has no facility for detecting a closed connection mid-request,
  86. # so we can simply ignore the callback.
  87. pass
  88. def write_headers(self, start_line, headers, chunk=None, callback=None):
  89. if self.method == 'HEAD':
  90. self._expected_content_remaining = 0
  91. elif 'Content-Length' in headers:
  92. self._expected_content_remaining = int(headers['Content-Length'])
  93. else:
  94. self._expected_content_remaining = None
  95. self.start_response(
  96. '%s %s' % (start_line.code, start_line.reason),
  97. [(native_str(k), native_str(v)) for (k, v) in headers.get_all()])
  98. if chunk is not None:
  99. self.write(chunk, callback)
  100. elif callback is not None:
  101. callback()
  102. return _dummy_future()
  103. def write(self, chunk, callback=None):
  104. if self._expected_content_remaining is not None:
  105. self._expected_content_remaining -= len(chunk)
  106. if self._expected_content_remaining < 0:
  107. self._error = httputil.HTTPOutputError(
  108. "Tried to write more data than Content-Length")
  109. raise self._error
  110. self._write_buffer.append(chunk)
  111. if callback is not None:
  112. callback()
  113. return _dummy_future()
  114. def finish(self):
  115. if (self._expected_content_remaining is not None and
  116. self._expected_content_remaining != 0):
  117. self._error = httputil.HTTPOutputError(
  118. "Tried to write %d bytes less than Content-Length" %
  119. self._expected_content_remaining)
  120. raise self._error
  121. self._finished = True
  122. class _WSGIRequestContext(object):
  123. def __init__(self, remote_ip, protocol):
  124. self.remote_ip = remote_ip
  125. self.protocol = protocol
  126. def __str__(self):
  127. return self.remote_ip
  128. class WSGIAdapter(object):
  129. """Converts a `tornado.web.Application` instance into a WSGI application.
  130. Example usage::
  131. import tornado.web
  132. import tornado.wsgi
  133. import wsgiref.simple_server
  134. class MainHandler(tornado.web.RequestHandler):
  135. def get(self):
  136. self.write("Hello, world")
  137. if __name__ == "__main__":
  138. application = tornado.web.Application([
  139. (r"/", MainHandler),
  140. ])
  141. wsgi_app = tornado.wsgi.WSGIAdapter(application)
  142. server = wsgiref.simple_server.make_server('', 8888, wsgi_app)
  143. server.serve_forever()
  144. See the `appengine demo
  145. <https://github.com/tornadoweb/tornado/tree/stable/demos/appengine>`_
  146. for an example of using this module to run a Tornado app on Google
  147. App Engine.
  148. In WSGI mode asynchronous methods are not supported. This means
  149. that it is not possible to use `.AsyncHTTPClient`, or the
  150. `tornado.auth` or `tornado.websocket` modules.
  151. In multithreaded WSGI servers on Python 3, it may be necessary to
  152. permit `asyncio` to create event loops on any thread. Run the
  153. following at startup (typically import time for WSGI
  154. applications)::
  155. import asyncio
  156. from tornado.platform.asyncio import AnyThreadEventLoopPolicy
  157. asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
  158. .. versionadded:: 4.0
  159. .. deprecated:: 5.1
  160. This class is deprecated and will be removed in Tornado 6.0.
  161. Use Tornado's `.HTTPServer` instead of a WSGI container.
  162. """
  163. def __init__(self, application):
  164. warnings.warn("WSGIAdapter is deprecated, use Tornado's HTTPServer instead",
  165. DeprecationWarning)
  166. if isinstance(application, WSGIApplication):
  167. self.application = lambda request: web.Application.__call__(
  168. application, request)
  169. else:
  170. self.application = application
  171. def __call__(self, environ, start_response):
  172. method = environ["REQUEST_METHOD"]
  173. uri = urllib_parse.quote(from_wsgi_str(environ.get("SCRIPT_NAME", "")))
  174. uri += urllib_parse.quote(from_wsgi_str(environ.get("PATH_INFO", "")))
  175. if environ.get("QUERY_STRING"):
  176. uri += "?" + environ["QUERY_STRING"]
  177. headers = httputil.HTTPHeaders()
  178. if environ.get("CONTENT_TYPE"):
  179. headers["Content-Type"] = environ["CONTENT_TYPE"]
  180. if environ.get("CONTENT_LENGTH"):
  181. headers["Content-Length"] = environ["CONTENT_LENGTH"]
  182. for key in environ:
  183. if key.startswith("HTTP_"):
  184. headers[key[5:].replace("_", "-")] = environ[key]
  185. if headers.get("Content-Length"):
  186. body = environ["wsgi.input"].read(
  187. int(headers["Content-Length"]))
  188. else:
  189. body = b""
  190. protocol = environ["wsgi.url_scheme"]
  191. remote_ip = environ.get("REMOTE_ADDR", "")
  192. if environ.get("HTTP_HOST"):
  193. host = environ["HTTP_HOST"]
  194. else:
  195. host = environ["SERVER_NAME"]
  196. connection = _WSGIConnection(method, start_response,
  197. _WSGIRequestContext(remote_ip, protocol))
  198. request = httputil.HTTPServerRequest(
  199. method, uri, "HTTP/1.1", headers=headers, body=body,
  200. host=host, connection=connection)
  201. request._parse_body()
  202. self.application(request)
  203. if connection._error:
  204. raise connection._error
  205. if not connection._finished:
  206. raise Exception("request did not finish synchronously")
  207. return connection._write_buffer
  208. class WSGIContainer(object):
  209. r"""Makes a WSGI-compatible function runnable on Tornado's HTTP server.
  210. .. warning::
  211. WSGI is a *synchronous* interface, while Tornado's concurrency model
  212. is based on single-threaded asynchronous execution. This means that
  213. running a WSGI app with Tornado's `WSGIContainer` is *less scalable*
  214. than running the same app in a multi-threaded WSGI server like
  215. ``gunicorn`` or ``uwsgi``. Use `WSGIContainer` only when there are
  216. benefits to combining Tornado and WSGI in the same process that
  217. outweigh the reduced scalability.
  218. Wrap a WSGI function in a `WSGIContainer` and pass it to `.HTTPServer` to
  219. run it. For example::
  220. def simple_app(environ, start_response):
  221. status = "200 OK"
  222. response_headers = [("Content-type", "text/plain")]
  223. start_response(status, response_headers)
  224. return ["Hello world!\n"]
  225. container = tornado.wsgi.WSGIContainer(simple_app)
  226. http_server = tornado.httpserver.HTTPServer(container)
  227. http_server.listen(8888)
  228. tornado.ioloop.IOLoop.current().start()
  229. This class is intended to let other frameworks (Django, web.py, etc)
  230. run on the Tornado HTTP server and I/O loop.
  231. The `tornado.web.FallbackHandler` class is often useful for mixing
  232. Tornado and WSGI apps in the same server. See
  233. https://github.com/bdarnell/django-tornado-demo for a complete example.
  234. """
  235. def __init__(self, wsgi_application):
  236. self.wsgi_application = wsgi_application
  237. def __call__(self, request):
  238. data = {}
  239. response = []
  240. def start_response(status, response_headers, exc_info=None):
  241. data["status"] = status
  242. data["headers"] = response_headers
  243. return response.append
  244. app_response = self.wsgi_application(
  245. WSGIContainer.environ(request), start_response)
  246. try:
  247. response.extend(app_response)
  248. body = b"".join(response)
  249. finally:
  250. if hasattr(app_response, "close"):
  251. app_response.close()
  252. if not data:
  253. raise Exception("WSGI app did not call start_response")
  254. status_code, reason = data["status"].split(' ', 1)
  255. status_code = int(status_code)
  256. headers = data["headers"]
  257. header_set = set(k.lower() for (k, v) in headers)
  258. body = escape.utf8(body)
  259. if status_code != 304:
  260. if "content-length" not in header_set:
  261. headers.append(("Content-Length", str(len(body))))
  262. if "content-type" not in header_set:
  263. headers.append(("Content-Type", "text/html; charset=UTF-8"))
  264. if "server" not in header_set:
  265. headers.append(("Server", "TornadoServer/%s" % tornado.version))
  266. start_line = httputil.ResponseStartLine("HTTP/1.1", status_code, reason)
  267. header_obj = httputil.HTTPHeaders()
  268. for key, value in headers:
  269. header_obj.add(key, value)
  270. request.connection.write_headers(start_line, header_obj, chunk=body)
  271. request.connection.finish()
  272. self._log(status_code, request)
  273. @staticmethod
  274. def environ(request):
  275. """Converts a `tornado.httputil.HTTPServerRequest` to a WSGI environment.
  276. """
  277. hostport = request.host.split(":")
  278. if len(hostport) == 2:
  279. host = hostport[0]
  280. port = int(hostport[1])
  281. else:
  282. host = request.host
  283. port = 443 if request.protocol == "https" else 80
  284. environ = {
  285. "REQUEST_METHOD": request.method,
  286. "SCRIPT_NAME": "",
  287. "PATH_INFO": to_wsgi_str(escape.url_unescape(
  288. request.path, encoding=None, plus=False)),
  289. "QUERY_STRING": request.query,
  290. "REMOTE_ADDR": request.remote_ip,
  291. "SERVER_NAME": host,
  292. "SERVER_PORT": str(port),
  293. "SERVER_PROTOCOL": request.version,
  294. "wsgi.version": (1, 0),
  295. "wsgi.url_scheme": request.protocol,
  296. "wsgi.input": BytesIO(escape.utf8(request.body)),
  297. "wsgi.errors": sys.stderr,
  298. "wsgi.multithread": False,
  299. "wsgi.multiprocess": True,
  300. "wsgi.run_once": False,
  301. }
  302. if "Content-Type" in request.headers:
  303. environ["CONTENT_TYPE"] = request.headers.pop("Content-Type")
  304. if "Content-Length" in request.headers:
  305. environ["CONTENT_LENGTH"] = request.headers.pop("Content-Length")
  306. for key, value in request.headers.items():
  307. environ["HTTP_" + key.replace("-", "_").upper()] = value
  308. return environ
  309. def _log(self, status_code, request):
  310. if status_code < 400:
  311. log_method = access_log.info
  312. elif status_code < 500:
  313. log_method = access_log.warning
  314. else:
  315. log_method = access_log.error
  316. request_time = 1000.0 * request.request_time()
  317. summary = request.method + " " + request.uri + " (" + \
  318. request.remote_ip + ")"
  319. log_method("%d %s %.2fms", status_code, summary, request_time)
  320. HTTPRequest = httputil.HTTPServerRequest