Write websocket header directly to the repsonse headers (#253)

This commit is contained in:
David Fowler 2018-08-02 16:45:29 -07:00 committed by GitHub
parent 768d2a023e
commit bc13fd5450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 10 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.WebSockets.Internal namespace Microsoft.AspNetCore.WebSockets.Internal
{ {
@ -63,14 +64,14 @@ namespace Microsoft.AspNetCore.WebSockets.Internal
return validConnection && validUpgrade && validVersion && validKey; return validConnection && validUpgrade && validVersion && validKey;
} }
public static IEnumerable<KeyValuePair<string, string>> GenerateResponseHeaders(string key, string subProtocol) public static void GenerateResponseHeaders(string key, string subProtocol, IHeaderDictionary headers)
{ {
yield return new KeyValuePair<string, string>(Constants.Headers.Connection, Constants.Headers.ConnectionUpgrade); headers[Constants.Headers.Connection] = Constants.Headers.ConnectionUpgrade;
yield return new KeyValuePair<string, string>(Constants.Headers.Upgrade, Constants.Headers.UpgradeWebSocket); headers[Constants.Headers.Upgrade] = Constants.Headers.UpgradeWebSocket;
yield return new KeyValuePair<string, string>(Constants.Headers.SecWebSocketAccept, CreateResponseKey(key)); headers[Constants.Headers.SecWebSocketAccept] = CreateResponseKey(key);
if (!string.IsNullOrWhiteSpace(subProtocol)) if (!string.IsNullOrWhiteSpace(subProtocol))
{ {
yield return new KeyValuePair<string, string>(Constants.Headers.SecWebSocketProtocol, subProtocol); headers[Constants.Headers.SecWebSocketProtocol] = subProtocol;
} }
} }

View File

@ -155,11 +155,7 @@ namespace Microsoft.AspNetCore.WebSockets
string key = string.Join(", ", _context.Request.Headers[Constants.Headers.SecWebSocketKey]); string key = string.Join(", ", _context.Request.Headers[Constants.Headers.SecWebSocketKey]);
var responseHeaders = HandshakeHelpers.GenerateResponseHeaders(key, subProtocol); HandshakeHelpers.GenerateResponseHeaders(key, subProtocol, _context.Response.Headers);
foreach (var headerPair in responseHeaders)
{
_context.Response.Headers[headerPair.Key] = headerPair.Value;
}
Stream opaqueTransport = await _upgradeFeature.UpgradeAsync(); // Sets status code to 101 Stream opaqueTransport = await _upgradeFeature.UpgradeAsync(); // Sets status code to 101