Skip to content

Commit e24a2e6

Browse files
HttpServer: match the Upgrade header value case-insensitively (#584)
RFC 6455 4.2.1 requires the Upgrade header value to be matched case-insensitively. Fixes the server-side exact "websocket" comparison and a related bug in WebSocketHandshake::insensitiveStringCompare (which used cmp()==0, accepting any value sorting at/above the expected one). Fixes #583 Build-verified locally with OpenSSL. Co-authored-by: Audrius Butkevicius
1 parent 3230490 commit e24a2e6

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

ixwebsocket/IXHttpServer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "IXGzipCodec.h"
1010
#include "IXNetSystem.h"
1111
#include "IXSocketConnect.h"
12+
#include "IXStrCaseCompare.h"
1213
#include "IXUserAgent.h"
1314
#include <cstdint>
1415
#include <cstring>
@@ -98,7 +99,11 @@ namespace ix
9899
{
99100
auto request = std::get<2>(ret);
100101
std::shared_ptr<ix::HttpResponse> response;
101-
if (request->headers["Upgrade"] == "websocket")
102+
// The Upgrade header value is case-insensitive (RFC 6455 4.2.1);
103+
// e.g. Chrome's remote-debugging proxy sends "Upgrade: WebSocket".
104+
const std::string& upgradeHeader = request->headers["Upgrade"];
105+
if (!CaseInsensitiveLess::cmp(upgradeHeader, "websocket") &&
106+
!CaseInsensitiveLess::cmp("websocket", upgradeHeader))
102107
{
103108
WebSocketServer::handleUpgrade(std::move(socket), connectionState, request);
104109
}

ixwebsocket/IXWebSocketHandshake.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ namespace ix
3636

3737
bool WebSocketHandshake::insensitiveStringCompare(const std::string& a, const std::string& b)
3838
{
39-
return CaseInsensitiveLess::cmp(a, b) == 0;
39+
// Equivalence under the case-insensitive strict weak ordering: neither
40+
// string sorts below the other. (cmp(a, b) == 0 only checked a >= b.)
41+
return !CaseInsensitiveLess::cmp(a, b) && !CaseInsensitiveLess::cmp(b, a);
4042
}
4143

4244
std::string WebSocketHandshake::genRandomString(const int len)

0 commit comments

Comments
 (0)