Fix Content-Type generated twice after SetHeader('Content-Type',...)

This commit is contained in:
Paul Kulchenko 2021-11-18 14:47:46 -08:00
parent d6a039821f
commit 8d6b729c0c

View file

@ -353,6 +353,7 @@ static bool connectionclose;
static bool hasonworkerstop; static bool hasonworkerstop;
static bool hasonworkerstart; static bool hasonworkerstart;
static bool hasonhttprequest; static bool hasonhttprequest;
static bool hascontenttype;
static bool ishandlingrequest; static bool ishandlingrequest;
static bool keyboardinterrupt; static bool keyboardinterrupt;
static bool listeningonport443; static bool listeningonport443;
@ -1958,6 +1959,7 @@ static char *AppendContentType(char *p, const char *ct) {
referrerpolicy = "no-referrer-when-downgrade"; referrerpolicy = "no-referrer-when-downgrade";
} }
} }
hascontenttype = true;
return AppendCrlf(p); return AppendCrlf(p);
} }
@ -2865,12 +2867,7 @@ static char *ServeIndex(const char *path, size_t pathlen) {
} }
static char *GetLuaResponse(void) { static char *GetLuaResponse(void) {
char *p; return luaheaderp ? luaheaderp : SetStatus(200, "OK");
if (!(p = luaheaderp)) {
p = SetStatus(200, "OK");
p = AppendContentType(p, "text/html");
}
return p;
} }
static bool IsLoopbackClient() { static bool IsLoopbackClient() {
@ -6349,6 +6346,7 @@ static char *SetStatus(unsigned code, const char *reason) {
if (code == 308) code = 301; if (code == 308) code = 301;
} }
statuscode = code; statuscode = code;
hascontenttype = false; // reset, as the headers are reset
stpcpy(hdrbuf.p, "HTTP/1.0 000 "); stpcpy(hdrbuf.p, "HTTP/1.0 000 ");
hdrbuf.p[7] += msg.version & 1; hdrbuf.p[7] += msg.version & 1;
hdrbuf.p[9] += code / 100; hdrbuf.p[9] += code / 100;
@ -6495,6 +6493,11 @@ static bool HandleMessageAcutal(void) {
p = stpcpy(p, "Connection: keep-alive\r\n"); p = stpcpy(p, "Connection: keep-alive\r\n");
} }
} }
// keep content-type update *before* referrerpolicy
// https://datatracker.ietf.org/doc/html/rfc2616#section-7.2.1
if (!hascontenttype && contentlength > 0) {
p = AppendContentType(p, "text/html");
}
if (referrerpolicy) { if (referrerpolicy) {
p = stpcpy(p, "Referrer-Policy: "); p = stpcpy(p, "Referrer-Policy: ");
p = stpcpy(p, referrerpolicy); p = stpcpy(p, referrerpolicy);
@ -6533,6 +6536,7 @@ static void InitRequest(void) {
generator = 0; generator = 0;
luaheaderp = 0; luaheaderp = 0;
contentlength = 0; contentlength = 0;
hascontenttype = false;
referrerpolicy = 0; referrerpolicy = 0;
InitHttpMessage(&msg, kHttpRequest); InitHttpMessage(&msg, kHttpRequest);
} }