From 603f0de2e47b9a0cbcddb523b9a915c0b9981a0e Mon Sep 17 00:00:00 2001
From: Matthias Dreher <dreherm@web.de>
Date: Mon, 24 Jan 2022 09:11:18 +0100
Subject: [PATCH] Support concatenation of headers (as in
 https://github.com/esp8266/Arduino/commit/1de0c341b55ba8c0993fd3d2e0c5696935578751#diff-977435a9cc4619fa0b8b995085f6ae683485cf563722756bab57108b362da316
 for ESP8266, fixes https://github.com/espressif/arduino-esp32/issues/4069)

---
 libraries/HTTPClient/src/HTTPClient.cpp | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp
index a7bf13e89f5..9750bb8850f 100644
--- a/libraries/HTTPClient/src/HTTPClient.cpp
+++ b/libraries/HTTPClient/src/HTTPClient.cpp
@@ -1263,12 +1263,19 @@ int HTTPClient::handleHeaderResponse()
                     _location = headerValue;
                 }
 
-                for(size_t i = 0; i < _headerKeysCount; i++) {
-                    if(_currentHeaders[i].key.equalsIgnoreCase(headerName)) {
-                        _currentHeaders[i].value = headerValue;
-                        break;
+                for (size_t i = 0; i < _headerKeysCount; i++) {
+                    if (_currentHeaders[i].key.equalsIgnoreCase(headerName)) {
+                        if (!_currentHeaders[i].value.isEmpty()) {
+                            // Existing value, append this one with a comma
+                            _currentHeaders[i].value += ',';
+                            _currentHeaders[i].value += headerValue;
+                        } else {
+                            _currentHeaders[i].value = headerValue;
+                        }
+                        break; // We found a match, stop looking
                     }
                 }
+
             }
 
             if(headerLine == "") {