attempt to fix #24

This commit is contained in:
Maximilian Hils 2013-11-19 04:11:24 +01:00
parent d081b058e6
commit 5e4ccbd7ed
2 changed files with 9 additions and 30 deletions

View File

@ -283,32 +283,23 @@ def parse_init_http(line):
return method, url, httpversion return method, url, httpversion
def request_connection_close(httpversion, headers): def connection_close(httpversion, headers):
""" """
Checks the request to see if the client connection should be closed. Checks the message to see if the client connection should be closed according to RFC 2616 Section 8.1
""" """
# At first, check if we have an explicit Connection header.
if "connection" in headers: if "connection" in headers:
toks = get_header_tokens(headers, "connection") toks = get_header_tokens(headers, "connection")
if "close" in toks: if "close" in toks:
return True return True
elif "keep-alive" in toks: elif "keep-alive" in toks:
return False return False
# HTTP 1.1 connections are assumed to be persistent # If we don't have a Connection header, HTTP 1.1 connections are assumed to be persistent
if httpversion == (1, 1): if httpversion == (1, 1):
return False return False
return True return True
def response_connection_close(httpversion, headers):
"""
Checks the response to see if the client connection should be closed.
"""
if request_connection_close(httpversion, headers):
return True
elif (not has_chunked_encoding(headers)) and "content-length" in headers:
return False
return True
def read_http_body_request(rfile, wfile, headers, httpversion, limit): def read_http_body_request(rfile, wfile, headers, httpversion, limit):
""" """

View File

@ -38,28 +38,16 @@ def test_read_chunked():
tutils.raises("too large", http.read_chunked, 500, s, 2) tutils.raises("too large", http.read_chunked, 500, s, 2)
def test_request_connection_close(): def test_connection_close():
h = odict.ODictCaseless() h = odict.ODictCaseless()
assert http.request_connection_close((1, 0), h) assert http.connection_close((1, 0), h)
assert not http.request_connection_close((1, 1), h) assert not http.connection_close((1, 1), h)
h["connection"] = ["keep-alive"] h["connection"] = ["keep-alive"]
assert not http.request_connection_close((1, 1), h) assert not http.connection_close((1, 1), h)
h["connection"] = ["close"] h["connection"] = ["close"]
assert http.request_connection_close((1, 1), h) assert http.connection_close((1, 1), h)
def test_response_connection_close():
h = odict.ODictCaseless()
assert http.response_connection_close((1, 1), h)
h["content-length"] = [10]
assert not http.response_connection_close((1, 1), h)
h["connection"] = ["close"]
assert http.response_connection_close((1, 1), h)
def test_read_http_body_response(): def test_read_http_body_response():
h = odict.ODictCaseless() h = odict.ODictCaseless()