--- /dev/null
+varnishtest "H1->H2 upgrade with the H2 preface received in several segments"
+
+#REGTEST_TYPE=bug
+
+feature ignore_unknown_macro
+
+# The H2 preface may be received in several segments on an HTTP/1 listener.
+# It must not be parsed as an incomplete or invalid H1 request: the connection
+# must be upgraded once the whole preface was received. A preface split
+# between 18 and 23 bytes used to fail: it forms a complete but invalid H1
+# request. Below, the preface is split at 18 and 23 bytes (the lowest and the
+# highest splits that used to fail), at 1 and 16 bytes, and in several small
+# segments.
+#
+# The client is a raw one, sending its preface in segments, because vtest
+# always sends the whole preface itself when the HTTP/2 mode is started. A
+# small delay is required between the segments, otherwise they could be
+# received at once. Nothing is sent back by haproxy on a partial preface, so
+# there is no way to synchronize on something else. The upgrade is verified
+# by sending a raw H2 request (SETTINGS + HEADERS frames) after the preface:
+# the backend server must receive it. The client waits for the server, using
+# a barrier, before closing its connection.
+
+barrier b1 cond 2 -cyclic
+
+server s1 {
+ # split at 18 bytes
+ rxreq
+ expect req.url == "/1"
+ txresp -status 200
+ barrier b1 sync
+
+ accept
+
+ # split at 1 byte
+ rxreq
+ expect req.url == "/2"
+ txresp -status 200
+ barrier b1 sync
+
+ accept
+
+ # split at 23 bytes
+ rxreq
+ expect req.url == "/3"
+ txresp -status 200
+ barrier b1 sync
+
+ accept
+
+ # split at 16 bytes
+ rxreq
+ expect req.url == "/4"
+ txresp -status 200
+ barrier b1 sync
+
+ accept
+
+ # several small segments
+ rxreq
+ expect req.url == "/5"
+ txresp -status 200
+ barrier b1 sync
+
+ accept
+
+ # nothing else must be received
+ accept
+} -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}"
+
+ listen feh1
+ bind "fd@${feh1}"
+ server s1 ${s1_addr}:${s1_port}
+} -start
+
+# raw H2 client sending its preface split at 18 bytes then a complete
+# request: SETTINGS frame + HEADERS frame with literal headers, no
+# HPACK dynamic table.
+client c1 -connect ${h1_feh1_sock} {
+ # preface: "PRI * HTTP/2.0\r\n\r\n"
+ sendhex "50 52 49 20 2a 20 48 54 54 50 2f 32 2e 30 0d 0a 0d 0a"
+ delay 0.05
+ # preface: "SM\r\n\r\n"
+ sendhex "53 4d 0d 0a 0d 0a"
+
+ # SETTINGS frame (empty)
+ sendhex "00 00 00 04 00 00 00 00 00"
+
+ # HEADERS frame, stream 1, END_HEADERS, literal headers:
+ # :method GET, :scheme http, :path /1, :authority x
+ sendhex "00 00 33 01 05 00 00 00 01 00 07 3a 6d 65 74 68"
+ sendhex "6f 64 03 47 45 54 00 07 3a 73 63 68 65 6d 65 04"
+ sendhex "68 74 74 70 00 05 3a 70 61 74 68 02 2f 31 00"
+ sendhex "0a 3a 61 75 74 68 6f 72 69 74 79 01 78"
+
+ barrier b1 sync
+} -run
+
+# same preface split at 1 byte
+client c2 -connect ${h1_feh1_sock} {
+ # preface: "P" then "RI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
+ sendhex "50"
+ delay 0.05
+ sendhex "52 49 20 2a 20 48 54 54 50 2f 32 2e 30 0d 0a 0d 0a 53 4d 0d 0a 0d 0a"
+
+ # SETTINGS + HEADERS (:path /2)
+ sendhex "00 00 00 04 00 00 00 00 00"
+ sendhex "00 00 33 01 05 00 00 00 01 00 07 3a 6d 65 74 68"
+ sendhex "6f 64 03 47 45 54 00 07 3a 73 63 68 65 6d 65 04"
+ sendhex "68 74 74 70 00 05 3a 70 61 74 68 02 2f 32 00"
+ sendhex "0a 3a 61 75 74 68 6f 72 69 74 79 01 78"
+
+ barrier b1 sync
+} -run
+
+# same preface split at 23 bytes ("...\r\n\r\nSM\r\n\r" then "\n")
+client c3 -connect ${h1_feh1_sock} {
+ sendhex "50 52 49 20 2a 20 48 54 54 50 2f 32 2e 30 0d 0a 0d 0a 53 4d 0d 0a 0d"
+ delay 0.05
+ sendhex "0a"
+
+ # SETTINGS + HEADERS (:path /3)
+ sendhex "00 00 00 04 00 00 00 00 00"
+ sendhex "00 00 33 01 05 00 00 00 01 00 07 3a 6d 65 74 68"
+ sendhex "6f 64 03 47 45 54 00 07 3a 73 63 68 65 6d 65 04"
+ sendhex "68 74 74 70 00 05 3a 70 61 74 68 02 2f 33 00"
+ sendhex "0a 3a 61 75 74 68 6f 72 69 74 79 01 78"
+
+ barrier b1 sync
+} -run
+
+# same preface split at 16 bytes
+client c4 -connect ${h1_feh1_sock} {
+ sendhex "50 52 49 20 2a 20 48 54 54 50 2f 32 2e 30 0d 0a"
+ delay 0.05
+ sendhex "0d 0a 53 4d 0d 0a 0d 0a"
+
+ # SETTINGS + HEADERS (:path /4)
+ sendhex "00 00 00 04 00 00 00 00 00"
+ sendhex "00 00 33 01 05 00 00 00 01 00 07 3a 6d 65 74 68"
+ sendhex "6f 64 03 47 45 54 00 07 3a 73 63 68 65 6d 65 04"
+ sendhex "68 74 74 70 00 05 3a 70 61 74 68 02 2f 34 00"
+ sendhex "0a 3a 61 75 74 68 6f 72 69 74 79 01 78"
+
+ barrier b1 sync
+} -run
+
+# same preface sent in several small segments: the upgrade must be
+# performed once the last one was received
+client c5 -connect ${h1_feh1_sock} {
+ sendhex "50"
+ delay 0.05
+ sendhex "52 49 20 2a 20"
+ delay 0.05
+ sendhex "48 54 54 50 2f 32 2e 30 0d 0a 0d 0a"
+ delay 0.05
+ sendhex "53 4d 0d 0a 0d 0a"
+
+ # SETTINGS + HEADERS (:path /5)
+ sendhex "00 00 00 04 00 00 00 00 00"
+ sendhex "00 00 33 01 05 00 00 00 01 00 07 3a 6d 65 74 68"
+ sendhex "6f 64 03 47 45 54 00 07 3a 73 63 68 65 6d 65 04"
+ sendhex "68 74 74 70 00 05 3a 70 61 74 68 02 2f 35 00"
+ sendhex "0a 3a 61 75 74 68 6f 72 69 74 79 01 78"
+
+ barrier b1 sync
+} -run
+
+# A partial preface (18 bytes) followed by garbage is not a preface: the
+# data are parsed as an H1 request and answered with a 400.
+client c_err1 -connect ${h1_feh1_sock} {
+ sendhex "50 52 49 20 2a 20 48 54 54 50 2f 32 2e 30 0d 0a 0d 0a"
+ send "GARBAGE\r\n\r\n"
+ rxresp
+ expect resp.status == 400
+ expect_close
+} -run
+
+# A preface-like magic ("XY" instead of "SM") is not a preface: no
+# upgrade, the answer is a 400.
+client c_err2 -connect ${h1_feh1_sock} {
+ send "PRI * HTTP/2.0\r\n\r\nXY\r\n\r\n"
+ rxresp
+ expect resp.status == 400
+ expect_close
+} -run