From b3d85a0b670bb90e1297ebe2f4e871cdc862f329 Mon Sep 17 00:00:00 2001 From: benoitc Date: Mon, 12 Aug 2013 11:39:11 +0200 Subject: [PATCH] optimise response connection header check Get the status code from the response once so we can use it to check the need for the connection header later without parsing the string each time we need it. --- gunicorn/http/wsgi.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index d017c4b8..e50c49fc 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -194,8 +194,7 @@ class Response(object): return True if self.response_length is not None or self.chunked: return False - status = self.status.split()[0] if self.status else '' - if status.startswith('1') or status in ('204', '304'): + if self.status_code < 200 or self.status_code in (204, 304): return False return True @@ -210,6 +209,15 @@ class Response(object): raise AssertionError("Response headers already set!") self.status = status + + # get the status code from the response here so we can use it to check + # the need for the connection header later without parsing the string + # each time. + try: + self.status_code = int(self.status.split()[0]) + except ValueError: + self.status_code = None + self.process_headers(headers) self.chunked = self.is_chunked() return self.write @@ -243,7 +251,7 @@ class Response(object): return False elif self.req.version <= (1, 0): return False - elif self.status.startswith("304") or self.status.startswith("204"): + elif self.status_code in (204, 304): # Do not use chunked responses when the response is guaranteed to # not have a response body. return False