From: Willy Tarreau Date: Mon, 16 Nov 2026 14:53:44 +0000 (+0000) Subject: DEV: h2: add mkhdr.c, a C port of mkhdr.sh X-Git-Tag: v3.5-dev13~48 X-Git-Url: https://git.kaiwu.me/web//%22?a=commitdiff_plain;h=c53aa3f9ddf1d0406baf9ab36b5c1926752eb544;p=haproxy.git DEV: h2: add mkhdr.c, a C port of mkhdr.sh 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). It's built with "make dev/h2/mkhdr". --- diff --git a/.gitignore b/.gitignore index ebeb7b63d..91ea519f7 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,7 @@ dev/flags/flags dev/poll/poll dev/tcploop/tcploop dev/h2/h2dec +dev/h2/mkhdr dev/haring/haring dev/hpack/decode dev/hpack/gen-rht diff --git a/Makefile b/Makefile index 5a1adb629..87cbb0349 100644 --- a/Makefile +++ b/Makefile @@ -1104,7 +1104,7 @@ help: IGNORE_OPTS=help install install-man install-doc install-bin \ uninstall clean tags cscope tar git-tar version update-version \ opts reg-tests reg-tests-help unit-tests admin/halog/halog dev/flags/flags \ - dev/h2/h2dec dev/haring/haring dev/ncpu/ncpu dev/poll/poll \ + dev/h2/h2dec dev/h2/mkhdr dev/haring/haring dev/ncpu/ncpu dev/poll/poll \ dev/tcploop/tcploop dev/term_events/term_events dev/gdb/pm-from-core \ dev/gdb/libs-from-core dev/sse-latency/sse-latency @@ -1201,6 +1201,9 @@ dev/gdb/pm-from-core: dev/gdb/pm-from-core.o dev/h2/h2dec: dev/h2/h2dec.o $(cmd_LD) $(ARCH_FLAGS) $(LDFLAGS) -o $@ $^ $(LDOPTS) +dev/h2/mkhdr: dev/h2/mkhdr.o + $(cmd_LD) $(ARCH_FLAGS) $(LDFLAGS) -o $@ $^ $(LDOPTS) + dev/haring/haring: dev/haring/haring.o $(cmd_LD) $(ARCH_FLAGS) $(LDFLAGS) -o $@ $^ $(LDOPTS) @@ -1306,7 +1309,7 @@ distclean: clean $(Q)rm -f dev/haring/haring dev/ncpu/ncpu{,.so} dev/poll/poll dev/tcploop/tcploop $(Q)rm -f dev/hpack/decode dev/hpack/gen-enc dev/hpack/gen-rht $(Q)rm -f dev/qpack/decode dev/gdb/pm-from-core dev/gdb/libs-from-core - $(Q)rm -f dev/sse-latency/sse-latency dev/h2/h2dec + $(Q)rm -f dev/sse-latency/sse-latency dev/h2/h2dec dev/h2/mkhdr tags: $(Q)find src include \( -name '*.c' -o -name '*.h' \) -print0 | \ diff --git a/dev/h2/mkhdr.c b/dev/h2/mkhdr.c new file mode 100644 index 000000000..82be21e4f --- /dev/null +++ b/dev/h2/mkhdr.c @@ -0,0 +1,503 @@ +/* + * Raw HTTP/2 frame generator. This is a C port of mkhdr.sh, which is much + * faster when many frames need to be produced. The frames are emitted on + * stdout. + * + * Usage: mkhdr -l -t -f -i > hdr.bin + * See the usage message below for more options. + * + * Build like this from the top directory : + * make dev/h2/mkhdr + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static const char usage_msg[] = + "Usage: %s [options] > hdr.bin\n" + "\n" + "Options:\n" + " -t frame type, symbolic or numeric (default: DATA, or HEADERS\n" + " when -e is used). Type \"p\" emits the connection preface.\n" + " -f [,...] comma-delimited symbolic or numeric flags (default: 0)\n" + " -i stream ID (default: 0)\n" + " -l value of the length field (default: payload length). With\n" + " -d, maximum payload size of each frame (default: 16384).\n" + " -r append raw data to the payload. Backslash escapes are\n" + " interpreted (\\\\, \\n, \\r, \\t, \\xHH, \\NNN...).\n" + " -R append hex codes to the payload. Blanks are ignored, and a\n" + " leading 0 is implied for an odd number of digits.\n" + " -e append a header field to the payload as a literal with\n" + " incremental indexing. Escapes are interpreted as with -r.\n" + " -d take the payload from , split into as many frames as\n" + " needed. Cannot be combined with -r, -R or -e.\n" + " -h, --help show this help and exit.\n" + "\n" + "-r, -R and -e may be repeated and combined, the payload being made of their\n" + "contents in their order of appearance. Numbers are decimal, 0xhex or 0octal.\n" + "\n" + "Supported symbolic types (case insensitive prefix match):\n" + " DATA (0x00) PUSH_PROMISE (0x05)\n" + " HEADERS (0x01) PING (0x06)\n" + " PRIORITY (0x02) GOAWAY (0x07)\n" + " RST_STREAM (0x03) WINDOW_UPDATE (0x08)\n" + " SETTINGS (0x04) CONTINUATION (0x09)\n" + "\n" + "Supported symbolic flags (case insensitive prefix match):\n" + " ES (0x01) PAD (0x08)\n" + " EH (0x04) PRIO (0x20)\n" + " ACK (0x01)\n" + "\n"; + +/* stdout buffer. It must be provided as glibc ignores the size otherwise */ +static char outbuf[65536]; + +static const char preface[] = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"; + +/* symbolic names, in the order they are checked for prefix matching */ +static const struct { const char *name; int val; } types[] = { + { "DATA", 0x00 }, { "HEADERS", 0x01 }, { "RST_STREAM", 0x03 }, + { "SETTINGS", 0x04 }, { "PING", 0x06 }, { "GOAWAY", 0x07 }, + { "WINDOW_UPDATE", 0x08 }, { "CONTINUATION", 0x09 }, + { "PRIORITY", 0x02 }, { "PUSH_PROMISE", 0x05 }, +}; + +static const struct { const char *name; int val; } flag_names[] = { + { "ES", 0x01 }, { "EH", 0x04 }, { "PAD", 0x08 }, { "PRIO", 0x20 }, + { "ACK", 0x01 }, +}; + +/* options seen on the command line, to detect conflicts */ +#define OPT_L 0x0001 +#define OPT_T 0x0002 +#define OPT_F 0x0004 +#define OPT_I 0x0008 +#define OPT_D 0x0010 +#define OPT_R 0x0020 /* -r or -R */ +#define OPT_E 0x0040 + +/* a growable byte buffer */ +struct bbuf { + char *area; + size_t len, size; +}; + +static const char *progname; + +/* prints the usage message (to stderr on error) and exits with code */ +static void usage(int code) +{ + fprintf(code ? stderr : stdout, usage_msg, progname); + exit(code); +} + +/* prints the message to stderr followed by the usage message, and exits + * with code 1. + */ +__attribute__((noreturn, format(printf, 1, 2))) +static void fail(const char *format, ...) +{ + va_list args; + + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + fputc('\n', stderr); + usage(1); + exit(1); +} + +/* appends bytes from to */ +static void bbuf_append(struct bbuf *b, const void *data, size_t len) +{ + if (b->len + len > b->size) { + size_t new_size = b->size ? b->size : 256; + + while (new_size < b->len + len) + new_size *= 2; + b->area = realloc(b->area, new_size); + if (!b->area) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + b->size = new_size; + } + memcpy(b->area + b->len, data, len); + b->len += len; +} + +static void bbuf_putc(struct bbuf *b, unsigned char c) +{ + bbuf_append(b, &c, 1); +} + +/* appends HPACK integer with an -bit prefix and as the first + * byte's high bits (RFC7541#5.1). + */ +static void bbuf_put_varint(struct bbuf *b, unsigned char pfx, int n, unsigned long v) +{ + unsigned long max = (1UL << n) - 1; + + if (v < max) { + bbuf_putc(b, pfx | v); + return; + } + bbuf_putc(b, pfx | max); + v -= max; + while (v >= 128) { + bbuf_putc(b, 0x80 | (v & 127)); + v >>= 7; + } + bbuf_putc(b, v); +} + +/* returns the value of hex digit , or -1 if invalid */ +static int hexval(int c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return -1; +} + +/* appends string to after interpreting printf-like backslash + * escapes, and "%%" as "%". + */ +static void bbuf_put_unescaped(struct bbuf *b, const char *str) +{ + int c, i, v; + + while ((c = (unsigned char)*str++)) { + if (c == '%' && *str == '%') { + str++; + } + else if (c == '\\' && *str) { + c = (unsigned char)*str++; + switch (c) { + case 'a': c = '\a'; break; + case 'b': c = '\b'; break; + case 'e': c = 0x1b; break; + case 'f': c = '\f'; break; + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'v': c = '\v'; break; + case 'x': + if (hexval(*str) < 0) { + /* not an escape */ + bbuf_putc(b, '\\'); + break; + } + for (i = v = 0; i < 2 && hexval(*str) >= 0; i++) + v = v * 16 + hexval(*str++); + c = v; + break; + case '0' ... '7': + v = c - '0'; + for (i = 1; i < 3 && *str >= '0' && *str <= '7'; i++) + v = v * 8 + *str++ - '0'; + c = v & 255; + break; + case '\\': + case '"': + case '\'': + break; + default: + /* unknown escape, keep it as-is */ + bbuf_putc(b, '\\'); + break; + } + } + bbuf_putc(b, c); + } +} + +/* appends the binary form of hex string to , ignoring blanks. If + * the number of digits is odd, a leading zero is implied (e.g. "4" is 0x04). + */ +static void bbuf_put_hex(struct bbuf *b, const char *hex) +{ + const char *p; + int hi = -1, v; + int digits = 0; + + for (p = hex; *p; p++) + digits += (*p != ' ' && *p != '\t'); + if (digits & 1) + hi = 0; + + for (; *hex; hex++) { + if (*hex == ' ' || *hex == '\t') + continue; + v = hexval(*hex); + if (v < 0) + fail("Invalid hex digit '%c' in raw hex data", *hex); + if (hi < 0) + hi = v; + else { + bbuf_putc(b, (hi << 4) | v); + hi = -1; + } + } +} + +/* parses number (decimal, 0xhex or 0octal), and fails with in + * the error message if it cannot be parsed. Note that the number must start + * with a digit. + */ +static unsigned long parse_num(const char *str, const char *what) +{ + unsigned long ret; + char *end; + + if (!isdigit((unsigned char)*str)) + fail("Unparsable %s '%s'", what, str); + errno = 0; + ret = strtoul(str, &end, 0); + if (errno || *end) + fail("Unparsable %s '%s'", what, str); + return ret; +} + +/* parses frame type and returns its value. Symbolic names are matched + * by case-insensitive prefix, in the order of the types[] array. + */ +static int parse_type(const char *str) +{ + size_t len = strlen(str); + int i; + + for (i = 0; i < sizeof(types) / sizeof(types[0]); i++) { + if (strncasecmp(types[i].name, str, len) == 0) + return types[i].val; + } + if (isdigit((unsigned char)*str)) + return parse_num(str, "type"); + fail("Unknown type '%s'", str); +} + +/* parses comma-delimited flags and returns their combined value. Each + * entry is either a symbolic name matched by case-insensitive prefix, or a + * number. + */ +static int parse_flags(const char *str) +{ + char buf[64]; + const char *end; + size_t len; + int flags = 0; + int i; + + while (*str) { + end = strchr(str, ','); + if (!end) + end = str + strlen(str); + len = end - str; + if (len >= sizeof(buf)) + fail("Unknown flag(s) '%s'", str); + memcpy(buf, str, len); + buf[len] = 0; + + if (len) { + for (i = 0; i < sizeof(flag_names) / sizeof(flag_names[0]); i++) { + if (strncasecmp(flag_names[i].name, buf, len) == 0) + break; + } + + if (i < sizeof(flag_names) / sizeof(flag_names[0])) + flags |= flag_names[i].val; + else if (isdigit((unsigned char)*buf)) + flags |= parse_num(buf, "flags"); + else + fail("Unknown flag(s) '%s'", buf); + } + str = *end ? end + 1 : end; + } + return flags; +} + +/* reads file entirely into */ +static void read_file(struct bbuf *b, const char *name) +{ + char buf[65536]; + size_t got; + FILE *f; + + f = fopen(name, "rb"); + if (!f) { + fprintf(stderr, "Cannot open data file '%s': %s\n", name, strerror(errno)); + exit(1); + } + while ((got = fread(buf, 1, sizeof(buf), f)) > 0) + bbuf_append(b, buf, got); + if (ferror(f)) { + fprintf(stderr, "Error reading data file '%s'\n", name); + exit(1); + } + fclose(f); +} + +/* appends a frame header made of , , , to */ +static void mkframe(struct bbuf *b, unsigned long len, int type, int flags, unsigned long sid) +{ + unsigned char hdr[9]; + + hdr[0] = len >> 16; + hdr[1] = len >> 8; + hdr[2] = len; + hdr[3] = type; + hdr[4] = flags; + hdr[5] = (sid >> 24) & 0x7f; + hdr[6] = sid >> 16; + hdr[7] = sid >> 8; + hdr[8] = sid; + bbuf_append(b, hdr, sizeof(hdr)); +} + +int main(int argc, char **argv) +{ + struct bbuf raw = { 0 }; /* payload (-r/-R/-e, in this order) */ + int has_hdrs = 0; /* -e was used */ + struct bbuf data = { 0 }; /* data file contents (-d) */ + struct bbuf out = { 0 }; /* output for one stream */ + const char *data_file = NULL; + const char *type_str = NULL; + unsigned long len = 0, sid = 0; + int seen = 0; + int flags = 0; + int type; + + progname = strrchr(argv[0], '/'); + progname = progname ? progname + 1 : argv[0]; + + /* use a large output buffer so that frames are not emitted in small + * chunks (stdio defaults to 4kB on pipes). + */ + setvbuf(stdout, outbuf, _IOFBF, sizeof(outbuf)); + + if (argc <= 1) + usage(1); + + argv++; argc--; + while (argc > 0 && **argv == '-') { + const char *opt = *argv; + int nargs = 1; + int bit; + + if (strcmp(opt, "-h") == 0 || strcmp(opt, "--help") == 0) + usage(0); + else if (strcmp(opt, "-e") == 0) + nargs = 2; + else if (strcmp(opt, "-l") != 0 && strcmp(opt, "-t") != 0 && + strcmp(opt, "-f") != 0 && strcmp(opt, "-i") != 0 && + strcmp(opt, "-d") != 0 && strcmp(opt, "-r") != 0 && + strcmp(opt, "-R") != 0) + fail("Unknown option '%s'", opt); + + if (argc <= nargs) + fail("Missing argument for option '%s'", opt); + + /* all options but -e may only appear once */ + bit = opt[1] == 'l' ? OPT_L : opt[1] == 't' ? OPT_T : + opt[1] == 'f' ? OPT_F : opt[1] == 'i' ? OPT_I : + opt[1] == 'd' ? OPT_D : opt[1] == 'e' ? OPT_E : OPT_R; + if ((seen & bit) && bit != OPT_E && bit != OPT_R) + fail("Option '%s' may only appear once", opt); + seen |= bit; + + if (strcmp(opt, "-l") == 0) + len = parse_num(argv[1], "length"); + else if (strcmp(opt, "-t") == 0) + type_str = argv[1]; + else if (strcmp(opt, "-f") == 0) + flags = parse_flags(argv[1]); + else if (strcmp(opt, "-i") == 0) + sid = parse_num(argv[1], "stream ID"); + else if (strcmp(opt, "-d") == 0) + data_file = argv[1]; + else if (strcmp(opt, "-r") == 0) + bbuf_put_unescaped(&raw, argv[1]); + else if (strcmp(opt, "-R") == 0) + bbuf_put_hex(&raw, argv[1]); + else if (strcmp(opt, "-e") == 0) { + /* literal header field with incremental indexing, new name */ + struct bbuf tmp = { 0 }; + + has_hdrs = 1; + bbuf_putc(&raw, 0x40); + bbuf_put_unescaped(&tmp, argv[1]); + bbuf_put_varint(&raw, 0, 7, tmp.len); + bbuf_append(&raw, tmp.area, tmp.len); + tmp.len = 0; + bbuf_put_unescaped(&tmp, argv[2]); + bbuf_put_varint(&raw, 0, 7, tmp.len); + bbuf_append(&raw, tmp.area, tmp.len); + free(tmp.area); + } + argv += nargs + 1; + argc -= nargs + 1; + } + + if (argc > 0) + fail("Unexpected argument '%s'", *argv); + + /* reject options that would otherwise be silently ignored */ + if (type_str && strcmp(type_str, "p") == 0 && (seen & ~OPT_T)) + fail("The preface doesn't support any other option"); + if ((seen & OPT_D) && (seen & (OPT_R | OPT_E))) + fail("-d cannot be combined with -r, -R or -e"); + + if (type_str && strcmp(type_str, "p") == 0) { + bbuf_append(&out, preface, sizeof(preface) - 1); + } + else if (!data_file) { + /* -e implies HEADERS unless a type was set */ + type = type_str ? parse_type(type_str) : has_hdrs ? 0x01 : 0x00; + + /* the length defaults to that of the payload */ + if (!(seen & OPT_L)) + len = raw.len; + + mkframe(&out, len, type, flags, sid); + bbuf_append(&out, raw.area, raw.len); + } + else { + /* read file in chunks and send it in multiple + * frames advertising their respective lengths. + */ + size_t ofs, chunk; + + type = type_str ? parse_type(type_str) : 0; + read_file(&data, data_file); + if (!len) + len = 16384; + + for (ofs = 0; ofs < data.len; ofs += chunk) { + chunk = data.len - ofs; + if (chunk > len) + chunk = len; + mkframe(&out, chunk, type, flags, sid); + bbuf_append(&out, data.area + ofs, chunk); + } + } + + if (out.len && fwrite(out.area, 1, out.len, stdout) != out.len) { + fprintf(stderr, "Write error: %s\n", strerror(errno)); + return 1; + } + + if (fflush(stdout) != 0) { + fprintf(stderr, "Write error: %s\n", strerror(errno)); + return 1; + } + return 0; +}