Skip to content

Commit 06a77af

Browse files
committed
cache_fetch: Send Content-Length: 0 for anything not GET or HEAD
otherwise keep the existing logic of sending no Content-Length header if there is no request body. This approach was determined to be better than the inverse of sending C-L: 0 for specific methods to avoid backends sending 411 responses as noted in #4331 (comment) 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
1 parent 36fad69 commit 06a77af

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

bin/varnishd/cache/cache_fetch.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,14 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
411411

412412
http_PrintfHeader(bo->bereq, "X-Varnish: %ju", VXID(bo->vsl->wid));
413413

414-
if (bo->bereq_body == NULL && bo->req == NULL)
415-
http_Unset(bo->bereq, H_Content_Length);
414+
if (bo->bereq_body == NULL && bo->req == NULL) {
415+
const char *met = http_GetMethod(bo->bereq);
416+
if (http_method_eq(met, GET) ||
417+
http_method_eq(met, HEAD))
418+
http_Unset(bo->bereq, H_Content_Length);
419+
else
420+
http_ForceHeader(bo->bereq, H_Content_Length, "0");
421+
}
416422

417423
VCL_backend_fetch_method(bo->vcl, wrk, NULL, bo, NULL);
418424

bin/varnishtest/tests/e00036.vtc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ server s1 {
1717
expect req.method == XGET
1818
expect req.url == "/foo/body1"
1919
expect req.bodylen == 0
20-
expect req.http.Content-Length == "<undef>"
20+
expect req.http.Content-Length == "0"
2121
expect req.http.Transfer-Encoding == "<undef>"
2222
txresp -body {
2323
Included file
@@ -40,7 +40,7 @@ server s1 {
4040
expect req.method == XGET
4141
expect req.url == "/foo/body2"
4242
expect req.bodylen == 0
43-
expect req.http.Content-Length == "<undef>"
43+
expect req.http.Content-Length == "0"
4444
expect req.http.Transfer-Encoding == "<undef>"
4545
txresp -body {
4646
Included file

bin/varnishtest/tests/r04340.vtc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
varnishtest "C-L with empty POST"
2+
3+
server s1 {
4+
rxreq
5+
txresp
6+
expect req.http.content-length == 0
7+
} -start
8+
9+
varnish v1 -vcl+backend {
10+
11+
} -start
12+
13+
client c1 {
14+
txreq -method POST -hdr "content-length: 0"
15+
rxresp
16+
} -run
17+
18+
server s1 -wait

0 commit comments

Comments
 (0)