From: Christopher Faulet Date: Fri, 20 Nov 2026 16:02:29 +0000 (+0100) Subject: MINOR: htx: Add a noop block to carry the EOM flag on an empty message X-Git-Url: https://git.kaiwu.me/http/$%7BGITURL%7D$%7Bcid%7D?a=commitdiff_plain;h=221c40bbf857ef0ba1feb7771e7aad4ea4a86bb9;p=haproxy.git MINOR: htx: Add a noop block to carry the EOM flag on an empty message A new HTX block type, HTX_BLK_NOOP, is added. It has the same traits as the EOH and EOT blocks: a size of 1, to be able to wake the endpoint up when it is emitted alone, but nothing is sent when a consumer encounters it. It may carry the EOM flag and it is allowed anywhere in the message. It is used by htx_set_eom() to report the end of an empty message: the EOM flag was previously carried by an EOT block in this case, which was misleading, an end-of-trailers block carrying no data. As a consequence, an EOT block without the EOM flag is now a valid state, the message is simply not ended. This must be handled by all the consumers, especially the muxes: - The H1 mux skips the NOOP blocks and, when one carries the EOM flag, it reports the end of the message exactly like a last DATA block. An EOT block without the EOM flag still ends the message framing, as before: on the wire, the trailers terminate a chunked message, there is nothing to add after them. - The H2 mux skips the NOOP blocks and, when one carries the EOM flag, it emits an empty DATA frame with the END_STREAM flag and closes the stream. For a trailers frame, the END_STREAM flag is now only set when the EOT block carries the EOM flag, and the stream is only closed in this case. An EOT block without the EOM flag means the message is not finished and more data may follow. - The H3 mux skips the NOOP blocks and, when one carries the EOM flag, it sets the fin flag on the stream. Like the H2 mux, the fin flag is only reported for an EOT block carrying the EOM flag. - The FCGI mux skips the NOOP blocks and, when one carries the EOM flag, it emits an empty STDIN record to signal the end of the request. All the other consumers, the filters, the applets, the sample fetches and the health-checks, were audited: they either ignore the unknown block types or stop their walk on the block carrying the EOM flag, so a NOOP block is properly handled by all of them. There is no need to backport this patch, it is an enabler for future uses. --- diff --git a/include/haproxy/htx-t.h b/include/haproxy/htx-t.h index 70f82f356..5b4cfaad1 100644 --- a/include/haproxy/htx-t.h +++ b/include/haproxy/htx-t.h @@ -210,7 +210,7 @@ enum htx_blk_type { HTX_BLK_TLR = 5, /* trailer name/value block */ HTX_BLK_EOT = 6, /* end-of-trailers block */ HTX_BLK_RAW_DATA = 7, /* raw data block (tunneled data) */ - /* 8 .. 14 unused */ + HTX_BLK_NOOP = 8, /* no-op block, carries nothing, may carry the EOM flag */ HTX_BLK_UNUSED = 15, /* unused/removed block */ }; diff --git a/include/haproxy/htx.h b/include/haproxy/htx.h index 583a2ac7b..758bb5f04 100644 --- a/include/haproxy/htx.h +++ b/include/haproxy/htx.h @@ -635,8 +635,8 @@ static inline struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist return blk; } -/* Adds an HTX block of type EOH or EOT in . It returns the new block on - * success. Otherwise, it returns NULL. +/* Adds an HTX block of type EOH, EOT or NOOP in . It returns the new + * block on success. Otherwise, it returns NULL. */ static inline struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type type) { @@ -650,6 +650,19 @@ static inline struct htx_blk *htx_add_endof(struct htx *htx, enum htx_blk_type t return blk; } +/* Adds a NOOP block in . It carries no data and nothing is emitted when + * it is encountered by the consumers. But, like the EOH and EOT blocks, its + * size is 1 to be able to wake the endpoint up when it is emitted alone. It + * may carry the EOM flag and it is allowed anywhere in the message. + * + * It returns the new block on success. Otherwise, it returns NULL. + */ +static inline struct htx_blk *htx_add_noop(struct htx *htx) +{ + return htx_add_endof(htx, HTX_BLK_NOOP); +} + + /* Add all headers from the list into the HTX message , followed by * the EOH. On success, it returns the last block inserted (the EOH), otherwise * NULL is returned. @@ -888,8 +901,8 @@ static inline int htx_expect_more(const struct htx *htx) /* Set EOM flag in . This function must always be called to report the end * of the message. Corresponding flags must never be added by hand, except from * the HTX API itself. It must also be called after the last block of the - * message was added. Finally, this function takes care to add an EOT block if - * the HTX message is empty. + * message was added. Finally, this function takes care to add a NOOP block if + * the HTX message is empty, so the EOM flag is not carried by an EOT block. * * Returns 1 on success else 0. */ @@ -898,7 +911,7 @@ static inline int htx_set_eom(struct htx *htx) struct htx_blk *blk; if (htx_is_empty(htx)) { - if (!htx_add_endof(htx, HTX_BLK_EOT)) + if (!htx_add_noop(htx)) return 0; } @@ -906,6 +919,7 @@ static inline int htx_set_eom(struct htx *htx) BUG_ON(htx_get_blk_type(blk) != HTX_BLK_EOH && htx_get_blk_type(blk) != HTX_BLK_EOT && + htx_get_blk_type(blk) != HTX_BLK_NOOP && htx_get_blk_type(blk) != HTX_BLK_DATA); blk->flags |= HTX_BLK_FL_EOM; return 1; @@ -931,9 +945,9 @@ static inline int htx_copy_msg(struct htx *htx, const struct buffer *msg) return htx_append_msg(htx, htx_msg); } -/* Remove all blocks except headers. Trailers will also be removed too. The EOM - * flag, if set on the payload part, it is then reinserted. It means a EOT black - * could be added if the message only contains payload. +/* Remove all blocks except headers. Trailers will also be removed too. The + * EOM flag, if set on the payload part, it is then reinserted. It means a + * NOOP block could be added if the message only contains payload. */ static inline void htx_skip_msg_payload(struct htx *htx) { @@ -974,6 +988,7 @@ static inline const char *htx_blk_type_str(enum htx_blk_type type) case HTX_BLK_RAW_DATA: return "HTX_BLK_RAW_DATA"; case HTX_BLK_TLR: return "HTX_BLK_TLR"; case HTX_BLK_EOT: return "HTX_BLK_EOT"; + case HTX_BLK_NOOP: return "HTX_BLK_NOOP"; case HTX_BLK_UNUSED: return "HTX_BLK_UNUSED"; default: return "HTX_BLK_???"; }; diff --git a/src/flt_http_comp.c b/src/flt_http_comp.c index 90ed66360..f8076ceee 100644 --- a/src/flt_http_comp.c +++ b/src/flt_http_comp.c @@ -377,7 +377,7 @@ comp_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, * carried by this block. It was lost if the block * was removed and was the last one of the * message. In this case, it must be restored, - * adding an EOT block to support it. + * adding a NOOP block to support it. */ if (!htx_msg_ended(htx)) { unsigned int data = htx->data; diff --git a/src/h3.c b/src/h3.c index 1f990dad2..84e89f47d 100644 --- a/src/h3.c +++ b/src/h3.c @@ -3052,7 +3052,7 @@ static int h3_resp_trailers_send(struct qcs *qcs, struct htx *htx, char *fin) list[hdr].v = htx_get_blk_value(htx, blk); hdr++; } - else { + else if (type != HTX_BLK_NOOP) { TRACE_ERROR("unexpected HTX block", H3_EV_TX_FRAME|H3_EV_TX_HDR, qcs->qcc->conn, qcs); goto err; } @@ -3407,6 +3407,20 @@ static size_t h3_snd_buf(struct qcs *qcs, struct buffer *buf, size_t count, char } break; + case HTX_BLK_NOOP: + /* Nothing is emitted for a NOOP block. But if it + * carries the EOM flag, the end of the message is + * reported. + */ + if (blk->flags & HTX_BLK_FL_EOM) { + TRACE_USER("transcoding last HTX message", H3_EV_STRM_SEND, qcs->qcc->conn, qcs); + *fin = 1; + } + htx_remove_blk(htx, blk); + total += bsize; + count -= bsize; + break; + default: htx_remove_blk(htx, blk); total += bsize; diff --git a/src/mux_fcgi.c b/src/mux_fcgi.c index 01f726bfb..6d7608db4 100644 --- a/src/mux_fcgi.c +++ b/src/mux_fcgi.c @@ -4370,6 +4370,19 @@ static size_t fcgi_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, } __fallthrough; + case HTX_BLK_NOOP: + /* Nothing is emitted for a NOOP block. But if it + * carries the EOM flag, an empty STDIN record + * is sent to signal the end of the request. + */ + if (blk->flags & HTX_BLK_FL_EOM) { + TRACE_PROTO("sending FCGI STDIN record", FCGI_EV_TX_RECORD|FCGI_EV_TX_STDIN, fconn->conn, fstrm, htx); + ret = fcgi_strm_send_empty_stdin(fconn, fstrm); + if (!ret) + goto done; + } + __fallthrough; + default: remove_blk: htx_remove_blk(htx, blk); diff --git a/src/mux_h1.c b/src/mux_h1.c index 6f9a4b69b..188bcdbfa 100644 --- a/src/mux_h1.c +++ b/src/mux_h1.c @@ -3361,6 +3361,32 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf, h1m->state = H1_MSG_TRAILERS; break; } + else if (type == HTX_BLK_NOOP) { + /* Consume the NOOP block, nothing is emitted. If it + * carries the EOM flag, the end of the message is + * reported, exactly like a last DATA block. + */ + if (blk->flags & HTX_BLK_FL_EOM) { + if ((!(h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_REQ)) || + ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP))) { + last_data = 1; + } + else if (h1m->flags & H1_MF_CHNK) { + if (h1m->curr_len) { + COUNT("chunk smaller than announced"); + TRACE_ERROR("chunk smaller than announced", + H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); + goto error; + } + /* Emit the last chunk for chunked messages */ + if (!chunk_memcat(&outbuf, "0\r\n\r\n", 5)) + goto full; + last_data = 1; + } + else + last_data = 1; + } + } else goto error; @@ -3478,6 +3504,9 @@ static size_t h1_make_tunnel(struct h1s *h1s, struct h1m *h1m, struct buffer *bu if (!h1_format_htx_data(v, &outbuf, 0)) goto full; } + else if (type == HTX_BLK_NOOP) { + /* Consume the NOOP block, nothing is emitted */ + } else goto error; @@ -3637,8 +3666,9 @@ static size_t h1_make_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx } h1m->state = H1_MSG_DONE; } - else - goto error; + else if (type == HTX_BLK_NOOP) { + /* Consume the NOOP block, nothing is emitted */ + } nextblk: ret += sz; diff --git a/src/mux_h2.c b/src/mux_h2.c index 8dd296ade..60e438598 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -8010,6 +8010,7 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) int ret = 0; int hdr; int idx; + int eom = 0; TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s); @@ -8024,8 +8025,10 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { type = htx_get_blk_type(blk); - if (type == HTX_BLK_EOT) + if (type == HTX_BLK_EOT) { + eom = !!(blk->flags & HTX_BLK_FL_EOM); break; + } if (type == HTX_BLK_TLR) { if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) { TRACE_ERROR("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s); @@ -8036,7 +8039,7 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) list[hdr].v = htx_get_blk_value(htx, blk); hdr++; } - else { + else if (type != HTX_BLK_NOOP) { TRACE_ERROR("will not encode unexpected htx block", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s); goto fail; } @@ -8046,6 +8049,13 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) /* marker for end of trailers */ list[hdr].n = ist(""); + /* An EOT block without the EOM flag means the message is not + * finished. The trailers frame must not carry the END_STREAM flag + * and, if there is nothing to emit, it is simply skipped. + */ + if (!eom && !hdr) + goto done; + mbuf = br_tail(h2c->mbuf); retry: if (!h2_get_buf(h2c, mbuf)) { @@ -8068,11 +8078,14 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) if (outbuf.size < 9) goto full; - /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ - memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); + /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4[+ES=1 if eom] */ + memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); + if (eom) + outbuf.area[4] |= 0x01; write_n32(outbuf.area + 5, h2s->id); // 4 bytes outbuf.data = 9; + /* encode all headers */ for (idx = 0; idx < hdr; idx++) { /* these ones do not exist in H2 or must not appear in @@ -8123,8 +8136,11 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) * received an empty trailers block followed or we removed its * contents above). Because of this we can't send a HEADERS * frame, so we have to cheat and instead send an empty DATA - * frame conveying the ES flag. + * frame conveying the ES flag. It can only be done when the + * message is finished, an empty DATA frame without the ES + * flag is pointless. */ + BUG_ON(!eom); outbuf.area[3] = H2_FT_DATA; outbuf.area[4] = H2_F_DATA_END_STREAM; } @@ -8153,10 +8169,12 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) h2c->flags &= ~H2_CF_SHTS_UPDATED; } - if (h2s->st == H2_SS_OPEN) - h2s->st = H2_SS_HLOC; - else - h2s_close(h2s); + if (eom) { + if (h2s->st == H2_SS_OPEN) + h2s->st = H2_SS_HLOC; + else + h2s_close(h2s); + } /* OK we could properly deliver the response */ done: @@ -8528,6 +8546,27 @@ static size_t h2_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, in } break; + case HTX_BLK_NOOP: + /* Nothing is emitted for a NOOP block. But if it + * carries the EOM flag, the end of the message + * is reported, like for an empty DATA block. + */ + if (blk->flags & HTX_BLK_FL_EOM) { + h2s->flags &= ~H2_SF_MORE_HTX_DATA; + if (!(h2s->flags & H2_SF_BODY_TUNNEL)) { + if (h2_send_empty_data_es(h2s) > 0) { + if (h2s->st == H2_SS_OPEN) + h2s->st = H2_SS_HLOC; + else + h2s_close(h2s); + } + } + } + htx_remove_blk(htx, blk); + total += bsize; + count -= bsize; + break; + default: htx_remove_blk(htx, blk); total += bsize;