]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: compression: pass COMP_CT_EVENT to the algorithms for server-sent events
authorWilly Tarreau <w@1wt.eu>
Sun, 15 Nov 2026 21:42:06 +0000 (22:42 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 16 Nov 2026 08:20:00 +0000 (09:20 +0100)
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%

include/haproxy/compression-t.h
src/flt_http_comp.c

index a2b2a77f1f23271d05ed4a34129c1a1bbe68bb29..7847ee586a2776cfd9ef821370830d8756e58e0f 100644 (file)
@@ -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
index b0cfd77ed02a08e3b77297d03870e211fdddc0ae..93b7107f71b23487404db530bfaa76b73d42599c 100644 (file)
@@ -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))) {