From: Christopher Faulet Date: Wed, 2 Dec 2026 14:26:27 +0000 (+0100) Subject: BUG/MEDIUM: mux-h2: send END_STREAM when the trailers are dropped X-Git-Tag: v3.5.0~5 X-Git-Url: https://git.kaiwu.me/http/%22..//%22%22)?a=commitdiff_plain;h=3b6c63b9276354069d6dffa319eab2ca982e9fa4;p=haproxy.git BUG/MEDIUM: mux-h2: send END_STREAM when the trailers are dropped When "option http-drop-response-trailers" or "option http-drop-request-trailers" was set, the trailers loop in h2s_make_trailers() was skipped. But since the commit 221c40bbf ("MINOR: htx: Add a noop block to carry the EOM flag on an empty message"), this loop is also responsible for retrieving the EOM flag on the EOT block. So, when the trailers were dropped, the end of the message was never detected and nothing was emitted to close the stream. The H2 clients received a RST_STREAM(CANCEL) on complete responses and, on the server side, a 504 was returned after the server timeout. Because a chunked H1 message always ends with an EOT block, every chunked H1 message was affected, with or without trailers. Now, the loop is always performed to find the EOT block, but the trailers are skipped when they must be dropped. In that case, an empty DATA frame with the ES flag is emitted. No backport needed. --- diff --git a/reg-tests/http-messaging/drop_trailers.vtc b/reg-tests/http-messaging/drop_trailers.vtc index 73d134997..ccf0fc50b 100644 --- a/reg-tests/http-messaging/drop_trailers.vtc +++ b/reg-tests/http-messaging/drop_trailers.vtc @@ -11,6 +11,11 @@ varnishtest "Test the options dropping the HTTP trailers" # through its log whether the trailer field name is present in what it # relayed. This gives a positive assertion for both the dropped and the # forwarded case. +# +# When the trailers are dropped on the H2 side, the end of the message must +# still be reported (END_STREAM flag). Here vtest is able to parse the H2 +# trailers, so the H2 peers are checked directly. Chunked messages without +# trailers are also tested because they end with an EOT block too. feature ignore_unknown_macro @@ -52,12 +57,46 @@ haproxy horigin -conf { } -start # the origin of the response side, which emits the trailers -server s2 -repeat 2 { +server s2 -repeat 3 { rxreq txresp -nolen -hdr "transfer-encoding: chunked" send "5\r\nworld\r\n0\r\nx-rtlr1: rvalue1\r\n\r\n" } -start +# the origin of the response side, emitting a chunked message without +# trailers +server s3 { + rxreq + txresp -nolen -hdr "transfer-encoding: chunked" + send "5\r\nworld\r\n0\r\n\r\n" +} -start + +# H2 origin of the request side. The whole request, up to the END_STREAM +# flag, must be received. +server s4 { + rxpri + stream 0 { + txsettings + rxsettings + txsettings -ack + } -run + + stream 1 { + rxreq + expect req.url == "/drop" + expect req.bodylen == 5 + expect req.http.x-tlr1 == "" + txresp + } -run + + stream 3 { + rxreq + expect req.url == "/no-trailers" + expect req.bodylen == 5 + txresp + } -run +} -start + # request side observer, between the proxy and the origin haproxy hreq -conf { global @@ -129,6 +168,25 @@ haproxy h1 -conf { backend be_res server s2 ${s2_addr}:${s2_port} + + # H2 on the server side, the request trailers are dropped + frontend fe_req_h2 + bind "fd@${fereqh2}" + default_backend be_req_h2_drop + + backend be_req_h2_drop + option http-drop-request-trailers + server s4 ${s4_addr}:${s4_port} proto h2 + + # H2 on the client side, the response trailers are dropped + frontend fe_res_h2_drop + bind "fd@${fereshdrop}" proto h2 + option http-drop-response-trailers + use_backend be_res_no_tlr if { path /no-trailers } + default_backend be_res + + backend be_res_no_tlr + server s3 ${s3_addr}:${s3_port} } -start # response side observer, between the client and the proxy @@ -200,5 +258,47 @@ client c4 -connect ${hres_sinkkeep_sock} { recv 33 } -run +# the request trailers are dropped on their way to the H2 origin, but the +# end of the message is still reported +client c5 -connect ${h1_fereqh2_sock} { + txreq -req POST -url "/drop" -hdr "transfer-encoding: chunked" -nolen + send "5\r\nhello\r\n0\r\nx-tlr1: value1\r\n\r\n" + rxresp + expect resp.status == 200 + + txreq -req POST -url "/no-trailers" -hdr "transfer-encoding: chunked" -nolen + send "5\r\nhello\r\n0\r\n\r\n" + rxresp + expect resp.status == 200 +} -run + +# the response trailers are dropped on their way to the H2 client, but the +# end of the message is still reported +client c6 -connect ${h1_fereshdrop_sock} { + txpri + stream 0 { + txsettings + rxsettings + txsettings -ack + rxsettings + expect settings.ack == true + } -run + + stream 1 { + txreq -url "/res" + rxresp + expect resp.status == 200 + expect resp.bodylen == 5 + expect resp.http.x-rtlr1 == "" + } -run + + stream 3 { + txreq -url "/no-trailers" + rxresp + expect resp.status == 200 + expect resp.bodylen == 5 + } -run +} -run + syslog Sreq -wait syslog Sres -wait diff --git a/src/mux_h2.c b/src/mux_h2.c index 08e5a404d..7ef197307 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -8035,16 +8035,19 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) int hdr; int idx; int eom = 0; + int drop; TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s); /* get trailers. */ hdr = 0; - /* Skip the trailers because the corresponding conf option was set */ - if ((!(h2c->flags & H2_CF_IS_BACK) && (h2c->proxy->options & PR_O_HTTP_DROP_RES_TRLS)) || - ((h2c->flags & H2_CF_IS_BACK) && (h2c->proxy->options & PR_O_HTTP_DROP_REQ_TRLS))) - goto skip_trailers; + /* Skip the trailers if the corresponding conf option was set. But the + * blocks must still be parsed to find the EOT and to know if the + * message is finished, to emit the ES flag. + */ + drop = ((!(h2c->flags & H2_CF_IS_BACK) && (h2c->proxy->options & PR_O_HTTP_DROP_RES_TRLS)) || + ((h2c->flags & H2_CF_IS_BACK) && (h2c->proxy->options & PR_O_HTTP_DROP_REQ_TRLS))); for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { type = htx_get_blk_type(blk); @@ -8054,6 +8057,8 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) break; } if (type == HTX_BLK_TLR) { + if (drop) + continue; if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) { TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s); goto fail; @@ -8069,7 +8074,6 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) } } - skip_trailers: /* marker for end of trailers */ list[hdr].n = ist("");