Skip to content

Commit

Permalink
[3.6] Accept non-GET request for starting websocket handshake on serv…
Browse files Browse the repository at this point in the history
…er side (#3980)

(cherry picked from commit 8f619a5)

Co-authored-by: Andrew Svetlov <[email protected]>
  • Loading branch information
asvetlov committed Aug 15, 2019
1 parent d4f05dd commit 58c72e1
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 20 deletions.
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 @@ -130,8 +130,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 @@ -203,12 +203,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 @@ -302,11 +296,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

0 comments on commit 58c72e1

Please sign in to comment.