MINOR: compression: Improve the way Vary header is added
When a message is compressed, A "Vary" header is added with
"accept-encoding" value. However, a new header is always added, regardless
there is already a Vary header or not. In addition, if there is already a
Vary header, there is no check on values to be sure "accept-encoding" value
is not already there. So it is possible to have it twice.
To improve this part, we now test Vary header values and "accept-encoding"
is only added if it was not found. In addition, "accept-encoding" value is
appended to the last Vary header found, if any. Otherwise, a new header is
added.
diff --git a/src/flt_http_comp.c b/src/flt_http_comp.c
index 3b939e1..d34b56a 100644
--- a/src/flt_http_comp.c
+++ b/src/flt_http_comp.c
@@ -408,7 +408,7 @@
{
struct htx *htx = htxbuf(&msg->chn->buf);
struct htx_sl *sl;
- struct http_hdr_ctx ctx;
+ struct http_hdr_ctx ctx, last_vary;
struct comp_algo *comp_algo;
int comp_index;
@@ -454,8 +454,29 @@
}
}
- if (!http_add_header(htx, ist("Vary"), ist("Accept-Encoding")))
- goto error;
+ /* Add "Vary: Accept-Encoding" header but only if it is not found. */
+ ctx.blk = NULL;
+ last_vary.blk = NULL;
+ while (http_find_header(htx, ist("Vary"), &ctx, 0)) {
+ if (isteqi(ctx.value, ist("Accept-Encoding")))
+ break;
+ last_vary = ctx;
+ }
+ /* No "Accept-Encoding" value found. */
+ if (ctx.blk == NULL) {
+ if (last_vary.blk == NULL) {
+ /* No Vary header found at all. Add our header */
+ if (!http_add_header(htx, ist("Vary"), ist("Accept-Encoding")))
+ goto error;
+ }
+ else {
+ /* At least one Vary header found. Append the value to
+ * the last one.
+ */
+ if (!http_append_header_value(htx, &last_vary, ist("Accept-Encoding")))
+ goto error;
+ }
+ }
/*
* Add Content-Encoding header when it's not identity encoding.