From: Ben Darnell Date: Sun, 5 May 2013 15:46:55 +0000 (-0400) Subject: Precompile the regex used on severy set_header call. X-Git-Tag: v3.1.0~81 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=671f992409155786d5587a2cdfd30127fd2df5a0;p=thirdparty%2Ftornado.git 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. --- 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