--- /dev/null
+varnishtest "Bandwidth limitation of a message with trailers"
+
+# Only the payload is subject to the bandwidth limitation. The trailers (TLR
+# blocks and EOT block) are not, and they cannot be partially forwarded
+# anyway, the muxes emit them all at once. So, when the limit ends on the
+# trailers, they must all be forwarded. Here the server sends a chunked
+# response with 10 bytes of payload and two trailers. In the HTX message, the
+# payload is a DATA block of 10 bytes, each trailer is a TLR block of 13 bytes
+# and the EOT block is 1 byte length. The limits are chosen to stop the
+# forwarding:
+#
+# - at the beginning of the first TLR block (10 bytes)
+# - in the middle of the first TLR block (15 bytes)
+# - at the beginning of the second TLR block (23 bytes)
+# - at the beginning of the EOT block (36 bytes)
+#
+# In all cases, the response must be received with its trailers.
+
+feature ignore_unknown_macro
+
+server s1 -repeat 4 {
+ rxreq
+ txresp -nolen -hdr "Transfer-Encoding: chunked" \
+ -body "a\r\n0123456789\r\n0\r\nx-trailer1: foo\r\nx-trailer2: bar\r\n\r\n"
+} -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 "${HAPROXY_TEST_TIMEOUT-5s}"
+
+ frontend fe
+ bind "fd@${fe}" proto h2
+ filter bwlim-out lim10 default-limit 10 default-period 1s min-size 1
+ filter bwlim-out lim15 default-limit 15 default-period 1s min-size 1
+ filter bwlim-out lim23 default-limit 23 default-period 1s min-size 1
+ filter bwlim-out lim36 default-limit 36 default-period 1s min-size 1
+ http-request set-var(txn.path) path
+ http-response set-bandwidth-limit lim10 if { var(txn.path) -m str /10 }
+ http-response set-bandwidth-limit lim15 if { var(txn.path) -m str /15 }
+ http-response set-bandwidth-limit lim23 if { var(txn.path) -m str /23 }
+ http-response set-bandwidth-limit lim36 if { var(txn.path) -m str /36 }
+ default_backend be
+
+ backend be
+ server s1 ${s1_addr}:${s1_port}
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+ txpri
+ stream 0 {
+ txsettings
+ rxsettings
+ txsettings -ack
+ rxsettings
+ expect settings.ack == true
+ } -run
+
+ stream 1 {
+ txreq -url "/15"
+ rxhdrs
+ expect resp.status == 200
+ rxdata
+ expect resp.body == "0123456789"
+ rxhdrs
+ expect resp.http.x-trailer1 == "foo"
+ expect resp.http.x-trailer2 == "bar"
+ } -run
+
+ stream 3 {
+ txreq -url "/23"
+ rxhdrs
+ expect resp.status == 200
+ rxdata
+ expect resp.body == "0123456789"
+ rxhdrs
+ expect resp.http.x-trailer1 == "foo"
+ expect resp.http.x-trailer2 == "bar"
+ } -run
+
+ stream 5 {
+ txreq -url "/36"
+ rxhdrs
+ expect resp.status == 200
+ rxdata
+ expect resp.body == "0123456789"
+ rxhdrs
+ expect resp.http.x-trailer1 == "foo"
+ expect resp.http.x-trailer2 == "bar"
+ } -run
+
+ stream 7 {
+ txreq -url "/10"
+ rxhdrs
+ expect resp.status == 200
+ rxdata
+ expect resp.body == "0123456789"
+ rxhdrs
+ expect resp.http.x-trailer1 == "foo"
+ expect resp.http.x-trailer2 == "bar"
+ } -run
+} -run
#include <haproxy/global.h>
#include <haproxy/http_ana-t.h>
#include <haproxy/http_rules.h>
+#include <haproxy/htx.h>
#include <haproxy/proxy.h>
#include <haproxy/sample.h>
#include <haproxy/stream.h>
static int bwlim_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
unsigned int offset, unsigned int len)
{
- return bwlim_apply_limit(filter, msg->chn, len);
+ struct htx *htx = htxbuf(&msg->chn->buf);
+ struct htx_ret htxret;
+ int ret;
+
+ ret = bwlim_apply_limit(filter, msg->chn, len);
+ if (ret < 0 || ret >= len)
+ return ret;
+
+ /* Only the payload is subject to the limitation. If the limit ends on
+ * the trailers (TLR blocks) or on the EOT block, it means all the data
+ * were forwarded. So forward everything. The muxes cannot emit the
+ * trailers partially anyway.
+ */
+ htxret = htx_find_offset(htx, offset + ret);
+ if (htxret.blk &&
+ (htx_get_blk_type(htxret.blk) == HTX_BLK_TLR || htx_get_blk_type(htxret.blk) == HTX_BLK_EOT))
+ ret = len;
+ return ret;
}
/**************************************************************************