From: Willy Tarreau Date: Mon, 16 Nov 2026 14:56:55 +0000 (+0000) Subject: DEV: h2: mkhdr: support "--" as a delimiter between frame descriptions X-Git-Tag: v3.5-dev13~45 X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/%7B@url%7D?a=commitdiff_plain;h=933eeefc5f19b471851932170205611f0a8fca9a;p=haproxy.git DEV: h2: mkhdr: support "--" as a delimiter between frame descriptions Producing a sequence of different frames required one call per frame. Now "--" may be used to delimit multiple frame descriptions on the same command line. Each description is processed independently (options are reset between them), and the resulting frames are emitted in the same order. All descriptions are checked before anything is emitted, so that an error in one of them doesn't produce a partial output. Empty descriptions are rejected. Example: $ dev/h2/mkhdr -t p -- -t s -- -t h -i 1-3 -f es,eh -e :method GET \ -- -t ping -R 0000000000000001 -n 2 --- diff --git a/dev/h2/mkhdr.c b/dev/h2/mkhdr.c index c08269c21..e38b16d3f 100644 --- a/dev/h2/mkhdr.c +++ b/dev/h2/mkhdr.c @@ -3,7 +3,7 @@ * faster when many frames need to be produced. The frames are emitted on * stdout. * - * Usage: mkhdr -l -t -f -i > hdr.bin + * Usage: mkhdr [ [--] ]* > hdr.bin * See the usage message below for more options. * * Build like this from the top directory : @@ -20,7 +20,12 @@ #include static const char usage_msg[] = - "Usage: %s [options] > hdr.bin\n" + "Usage: %s [ [--] ]* > hdr.bin\n" + " %s -h | --help\n" + "\n" + "Each describes one frame (or series of frames) using the options\n" + "below. \"--\" is used as an optional delimiter between such descriptions. They\n" + "are processed independently from each other, and emitted in the same order.\n" "\n" "Options:\n" " -t frame type, symbolic or numeric (default: DATA, or HEADERS\n" @@ -98,7 +103,7 @@ 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); + fprintf(code ? stderr : stdout, usage_msg, progname, progname); exit(code); } @@ -412,7 +417,10 @@ static void mkframe(struct bbuf *b, unsigned long len, int type, int flags, unsi bbuf_append(b, hdr, sizeof(hdr)); } -int main(int argc, char **argv) +/* parses the options in describing one frame (or series of + * frames), and emits it unless is set. Exits on error. + */ +static void emit_frame(int argc, char **argv, int check_only) { struct bbuf raw = { 0 }; /* payload (-r/-R/-e, in this order) */ int has_hdrs = 0; /* -e was used */ @@ -428,18 +436,9 @@ int main(int argc, char **argv) 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); + if (argc <= 0) + fail("Empty frame description"); - argv++; argc--; while (argc > 0 && **argv == '-') { const char *opt = *argv; int nargs = 1; @@ -511,6 +510,12 @@ int main(int argc, char **argv) if ((seen & OPT_D) && (seen & (OPT_R | OPT_E))) fail("-d cannot be combined with -r, -R or -e"); + if (check_only) { + if (type_str && strcmp(type_str, "p") != 0) + parse_type(type_str); + goto leave; + } + if (type_str && strcmp(type_str, "p") == 0) { bbuf_append(&out, preface, sizeof(preface) - 1); preface_only = 1; @@ -553,7 +558,7 @@ int main(int argc, char **argv) for (n = 0; n < count; n++) { if (fwrite(out.area, 1, out.len, stdout) != out.len) { fprintf(stderr, "Write error: %s\n", strerror(errno)); - return 1; + exit(1); } } if (sid >= last_sid) @@ -563,6 +568,39 @@ int main(int argc, char **argv) set_sid(&out, sid, !!data_file); } +leave: + free(raw.area); + free(data.area); + free(out.area); +} + +int main(int argc, char **argv) +{ + int check_only, first, i; + + 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); + + /* frame descriptions are delimited by "--". They are all checked + * first so that nothing is emitted if any of them is invalid. + */ + for (check_only = 1; check_only >= 0; check_only--) { + for (first = i = 1; i <= argc; i++) { + if (i < argc && strcmp(argv[i], "--") != 0) + continue; + emit_frame(i - first, argv + first, check_only); + first = i + 1; + } + } + if (fflush(stdout) != 0) { fprintf(stderr, "Write error: %s\n", strerror(errno)); return 1;