Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failed websocket handshake leaving connection hanging #3971

Merged
merged 1 commit into from
Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/3380.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix failed websocket handshake leaving connection hanging.
3 changes: 3 additions & 0 deletions aiohttp/_http_parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,9 @@ cdef class HttpParser:
else:
return messages, False, b''

def set_upgraded(self, val):
self._upgraded = val


cdef class HttpRequestParser(HttpParser):

Expand Down
7 changes: 7 additions & 0 deletions aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ def parse_headers(

return (headers, raw_headers, close_conn, encoding, upgrade, chunked)

def set_upgraded(self, val: bool) -> None:
"""Set connection upgraded (to websocket) mode.

:param bool val: new state.
"""
self._upgraded = val


class HttpRequestParser(HttpParser):
"""Read request status line. Exception .http_exceptions.BadStatusLine
Expand Down
6 changes: 6 additions & 0 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ async def finish_response(self,
can get exception information. Returns True if the client disconnects
prematurely.
"""
if self._request_parser is not None:
self._request_parser.set_upgraded(False)
self._upgrade = False
if self._message_tail:
self._request_parser.feed_data(self._message_tail)
self._message_tail = b''
try:
prepare_meth = resp.prepare
except AttributeError:
Expand Down
28 changes: 27 additions & 1 deletion tests/test_web_websocket_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

import aiohttp
from aiohttp import web
from aiohttp import WSServerHandshakeError, web
from aiohttp.http import WSMsgType


Expand Down Expand Up @@ -827,3 +827,29 @@ async def handler(request):
ws = await client.ws_connect('/')
with pytest.raises(TypeError):
await ws.receive_bytes()


async def test_bug3380(loop, aiohttp_client) -> None:

async def handle_null(request):
return aiohttp.web.json_response({'err': None})

async def ws_handler(request):
return web.Response(status=401)

app = web.Application()
app.router.add_route('GET', '/ws', ws_handler)
app.router.add_route('GET', '/api/null', handle_null)

client = await aiohttp_client(app)

resp = await client.get('/api/null')
assert (await resp.json()) == {'err': None}
resp.close()

with pytest.raises(WSServerHandshakeError):
await client.ws_connect('/ws')

resp = await client.get('/api/null', timeout=1)
assert (await resp.json()) == {'err': None}
resp.close()