From ca07c03bf3e727008d7be95fa9098b0eb425613a Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Wed, 30 Oct 2024 19:22:07 +0100 Subject: [PATCH] relay/api: combine request headers with the same name If a request repeats the same header name multiple times, merge the header values into a comma separated string. Previously, only the last header specified would be used. For header fields that are defined as a comma-separated list, a client may choose to send it as multiple headers instead of one header with comma-separated values. The specification says that these are equivalent, so we can therefore join the headers into a comma-separated string. This is specified at https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.2 which says: A sender MUST NOT generate multiple header fields with the same field name in a message unless either the entire field value for that header field is defined as a comma-separated list [i.e., #(values)] or the header field is a well-known exception (as noted below). A recipient MAY combine multiple header fields with the same field name into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field value to the combined field value in order, separated by a comma. The order in which header fields with the same field name are received is therefore significant to the interpretation of the combined field value; a proxy MUST NOT change the order of these field values when forwarding a message. --- src/plugins/relay/relay-http.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/plugins/relay/relay-http.c b/src/plugins/relay/relay-http.c index 8cde856b9..239e127a6 100644 --- a/src/plugins/relay/relay-http.c +++ b/src/plugins/relay/relay-http.c @@ -385,7 +385,7 @@ relay_http_parse_header (struct t_relay_http_request *request, int ws_deflate_allowed) { char *pos, *name, *name_lower, *error, **items; - const char *ptr_value; + const char *existing_value, *ptr_value; int i, num_items; long number; @@ -424,6 +424,10 @@ relay_http_parse_header (struct t_relay_http_request *request, ptr_value++; } + existing_value = weechat_hashtable_get (request->headers, name_lower); + if (existing_value) + ptr_value = WEECHAT_STR_CONCAT(", ", existing_value, ptr_value); + /* add header in the hashtable */ weechat_hashtable_set (request->headers, name_lower, ptr_value);