Skip to content

Commit c53b0dd

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 033d1d4 commit c53b0dd

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

bin/varnishd/cache/cache_fetch.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
397397
vtim_real now;
398398
unsigned handling;
399399
struct objcore *oc;
400+
const char *met;
400401

401402
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
402403
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
@@ -411,8 +412,14 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
411412

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

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

417424
VCL_backend_fetch_method(bo->vcl, wrk, NULL, bo, NULL);
418425

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)