From: Ben Darnell Date: Sun, 8 Mar 2015 23:05:28 +0000 (-0400) Subject: Make HTTPServer Configurable. X-Git-Tag: v4.2.0b1~72 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b43fccbc9ae24d4a796c448a429d7db75f9363aa;p=thirdparty%2Ftornado.git Make HTTPServer Configurable. Relax some overly-specific tests. --- diff --git a/tornado/httpserver.py b/tornado/httpserver.py index 13a6e92fa..226f966a3 100644 --- a/tornado/httpserver.py +++ b/tornado/httpserver.py @@ -37,9 +37,11 @@ from tornado import httputil from tornado import iostream from tornado import netutil from tornado.tcpserver import TCPServer +from tornado.util import Configurable -class HTTPServer(TCPServer, httputil.HTTPServerConnectionDelegate): +class HTTPServer(TCPServer, Configurable, + httputil.HTTPServerConnectionDelegate): r"""A non-blocking, single-threaded HTTP server. A server is defined by a subclass of `.HTTPServerConnectionDelegate`, @@ -120,12 +122,20 @@ class HTTPServer(TCPServer, httputil.HTTPServerConnectionDelegate): two arguments ``(server_conn, request_conn)`` (in accordance with the documentation) instead of one ``(request_conn)``. """ - def __init__(self, request_callback, no_keep_alive=False, io_loop=None, - xheaders=False, ssl_options=None, protocol=None, - decompress_request=False, - chunk_size=None, max_header_size=None, - idle_connection_timeout=None, body_timeout=None, - max_body_size=None, max_buffer_size=None): + def __init__(self, *args, **kwargs): + # Ignore args to __init__; real initialization belongs in + # initialize since we're Configurable. (there's something + # weird in initialization order between this class, + # Configurable, and TCPServer so we can't leave __init__ out + # completely) + pass + + def initialize(self, request_callback, no_keep_alive=False, io_loop=None, + xheaders=False, ssl_options=None, protocol=None, + decompress_request=False, + chunk_size=None, max_header_size=None, + idle_connection_timeout=None, body_timeout=None, + max_body_size=None, max_buffer_size=None): self.request_callback = request_callback self.no_keep_alive = no_keep_alive self.xheaders = xheaders @@ -142,6 +152,14 @@ class HTTPServer(TCPServer, httputil.HTTPServerConnectionDelegate): read_chunk_size=chunk_size) self._connections = set() + @classmethod + def configurable_base(cls): + return HTTPServer + + @classmethod + def configurable_default(cls): + return HTTPServer + @gen.coroutine def close_all_connections(self): while self._connections: diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index 62ef6ca3d..c1ba831cf 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -162,19 +162,22 @@ class BadSSLOptionsTest(unittest.TestCase): application = Application() module_dir = os.path.dirname(__file__) existing_certificate = os.path.join(module_dir, 'test.crt') + existing_key = os.path.join(module_dir, 'test.key') - self.assertRaises(ValueError, HTTPServer, application, ssl_options={ - "certfile": "/__mising__.crt", + self.assertRaises((ValueError, IOError), + HTTPServer, application, ssl_options={ + "certfile": "/__mising__.crt", }) - self.assertRaises(ValueError, HTTPServer, application, ssl_options={ - "certfile": existing_certificate, - "keyfile": "/__missing__.key" + self.assertRaises((ValueError, IOError), + HTTPServer, application, ssl_options={ + "certfile": existing_certificate, + "keyfile": "/__missing__.key" }) # This actually works because both files exist HTTPServer(application, ssl_options={ "certfile": existing_certificate, - "keyfile": existing_certificate + "keyfile": existing_key, }) diff --git a/tornado/test/runtests.py b/tornado/test/runtests.py index 20133d4e2..cb9969d3c 100644 --- a/tornado/test/runtests.py +++ b/tornado/test/runtests.py @@ -8,6 +8,7 @@ import operator import textwrap import sys from tornado.httpclient import AsyncHTTPClient +from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from tornado.netutil import Resolver from tornado.options import define, options, add_parse_callback @@ -123,6 +124,8 @@ def main(): define('httpclient', type=str, default=None, callback=lambda s: AsyncHTTPClient.configure( s, defaults=dict(allow_ipv6=False))) + define('httpserver', type=str, default=None, + callback=HTTPServer.configure) define('ioloop', type=str, default=None) define('ioloop_time_monotonic', default=False) define('resolver', type=str, default=None,