Skip to content

Commit

Permalink
Fix " Illegal target characters" when connecting to URL with zero-len…
Browse files Browse the repository at this point in the history
…gth path

This regressed in 0233e21 (version 0.9.0) due
to different behavior between yarl and urllib.parse.urlsplit on a URL
without a path component like "wss://ws.kraken.com". yarl makes this `/`
while urlsplit makes it ''. h11 expects `/` as specified in the quoted
RFC.
  • Loading branch information
bluetech committed Dec 6, 2020
1 parent ac1c976 commit b84ff74
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tests/test_connection.py
Expand Up @@ -284,6 +284,11 @@ async def test_client_open_url(echo_server):
async with open_websocket_url(url) as conn:
assert conn.path == RESOURCE + '?foo=bar'

# Zero-length path becomes '/' in the HTTP Request Target.
url = 'ws://{}:{}'.format(HOST, echo_server.port)
async with open_websocket_url(url) as conn:
assert conn.path == ''


async def test_client_open_invalid_url(echo_server):
with pytest.raises(ValueError):
Expand Down
9 changes: 8 additions & 1 deletion trio_websocket/_impl.py
Expand Up @@ -708,7 +708,14 @@ def __init__(self, stream, wsproto, *, host=None, path=None,
self._max_message_size = max_message_size
self._reader_running = True
if wsproto.client:
self._initial_request = Request(host=host, target=path,
if path:
target = path
else:
# RFC 7230, Section 5.3.1:
# If the target URI's path component is empty, the client MUST
# send "/" as the path within the origin-form of request-target.
target = '/'
self._initial_request = Request(host=host, target=target,
subprotocols=client_subprotocols,
extra_headers=client_extra_headers or [])
else:
Expand Down

0 comments on commit b84ff74

Please sign in to comment.