Skip to content

Commit

Permalink
Remove some excess vertical space.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed Jan 25, 2025
1 parent bd92cf7 commit 919cd92
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 36 deletions.
21 changes: 4 additions & 17 deletions src/websockets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,26 @@ def connect(self) -> Request:
"""
headers = Headers()

headers["Host"] = build_host(self.uri.host, self.uri.port, self.uri.secure)

if self.uri.user_info:
headers["Authorization"] = build_authorization_basic(*self.uri.user_info)

if self.origin is not None:
headers["Origin"] = self.origin

headers["Upgrade"] = "websocket"
headers["Connection"] = "Upgrade"
headers["Sec-WebSocket-Key"] = self.key
headers["Sec-WebSocket-Version"] = "13"

if self.available_extensions is not None:
extensions_header = build_extension(
headers["Sec-WebSocket-Extensions"] = build_extension(
[
(extension_factory.name, extension_factory.get_request_params())
for extension_factory in self.available_extensions
]
)
headers["Sec-WebSocket-Extensions"] = extensions_header

if self.available_subprotocols is not None:
protocol_header = build_subprotocol(self.available_subprotocols)
headers["Sec-WebSocket-Protocol"] = protocol_header

headers["Sec-WebSocket-Protocol"] = build_subprotocol(
self.available_subprotocols
)
return Request(self.uri.resource_name, headers)

def process_response(self, response: Response) -> None:
Expand All @@ -153,7 +146,6 @@ def process_response(self, response: Response) -> None:
connection: list[ConnectionOption] = sum(
[parse_connection(value) for value in headers.get_all("Connection")], []
)

if not any(value.lower() == "upgrade" for value in connection):
raise InvalidUpgrade(
"Connection", ", ".join(connection) if connection else None
Expand All @@ -162,7 +154,6 @@ def process_response(self, response: Response) -> None:
upgrade: list[UpgradeProtocol] = sum(
[parse_upgrade(value) for value in headers.get_all("Upgrade")], []
)

# For compatibility with non-strict implementations, ignore case when
# checking the Upgrade header. It's supposed to be 'WebSocket'.
if not (len(upgrade) == 1 and upgrade[0].lower() == "websocket"):
Expand All @@ -174,12 +165,10 @@ def process_response(self, response: Response) -> None:
raise InvalidHeader("Sec-WebSocket-Accept") from None
except MultipleValuesError:
raise InvalidHeader("Sec-WebSocket-Accept", "multiple values") from None

if s_w_accept != accept_key(self.key):
raise InvalidHeaderValue("Sec-WebSocket-Accept", s_w_accept)

self.extensions = self.process_extensions(headers)

self.subprotocol = self.process_subprotocol(headers)

def process_extensions(self, headers: Headers) -> list[Extension]:
Expand Down Expand Up @@ -279,15 +268,13 @@ def process_subprotocol(self, headers: Headers) -> Subprotocol | None:
parsed_subprotocols: Sequence[Subprotocol] = sum(
[parse_subprotocol(header_value) for header_value in subprotocols], []
)

if len(parsed_subprotocols) > 1:
raise InvalidHeader(
"Sec-WebSocket-Protocol",
f"multiple values: {', '.join(parsed_subprotocols)}",
)

subprotocol = parsed_subprotocols[0]

if subprotocol not in self.available_subprotocols:
raise NegotiationError(f"unsupported subprotocol: {subprotocol}")

Expand Down
21 changes: 2 additions & 19 deletions src/websockets/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,14 @@ def accept(self, request: Request) -> Response:
)

headers = Headers()

headers["Date"] = email.utils.formatdate(usegmt=True)

headers["Upgrade"] = "websocket"
headers["Connection"] = "Upgrade"
headers["Sec-WebSocket-Accept"] = accept_header

if extensions_header is not None:
headers["Sec-WebSocket-Extensions"] = extensions_header

if protocol_header is not None:
headers["Sec-WebSocket-Protocol"] = protocol_header

return Response(101, "Switching Protocols", headers)

def process_request(
Expand Down Expand Up @@ -234,7 +229,6 @@ def process_request(
connection: list[ConnectionOption] = sum(
[parse_connection(value) for value in headers.get_all("Connection")], []
)

if not any(value.lower() == "upgrade" for value in connection):
raise InvalidUpgrade(
"Connection", ", ".join(connection) if connection else None
Expand All @@ -243,7 +237,6 @@ def process_request(
upgrade: list[UpgradeProtocol] = sum(
[parse_upgrade(value) for value in headers.get_all("Upgrade")], []
)

# For compatibility with non-strict implementations, ignore case when
# checking the Upgrade header. The RFC always uses "websocket", except
# in section 11.2. (IANA registration) where it uses "WebSocket".
Expand All @@ -256,37 +249,28 @@ def process_request(
raise InvalidHeader("Sec-WebSocket-Key") from None
except MultipleValuesError:
raise InvalidHeader("Sec-WebSocket-Key", "multiple values") from None

try:
raw_key = base64.b64decode(key.encode(), validate=True)
except binascii.Error as exc:
raise InvalidHeaderValue("Sec-WebSocket-Key", key) from exc
if len(raw_key) != 16:
raise InvalidHeaderValue("Sec-WebSocket-Key", key)
accept_header = accept_key(key)

try:
version = headers["Sec-WebSocket-Version"]
except KeyError:
raise InvalidHeader("Sec-WebSocket-Version") from None
except MultipleValuesError:
raise InvalidHeader("Sec-WebSocket-Version", "multiple values") from None

if version != "13":
raise InvalidHeaderValue("Sec-WebSocket-Version", version)

accept_header = accept_key(key)

self.origin = self.process_origin(headers)

extensions_header, self.extensions = self.process_extensions(headers)

protocol_header = self.subprotocol = self.process_subprotocol(headers)

return (
accept_header,
extensions_header,
protocol_header,
)
return (accept_header, extensions_header, protocol_header)

def process_origin(self, headers: Headers) -> Origin | None:
"""
Expand Down Expand Up @@ -426,7 +410,6 @@ def process_subprotocol(self, headers: Headers) -> Subprotocol | None:
],
[],
)

return self.select_subprotocol(subprotocols)

def select_subprotocol(
Expand Down

0 comments on commit 919cd92

Please sign in to comment.