From f355ba325261c219900f0921e1a1a166b46628aa Mon Sep 17 00:00:00 2001 From: Nils Goroll Date: Fri, 16 May 2025 13:29:16 +0200 Subject: [PATCH] cache_fetch: Send Content-Length: 0 for POST, PUT and PATCH otherwise keep the existing logic of sending no Content-Length header if there is no request body. This code runs before vcl_backend_fetch, so should VCL gain back control over Content-Length as proposed in #4331, it will be possible again to override this default. Fixes #4331 for the special case --- bin/varnishd/cache/cache_fetch.c | 11 +++++++++-- bin/varnishtest/tests/r04331.vtc | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 bin/varnishtest/tests/r04331.vtc diff --git a/bin/varnishd/cache/cache_fetch.c b/bin/varnishd/cache/cache_fetch.c index 0521d0314a..96993a1908 100644 --- a/bin/varnishd/cache/cache_fetch.c +++ b/bin/varnishd/cache/cache_fetch.c @@ -411,8 +411,15 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo) http_PrintfHeader(bo->bereq, "X-Varnish: %ju", VXID(bo->vsl->wid)); - if (bo->bereq_body == NULL && bo->req == NULL) - http_Unset(bo->bereq, H_Content_Length); + if (bo->bereq_body == NULL && bo->req == NULL) { + const char *met = http_GetMethod(bo->bereq); + if (http_method_eq(met, POST) || + http_method_eq(met, PUT) || + http_method_eq(met, PATCH)) + http_ForceHeader(bo->bereq, H_Content_Length, "0"); + else + http_Unset(bo->bereq, H_Content_Length); + } VCL_backend_fetch_method(bo->vcl, wrk, NULL, bo, NULL); diff --git a/bin/varnishtest/tests/r04331.vtc b/bin/varnishtest/tests/r04331.vtc new file mode 100644 index 0000000000..4a5c6b934a --- /dev/null +++ b/bin/varnishtest/tests/r04331.vtc @@ -0,0 +1,18 @@ +varnishtest "C-L with empty POST" + +server s1 { + rxreq + txresp + expect req.http.content-length == 0 +} -start + +varnish v1 -vcl+backend { + +} -start + +client c1 { + txreq -method POST -hdr "content-length: 0" + rxresp +} -run + +server s1 -wait