From: Ben Darnell Date: Sun, 25 Nov 2012 16:38:25 +0000 (-0500) Subject: Add protocol kwarg to HTTPServer constructor. X-Git-Tag: v3.0.0~212 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d6f583fca7983c0f98ef45f150cfc5a40680ca90;p=thirdparty%2Ftornado.git Add protocol kwarg to HTTPServer constructor. --- diff --git a/tornado/httpserver.py b/tornado/httpserver.py index 9de647e39..2ef784183 100644 --- a/tornado/httpserver.py +++ b/tornado/httpserver.py @@ -78,10 +78,14 @@ class HTTPServer(TCPServer): ensure the connection is closed on every request no matter what HTTP version the client is using. - If ``xheaders`` is ``True``, we support the ``X-Real-Ip`` and ``X-Scheme`` - headers, which override the remote IP and HTTP scheme for all requests. - These headers are useful when running Tornado behind a reverse proxy or - load balancer. + If ``xheaders`` is ``True``, we support the + ``X-Real-Ip``/``X-Forwarded-For`` and + ``X-Scheme``/``X-Forwarded-Proto`` headers, which override the + remote IP and URI scheme/protocol for all requests. These headers + are useful when running Tornado behind a reverse proxy or load + balancer. The ``protocol`` argument can also be set to ``https`` + if Tornado is run behind an SSL-decoding proxy that does not set one of + the supported ``xheaders``. `HTTPServer` can serve SSL traffic with Python 2.6+ and OpenSSL. To make this server serve SSL traffic, send the ssl_options dictionary @@ -134,16 +138,17 @@ class HTTPServer(TCPServer): """ def __init__(self, request_callback, no_keep_alive=False, io_loop=None, - xheaders=False, ssl_options=None, **kwargs): + xheaders=False, ssl_options=None, protocol=None, **kwargs): self.request_callback = request_callback self.no_keep_alive = no_keep_alive self.xheaders = xheaders + self.protocol = protocol TCPServer.__init__(self, io_loop=io_loop, ssl_options=ssl_options, **kwargs) def handle_stream(self, stream, address): HTTPConnection(stream, address, self.request_callback, - self.no_keep_alive, self.xheaders) + self.no_keep_alive, self.xheaders, self.protocol) class _BadRequestException(Exception): @@ -158,12 +163,13 @@ class HTTPConnection(object): until the HTTP conection is closed. """ def __init__(self, stream, address, request_callback, no_keep_alive=False, - xheaders=False): + xheaders=False, protocol=None): self.stream = stream self.address = address self.request_callback = request_callback self.no_keep_alive = no_keep_alive self.xheaders = xheaders + self.protocol = protocol self._request = None self._request_finished = False # Save stack context here, outside of any request. This keeps @@ -259,7 +265,7 @@ class HTTPConnection(object): self._request = HTTPRequest( connection=self, method=method, uri=uri, version=version, - headers=headers, remote_ip=remote_ip) + headers=headers, remote_ip=remote_ip, protocol=self.protocol) content_length = headers.get("Content-Length") if content_length: diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index fbcec0b07..9c701a464 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -370,6 +370,17 @@ class XHeaderTest(HandlerBaseTestCase): self.fetch_json("/", headers=invalid_host)["remote_ip"], "127.0.0.1") +class ManualProtocolTest(HandlerBaseTestCase): + class Handler(RequestHandler): + def get(self): + self.write(dict(protocol=self.request.protocol)) + + def get_httpserver_options(self): + return dict(protocol='https') + + def test_manual_protocol(self): + self.assertEqual(self.fetch_json('/')['protocol'], 'https') + class UnixSocketTest(AsyncTestCase): """HTTPServers can listen on Unix sockets too. diff --git a/website/sphinx/releases/next.rst b/website/sphinx/releases/next.rst index 32ea2b819..8a1ba140a 100644 --- a/website/sphinx/releases/next.rst +++ b/website/sphinx/releases/next.rst @@ -180,3 +180,6 @@ In progress client implementations. * `simple_httpclient` now accepts responses with a 304 status code that include a ``Content-Length`` header. +* `HTTPServer` now takes a ``protocol`` keyword argument which can be set + to ``https`` if the server is behind an SSL-decoding proxy that does not + set any supported X-headers.