From e8ae99b111b4b365a9615f6ac7c60aba101d6ea6 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 26 Apr 2023 11:25:46 +0200 Subject: [PATCH] DEV: h2: support reading frame payload from a file 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 | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/dev/h2/mkhdr.sh b/dev/h2/mkhdr.sh index 350514c88..4d129fa40 100755 --- a/dev/h2/mkhdr.sh +++ b/dev/h2/mkhdr.sh @@ -4,8 +4,9 @@ # All fields are optional. 0 assumed when absent. USAGE=\ -"Usage: %s [-l ] [-t ] [-f ] [-i ] > hdr.bin - Numbers are decimal or 0xhex. Not set=0. +"Usage: %s [-l ] [-t ] [-f ] [-i ] [ -d ] > hdr.bin + Numbers are decimal or 0xhex. Not set=0. If is passed, it points + to a file that is read and chunked into frames of 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 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