From 671f992409155786d5587a2cdfd30127fd2df5a0 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sun, 5 May 2013 11:46:55 -0400 Subject: [PATCH] Precompile the regex used on severy set_header call. The regex was cached by the re module, but the overhead of looking it up in the cache added up to almost 1% of the time in benchmark.py. --- tornado/web.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tornado/web.py b/tornado/web.py index 1c1af102a..ff3572c8b 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -301,6 +301,8 @@ class RequestHandler(object): if name in self._headers: del self._headers[name] + _INVALID_HEADER_CHAR_RE = re.compile(br"[\x00-\x1f]") + def _convert_header_value(self, value): if isinstance(value, bytes_type): pass @@ -316,7 +318,8 @@ class RequestHandler(object): # If \n is allowed into the header, it is possible to inject # additional headers or split the request. Also cap length to # prevent obviously erroneous values. - if len(value) > 4000 or re.search(br"[\x00-\x1f]", value): + if (len(value) > 4000 or + RequestHandler._INVALID_HEADER_CHAR_RE.search(value)): raise ValueError("Unsafe header value %r", value) return value -- 2.47.2