From: Willy Tarreau Date: Mon, 16 Nov 2026 14:54:41 +0000 (+0000) Subject: DEV: h2: mkhdr: support stream ID ranges with -i X-Git-Tag: v3.5-dev13~46 X-Git-Url: https://git.kaiwu.me/http/%7B@url%7D?a=commitdiff_plain;h=f256419ba951c171bc69333e43c371d0354048e0;p=haproxy.git DEV: h2: mkhdr: support stream ID ranges with -i Opening many streams at once (e.g. to test the concurrent streams limits) required one call per stream. Now "-i -" emits the same frame(s) for each stream ID from to included, in ascending order, only the stream ID differing between them. When combined with -n, each stream's frame(s) are repeated times before switching to the next stream. All stream IDs of the range are used, regardless of their parity. --- diff --git a/dev/h2/mkhdr.c b/dev/h2/mkhdr.c index f5df21187..c08269c21 100644 --- a/dev/h2/mkhdr.c +++ b/dev/h2/mkhdr.c @@ -26,7 +26,9 @@ static const char usage_msg[] = " -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" + " -i [-] stream ID, or range of stream IDs (default: 0). With a\n" + " range, the frame(s) are emitted for each stream ID in\n" + " ascending order, each of them times (see -n).\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" @@ -271,6 +273,29 @@ static unsigned long parse_num(const char *str, const char *what) return ret; } +/* parses stream ID or stream ID range ("[-]") into + * and . equals when no range is specified. + */ +static void parse_sid_range(const char *str, unsigned long *first, unsigned long *last) +{ + const char *dash = strchr(str, '-'); + char buf[64]; + + if (!dash) { + *first = *last = parse_num(str, "stream ID"); + return; + } + + if (dash - str >= sizeof(buf)) + fail("Unparsable stream ID '%s'", str); + memcpy(buf, str, dash - str); + buf[dash - str] = 0; + *first = parse_num(buf, "stream ID"); + *last = parse_num(dash + 1, "stream ID"); + if (*last < *first) + fail("Invalid stream ID range '%s'", str); +} + /* parses frame type and returns its value. Symbolic names are matched * by case-insensitive prefix, in the order of the types[] array. */ @@ -349,6 +374,27 @@ static void read_file(struct bbuf *b, const char *name) fclose(f); } +/* sets the stream ID of the frame starting at the beginning of to . + * If is non-zero, all the frames that follow in are updated as + * well, which requires that their advertised lengths match their payload. + */ +static void set_sid(struct bbuf *b, unsigned long sid, int all) +{ + unsigned char *hdr; + size_t ofs = 0; + + while (ofs + 9 <= b->len) { + hdr = (unsigned char *)b->area + ofs; + hdr[5] = (sid >> 24) & 0x7f; + hdr[6] = sid >> 16; + hdr[7] = sid >> 8; + hdr[8] = sid; + if (!all) + break; + ofs += 9 + ((hdr[0] << 16) | (hdr[1] << 8) | hdr[2]); + } +} + /* appends a frame header made of , , , to */ static void mkframe(struct bbuf *b, unsigned long len, int type, int flags, unsigned long sid) { @@ -374,8 +420,10 @@ int main(int argc, char **argv) struct bbuf out = { 0 }; /* output for one stream */ const char *data_file = NULL; const char *type_str = NULL; - unsigned long len = 0, sid = 0; + unsigned long len = 0, sid = 0, last_sid = 0; unsigned long count = 1; + unsigned long n; + int preface_only = 0; int seen = 0; int flags = 0; int type; @@ -426,7 +474,7 @@ int main(int argc, char **argv) else if (strcmp(opt, "-f") == 0) flags = parse_flags(argv[1]); else if (strcmp(opt, "-i") == 0) - sid = parse_num(argv[1], "stream ID"); + parse_sid_range(argv[1], &sid, &last_sid); else if (strcmp(opt, "-d") == 0) data_file = argv[1]; else if (strcmp(opt, "-n") == 0) @@ -465,6 +513,7 @@ int main(int argc, char **argv) if (type_str && strcmp(type_str, "p") == 0) { bbuf_append(&out, preface, sizeof(preface) - 1); + preface_only = 1; } else if (!data_file) { /* -e implies HEADERS unless a type was set */ @@ -497,11 +546,21 @@ int main(int argc, char **argv) } } - while (out.len && count--) { - if (fwrite(out.area, 1, out.len, stdout) != out.len) { - fprintf(stderr, "Write error: %s\n", strerror(errno)); - return 1; + /* emit the frame(s) times for each stream ID of the range. + * The frames were built for the first one. + */ + while (out.len) { + 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; + } } + if (sid >= last_sid) + break; + sid++; + if (!preface_only) + set_sid(&out, sid, !!data_file); } if (fflush(stdout) != 0) {