]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: mux-fcgi: send the empty STDIN record after a chunked request
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 2 Dec 2026 14:26:27 +0000 (15:26 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 2 Dec 2026 14:54:41 +0000 (15:54 +0100)
When a chunked request was sent to a FastCGI application, the empty
STDIN record marking the end of the request was not sent when the
request body and the EOT block were processed at once by
fcgi_strm_send_stdin(). In this function, all blocks but the DATA ones
were removed without checking the EOM flag. Only the EOM flag on a DATA
block was considered, which is only the case for content-length
requests. So for chunked requests, with or without trailers, an
application reading its input to the end waited forever. The request
ended on a 504 after the server timeout, holding a server slot
meanwhile.

Before the commit 9b1a3215f ("MINOR: mux-fcgi: Use flags of HTX blocks to
detect end of message during sending"), it was handled at the end of the
function by testing the HTX_FL_EOM flag on the emptied HTX message.

Now, fcgi_strm_send_stdin() stops on a block carrying the EOM flag and
lets fcgi_snd_buf() handle it, to send the empty STDIN record.

No backport needed.

reg-tests/http-messaging/fcgi_end_of_request.vtc [new file with mode: 0644]
src/mux_fcgi.c

diff --git a/reg-tests/http-messaging/fcgi_end_of_request.vtc b/reg-tests/http-messaging/fcgi_end_of_request.vtc
new file mode 100644 (file)
index 0000000..9db4547
--- /dev/null
@@ -0,0 +1,103 @@
+varnishtest "FCGI: end of the request body"
+
+# The end of the request body must be signaled to the FastCGI application by
+# exactly one empty STDIN record, whatever the way the request body length is
+# announced: content-length, chunked or chunked with trailers.
+#
+# vtest cannot play a FastCGI application. So the application is emulated by
+# a TCP proxy, inspecting the received records, chained to a vtest server
+# sending a canned FCGI response. The connection is only accepted once the
+# STDIN record carrying the request body is followed by a unique empty STDIN
+# record, ending the payload. Otherwise it is rejected after the inspect
+# delay.
+
+feature ignore_unknown_macro
+
+barrier b1 cond 2 -cyclic
+
+# the canned FCGI response, for the request ID 1. It is sent once the request
+# is fully received, since the TCP proxy only connects to the server at this
+# stage:
+#  - STDOUT record: "Status: 200\r\nContent-Type: text/plain\r\n
+#                    Content-Length: 2\r\n\r\nok"
+#  - empty STDOUT record
+#  - END_REQUEST record (app status 0, FCGI_REQUEST_COMPLETE)
+server s1 -repeat 3 {
+       sendhex "01060001003E0000"
+       sendhex "5374617475733A203230300D0A436F6E74656E742D547970653A20746578742F706C61696E0D0A436F6E74656E742D4C656E6774683A20320D0A0D0A6F6B"
+       sendhex "0106000100000000"
+       sendhex "01030001000800000000000000000000"
+       barrier b1 sync
+} -start
+
+haproxy happ -conf {
+    global
+    .if feature(THREAD)
+        thread-groups 1
+    .endif
+
+    defaults
+       mode tcp
+       timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+       timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
+       timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"
+
+    listen app
+       bind "fd@${app}"
+       tcp-request inspect-delay "${HAPROXY_TEST_TIMEOUT-5s}"
+       # STDIN record with "hello" (request ID 1) followed by an empty STDIN
+       # record, at the end of the payload
+       tcp-request content accept if { req.payload(0,0),hex -m reg 010500010005000068656C6C6F0105000100000000$ }
+       tcp-request content reject if WAIT_END
+       server s1 ${s1_addr}:${s1_port}
+} -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}"
+
+    fcgi-app app
+       docroot /
+
+    listen fe
+       bind "fd@${fe}"
+       # chunked requests must be fully received before being sent to the
+       # FastCGI application
+       option http-buffer-request
+       use-fcgi-app app
+       server app ${happ_app_addr}:${happ_app_port} proto fcgi
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+       txreq -req POST -url "/content-length" -body "hello"
+       rxresp
+       expect resp.status == 200
+       expect resp.body == "ok"
+       barrier b1 sync
+} -run
+
+client c2 -connect ${h1_fe_sock} {
+       txreq -req POST -url "/chunked" -nolen -hdr "Transfer-Encoding: chunked" \
+           -body "5\r\nhello\r\n0\r\n\r\n"
+       rxresp
+       expect resp.status == 200
+       expect resp.body == "ok"
+       barrier b1 sync
+} -run
+
+client c3 -connect ${h1_fe_sock} {
+       txreq -req POST -url "/trailers" -nolen -hdr "Transfer-Encoding: chunked" \
+           -body "5\r\nhello\r\n0\r\nX-Trailer: foo\r\n\r\n"
+       rxresp
+       expect resp.status == 200
+       expect resp.body == "ok"
+       barrier b1 sync
+} -run
index 523c0877e288ca1af71d596759dcb7d13e7394c2..14a8618f36b7426a91cad5fe787321f3d3aa4acf 100644 (file)
@@ -2363,6 +2363,13 @@ static size_t fcgi_strm_send_stdin(struct fcgi_conn *fconn, struct fcgi_strm *fs
                                break;
 
                        default:
+                               /* A block carrying the EOM flag (EOT or NOOP)
+                                * ends the message. Stop here and let the
+                                * caller handle it, to send the empty STDIN
+                                * record. Other blocks (trailers) are dropped.
+                                */
+                               if (blk->flags & HTX_BLK_FL_EOM)
+                                       goto done;
                                break;
                }
                total += size;