From 0a7b1718c2e66e5f53e3e728910badf535ef82dd Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Sun, 14 May 2023 00:13:07 -0700 Subject: [PATCH] Update redbean Fetch to close connection upon server request --- tool/net/fetch.inc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tool/net/fetch.inc b/tool/net/fetch.inc index 215d1a9aa..a018c5d8f 100644 --- a/tool/net/fetch.inc +++ b/tool/net/fetch.inc @@ -227,7 +227,7 @@ static int LuaFetch(lua_State *L) { requestlen = appendz(request).i; _gc(request); - if (keepalive != kaKEEP && keepalive != kaCLOSE) { + if (keepalive == kaNONE || keepalive == kaOPEN) { /* * Perform DNS lookup. */ @@ -449,6 +449,15 @@ Finished: msg.status, urlarg, FetchHeaderLength(kHttpServer), FetchHeaderData(kHttpServer)); + // check if the server has requested to close the connection + // https://www.rfc-editor.org/rfc/rfc2616#section-14.10 + if (keepalive && keepalive != kaCLOSE + && FetchHasHeader(kHttpConnection) + && FetchHeaderEqualCase(kHttpConnection, "close")) { + VERBOSEF("(ftch) close keepalive on server request"); + keepalive = kaCLOSE; + } + // need to save updated sock for keepalive if (keepalive && keepalive != kaCLOSE && lua_istable(L, 2)) { lua_getfield(L, 2, "keepalive");