mkhdr.sh forks several processes per frame, which makes it slow when
producing many frames (around 4ms per invocation here, vs 0.2ms for
the C version). This is a port of the script to C, accepting the same
options and producing byte-identical output for valid inputs, with a
few differences:
- the data file passed with -d is read in binary mode, so NUL bytes
are preserved (bash's "read" silently drops them);
- header names and values passed with -e may be longer than 127
bytes, as their lengths are now properly encoded as HPACK integers,
and backslash escapes are interpreted there as with -r, the length
being computed after unescaping;
- -r, -R and -e may be repeated and combined, their contents being
concatenated in their order of appearance to form the payload
(e.g. "-R 4 -r abcd" gives 04 61 62 63 64). An odd number of hex
digits in -R implies a leading zero;
- -e no longer forces the frame type to HEADERS, it only changes the
default type, so that -t always wins regardless of the option order
(the script would turn "-t c -e foo bar" into a HEADERS frame);
- -l always sets the frame's length field, which only defaults to the
payload's length (the script ignored -l when a payload was given).
This permits to emit a frame header alone and append its payload
separately, or to advertise a wrong length;
- options that would otherwise be silently ignored are rejected: any
option other than -e, -r or -R repeated, -t p with other options,
-d with -r/-R/-e;
- the "ACK" flag (0x01) is supported for SETTINGS and PING;
- unknown flags or invalid hex digits are reported as errors instead
of being silently ignored;
- the usage message describes each option on its own line, and is
sent to stderr on errors.
The output is emitted through a 64kB buffer so that frames are sent in
large blocks (stdio defaults to 4kB on pipes).