Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion MaxLib.WebServer/WebSocket/WebSocketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public void Add<T>(WebSocketEndpoint<T> endpoint)
public override bool CanWorkWith(WebProgressTask task)
{
ArgumentNullException.ThrowIfNull(task);
return task.Request.GetHeader("Upgrade") == "websocket" &&
// RFC 6455 section 4.2.1: the Upgrade value is case-insensitive
return string.Equals(task.Request.GetHeader("Upgrade"), "websocket", StringComparison.OrdinalIgnoreCase) &&
(task.Request.GetHeader("Connection")?.Contains("upgrade", StringComparison.OrdinalIgnoreCase) ?? false);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/MaxLib.WebServer.Test/WebSocket/TestWebSocketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,29 @@ public async Task TestResponseEchoesTheClientsOwnCasingNotTheEndpointsRegistered
Assert.AreEqual("Chat", task.Response.GetHeader("Sec-WebSocket-Protocol"),
"the response must echo the client's own casing, not the endpoint's");
}

[TestMethod]
[DataRow("websocket")]
[DataRow("WebSocket")]
[DataRow("WEBSOCKET")]
public void TestCanWorkWithAcceptsTheUpgradeValueRegardlessOfCase(string upgrade)
{
// RFC 6455 section 4.2.1: the Upgrade header value is case-insensitive
var service = new WebSocketService();
var task = CreateUpgradeTask();
task.Request.SetHeader("Upgrade", upgrade);

Assert.IsTrue(service.CanWorkWith(task));
}

[TestMethod]
public void TestCanWorkWithRejectsANonWebSocketUpgrade()
{
var service = new WebSocketService();
var task = CreateUpgradeTask();
task.Request.SetHeader("Upgrade", "h2c");

Assert.IsFalse(service.CanWorkWith(task));
}
}
}
Loading