Skip to content
This repository was archived by the owner on Oct 18, 2018. It is now read-only.

Commit bc13fd5

Browse files
authored
Write websocket header directly to the repsonse headers (#253)
1 parent 768d2a0 commit bc13fd5

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/Microsoft.AspNetCore.WebSockets/Internal/HandshakeHelpers.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Security.Cryptography;
77
using System.Text;
8+
using Microsoft.AspNetCore.Http;
89

910
namespace Microsoft.AspNetCore.WebSockets.Internal
1011
{
@@ -63,14 +64,14 @@ public static bool CheckSupportedWebSocketRequest(string method, IEnumerable<Key
6364
return validConnection && validUpgrade && validVersion && validKey;
6465
}
6566

66-
public static IEnumerable<KeyValuePair<string, string>> GenerateResponseHeaders(string key, string subProtocol)
67+
public static void GenerateResponseHeaders(string key, string subProtocol, IHeaderDictionary headers)
6768
{
68-
yield return new KeyValuePair<string, string>(Constants.Headers.Connection, Constants.Headers.ConnectionUpgrade);
69-
yield return new KeyValuePair<string, string>(Constants.Headers.Upgrade, Constants.Headers.UpgradeWebSocket);
70-
yield return new KeyValuePair<string, string>(Constants.Headers.SecWebSocketAccept, CreateResponseKey(key));
69+
headers[Constants.Headers.Connection] = Constants.Headers.ConnectionUpgrade;
70+
headers[Constants.Headers.Upgrade] = Constants.Headers.UpgradeWebSocket;
71+
headers[Constants.Headers.SecWebSocketAccept] = CreateResponseKey(key);
7172
if (!string.IsNullOrWhiteSpace(subProtocol))
7273
{
73-
yield return new KeyValuePair<string, string>(Constants.Headers.SecWebSocketProtocol, subProtocol);
74+
headers[Constants.Headers.SecWebSocketProtocol] = subProtocol;
7475
}
7576
}
7677

src/Microsoft.AspNetCore.WebSockets/WebSocketMiddleware.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ public async Task<WebSocket> AcceptAsync(WebSocketAcceptContext acceptContext)
155155

156156
string key = string.Join(", ", _context.Request.Headers[Constants.Headers.SecWebSocketKey]);
157157

158-
var responseHeaders = HandshakeHelpers.GenerateResponseHeaders(key, subProtocol);
159-
foreach (var headerPair in responseHeaders)
160-
{
161-
_context.Response.Headers[headerPair.Key] = headerPair.Value;
162-
}
158+
HandshakeHelpers.GenerateResponseHeaders(key, subProtocol, _context.Response.Headers);
163159

164160
Stream opaqueTransport = await _upgradeFeature.UpgradeAsync(); // Sets status code to 101
165161

0 commit comments

Comments
 (0)