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.
This commit is contained in:
benoitc 2013-08-12 11:39:11 +02:00
parent 9f4cf4181f
commit b3d85a0b67

View File

@ -194,8 +194,7 @@ class Response(object):
return True return True
if self.response_length is not None or self.chunked: if self.response_length is not None or self.chunked:
return False return False
status = self.status.split()[0] if self.status else '' if self.status_code < 200 or self.status_code in (204, 304):
if status.startswith('1') or status in ('204', '304'):
return False return False
return True return True
@ -210,6 +209,15 @@ class Response(object):
raise AssertionError("Response headers already set!") raise AssertionError("Response headers already set!")
self.status = status 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.process_headers(headers)
self.chunked = self.is_chunked() self.chunked = self.is_chunked()
return self.write return self.write
@ -243,7 +251,7 @@ class Response(object):
return False return False
elif self.req.version <= (1, 0): elif self.req.version <= (1, 0):
return False 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 # Do not use chunked responses when the response is guaranteed to
# not have a response body. # not have a response body.
return False return False