--- /dev/null
+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