From: Ben Darnell Date: Sun, 23 Feb 2014 17:28:02 +0000 (-0500) Subject: Move some logic from HTTPRequest to HTTP1Connection. X-Git-Tag: v4.0.0b1~91^2~59 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7cda6081da91466ea044173c0dc5bc37eb92a7f;p=thirdparty%2Ftornado.git Move some logic from HTTPRequest to HTTP1Connection. This removes some imports from httputil that are not allowed on app engine. --- diff --git a/tornado/http1connection.py b/tornado/http1connection.py index 23c2cc398..5c931dd48 100644 --- a/tornado/http1connection.py +++ b/tornado/http1connection.py @@ -22,6 +22,7 @@ from tornado.escape import native_str from tornado import httputil from tornado import iostream from tornado.log import gen_log +from tornado import netutil from tornado import stack_context @@ -47,7 +48,12 @@ class HTTP1Connection(object): self.request_callback = request_callback self.no_keep_alive = no_keep_alive self.xheaders = xheaders - self.protocol = protocol + if protocol: + self.protocol = protocol + elif isinstance(stream, iostream.SSLIOStream): + self.protocol = "https" + else: + self.protocol = "http" self._clear_request_state() # Save stack context here, outside of any request. This keeps # contexts from one request from leaking into the next. @@ -178,9 +184,25 @@ class HTTP1Connection(object): # Unix (or other) socket; fake the remote address remote_ip = '0.0.0.0' + protocol = self.protocol + + # xheaders can override the defaults + if self.xheaders: + # Squid uses X-Forwarded-For, others use X-Real-Ip + ip = headers.get("X-Forwarded-For", remote_ip) + ip = ip.split(',')[-1].strip() + ip = headers.get("X-Real-Ip", ip) + if netutil.is_valid_ip(ip): + remote_ip = ip + # AWS uses X-Forwarded-Proto + proto_header = headers.get( + "X-Scheme", headers.get("X-Forwarded-Proto", self.protocol)) + if proto_header in ("http", "https"): + protocol = proto_header + self._request = httputil.HTTPServerRequest( connection=self, method=method, uri=uri, version=version, - headers=headers, remote_ip=remote_ip, protocol=self.protocol) + headers=headers, remote_ip=remote_ip, protocol=protocol) content_length = headers.get("Content-Length") if content_length: diff --git a/tornado/httputil.py b/tornado/httputil.py index 27d391063..1ca54bc8a 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -24,13 +24,10 @@ import copy import datetime import email.utils import numbers -import ssl import time from tornado.escape import native_str, parse_qs_bytes, utf8 -from tornado import iostream from tornado.log import gen_log -from tornado import netutil from tornado.util import ObjectDict, bytes_type try: @@ -52,6 +49,11 @@ try: except ImportError: from urllib.parse import urlencode # py3 +try: + from ssl import SSLError +except ImportError: + # ssl is unavailable on app engine. + class SSLError(Exception): pass class _NormalizedHeaderCache(dict): """Dynamic cached mapping of header names to Http-Header-Case. @@ -321,27 +323,7 @@ class HTTPServerRequest(object): # set remote IP and protocol self.remote_ip = remote_ip - if protocol: - self.protocol = protocol - elif connection and isinstance(connection.stream, iostream.SSLIOStream): - self.protocol = "https" - else: - self.protocol = "http" - - # xheaders can override the defaults - if connection and connection.xheaders: - # Squid uses X-Forwarded-For, others use X-Real-Ip - ip = self.headers.get("X-Forwarded-For", self.remote_ip) - ip = ip.split(',')[-1].strip() - ip = self.headers.get( - "X-Real-Ip", ip) - if netutil.is_valid_ip(ip): - self.remote_ip = ip - # AWS uses X-Forwarded-Proto - proto = self.headers.get( - "X-Scheme", self.headers.get("X-Forwarded-Proto", self.protocol)) - if proto in ("http", "https"): - self.protocol = proto + self.protocol = protocol or "http" self.host = host or self.headers.get("Host") or "127.0.0.1" self.files = files or {} @@ -415,7 +397,7 @@ class HTTPServerRequest(object): try: return self.connection.stream.socket.getpeercert( binary_form=binary_form) - except ssl.SSLError: + except SSLError: return None def __repr__(self):