From ab0fc800f89783f957ab93341a0c9ac32de44b73 Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Wed, 11 May 2022 20:13:15 -0700 Subject: [PATCH] Improve based on code review --- tool/net/redbean.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index fff89c152..42730adbd 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -3685,7 +3685,7 @@ static int LuaFetch(lua_State *L) { bool usessl; uint32_t ip; struct Url url; - int t, ret, sock, methodidx; + int t, ret, sock, methodidx, hdridx; char *host, *port; struct TlsBio *bio; struct addrinfo *addr; @@ -3742,18 +3742,20 @@ static int LuaFetch(lua_State *L) { return luaL_argerror(L, 2, "invalid header value encoding"); // Content-Length and Connection will be overwritten; - // skip them to avoid duplicates - if (strcasecmp(key, "content-length") && - strcasecmp(key, "connection")) { - if (!strcasecmp(key, "user-agent")) { - agenthdr = gc(strdup(hdr)); - } else if (!strcasecmp(key, "host")) { - hosthdr = gc(strdup(hdr)); + // skip them to avoid duplicates; + // also allow unknown headers + if ((hdridx = GetHttpHeader(key, keylen)) == -1 || + hdridx != kHttpContentLength && + hdridx != kHttpConnection) { + if (hdridx == kHttpUserAgent) { + agenthdr = hdr; + } else if (hdridx == kHttpHost) { + hosthdr = hdr; } else { appendd(&headers, key, keylen); - appendd(&headers, ": ", 2); + appendw(&headers, READ16LE(": ")); appends(&headers, hdr); - appendd(&headers, "\r\n", 2); + appendw(&headers, READ16LE("\r\n")); } } }