Skip to content

Commit a1bf000

Browse files
committed
Rename the wsuri variable.
The name looked confusing.
1 parent c42672b commit a1bf000

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

docs/howto/sansio.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ If you're building a client, parse the URI you'd like to connect to::
2828

2929
from websockets.uri import parse_uri
3030

31-
wsuri = parse_uri("ws://example.com/")
31+
uri = parse_uri("ws://example.com/")
3232

33-
Open a TCP connection to ``(wsuri.host, wsuri.port)`` and perform a TLS
34-
handshake if ``wsuri.secure`` is :obj:`True`.
33+
Open a TCP connection to ``(uri.host, uri.port)`` and perform a TLS handshake
34+
if ``uri.secure`` is :obj:`True`.
3535

3636
Initialize a :class:`~client.ClientProtocol`::
3737

3838
from websockets.client import ClientProtocol
3939

40-
protocol = ClientProtocol(wsuri)
40+
protocol = ClientProtocol(uri)
4141

4242
Create a WebSocket handshake request
4343
with :meth:`~client.ClientProtocol.connect` and send it

src/websockets/asyncio/client.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,10 @@ def __init__(
311311
if create_connection is None:
312312
create_connection = ClientConnection
313313

314-
def protocol_factory(wsuri: WebSocketURI) -> ClientConnection:
314+
def protocol_factory(uri: WebSocketURI) -> ClientConnection:
315315
# This is a protocol in the Sans-I/O implementation of websockets.
316316
protocol = ClientProtocol(
317-
wsuri,
317+
uri,
318318
origin=origin,
319319
extensions=extensions,
320320
subprotocols=subprotocols,
@@ -346,15 +346,15 @@ async def create_connection(self) -> ClientConnection:
346346
"""Create TCP or Unix connection."""
347347
loop = asyncio.get_running_loop()
348348

349-
wsuri = parse_uri(self.uri)
349+
ws_uri = parse_uri(self.uri)
350350
kwargs = self.connection_kwargs.copy()
351351

352352
def factory() -> ClientConnection:
353-
return self.protocol_factory(wsuri)
353+
return self.protocol_factory(ws_uri)
354354

355-
if wsuri.secure:
355+
if ws_uri.secure:
356356
kwargs.setdefault("ssl", True)
357-
kwargs.setdefault("server_hostname", wsuri.host)
357+
kwargs.setdefault("server_hostname", ws_uri.host)
358358
if kwargs.get("ssl") is None:
359359
raise ValueError("ssl=None is incompatible with a wss:// URI")
360360
else:
@@ -365,8 +365,8 @@ def factory() -> ClientConnection:
365365
_, connection = await loop.create_unix_connection(factory, **kwargs)
366366
else:
367367
if kwargs.get("sock") is None:
368-
kwargs.setdefault("host", wsuri.host)
369-
kwargs.setdefault("port", wsuri.port)
368+
kwargs.setdefault("host", ws_uri.host)
369+
kwargs.setdefault("port", ws_uri.port)
370370
_, connection = await loop.create_connection(factory, **kwargs)
371371
return connection
372372

@@ -392,9 +392,9 @@ def process_redirect(self, exc: Exception) -> Exception | str:
392392
):
393393
return exc
394394

395-
old_wsuri = parse_uri(self.uri)
395+
old_ws_uri = parse_uri(self.uri)
396396
new_uri = urllib.parse.urljoin(self.uri, exc.response.headers["Location"])
397-
new_wsuri = parse_uri(new_uri)
397+
new_ws_uri = parse_uri(new_uri)
398398

399399
# If connect() received a socket, it is closed and cannot be reused.
400400
if self.connection_kwargs.get("sock") is not None:
@@ -403,14 +403,14 @@ def process_redirect(self, exc: Exception) -> Exception | str:
403403
)
404404

405405
# TLS downgrade is forbidden.
406-
if old_wsuri.secure and not new_wsuri.secure:
406+
if old_ws_uri.secure and not new_ws_uri.secure:
407407
return SecurityError(f"cannot follow redirect to non-secure URI {new_uri}")
408408

409409
# Apply restrictions to cross-origin redirects.
410410
if (
411-
old_wsuri.secure != new_wsuri.secure
412-
or old_wsuri.host != new_wsuri.host
413-
or old_wsuri.port != new_wsuri.port
411+
old_ws_uri.secure != new_ws_uri.secure
412+
or old_ws_uri.host != new_ws_uri.host
413+
or old_ws_uri.port != new_ws_uri.port
414414
):
415415
# Cross-origin redirects on Unix sockets don't quite make sense.
416416
if self.connection_kwargs.get("unix", False):

