--- /dev/null
+varnishtest "Caching of close-delimited responses"
+
+# A response without content-length and not chunked ends with the close of
+# the server connection. It must only be cached if the connection is cleanly
+# closed. If the response is interrupted by a timeout, it is truncated and
+# must not be cached. Each server is called once per expected request to the
+# origin. So, if a truncated response is cached, the second request on
+# /timeout is served from the cache and the test fails.
+
+feature ignore_unknown_macro
+
+# Synchronizes the clients with the end of each s2 iteration. vtest accepts
+# the next connection only once the previous iteration is finished.
+barrier b1 cond 2 -cyclic
+
+# Clean close: the response must be cached, the server is only called once
+server s1 {
+ rxreq
+ expect req.url == "/clean"
+ txresp -nolen -hdr "Cache-Control: max-age=60" -hdr "Connection: close" \
+ -body "0123456789"
+} -start
+
+# Server timeout: the response must not be cached, the server is called twice
+server s2 -repeat 2 {
+ rxreq
+ expect req.url == "/timeout"
+ txresp -nolen -hdr "Cache-Control: max-age=60" -hdr "Connection: close" \
+ -body "0123456789"
+ delay 1
+ barrier b1 sync
+} -start
+
+haproxy h1 -conf {
+ global
+ .if feature(THREAD)
+ thread-groups 1
+ .endif
+
+ defaults
+ mode http
+ timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout server 500ms
+
+ cache my_cache
+ total-max-size 3
+ max-age 60
+
+ frontend fe
+ bind "fd@${fe}"
+ http-after-response set-header x-cache-hit %[res.cache_hit]
+ use_backend be_clean if { path /clean }
+ default_backend be_timeout
+
+ backend be_clean
+ http-request cache-use my_cache
+ http-response cache-store my_cache
+ server s1 ${s1_addr}:${s1_port}
+
+ backend be_timeout
+ http-request cache-use my_cache
+ http-response cache-store my_cache
+ server s2 ${s2_addr}:${s2_port}
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+ txreq -url "/clean"
+ rxresp
+ expect resp.status == 200
+ expect resp.bodylen == 10
+ expect resp.http.x-cache-hit == 0
+} -run
+
+client c2 -connect ${h1_fe_sock} {
+ txreq -url "/clean"
+ rxresp
+ expect resp.status == 200
+ expect resp.bodylen == 10
+ expect resp.http.x-cache-hit == 1
+} -run
+
+client c3 -connect ${h1_fe_sock} {
+ txreq -url "/timeout"
+ rxresp
+ expect resp.status == 200
+ expect resp.bodylen == 10
+ expect resp.http.x-cache-hit == 0
+ barrier b1 sync
+} -run
+
+client c4 -connect ${h1_fe_sock} {
+ txreq -url "/timeout"
+ rxresp
+ expect resp.status == 200
+ expect resp.bodylen == 10
+ expect resp.http.x-cache-hit == 0
+ barrier b1 sync
+} -run
+
+server s2 -wait
return 1;
if (st && st->first_block) {
+ /* Without transfer length, the end of the response is the
+ * close of the server connection. It is only complete on a
+ * clean close. On error or on abort (a timeout for instance),
+ * it is truncated and must not be cached.
+ */
+ if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
+ (s->scb->flags & (SC_FL_EOI|SC_FL_ERROR)) != SC_FL_EOI) {
+ disable_cache_entry(st, filter, shctx);
+ return 1;
+ }
object = (struct cache_entry *)st->first_block->data;