]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Precompile the regex used on severy set_header call.
authorBen Darnell <ben@bendarnell.com>
Sun, 5 May 2013 15:46:55 +0000 (11:46 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 5 May 2013 15:46:55 +0000 (11:46 -0400)
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

index 1c1af102a9dc981c56dd21251244a60159d49a95..ff3572c8b9086e5fe0636a99c0ce593979b74354 100644 (file)
@@ -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