]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: http: Add function to remove all occurrences of a value in a header
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 26 May 2026 13:27:18 +0000 (15:27 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 26 May 2026 16:28:07 +0000 (18:28 +0200)
http_remove_header_value() function was added to parse a header value and
remove all occurrences of a specific value.

This patch is mandatory to fix a bug.

include/haproxy/http.h

index aa20f5e16792c621bda286fac3a39fc248fda072..8ac549d72c1e8b25823b0f49172bef251c7edcf7 100644 (file)
@@ -326,6 +326,50 @@ static inline int is_immutable_header(struct ist hdr)
        }
 }
 
+/* This function parses comma-separated values from <hv> and rewrite it in place,
+ * skip all occurrences of <value>. It is the caller responsibility to deal with
+ * empty header value.
+ */
+static inline void http_remove_header_value(struct ist *hv, struct ist value)
+{
+       char *e, *n, *p;
+       struct ist word;
+
+       word.ptr = hv->ptr - 1; // -1 for next loop's pre-increment
+       p = hv->ptr;
+       e = hv->ptr + hv->len;
+       hv->len = 0;
+
+       while (++word.ptr < e) {
+               /* skip leading delimiter and blanks */
+               if (HTTP_IS_LWS(*word.ptr))
+                       continue;
+
+               n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
+               word.len = n - word.ptr;
+
+               /* trim trailing blanks */
+               while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
+                       word.len--;
+
+               if (isteqi(word, value))
+                       goto skip_val;
+
+               if (hv->ptr + hv->len == p) {
+                       /* no rewrite done till now */
+                       hv->len = n - hv->ptr;
+               }
+               else {
+                       if (hv->len)
+                               hv->ptr[hv->len++] = ',';
+                       istcat(hv, word, e - hv->ptr);
+               }
+
+         skip_val:
+               word.ptr = p = n;
+       }
+}
+
 #endif /* _HAPROXY_HTTP_H */
 
 /*