|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Globalization; |
6 | 8 | using System.IO; |
7 | 9 | using System.Linq; |
| 10 | +using System.Net.Http.Headers; |
8 | 11 | using System.Net.WebSockets; |
| 12 | +using System.Text; |
9 | 13 | using System.Threading.Tasks; |
10 | 14 | using Microsoft.AspNetCore.Builder; |
11 | 15 | using Microsoft.AspNetCore.Http; |
@@ -151,9 +155,46 @@ public async Task<WebSocket> AcceptAsync(WebSocketAcceptContext acceptContext) |
151 | 155 |
|
152 | 156 | HandshakeHelpers.GenerateResponseHeaders(key, subProtocol, _context.Response.Headers); |
153 | 157 |
|
| 158 | + // TODO: get from options |
| 159 | + WebSocketDeflateOptions? deflateOptions = null; |
| 160 | + var ext = _context.Request.Headers["Sec-WebSocket-Extensions"]; |
| 161 | + if (ext.Count != 0) |
| 162 | + { |
| 163 | + var decline = false; |
| 164 | + foreach (var extension in ext) |
| 165 | + { |
| 166 | + if (extension.TrimStart().StartsWith(ClientWebSocketDeflateConstants.Extension, StringComparison.Ordinal)) |
| 167 | + { |
| 168 | + deflateOptions = new(); |
| 169 | + if (ParseDeflateOptions(extension, deflateOptions, out var hasClientMaxWindowBits)) |
| 170 | + { |
| 171 | + Resp(_context.Response.Headers, deflateOptions, hasClientMaxWindowBits); |
| 172 | + decline = false; |
| 173 | + break; |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + decline = true; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + if (decline) |
| 182 | + { |
| 183 | + throw new InvalidOperationException("'permessage-deflate' extension not accepted."); |
| 184 | + } |
| 185 | + } |
| 186 | + |
154 | 187 | Stream opaqueTransport = await _upgradeFeature.UpgradeAsync(); // Sets status code to 101 |
155 | 188 |
|
156 | | - return WebSocket.CreateFromStream(opaqueTransport, isServer: true, subProtocol: subProtocol, keepAliveInterval: keepAliveInterval); |
| 189 | + var options = new WebSocketCreationOptions() |
| 190 | + { |
| 191 | + IsServer = true, |
| 192 | + KeepAliveInterval = keepAliveInterval, |
| 193 | + SubProtocol = subProtocol, |
| 194 | + DangerousDeflateOptions = deflateOptions, |
| 195 | + }; |
| 196 | + |
| 197 | + return WebSocket.CreateFromStream(opaqueTransport, options); |
157 | 198 | } |
158 | 199 |
|
159 | 200 | public static bool CheckSupportedWebSocketRequest(string method, IHeaderDictionary requestHeaders) |
@@ -226,6 +267,142 @@ public static bool CheckSupportedWebSocketRequest(string method, IHeaderDictiona |
226 | 267 |
|
227 | 268 | return HandshakeHelpers.IsRequestKeyValid(requestHeaders.SecWebSocketKey.ToString()); |
228 | 269 | } |
| 270 | + |
| 271 | + internal static class ClientWebSocketDeflateConstants |
| 272 | + { |
| 273 | + /// <summary> |
| 274 | + /// The maximum length that this extension can have, assuming that we're not abusing white space. |
| 275 | + /// <para /> |
| 276 | + /// "permessage-deflate; client_max_window_bits=15; client_no_context_takeover; server_max_window_bits=15; server_no_context_takeover" |
| 277 | + /// </summary> |
| 278 | + public const int MaxExtensionLength = 128; |
| 279 | + |
| 280 | + public const string Extension = "permessage-deflate"; |
| 281 | + |
| 282 | + public const string ClientMaxWindowBits = "client_max_window_bits"; |
| 283 | + public const string ClientNoContextTakeover = "client_no_context_takeover"; |
| 284 | + |
| 285 | + public const string ServerMaxWindowBits = "server_max_window_bits"; |
| 286 | + public const string ServerNoContextTakeover = "server_no_context_takeover"; |
| 287 | + } |
| 288 | + |
| 289 | + private static bool ParseDeflateOptions(ReadOnlySpan<char> extension, WebSocketDeflateOptions options, out bool hasClientMaxWindowBits) |
| 290 | + { |
| 291 | + hasClientMaxWindowBits = false; |
| 292 | + while (true) |
| 293 | + { |
| 294 | + int end = extension.IndexOf(';'); |
| 295 | + ReadOnlySpan<char> value = (end >= 0 ? extension[..end] : extension).Trim(); |
| 296 | + |
| 297 | + if (value.Length > 0) |
| 298 | + { |
| 299 | + if (value.SequenceEqual(ClientWebSocketDeflateConstants.ClientNoContextTakeover)) |
| 300 | + { |
| 301 | + options.ClientContextTakeover = false; |
| 302 | + } |
| 303 | + else if (value.SequenceEqual(ClientWebSocketDeflateConstants.ServerNoContextTakeover)) |
| 304 | + { |
| 305 | + options.ServerContextTakeover = false; |
| 306 | + } |
| 307 | + else if (value.StartsWith(ClientWebSocketDeflateConstants.ClientMaxWindowBits)) |
| 308 | + { |
| 309 | + hasClientMaxWindowBits = true; |
| 310 | + var clientMaxWindowBits = ParseWindowBits(value); |
| 311 | + if (clientMaxWindowBits > options.ClientMaxWindowBits) |
| 312 | + { |
| 313 | + return false; |
| 314 | + } |
| 315 | + options.ClientMaxWindowBits = clientMaxWindowBits; |
| 316 | + } |
| 317 | + else if (value.StartsWith(ClientWebSocketDeflateConstants.ServerMaxWindowBits)) |
| 318 | + { |
| 319 | + var serverMaxWindowBits = ParseWindowBits(value); |
| 320 | + if (serverMaxWindowBits > options.ServerMaxWindowBits) |
| 321 | + { |
| 322 | + return false; |
| 323 | + } |
| 324 | + options.ServerMaxWindowBits = serverMaxWindowBits; |
| 325 | + } |
| 326 | + |
| 327 | + static int ParseWindowBits(ReadOnlySpan<char> value) |
| 328 | + { |
| 329 | + // parameters can be sent without a value by the client |
| 330 | + var startIndex = value.IndexOf('='); |
| 331 | + |
| 332 | + if (startIndex < 0 || |
| 333 | + !int.TryParse(value[(startIndex + 1)..], NumberStyles.Integer, CultureInfo.InvariantCulture, out int windowBits) || |
| 334 | + windowBits < 9 || |
| 335 | + windowBits > 15) |
| 336 | + { |
| 337 | + throw new WebSocketException(WebSocketError.HeaderError, ""); |
| 338 | + } |
| 339 | + |
| 340 | + return windowBits; |
| 341 | + } |
| 342 | + } |
| 343 | + |
| 344 | + if (end < 0) |
| 345 | + { |
| 346 | + break; |
| 347 | + } |
| 348 | + extension = extension[(end + 1)..]; |
| 349 | + } |
| 350 | + |
| 351 | + return true; |
| 352 | + } |
| 353 | + |
| 354 | + private static void Resp(IHeaderDictionary headers, WebSocketDeflateOptions options, bool hasClientMaxWindowBits) |
| 355 | + { |
| 356 | + headers.Add("Sec-WebSocket-Extensions", GetDeflateOptions(options, hasClientMaxWindowBits)); |
| 357 | + |
| 358 | + static string GetDeflateOptions(WebSocketDeflateOptions options, bool hasClientMaxWindowBits) |
| 359 | + { |
| 360 | + var builder = new StringBuilder(ClientWebSocketDeflateConstants.MaxExtensionLength); |
| 361 | + builder.Append(ClientWebSocketDeflateConstants.Extension); |
| 362 | + |
| 363 | + // If a received extension negotiation offer doesn't have the |
| 364 | + // "client_max_window_bits" extension parameter, the corresponding |
| 365 | + // extension negotiation response to the offer MUST NOT include the |
| 366 | + // "client_max_window_bits" extension parameter. |
| 367 | + // https://tools.ietf.org/html/rfc7692#section-7.1.2.2 |
| 368 | + if (hasClientMaxWindowBits) |
| 369 | + { |
| 370 | + if (options.ClientMaxWindowBits != 15) |
| 371 | + { |
| 372 | + builder.Append("; ").Append(ClientWebSocketDeflateConstants.ClientMaxWindowBits).Append('=') |
| 373 | + .Append(options.ClientMaxWindowBits.ToString(CultureInfo.InvariantCulture)); |
| 374 | + } |
| 375 | + else |
| 376 | + { |
| 377 | + builder.Append("; ").Append(ClientWebSocketDeflateConstants.ClientMaxWindowBits); |
| 378 | + } |
| 379 | + } |
| 380 | + |
| 381 | + if (!options.ClientContextTakeover) |
| 382 | + { |
| 383 | + builder.Append("; ").Append(ClientWebSocketDeflateConstants.ClientNoContextTakeover); |
| 384 | + } |
| 385 | + |
| 386 | + if (options.ServerMaxWindowBits != 15) |
| 387 | + { |
| 388 | + builder.Append("; ") |
| 389 | + .Append(ClientWebSocketDeflateConstants.ServerMaxWindowBits).Append('=') |
| 390 | + .Append(options.ServerMaxWindowBits.ToString(CultureInfo.InvariantCulture)); |
| 391 | + } |
| 392 | + else |
| 393 | + { |
| 394 | + builder.Append("; ").Append(ClientWebSocketDeflateConstants.ServerMaxWindowBits); |
| 395 | + } |
| 396 | + |
| 397 | + if (!options.ServerContextTakeover) |
| 398 | + { |
| 399 | + builder.Append("; ").Append(ClientWebSocketDeflateConstants.ServerNoContextTakeover); |
| 400 | + } |
| 401 | + |
| 402 | + Debug.Assert(builder.Length <= ClientWebSocketDeflateConstants.MaxExtensionLength); |
| 403 | + return builder.ToString(); |
| 404 | + } |
| 405 | + } |
229 | 406 | } |
230 | 407 | } |
231 | 408 | } |
0 commit comments