Skip to content

Commit e90f998

Browse files
committed
Use interned strings for websockets
1 parent 3e8943e commit e90f998

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/Middleware/WebSockets/src/Constants.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ namespace Microsoft.AspNetCore.WebSockets
66
internal static class Constants
77
{
88
public static class Headers
9-
{
10-
public const string UpgradeWebSocket = "websocket";
11-
public const string ConnectionUpgrade = "Upgrade";
12-
public const string SupportedVersion = "13";
9+
{
10+
public readonly static string UpgradeWebSocket = "websocket";
11+
public readonly static string SupportedVersion = "13";
1312
}
1413
}
1514
}

src/Middleware/WebSockets/src/HandshakeHelpers.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal static class HandshakeHelpers
3434
};
3535

3636
// Verify Method, Upgrade, Connection, version, key, etc..
37-
public static bool CheckSupportedWebSocketRequest(string method, List<KeyValuePair<string, string>> headers)
37+
public static bool CheckSupportedWebSocketRequest(string method, List<KeyValuePair<string, string>> interestingHeaders, IHeaderDictionary requestHeaders)
3838
{
3939
bool validUpgrade = false, validConnection = false, validKey = false, validVersion = false;
4040

@@ -43,11 +43,11 @@ public static bool CheckSupportedWebSocketRequest(string method, List<KeyValuePa
4343
return false;
4444
}
4545

46-
foreach (var pair in headers)
46+
foreach (var pair in interestingHeaders)
4747
{
4848
if (string.Equals(HeaderNames.Connection, pair.Key, StringComparison.OrdinalIgnoreCase))
4949
{
50-
if (string.Equals(Constants.Headers.ConnectionUpgrade, pair.Value, StringComparison.OrdinalIgnoreCase))
50+
if (string.Equals(HeaderNames.Upgrade, pair.Value, StringComparison.OrdinalIgnoreCase))
5151
{
5252
validConnection = true;
5353
}
@@ -72,12 +72,26 @@ public static bool CheckSupportedWebSocketRequest(string method, List<KeyValuePa
7272
}
7373
}
7474

75+
// WebSockets are long lived; so if the header values are valid we switch them out for the interned versions.
76+
if (validConnection && requestHeaders[HeaderNames.Connection].Count == 1)
77+
{
78+
requestHeaders[HeaderNames.Connection] = HeaderNames.Upgrade;
79+
}
80+
if (validUpgrade && requestHeaders[HeaderNames.Upgrade].Count == 1)
81+
{
82+
requestHeaders[HeaderNames.Upgrade] = Constants.Headers.UpgradeWebSocket;
83+
}
84+
if (validVersion && requestHeaders[HeaderNames.SecWebSocketVersion].Count == 1)
85+
{
86+
requestHeaders[HeaderNames.SecWebSocketVersion] = Constants.Headers.SupportedVersion;
87+
}
88+
7589
return validConnection && validUpgrade && validVersion && validKey;
7690
}
7791

7892
public static void GenerateResponseHeaders(string key, string? subProtocol, IHeaderDictionary headers)
7993
{
80-
headers[HeaderNames.Connection] = Constants.Headers.ConnectionUpgrade;
94+
headers[HeaderNames.Connection] = HeaderNames.Upgrade;
8195
headers[HeaderNames.Upgrade] = Constants.Headers.UpgradeWebSocket;
8296
headers[HeaderNames.SecWebSocketAccept] = CreateResponseKey(key);
8397
if (!string.IsNullOrWhiteSpace(subProtocol))

src/Middleware/WebSockets/src/WebSocketMiddleware.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,16 @@ public bool IsWebSocketRequest
118118
}
119119
else
120120
{
121-
var headers = new List<KeyValuePair<string, string>>();
122-
foreach (string headerName in HandshakeHelpers.NeededHeaders)
121+
var requestHeaders = _context.Request.Headers;
122+
var interestingHeaders = new List<KeyValuePair<string, string>>();
123+
foreach (var headerName in HandshakeHelpers.NeededHeaders)
123124
{
124-
foreach (var value in _context.Request.Headers.GetCommaSeparatedValues(headerName))
125+
foreach (var value in requestHeaders.GetCommaSeparatedValues(headerName))
125126
{
126-
headers.Add(new KeyValuePair<string, string>(headerName, value));
127+
interestingHeaders.Add(new KeyValuePair<string, string>(headerName, value));
127128
}
128129
}
129-
_isWebSocketRequest = HandshakeHelpers.CheckSupportedWebSocketRequest(_context.Request.Method, headers);
130+
_isWebSocketRequest = HandshakeHelpers.CheckSupportedWebSocketRequest(_context.Request.Method, interestingHeaders, requestHeaders);
130131
}
131132
}
132133
return _isWebSocketRequest.Value;

0 commit comments

Comments
 (0)