From: Willy Tarreau Date: Sun, 15 Nov 2026 21:42:06 +0000 (+0100) Subject: MINOR: compression: pass COMP_CT_EVENT to the algorithms for server-sent events X-Git-Tag: v3.5-dev13~70 X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/%7B@url%7D?a=commitdiff_plain;h=4d0db5e7bf6758430a581ab0ef5d41ba4ce470b6;p=haproxy.git MINOR: compression: pass COMP_CT_EVENT to the algorithms for server-sent events Server-sent events ("text/event-stream"), as emitted by AI and API gateways, are made of small events that are flushed one at a time, each of which repeats most of the previous one. Compressing them one at a time without any history is useless, but an algorithm may handle them differently if it knows about them. select_compression_response_header() now sets the content type to the new COMP_CT_EVENT when the Content-Type header starts with "text/event-stream". Requests are left untouched: server-sent events only exist in responses, and since an algorithm may need memory for this content type, clients must not be able to force it. The algorithms don't make use of it yet and treat it as COMP_CT_GENERIC. This already improves the compression ratio for SSE to be below unity: bytes in bytes out ratio before: 227584 232303 102% after: 227584 199829 88% --- diff --git a/include/haproxy/compression-t.h b/include/haproxy/compression-t.h index a2b2a77f1..7847ee586 100644 --- a/include/haproxy/compression-t.h +++ b/include/haproxy/compression-t.h @@ -77,6 +77,7 @@ struct comp_ctx { enum comp_ctype { COMP_CT_GENERIC = 0, /* unknown or binary contents */ COMP_CT_TEXT = 1, /* "text/..." contents */ + COMP_CT_EVENT = 2, /* "text/event-stream": small events flushed one at a time */ }; /* Thanks to MSIE/IIS, the "deflate" name is ambiguous, as according to the RFC diff --git a/src/flt_http_comp.c b/src/flt_http_comp.c index b0cfd77ed..93b7107f7 100644 --- a/src/flt_http_comp.c +++ b/src/flt_http_comp.c @@ -153,7 +153,11 @@ comp_prepare_compress_request(struct comp_state *st, struct stream *s, struct ht */ ctx.blk = NULL; if (http_find_header(htx, ist("Content-Type"), &ctx, 1)) { - /* text contents may be encoded differently by the algorithm */ + /* text contents may be encoded differently by the algorithm. + * Server-sent events only exist in responses, so a client + * claiming to send some doesn't get the event content type, + * which may cost memory to the algorithm. + */ if (ctx.value.len >= 5 && strncasecmp("text/", ctx.value.ptr, 5) == 0) ctype = COMP_CT_TEXT; @@ -768,9 +772,16 @@ select_compression_response_header(struct comp_state *st, struct stream *s, stru if (ctx.value.len >= 9 && strncasecmp("multipart", ctx.value.ptr, 9) == 0) goto fail; - /* text contents may be encoded differently by the algorithm */ - if (ctx.value.len >= 5 && strncasecmp("text/", ctx.value.ptr, 5) == 0) - ctype = COMP_CT_TEXT; + /* text contents may be encoded differently by the algorithm, + * and so may server-sent events, which are flushed one at a + * time. + */ + if (ctx.value.len >= 5 && strncasecmp("text/", ctx.value.ptr, 5) == 0) { + if (ctx.value.len >= 17 && strncasecmp("event-stream", ctx.value.ptr + 5, 12) == 0) + ctype = COMP_CT_EVENT; + else + ctype = COMP_CT_TEXT; + } if ((s->be->comp && (comp_type = s->be->comp->types_res)) || (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types_res))) {