|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Globalization; |
| 7 | +using System.Net.WebSockets; |
5 | 8 | using System.Security.Cryptography; |
6 | 9 | using System.Text; |
7 | 10 | using Microsoft.AspNetCore.Http; |
@@ -72,5 +75,89 @@ public static string CreateResponseKey(string requestKey) |
72 | 75 |
|
73 | 76 | return Convert.ToBase64String(hashedBytes); |
74 | 77 | } |
| 78 | + |
| 79 | + public static bool ParseDeflateOptions(ReadOnlySpan<char> extension, WebSocketDeflateOptions options, [NotNullWhen(true)] out string? response) |
| 80 | + { |
| 81 | + response = null; |
| 82 | + var builder = new StringBuilder(WebSocketDeflateConstants.MaxExtensionLength); |
| 83 | + builder.Append(WebSocketDeflateConstants.Extension); |
| 84 | + |
| 85 | + while (true) |
| 86 | + { |
| 87 | + int end = extension.IndexOf(';'); |
| 88 | + ReadOnlySpan<char> value = (end >= 0 ? extension[..end] : extension).Trim(); |
| 89 | + |
| 90 | + if (value.Length > 0) |
| 91 | + { |
| 92 | + if (value.SequenceEqual(WebSocketDeflateConstants.ClientNoContextTakeover)) |
| 93 | + { |
| 94 | + // REVIEW: If someone specifies true for server options, do we allow client to override this? |
| 95 | + options.ClientContextTakeover = false; |
| 96 | + builder.Append("; ").Append(WebSocketDeflateConstants.ClientNoContextTakeover); |
| 97 | + } |
| 98 | + else if (value.SequenceEqual(WebSocketDeflateConstants.ServerNoContextTakeover)) |
| 99 | + { |
| 100 | + options.ServerContextTakeover = false; |
| 101 | + builder.Append("; ").Append(WebSocketDeflateConstants.ServerNoContextTakeover); |
| 102 | + } |
| 103 | + else if (value.StartsWith(WebSocketDeflateConstants.ClientMaxWindowBits)) |
| 104 | + { |
| 105 | + var clientMaxWindowBits = ParseWindowBits(value, WebSocketDeflateConstants.ClientMaxWindowBits); |
| 106 | + if (clientMaxWindowBits > options.ClientMaxWindowBits) |
| 107 | + { |
| 108 | + return false; |
| 109 | + } |
| 110 | + // if client didn't send a value for ClientMaxWindowBits use the value the server set |
| 111 | + options.ClientMaxWindowBits = clientMaxWindowBits ?? options.ClientMaxWindowBits; |
| 112 | + builder.Append("; ").Append(WebSocketDeflateConstants.ClientMaxWindowBits).Append('=') |
| 113 | + .Append(options.ClientMaxWindowBits.ToString(CultureInfo.InvariantCulture)); |
| 114 | + } |
| 115 | + else if (value.StartsWith(WebSocketDeflateConstants.ServerMaxWindowBits)) |
| 116 | + { |
| 117 | + var serverMaxWindowBits = ParseWindowBits(value, WebSocketDeflateConstants.ServerMaxWindowBits); |
| 118 | + if (serverMaxWindowBits > options.ServerMaxWindowBits) |
| 119 | + { |
| 120 | + return false; |
| 121 | + } |
| 122 | + // if client didn't send a value for ServerMaxWindowBits use the value the server set |
| 123 | + options.ServerMaxWindowBits = serverMaxWindowBits ?? options.ServerMaxWindowBits; |
| 124 | + |
| 125 | + builder.Append("; ") |
| 126 | + .Append(WebSocketDeflateConstants.ServerMaxWindowBits).Append('=') |
| 127 | + .Append(options.ServerMaxWindowBits.ToString(CultureInfo.InvariantCulture)); |
| 128 | + } |
| 129 | + |
| 130 | + static int? ParseWindowBits(ReadOnlySpan<char> value, string propertyName) |
| 131 | + { |
| 132 | + var startIndex = value.IndexOf('='); |
| 133 | + |
| 134 | + // parameters can be sent without a value by the client, we'll use the values set by the app developer or the default of 15 |
| 135 | + if (startIndex < 0) |
| 136 | + { |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + if (!int.TryParse(value[(startIndex + 1)..], NumberStyles.Integer, CultureInfo.InvariantCulture, out int windowBits) || |
| 141 | + windowBits < 9 || |
| 142 | + windowBits > 15) |
| 143 | + { |
| 144 | + throw new WebSocketException(WebSocketError.HeaderError, $"invalid {propertyName} used: {value[(startIndex + 1)..].ToString()}"); |
| 145 | + } |
| 146 | + |
| 147 | + return windowBits; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if (end < 0) |
| 152 | + { |
| 153 | + break; |
| 154 | + } |
| 155 | + extension = extension[(end + 1)..]; |
| 156 | + } |
| 157 | + |
| 158 | + response = builder.ToString(); |
| 159 | + |
| 160 | + return true; |
| 161 | + } |
75 | 162 | } |
76 | 163 | } |
0 commit comments