Skip to content

Commit 919cd92

Browse files
committed
Remove some excess vertical space.
1 parent bd92cf7 commit 919cd92

File tree

2 files changed

+6
-36
lines changed

2 files changed

+6
-36
lines changed

src/websockets/client.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -104,33 +104,26 @@ def connect(self) -> Request:
104104
105105
"""
106106
headers = Headers()
107-
108107
headers["Host"] = build_host(self.uri.host, self.uri.port, self.uri.secure)
109-
110108
if self.uri.user_info:
111109
headers["Authorization"] = build_authorization_basic(*self.uri.user_info)
112-
113110
if self.origin is not None:
114111
headers["Origin"] = self.origin
115-
116112
headers["Upgrade"] = "websocket"
117113
headers["Connection"] = "Upgrade"
118114
headers["Sec-WebSocket-Key"] = self.key
119115
headers["Sec-WebSocket-Version"] = "13"
120-
121116
if self.available_extensions is not None:
122-
extensions_header = build_extension(
117+
headers["Sec-WebSocket-Extensions"] = build_extension(
123118
[
124119
(extension_factory.name, extension_factory.get_request_params())
125120
for extension_factory in self.available_extensions
126121
]
127122
)
128-
headers["Sec-WebSocket-Extensions"] = extensions_header
129-
130123
if self.available_subprotocols is not None:
131-
protocol_header = build_subprotocol(self.available_subprotocols)
132-
headers["Sec-WebSocket-Protocol"] = protocol_header
133-
124+
headers["Sec-WebSocket-Protocol"] = build_subprotocol(
125+
self.available_subprotocols
126+
)
134127
return Request(self.uri.resource_name, headers)
135128

136129
def process_response(self, response: Response) -> None:
@@ -153,7 +146,6 @@ def process_response(self, response: Response) -> None:
153146
connection: list[ConnectionOption] = sum(
154147
[parse_connection(value) for value in headers.get_all("Connection")], []
155148
)
156-
157149
if not any(value.lower() == "upgrade" for value in connection):
158150
raise InvalidUpgrade(
159151
"Connection", ", ".join(connection) if connection else None
@@ -162,7 +154,6 @@ def process_response(self, response: Response) -> None:
162154
upgrade: list[UpgradeProtocol] = sum(
163155
[parse_upgrade(value) for value in headers.get_all("Upgrade")], []
164156
)
165-
166157
# For compatibility with non-strict implementations, ignore case when
167158
# checking the Upgrade header. It's supposed to be 'WebSocket'.
168159
if not (len(upgrade) == 1 and upgrade[0].lower() == "websocket"):
@@ -174,12 +165,10 @@ def process_response(self, response: Response) -> None:
174165
raise InvalidHeader("Sec-WebSocket-Accept") from None
175166
except MultipleValuesError:
176167
raise InvalidHeader("Sec-WebSocket-Accept", "multiple values") from None
177-
178168
if s_w_accept != accept_key(self.key):
179169
raise InvalidHeaderValue("Sec-WebSocket-Accept", s_w_accept)
180170

181171
self.extensions = self.process_extensions(headers)
182-
183172
self.subprotocol = self.process_subprotocol(headers)
184173

185174
def process_extensions(self, headers: Headers) -> list[Extension]:
@@ -279,15 +268,13 @@ def process_subprotocol(self, headers: Headers) -> Subprotocol | None:
279268
parsed_subprotocols: Sequence[Subprotocol] = sum(
280269
[parse_subprotocol(header_value) for header_value in subprotocols], []
281270
)
282-
283271
if len(parsed_subprotocols) > 1:
284272
raise InvalidHeader(
285273
"Sec-WebSocket-Protocol",
286274
f"multiple values: {', '.join(parsed_subprotocols)}",
287275
)
288276

289277
subprotocol = parsed_subprotocols[0]
290-
291278
if subprotocol not in self.available_subprotocols:
292279
raise NegotiationError(f"unsupported subprotocol: {subprotocol}")
293280

src/websockets/server.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,14 @@ def accept(self, request: Request) -> Response:
190190
)
191191

192192
headers = Headers()
193-
194193
headers["Date"] = email.utils.formatdate(usegmt=True)
195-
196194
headers["Upgrade"] = "websocket"
197195
headers["Connection"] = "Upgrade"
198196
headers["Sec-WebSocket-Accept"] = accept_header
199-
200197
if extensions_header is not None:
201198
headers["Sec-WebSocket-Extensions"] = extensions_header
202-
203199
if protocol_header is not None:
204200
headers["Sec-WebSocket-Protocol"] = protocol_header
205-
206201
return Response(101, "Switching Protocols", headers)
207202

208203
def process_request(
@@ -234,7 +229,6 @@ def process_request(
234229
connection: list[ConnectionOption] = sum(
235230
[parse_connection(value) for value in headers.get_all("Connection")], []
236231
)
237-
238232
if not any(value.lower() == "upgrade" for value in connection):
239233
raise InvalidUpgrade(
240234
"Connection", ", ".join(connection) if connection else None
@@ -243,7 +237,6 @@ def process_request(
243237
upgrade: list[UpgradeProtocol] = sum(
244238
[parse_upgrade(value) for value in headers.get_all("Upgrade")], []
245239
)
246-
247240
# For compatibility with non-strict implementations, ignore case when
248241
# checking the Upgrade header. The RFC always uses "websocket", except
249242
# in section 11.2. (IANA registration) where it uses "WebSocket".
@@ -256,37 +249,28 @@ def process_request(
256249
raise InvalidHeader("Sec-WebSocket-Key") from None
257250
except MultipleValuesError:
258251
raise InvalidHeader("Sec-WebSocket-Key", "multiple values") from None
259-
260252
try:
261253
raw_key = base64.b64decode(key.encode(), validate=True)
262254
except binascii.Error as exc:
263255
raise InvalidHeaderValue("Sec-WebSocket-Key", key) from exc
264256
if len(raw_key) != 16:
265257
raise InvalidHeaderValue("Sec-WebSocket-Key", key)
258+
accept_header = accept_key(key)
266259

267260
try:
268261
version = headers["Sec-WebSocket-Version"]
269262
except KeyError:
270263
raise InvalidHeader("Sec-WebSocket-Version") from None
271264
except MultipleValuesError:
272265
raise InvalidHeader("Sec-WebSocket-Version", "multiple values") from None
273-
274266
if version != "13":
275267
raise InvalidHeaderValue("Sec-WebSocket-Version", version)
276268

277-
accept_header = accept_key(key)
278-
279269
self.origin = self.process_origin(headers)
280-
281270
extensions_header, self.extensions = self.process_extensions(headers)
282-
283271
protocol_header = self.subprotocol = self.process_subprotocol(headers)
284272

285-
return (
286-
accept_header,
287-
extensions_header,
288-
protocol_header,
289-
)
273+
return (accept_header, extensions_header, protocol_header)
290274

291275
def process_origin(self, headers: Headers) -> Origin | None:
292276
"""
@@ -426,7 +410,6 @@ def process_subprotocol(self, headers: Headers) -> Subprotocol | None:
426410
],
427411
[],
428412
)
429-
430413
return self.select_subprotocol(subprotocols)
431414

432415
def select_subprotocol(

0 commit comments

Comments
 (0)