]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEV: h2: support reading frame payload from a file
authorWilly Tarreau <w@1wt.eu>
Wed, 26 Apr 2023 09:25:46 +0000 (11:25 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 26 Apr 2023 09:36:02 +0000 (11:36 +0200)
Now we can build a series of data frames by reading from a file and
chunking it into frames of requested length. It's mostly useful for
data frames (e.g. post). One way to announce these upfront is to
capture the output of curl use without content-length:

  $ nc -lp4446 > post-h2-nocl.bin
  $ curl -v --http2-prior-knowledge http://127.0.0.1:4446/url -H "content-length:" -d @/dev/null

Then just change the 5th byte from the end from 1 to 0 to remove the
end-of-stream bit, it will allow to chain a file, then to send an
empty DATA frame with ES set :

  $ (dev/h2/mkhdr.sh -i 1 -t data -d CHANGELOG;
     dev/h2/mkhdr.sh -i 1 -t data -l 0 -f es) > h2-data-changelog.bin

Then post that to the server:
  $ cat post-h2-nocl.bin h2-data-changelog.bin | nc 0 4446

dev/h2/mkhdr.sh

index 350514c88c1d7b534a442026d867a0e3b57dcb1f..4d129fa40fe3f43a2552c4f3cf1c4761ce7b2ac4 100755 (executable)
@@ -4,8 +4,9 @@
 # All fields are optional. 0 assumed when absent.
 
 USAGE=\
-"Usage: %s [-l <len>] [-t <type>] [-f <flags>] [-i <sid>] > hdr.bin
-        Numbers are decimal or 0xhex. Not set=0.
+"Usage: %s [-l <len> ] [-t <type>] [-f <flags>] [-i <sid>] [ -d <data> ] > hdr.bin
+        Numbers are decimal or 0xhex. Not set=0. If <data> is passed, it points
+        to a file that is read and chunked into frames of <len> bytes.
 
 Supported symbolic types (case insensitive prefix match):
    DATA        (0x00)      PUSH_PROMISE   (0x05)
@@ -108,6 +109,7 @@ while [ -n "$1" -a -z "${1##-*}" ]; do
                -t)        TYPE="$2"     ; shift 2 ;;
                -f)        FLAGS="$2"    ; shift 2 ;;
                -i)        ID="$2"       ; shift 2 ;;
+               -d)        DATA="$2"     ; shift 2 ;;
                -h|--help) usage "${0##*}"; quit;;
                *)         usage "${0##*}"; die ;;
        esac
@@ -133,6 +135,17 @@ if [ -n "${ID##[0-9]*}" ]; then
        die
 fi
 
-mkframe "$LEN" "$TYPE" "$FLAGS" "$ID"
+if [ -z "$DATA" ]; then
+       mkframe "$LEN" "$TYPE" "$FLAGS" "$ID"
+else
+       # read file $DATA in <LEN> chunks and send it in multiple frames
+       # advertising their respective lengths.
+       [ $LEN -gt 0 ] || LEN=16384
+
+       while read -rN "$LEN" payload || [ ${#payload} -gt 0 ]; do
+               mkframe "${#payload}" "$TYPE" "$FLAGS" "$ID"
+               echo -n "$payload"
+       done < "$DATA"
+fi
 
 exit 0