From: Ben Darnell Date: Mon, 4 Jul 2011 17:17:32 +0000 (-0700) Subject: Add RequestHandler.set_default_headers, which may be overridden to set X-Git-Tag: v2.1.0~128 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9481ce17efa8b09c3f6ac5455ae66a8aac663872;p=thirdparty%2Ftornado.git Add RequestHandler.set_default_headers, which may be overridden to set headers that will not be reset during error handling. --- diff --git a/tornado/web.py b/tornado/web.py index 205cf645c..648c5cc79 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -186,12 +186,23 @@ class RequestHandler(object): "Server": "TornadoServer/%s" % tornado.version, "Content-Type": "text/html; charset=UTF-8", } + self.set_default_headers() if not self.request.supports_http_1_1(): if self.request.headers.get("Connection") == "Keep-Alive": self.set_header("Connection", "Keep-Alive") self._write_buffer = [] self._status_code = 200 + def set_default_headers(self): + """Override this to set HTTP headers at the beginning of the request. + + For example, this is the place to set a custom ``Server`` header. + Note that setting such headers in the normal flow of request + processing may not do what you want, since headers may be reset + during error handling. + """ + pass + def set_status(self, status_code): """Sets the status code for our response.""" assert status_code in httplib.responses diff --git a/website/sphinx/overview.rst b/website/sphinx/overview.rst index c33e49933..0efb453ab 100644 --- a/website/sphinx/overview.rst +++ b/website/sphinx/overview.rst @@ -166,6 +166,8 @@ Other methods designed for overriding include: ``@authenticated`` decorator (default is in ``Application`` settings) - ``get_template_path(self)`` - returns location of template files (default is in ``Application`` settings) +- ``set_default_headers(self)`` - may be used to set additional headers + on the response (such as a custom ``Server`` header) Redirection ~~~~~~~~~~~ diff --git a/website/sphinx/releases/next.rst b/website/sphinx/releases/next.rst index f9d56abaa..9b5166fd6 100644 --- a/website/sphinx/releases/next.rst +++ b/website/sphinx/releases/next.rst @@ -36,6 +36,8 @@ New features * In `tornado.web.Application`, handlers may be specified by (fully-qualified) name instead of importing and passing the class object itself. +* `tornado.web.RequestHandler.set_default_headers` may be overridden to set + headers in a way that does not get reset during error handling. Bug fixes diff --git a/website/sphinx/web.rst b/website/sphinx/web.rst index fd1af5b87..2840c240a 100644 --- a/website/sphinx/web.rst +++ b/website/sphinx/web.rst @@ -39,6 +39,7 @@ .. automethod:: RequestHandler.set_status .. automethod:: RequestHandler.set_header + .. automethod:: RequestHandler.set_default_headers .. automethod:: RequestHandler.write .. automethod:: RequestHandler.flush .. automethod:: RequestHandler.finish