Skip to content

Commit 6c3f1f8

Browse files
committed
always close connection for http < 1.0
1 parent 141dcd7 commit 6c3f1f8

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

aiohttp/protocol.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,11 @@ def enable_chunked_encoding(self):
550550

551551
def keep_alive(self):
552552
if self.keepalive is None:
553-
if self.version <= HttpVersion10:
554-
if self.headers.get('Connection') == 'keep-alive':
553+
if self.version < HttpVersion10:
554+
# keep alive not supported at all
555+
return False
556+
if self.version == HttpVersion10:
557+
if self.headers.get(hdrs.CONNECTION) == 'keep-alive':
555558
return True
556559
else: # no headers means we close for Http 1.0
557560
return False

aiohttp/web_reqrep.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
CIMultiDict,
1919
MultiDictProxy,
2020
MultiDict)
21-
from .protocol import Response as ResponseImpl
21+
from .protocol import Response as ResponseImpl, HttpVersion10
2222
from .streams import EOF_MARKER
2323

2424

@@ -95,7 +95,10 @@ def __init__(self, app, message, payload, transport, reader, writer, *,
9595
self._post = None
9696
self._post_files_cache = None
9797
self._headers = CIMultiDictProxy(message.headers)
98-
self._keep_alive = not message.should_close
98+
if self._version < HttpVersion10:
99+
self._keep_alive = False
100+
else:
101+
self._keep_alive = not message.should_close
99102

100103
# matchdict, route_name, handler
101104
# or information about traversal lookup

tests/test_web_functional.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import unittest
66
from aiohttp import web, request, FormData
77
from aiohttp.multidict import MultiDict
8-
from aiohttp.protocol import HttpVersion10, HttpVersion11
8+
from aiohttp.protocol import HttpVersion, HttpVersion10, HttpVersion11
99
from aiohttp.streams import EOF_MARKER
1010

1111

@@ -479,6 +479,24 @@ def go():
479479

480480
self.loop.run_until_complete(go())
481481

482+
def test_http09_keep_alive_default(self):
483+
484+
@asyncio.coroutine
485+
def handler(request):
486+
yield from request.read()
487+
return web.Response(body=b'OK')
488+
489+
@asyncio.coroutine
490+
def go():
491+
headers = {'Connection': 'keep-alive'} # should be ignored
492+
_, _, url = yield from self.create_server('GET', '/', handler)
493+
resp = yield from request('GET', url, loop=self.loop,
494+
headers=headers,
495+
version=HttpVersion(0, 9))
496+
self.assertEqual('close', resp.headers['CONNECTION'])
497+
498+
self.loop.run_until_complete(go())
499+
482500
def test_http10_keep_alive_with_headers_close(self):
483501

484502
@asyncio.coroutine

tests/test_web_response.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from aiohttp import hdrs
55
from aiohttp.multidict import CIMultiDict
66
from aiohttp.web import Request, StreamResponse, Response
7-
from aiohttp.protocol import RawRequestMessage, HttpVersion11, HttpVersion10
7+
from aiohttp.protocol import HttpVersion, HttpVersion11, HttpVersion10
8+
from aiohttp.protocol import RawRequestMessage
89

910

1011
class TestStreamResponse(unittest.TestCase):
@@ -356,7 +357,7 @@ def test_keep_alive_http10(self):
356357
req = self.request_from_message(message)
357358
resp = StreamResponse()
358359
resp.start(req)
359-
self.assertEqual(resp.keep_alive, False)
360+
self.assertFalse(resp.keep_alive)
360361

361362
headers = CIMultiDict(Connection='keep-alive')
362363
message = RawRequestMessage('GET', '/', HttpVersion10, headers,
@@ -366,6 +367,15 @@ def test_keep_alive_http10(self):
366367
resp.start(req)
367368
self.assertEqual(resp.keep_alive, True)
368369

370+
def test_keep_alive_http09(self):
371+
headers = CIMultiDict(Connection='keep-alive')
372+
message = RawRequestMessage('GET', '/', HttpVersion(0, 9), headers,
373+
False, False)
374+
req = self.request_from_message(message)
375+
resp = StreamResponse()
376+
resp.start(req)
377+
self.assertFalse(resp.keep_alive)
378+
369379

370380
class TestResponse(unittest.TestCase):
371381

0 commit comments

Comments
 (0)