Add Sec-WebSocket-Key HTTP header

This commit is contained in:
wingdeans 2023-12-28 18:39:19 -05:00
parent a39caefc8b
commit e759b8e381
5 changed files with 17 additions and 19 deletions

View file

@ -104,3 +104,4 @@ CF-Visitor, kHttpCfVisitor
CF-Connecting-IP, kHttpCfConnectingIp
CF-IPCountry, kHttpCfIpcountry
CDN-Loop, kHttpCdnLoop
Sec-WebSocket-Key, kHttpWebsocketKey

View file

@ -39,7 +39,7 @@
#line 12 "gethttpheader.gperf"
struct thatispacked HttpHeaderSlot { char *name; char code; };
#define TOTAL_KEYWORDS 93
#define TOTAL_KEYWORDS 94
#define MIN_WORD_LENGTH 2
#define MAX_WORD_LENGTH 32
#define MIN_HASH_VALUE 3
@ -387,7 +387,10 @@ LookupHttpHeader (register const char *str, register size_t len)
#line 87 "gethttpheader.gperf"
{"Strict-Transport-Security", kHttpStrictTransportSecurity},
{""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
{""}, {""}, {""}, {""}, {""},
{""}, {""},
#line 107 "gethttpheader.gperf"
{"Sec-WebSocket-Key", kHttpWebsocketKey},
{""}, {""},
#line 22 "gethttpheader.gperf"
{"X-Forwarded-For", kHttpXForwardedFor},
{""},

View file

@ -206,6 +206,8 @@ const char *GetHttpHeaderName(int h) {
return "CDN-Loop";
case kHttpSecChUaPlatform:
return "Sec-CH-UA-Platform";
case kHttpWebsocketKey:
return "Sec-WebSocket-Key";
default:
return NULL;
}

View file

@ -146,7 +146,8 @@
#define kHttpCfIpcountry 90
#define kHttpSecChUaPlatform 91
#define kHttpCdnLoop 92
#define kHttpHeadersMax 93
#define kHttpWebsocketKey 93
#define kHttpHeadersMax 94
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_

View file

@ -5066,23 +5066,14 @@ static int LuaUpgradeWS(lua_State *L) {
unsigned char hash[20];
OnlyCallDuringRequest(L, "UpgradeWS");
haskey = true;
for (i = 0; i < cpm.msg.xheaders.n; ++i) {
if (SlicesEqualCase(
"Sec-WebSocket-Key", strlen("Sec-WebSocket-Key"),
inbuf.p + cpm.msg.xheaders.p[i].k.a,
cpm.msg.xheaders.p[i].k.b - cpm.msg.xheaders.p[i].k.a)) {
mbedtls_sha1_init(&ctx);
mbedtls_sha1_starts_ret(&ctx);
mbedtls_sha1_update_ret(
&ctx, (unsigned char *)inbuf.p + cpm.msg.xheaders.p[i].v.a,
cpm.msg.xheaders.p[i].v.b - cpm.msg.xheaders.p[i].v.a);
haskey = true;
break;
}
}
if (!HasHeader(kHttpWebsocketKey))
luaL_error(L, "No Sec-WebSocket-Key header");
if (!haskey) luaL_error(L, "No Sec-WebSocket-Key header");
mbedtls_sha1_init(&ctx);
mbedtls_sha1_starts_ret(&ctx);
mbedtls_sha1_update_ret(&ctx, (unsigned char*)
HeaderData(kHttpWebsocketKey),
HeaderLength(kHttpWebsocketKey));
p = SetStatus(101, "Switching Protocols");
while (p - hdrbuf.p + (20 + 21 + (20 + 28 + 4)) + 512 > hdrbuf.n) {