From 6673a0d77775f91c6a5d62cb4ceccba40b0c3d63 Mon Sep 17 00:00:00 2001 From: Alec Murphy Date: Fri, 10 Oct 2025 11:31:57 -0400 Subject: [PATCH] System/Libraries/Http: Introduce @http_concat_header() This function will append values with existing keys to the same JSON array for further parsing, e.g. where multiple Set-Cookie instances are present in a response header. --- System/Libraries/Http.HC | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/System/Libraries/Http.HC b/System/Libraries/Http.HC index 66fbad6..98c8cc5 100644 --- a/System/Libraries/Http.HC +++ b/System/Libraries/Http.HC @@ -273,6 +273,29 @@ done_parsing_url: return url; } +U0 @http_concat_header(JsonObject* headers, U8* key, U8* value) +{ + // Concatenate multiple values w/ same key to JSON array, if necessary, OR simply store the value. + JsonKey* existing_key = headers->@(key, TRUE); + JsonArray* a = NULL; + if (existing_key) { + switch (existing_key->type) { + case JSON_ARRAY: + a = existing_key->value; + break; + default: + a = Json.CreateArray(erythros_mem_task); + a->append(existing_key->value, JSON_STRING); + existing_key->value = a; + existing_key->type = JSON_ARRAY; + break; + } + a->append(value, JSON_STRING); + } else { + headers->set(key, value, JSON_STRING); + } +} + U0 @http_parse_response_headers(@http_response* resp, U8* buffer, I64 length) { if (!resp || !buffer || !length) @@ -308,7 +331,7 @@ U0 @http_parse_response_headers(@http_response* resp, U8* buffer, I64 length) key_ptr = lines[i]; value_ptr = lines[i] + j + 2; (*StrFind("\r", value_ptr)) = NULL; - headers->set(key_ptr, value_ptr, JSON_STRING); + @http_concat_header(headers, key_ptr, value_ptr); goto @http_next_header_line; } }