@@ -104,33 +104,26 @@ def connect(self) -> Request:
104
104
105
105
"""
106
106
headers = Headers ()
107
-
108
107
headers ["Host" ] = build_host (self .uri .host , self .uri .port , self .uri .secure )
109
-
110
108
if self .uri .user_info :
111
109
headers ["Authorization" ] = build_authorization_basic (* self .uri .user_info )
112
-
113
110
if self .origin is not None :
114
111
headers ["Origin" ] = self .origin
115
-
116
112
headers ["Upgrade" ] = "websocket"
117
113
headers ["Connection" ] = "Upgrade"
118
114
headers ["Sec-WebSocket-Key" ] = self .key
119
115
headers ["Sec-WebSocket-Version" ] = "13"
120
-
121
116
if self .available_extensions is not None :
122
- extensions_header = build_extension (
117
+ headers [ "Sec-WebSocket-Extensions" ] = build_extension (
123
118
[
124
119
(extension_factory .name , extension_factory .get_request_params ())
125
120
for extension_factory in self .available_extensions
126
121
]
127
122
)
128
- headers ["Sec-WebSocket-Extensions" ] = extensions_header
129
-
130
123
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
+ )
134
127
return Request (self .uri .resource_name , headers )
135
128
136
129
def process_response (self , response : Response ) -> None :
@@ -153,7 +146,6 @@ def process_response(self, response: Response) -> None:
153
146
connection : list [ConnectionOption ] = sum (
154
147
[parse_connection (value ) for value in headers .get_all ("Connection" )], []
155
148
)
156
-
157
149
if not any (value .lower () == "upgrade" for value in connection ):
158
150
raise InvalidUpgrade (
159
151
"Connection" , ", " .join (connection ) if connection else None
@@ -162,7 +154,6 @@ def process_response(self, response: Response) -> None:
162
154
upgrade : list [UpgradeProtocol ] = sum (
163
155
[parse_upgrade (value ) for value in headers .get_all ("Upgrade" )], []
164
156
)
165
-
166
157
# For compatibility with non-strict implementations, ignore case when
167
158
# checking the Upgrade header. It's supposed to be 'WebSocket'.
168
159
if not (len (upgrade ) == 1 and upgrade [0 ].lower () == "websocket" ):
@@ -174,12 +165,10 @@ def process_response(self, response: Response) -> None:
174
165
raise InvalidHeader ("Sec-WebSocket-Accept" ) from None
175
166
except MultipleValuesError :
176
167
raise InvalidHeader ("Sec-WebSocket-Accept" , "multiple values" ) from None
177
-
178
168
if s_w_accept != accept_key (self .key ):
179
169
raise InvalidHeaderValue ("Sec-WebSocket-Accept" , s_w_accept )
180
170
181
171
self .extensions = self .process_extensions (headers )
182
-
183
172
self .subprotocol = self .process_subprotocol (headers )
184
173
185
174
def process_extensions (self , headers : Headers ) -> list [Extension ]:
@@ -279,15 +268,13 @@ def process_subprotocol(self, headers: Headers) -> Subprotocol | None:
279
268
parsed_subprotocols : Sequence [Subprotocol ] = sum (
280
269
[parse_subprotocol (header_value ) for header_value in subprotocols ], []
281
270
)
282
-
283
271
if len (parsed_subprotocols ) > 1 :
284
272
raise InvalidHeader (
285
273
"Sec-WebSocket-Protocol" ,
286
274
f"multiple values: { ', ' .join (parsed_subprotocols )} " ,
287
275
)
288
276
289
277
subprotocol = parsed_subprotocols [0 ]
290
-
291
278
if subprotocol not in self .available_subprotocols :
292
279
raise NegotiationError (f"unsupported subprotocol: { subprotocol } " )
293
280
0 commit comments