src/websockets/client.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ClientProtocol(Protocol):
5050
Sans-I/O implementation of a WebSocket client connection.
5151
5252
Args:
53-
wsuri: URI of the WebSocket server, parsed
53+
uri: URI of the WebSocket server, parsed
5454
with :func:`~websockets.uri.parse_uri`.
5555
origin: Value of the ``Origin`` header. This is useful when connecting
5656
to a server that validates the ``Origin`` header to defend against
@@ -70,7 +70,7 @@ class ClientProtocol(Protocol):
7070

7171
def __init__(
7272
self,
73-
wsuri: WebSocketURI,
73+
uri: WebSocketURI,
7474
*,
7575
origin: Origin | None = None,
7676
extensions: Sequence[ClientExtensionFactory] | None = None,
@@ -85,7 +85,7 @@ def __init__(
8585
max_size=max_size,
8686
logger=logger,
8787
)
88-
self.wsuri = wsuri
88+
self.uri = uri
8989
self.origin = origin
9090
self.available_extensions = extensions
9191
self.available_subprotocols = subprotocols
@@ -105,12 +105,10 @@ def connect(self) -> Request:
105105
"""
106106
headers = Headers()
107107

108-
headers["Host"] = build_host(
109-
self.wsuri.host, self.wsuri.port, self.wsuri.secure
110-
)
108+
headers["Host"] = build_host(self.uri.host, self.uri.port, self.uri.secure)
111109

112-
if self.wsuri.user_info:
113-
headers["Authorization"] = build_authorization_basic(*self.wsuri.user_info)
110+
if self.uri.user_info:
111+
headers["Authorization"] = build_authorization_basic(*self.uri.user_info)
114112

115113
if self.origin is not None:
116114
headers["Origin"] = self.origin
@@ -133,7 +131,7 @@ def connect(self) -> Request:
133131
protocol_header = build_subprotocol(self.available_subprotocols)
134132
headers["Sec-WebSocket-Protocol"] = protocol_header
135133

136-
return Request(self.wsuri.resource_name, headers)
134+
return Request(self.uri.resource_name, headers)
137135

138136
def process_response(self, response: Response) -> None:
139137
"""

src/websockets/sync/client.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ def connect(
230230
DeprecationWarning,
231231
)
232232

233-
wsuri = parse_uri(uri)
234-
if not wsuri.secure and ssl is not None:
233+
ws_uri = parse_uri(uri)
234+
if not ws_uri.secure and ssl is not None:
235235
raise ValueError("ssl argument is incompatible with a ws:// URI")
236236

237237
# Private APIs for unix_connect()
@@ -271,7 +271,7 @@ def connect(
271271
sock.connect(path)
272272
else:
273273
kwargs.setdefault("timeout", deadline.timeout())
274-
sock = socket.create_connection((wsuri.host, wsuri.port), **kwargs)
274+
sock = socket.create_connection((ws_uri.host, ws_uri.port), **kwargs)
275275
sock.settimeout(None)
276276

277277
# Disable Nagle algorithm
@@ -281,19 +281,19 @@ def connect(
281281

282282
# Initialize TLS wrapper and perform TLS handshake
283283

284-
if wsuri.secure:
284+
if ws_uri.secure:
285285
if ssl is None:
286286
ssl = ssl_module.create_default_context()
287287
if server_hostname is None:
288-
server_hostname = wsuri.host
288+
server_hostname = ws_uri.host
289289
sock.settimeout(deadline.timeout())
290290
sock = ssl.wrap_socket(sock, server_hostname=server_hostname)
291291
sock.settimeout(None)
292292

293293
# Initialize WebSocket protocol
294294

295295
protocol = ClientProtocol(
296-
wsuri,
296+
ws_uri,
297297
origin=origin,
298298
extensions=extensions,
299299
subprotocols=subprotocols,

tests/asyncio/test_client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def redirect(connection, request):
248248

249249
async with serve(*args, process_request=redirect) as server:
250250
async with connect(get_uri(server) + "/redirect") as client:
251-
self.assertEqual(client.protocol.wsuri.path, "/")
251+
self.assertEqual(client.protocol.uri.path, "/")
252252

253253
async def test_cross_origin_redirect(self):
254254
"""Client follows redirect to a secure URI on a different origin."""
@@ -297,7 +297,7 @@ def redirect(connection, request):
297297
async with connect(
298298
"ws://overridden/redirect", host=host, port=port
299299
) as client:
300-
self.assertEqual(client.protocol.wsuri.path, "/")
300+
self.assertEqual(client.protocol.uri.path, "/")
301301

302302
async def test_cross_origin_redirect_with_explicit_host_port(self):
303303
"""Client doesn't follow cross-origin redirect with an explicit host / port."""

0 commit comments

Comments
 (0)