Tornado update
This commit is contained in:
+21
-15
@@ -29,33 +29,39 @@ provides WSGI support in two ways:
|
||||
and Tornado handlers in a single server.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import, division, with_statement
|
||||
from __future__ import absolute_import, division, print_function, with_statement
|
||||
|
||||
import Cookie
|
||||
import httplib
|
||||
import sys
|
||||
import time
|
||||
import tornado
|
||||
import urllib
|
||||
|
||||
from tornado import escape
|
||||
from tornado import httputil
|
||||
from tornado.log import access_log
|
||||
from tornado import web
|
||||
from tornado.escape import native_str, utf8, parse_qs_bytes
|
||||
from tornado.util import b, bytes_type
|
||||
from tornado.escape import native_str, parse_qs_bytes
|
||||
from tornado.util import bytes_type, unicode_type
|
||||
|
||||
try:
|
||||
from io import BytesIO # python 3
|
||||
except ImportError:
|
||||
from cStringIO import StringIO as BytesIO # python 2
|
||||
|
||||
try:
|
||||
import Cookie # py2
|
||||
except ImportError:
|
||||
import http.cookies as Cookie # py3
|
||||
|
||||
try:
|
||||
import urllib.parse as urllib_parse # py3
|
||||
except ImportError:
|
||||
import urllib as urllib_parse
|
||||
|
||||
# PEP 3333 specifies that WSGI on python 3 generally deals with byte strings
|
||||
# that are smuggled inside objects of type unicode (via the latin1 encoding).
|
||||
# These functions are like those in the tornado.escape module, but defined
|
||||
# here to minimize the temptation to use them in non-wsgi contexts.
|
||||
if str is unicode:
|
||||
if str is unicode_type:
|
||||
def to_wsgi_str(s):
|
||||
assert isinstance(s, bytes_type)
|
||||
return s.decode('latin1')
|
||||
@@ -116,7 +122,7 @@ class WSGIApplication(web.Application):
|
||||
assert handler._finished
|
||||
reason = handler._reason
|
||||
status = str(handler._status_code) + " " + reason
|
||||
headers = handler._headers.items() + handler._list_headers
|
||||
headers = list(handler._headers.get_all())
|
||||
if hasattr(handler, "_new_cookie"):
|
||||
for cookie in handler._new_cookie.values():
|
||||
headers.append(("Set-Cookie", cookie.OutputString(None)))
|
||||
@@ -130,8 +136,8 @@ class HTTPRequest(object):
|
||||
def __init__(self, environ):
|
||||
"""Parses the given WSGI environ to construct the request."""
|
||||
self.method = environ["REQUEST_METHOD"]
|
||||
self.path = urllib.quote(from_wsgi_str(environ.get("SCRIPT_NAME", "")))
|
||||
self.path += urllib.quote(from_wsgi_str(environ.get("PATH_INFO", "")))
|
||||
self.path = urllib_parse.quote(from_wsgi_str(environ.get("SCRIPT_NAME", "")))
|
||||
self.path += urllib_parse.quote(from_wsgi_str(environ.get("PATH_INFO", "")))
|
||||
self.uri = self.path
|
||||
self.arguments = {}
|
||||
self.query = environ.get("QUERY_STRING", "")
|
||||
@@ -235,7 +241,7 @@ class WSGIContainer(object):
|
||||
app_response = self.wsgi_application(
|
||||
WSGIContainer.environ(request), start_response)
|
||||
response.extend(app_response)
|
||||
body = b("").join(response)
|
||||
body = b"".join(response)
|
||||
if hasattr(app_response, "close"):
|
||||
app_response.close()
|
||||
if not data:
|
||||
@@ -255,10 +261,10 @@ class WSGIContainer(object):
|
||||
|
||||
parts = [escape.utf8("HTTP/1.1 " + data["status"] + "\r\n")]
|
||||
for key, value in headers:
|
||||
parts.append(escape.utf8(key) + b(": ") + escape.utf8(value) + b("\r\n"))
|
||||
parts.append(b("\r\n"))
|
||||
parts.append(escape.utf8(key) + b": " + escape.utf8(value) + b"\r\n")
|
||||
parts.append(b"\r\n")
|
||||
parts.append(body)
|
||||
request.write(b("").join(parts))
|
||||
request.write(b"".join(parts))
|
||||
request.finish()
|
||||
self._log(status_code, request)
|
||||
|
||||
@@ -294,7 +300,7 @@ class WSGIContainer(object):
|
||||
environ["CONTENT_TYPE"] = request.headers.pop("Content-Type")
|
||||
if "Content-Length" in request.headers:
|
||||
environ["CONTENT_LENGTH"] = request.headers.pop("Content-Length")
|
||||
for key, value in request.headers.iteritems():
|
||||
for key, value in request.headers.items():
|
||||
environ["HTTP_" + key.replace("-", "_").upper()] = value
|
||||
return environ
|
||||
|
||||
|
||||
Reference in New Issue
Block a user