Improve generation of redbean static headers

This change fixes an issue where Cache-Control could be outputted twice
when Lua code calls SetHeader() from OnHttpRequest().
This commit is contained in:
Justine Tunney 2022-06-14 22:01:13 -07:00
parent 42b34c26f8
commit 579080cd4c

View file

@ -360,6 +360,7 @@ static bool checkedmethod;
static bool sslinitialized; static bool sslinitialized;
static bool sslfetchverify; static bool sslfetchverify;
static bool hascontenttype; static bool hascontenttype;
static bool gotcachecontrol;
static bool interpretermode; static bool interpretermode;
static bool sslclientverify; static bool sslclientverify;
static bool connectionclose; static bool connectionclose;
@ -375,8 +376,9 @@ static bool hasonprocessdestroy;
static bool loggednetworkorigin; static bool loggednetworkorigin;
static bool ishandlingconnection; static bool ishandlingconnection;
static bool hasonclientconnection; static bool hasonclientconnection;
static bool gotxcontenttypeoptions;
static bool evadedragnetsurveillance; static bool evadedragnetsurveillance;
_Atomic(bool) static terminatemonitor; static _Atomic(bool) terminatemonitor;
static int zfd; static int zfd;
static int frags; static int frags;
@ -2896,7 +2898,9 @@ td { padding-right: 3em; }\r\n\
appends(&outbuf, "</footer>\r\n"); appends(&outbuf, "</footer>\r\n");
p = SetStatus(200, "OK"); p = SetStatus(200, "OK");
p = AppendContentType(p, "text/html"); p = AppendContentType(p, "text/html");
p = AppendCache(p, 0); if (msg.version >= 11) {
p = stpcpy(p, "Cache-Control: no-store\r\n");
}
return CommitOutput(p); return CommitOutput(p);
} }
@ -2969,7 +2973,9 @@ static char *ServeStatusz(void) {
AppendRusage("children", &shared->children); AppendRusage("children", &shared->children);
p = SetStatus(200, "OK"); p = SetStatus(200, "OK");
p = AppendContentType(p, "text/plain"); p = AppendContentType(p, "text/plain");
p = AppendCache(p, 0); if (msg.version >= 11) {
p = stpcpy(p, "Cache-Control: no-store\r\n");
}
return CommitOutput(p); return CommitOutput(p);
} }
@ -4373,12 +4379,21 @@ static int LuaSetHeader(lua_State *L) {
case kHttpContentType: case kHttpContentType:
p = AppendContentType(p, eval); p = AppendContentType(p, eval);
break; break;
case kHttpReferrerPolicy:
referrerpolicy = FreeLater(strdup(eval));
break;
case kHttpServer: case kHttpServer:
branded = true; branded = true;
p = AppendHeader(p, "Server", eval); p = AppendHeader(p, "Server", eval);
break; break;
case kHttpReferrerPolicy: case kHttpExpires:
referrerpolicy = FreeLater(strdup(eval)); case kHttpCacheControl:
gotcachecontrol = true;
p = AppendHeader(p, key, eval);
break;
case kHttpXContentTypeOptions:
gotxcontenttypeoptions = true;
p = AppendHeader(p, key, eval);
break; break;
default: default:
p = AppendHeader(p, key, eval); p = AppendHeader(p, key, eval);
@ -5570,6 +5585,7 @@ static void HandleHeartbeat(void) {
} }
} }
// returns 0 on success or response on error
static char *OpenAsset(struct Asset *a) { static char *OpenAsset(struct Asset *a) {
int fd; int fd;
void *data; void *data;
@ -5905,13 +5921,17 @@ static inline bool IsLua(struct Asset *a) {
} }
static char *HandleAsset(struct Asset *a, const char *path, size_t pathlen) { static char *HandleAsset(struct Asset *a, const char *path, size_t pathlen) {
char *p;
#ifndef STATIC #ifndef STATIC
if (IsLua(a)) return ServeLua(a, path, pathlen); if (IsLua(a)) return ServeLua(a, path, pathlen);
#endif #endif
if (msg.method == kHttpGet || msg.method == kHttpHead) { if (msg.method == kHttpGet || msg.method == kHttpHead) {
LockInc(&shared->c.staticrequests); LockInc(&shared->c.staticrequests);
return stpcpy(ServeAsset(a, path, pathlen), p = ServeAsset(a, path, pathlen);
"X-Content-Type-Options: nosniff\r\n"); if (!gotxcontenttypeoptions) {
p = stpcpy(p, "X-Content-Type-Options: nosniff\r\n");
}
return p;
} else { } else {
return BadMethod(); return BadMethod();
} }
@ -5981,7 +6001,9 @@ static char *ServeAsset(struct Asset *a, const char *path, size_t pathlen) {
p = stpcpy(p, "Vary: Accept-Encoding\r\n"); p = stpcpy(p, "Vary: Accept-Encoding\r\n");
p = AppendHeader(p, "Last-Modified", a->lastmodifiedstr); p = AppendHeader(p, "Last-Modified", a->lastmodifiedstr);
if (msg.version >= 11) { if (msg.version >= 11) {
p = AppendCache(p, cacheseconds); if (!gotcachecontrol) {
p = AppendCache(p, cacheseconds);
}
if (!IsCompressed(a)) { if (!IsCompressed(a)) {
p = stpcpy(p, "Accept-Ranges: bytes\r\n"); p = stpcpy(p, "Accept-Ranges: bytes\r\n");
} }
@ -6187,8 +6209,10 @@ static void InitRequest(void) {
luaheaderp = 0; luaheaderp = 0;
isyielding = 0; isyielding = 0;
contentlength = 0; contentlength = 0;
hascontenttype = istext = false;
referrerpolicy = 0; referrerpolicy = 0;
gotcachecontrol = 0;
gotxcontenttypeoptions = 0;
hascontenttype = istext = false;
InitHttpMessage(&msg, kHttpRequest); InitHttpMessage(&msg, kHttpRequest);
} }