Skip to content

WebSocket client does not support query component #5868

@hoh

Description

@hoh

Describe the bug

According to rfc6455 section 11.1.1, a "ws" URI Scheme supports a query component with the URI scheme syntax "ws:" "//" authority path-abempty [ "?" query ].

While the query component can be defined in ClienSession.request using the params argument, this argument is absent from ClientSession.ws_connect.

This requires users of ws_connect to build and embed the query component in the url agument of `ws_connect.

To Reproduce

Try running the following example:

async def main():
    params = {'key1': 'value1', 'key2': 'value2'}
    async with aiohttp.ClientSession() as session:
        async with session.ws_connect('http://example.org/ws', params=params) as ws:
            async for msg in ws:
                if msg.type == aiohttp.WSMsgType.TEXT:
                    if msg.data == 'close cmd':
                        await ws.close()
                        break
                    else:
                        await ws.send_str(msg.data + '/answer')
                elif msg.type == aiohttp.WSMsgType.ERROR:
                    break
                    
import asyncio, aiohttp
asyncio.run(main())

Expected behavior

The expected result is a response for the url http://example.org/ws?key1=value1&key2=value2.

Using the example target, this would give:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "<input>", line 4, in do
  File "/home/hugo/Repos/Aleph/aiohttp/aiohttp/client.py", line 1072, in __aenter__
    self._resp = await self._coro
  File "/home/hugo/Repos/Aleph/aiohttp/aiohttp/client.py", line 755, in _ws_connect
    raise WSServerHandshakeError(
aiohttp.client_exceptions.WSServerHandshakeError: 404, message='Invalid response status', url=URL('http://example.org/ws?key1=value1&key2=value2')

Logs/tracebacks

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "<input>", line 4, in do
TypeError: ws_connect() got an unexpected keyword argument 'params'


### Python Version

```console
Python 3.8.8

aiohttp Version

aiohttp master on commit 3d73221072c350d973b2fb0c54c78ad30863d433

multidict Version

Name: multidict
Version: 5.1.0
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /opt/venv/aiohttp/lib/python3.8/site-packages
Requires: 
Required-by: yarl

yarl Version

Name: yarl
Version: 1.6.3
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /opt/venv/aiohttp/lib/python3.8/site-packages
Requires: multidict, idna
Required-by:

OS

Linux 5.10.0-7-amd64 #1 SMP Debian 5.10.40-1 (2021-05-28) x86_64 GNU/Linux

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions