From ef3e8475fbc02c0a9d69b96da91d33afdd42f8ba Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Fri, 7 Jul 2023 21:30:56 +0200 Subject: [PATCH] Honor Connection: closed received from reverse proxy peers --- src/lib/proxy.c | 12 ++++++++++-- src/lib/proxy.h | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/lib/proxy.c b/src/lib/proxy.c index 7d08ab7..0cc9350 100644 --- a/src/lib/proxy.c +++ b/src/lib/proxy.c @@ -139,6 +139,10 @@ proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) { void proxy_unlock_ctx(proxy_ctx_t *ctx) { int n = (int) ((ctx - proxies) / MAX_PROXY_CNX_PER_HOST); + if (ctx->close) { + proxy_close(ctx); + ctx->close = 0; + } debug("Released proxy connection slot %i/%i", (ctx - proxies) % MAX_PROXY_CNX_PER_HOST, MAX_PROXY_CNX_PER_HOST); ctx->in_use = 0; ctx->client = NULL; @@ -400,8 +404,9 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu return -1; // honor server timeout with one second buffer - if (!proxy->initialized || sock_has_pending(&proxy->proxy, SOCK_DONTWAIT) != 0 || srv_error || - (proxy->http_timeout != 0 && (clock_micros() - proxy->proxy.ts_last_send) >= proxy->http_timeout - 1000000)) + if (!proxy->initialized || srv_error || + (proxy->http_timeout > 0 && (clock_micros() - proxy->proxy.ts_last_send) >= proxy->http_timeout) || + sock_has_pending(&proxy->proxy, SOCK_DONTWAIT)) { if (proxy->initialized) proxy_close(proxy); @@ -549,6 +554,9 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu long keep_alive_timeout = http_get_keep_alive_timeout(&res->hdr); proxy->http_timeout = (keep_alive_timeout > 0) ? keep_alive_timeout * 1000000 : 0; + connection = http_get_header_field(&res->hdr, "Connection"); + proxy->close = !strcontains(connection, "keep-alive") && !strcontains(connection, "Keep-Alive"); + ret = proxy_response_header(req, res, conf); if (ret != 0) { res->status = http_get_status(500); diff --git a/src/lib/proxy.h b/src/lib/proxy.h index c7f1ade..be6f06f 100644 --- a/src/lib/proxy.h +++ b/src/lib/proxy.h @@ -19,7 +19,7 @@ #include "config.h" typedef struct { - volatile unsigned char initialized:1, in_use:1; + volatile unsigned char initialized:1, in_use:1, close:1; sock proxy; long cnx_s, cnx_e; long http_timeout;