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

Accept non-GET request for starting websocket handshake on server side #3980

Merged
merged 2 commits into from
Aug 15, 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/3980.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Accept non-GET request for starting websocket handshake on server side.
4 changes: 1 addition & 3 deletions aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from .log import ws_logger
from .streams import EofStream, FlowControlDataQueue
from .typedefs import JSONDecoder, JSONEncoder
from .web_exceptions import HTTPBadRequest, HTTPException, HTTPMethodNotAllowed
from .web_exceptions import HTTPBadRequest, HTTPException
from .web_request import BaseRequest
from .web_response import StreamResponse

Expand Down Expand Up @@ -134,8 +134,6 @@ def _handshake(self, request: BaseRequest) -> Tuple['CIMultiDict[str]',
bool,
bool]:
headers = request.headers
if request.method != hdrs.METH_GET:
raise HTTPMethodNotAllowed(request.method, [hdrs.METH_GET])
if 'websocket' != headers.get(hdrs.UPGRADE, '').lower().strip():
raise HTTPBadRequest(
text=('No WebSocket UPGRADE hdr: {}\n Can '
Expand Down
14 changes: 4 additions & 10 deletions tests/test_web_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from aiohttp.log import ws_logger
from aiohttp.streams import EofStream
from aiohttp.test_utils import make_mocked_coro, make_mocked_request
from aiohttp.web import HTTPBadRequest, HTTPMethodNotAllowed, WebSocketResponse
from aiohttp.web import HTTPBadRequest, WebSocketResponse
from aiohttp.web_ws import WS_CLOSED_MESSAGE, WebSocketReady


Expand Down Expand Up @@ -175,12 +175,6 @@ def test_can_prepare_unknown_protocol(make_request) -> None:
assert WebSocketReady(True, None) == ws.can_prepare(req)


def test_can_prepare_invalid_method(make_request) -> None:
req = make_request('POST', '/')
ws = WebSocketResponse()
assert WebSocketReady(False, None) == ws.can_prepare(req)


def test_can_prepare_without_upgrade(make_request) -> None:
req = make_request('GET', '/',
headers=CIMultiDict({}))
Expand Down Expand Up @@ -274,11 +268,11 @@ async def test_close_idempotent(make_request) -> None:
assert not (await ws.close(code=2, message='message2'))


async def test_prepare_invalid_method(make_request) -> None:
async def test_prepare_post_method_ok(make_request) -> None:
req = make_request('POST', '/')
ws = WebSocketResponse()
with pytest.raises(HTTPMethodNotAllowed):
await ws.prepare(req)
await ws.prepare(req)
assert ws.prepared


async def test_prepare_without_upgrade(make_request) -> None:
Expand Down
7 changes: 0 additions & 7 deletions tests/test_websocket_handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ def gen_ws_headers(protocols='', compress=0, extension_text='',
return hdrs, key


async def test_not_get() -> None:
ws = web.WebSocketResponse()
req = make_mocked_request('POST', '/')
with pytest.raises(web.HTTPMethodNotAllowed):
await ws.prepare(req)


async def test_no_upgrade() -> None:
ws = web.WebSocketResponse()
req = make_mocked_request('GET', '/')
Expand Down