From 5e0d2b9b2c6a0ba6149380a24539a6a2e6340948 Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 28 Oct 2020 16:30:11 +0100 Subject: [PATCH 1/2] Fix header parsing Valid responses can have other content than HTTP1.x as first line. A valid response is e.g. "ICY 200 OK". This fix allows other responses. This makes it easy to use the client e.g. for streaming audio. fixes https://github.com/espressif/arduino-esp32/issues/4454 --- libraries/HTTPClient/src/HTTPClient.cpp | 26 +++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index 0d84a6d61ff..f6804189d0a 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -1186,23 +1186,33 @@ int HTTPClient::handleHeaderResponse() _transferEncoding = HTTPC_TE_IDENTITY; unsigned long lastDataTime = millis(); + + String headerLine = _client->readStringUntil('\n'); + headerLine.trim(); // remove \r + lastDataTime = millis(); + + log_v("RX: '%s'", headerLine.c_str()); + + if(headerLine.startsWith("HTTP/1.")) { + if(_canReuse) { + _canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0'); + } + } + headerLine.substring(headerLine.indexOf(' ')); + _returnCode = headerLine.substring(headerLine.indexOf(' ')).toInt(); + while(connected()) { size_t len = _client->available(); if(len > 0) { - String headerLine = _client->readStringUntil('\n'); + headerLine = _client->readStringUntil('\n'); headerLine.trim(); // remove \r lastDataTime = millis(); log_v("RX: '%s'", headerLine.c_str()); - if(headerLine.startsWith("HTTP/1.")) { - if(_canReuse) { - _canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0'); - } - _returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt(); - } else if(headerLine.indexOf(':')) { + if(headerLine.indexOf(':')) { String headerName = headerLine.substring(0, headerLine.indexOf(':')); String headerValue = headerLine.substring(headerLine.indexOf(':') + 1); headerValue.trim(); @@ -1450,4 +1460,4 @@ bool HTTPClient::setURL(const String& url) const String &HTTPClient::getLocation(void) { return _location; -} \ No newline at end of file +} From 8c9d46bf5697943ba9bd5cbdb369eb3594e4caba Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 28 Oct 2020 16:43:53 +0100 Subject: [PATCH 2/2] Update HTTPClient.cpp --- libraries/HTTPClient/src/HTTPClient.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index f6804189d0a..aa2b6ed9d75 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -1199,7 +1199,6 @@ int HTTPClient::handleHeaderResponse() _canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0'); } } - headerLine.substring(headerLine.indexOf(' ')); _returnCode = headerLine.substring(headerLine.indexOf(' ')).toInt(); while(connected()) {