From a4019d451a24e19a2ed739daad1e6a3135f2cbc7 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 23 Oct 2023 16:41:29 +0200 Subject: [PATCH] Nano RP2040 connect: Add http response check --- src/utility/ota/OTA-nano-rp2040.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/utility/ota/OTA-nano-rp2040.cpp b/src/utility/ota/OTA-nano-rp2040.cpp index 89b079b6..7e259f07 100644 --- a/src/utility/ota/OTA-nano-rp2040.cpp +++ b/src/utility/ota/OTA-nano-rp2040.cpp @@ -53,6 +53,7 @@ enum class rp2040OTAError : int ServerConnectError = RP2040_OTA_ERROR_BASE - 10, HttpHeaderError = RP2040_OTA_ERROR_BASE - 11, HttpDataError = RP2040_OTA_ERROR_BASE - 12, + HttpResponse = RP2040_OTA_ERROR_BASE - 14, ErrorOpenUpdateFile = RP2040_OTA_ERROR_BASE - 19, ErrorWriteUpdateFile = RP2040_OTA_ERROR_BASE - 20, ErrorReformat = RP2040_OTA_ERROR_BASE - 21, @@ -207,6 +208,26 @@ int rp2040_connect_onOTARequest(char const * ota_url) return static_cast(rp2040OTAError::HttpHeaderError); } + /* Check HTTP response status code */ + char const * http_response_ptr = strstr(http_header.c_str(), "HTTP/1.1"); + if (!http_response_ptr) + { + DEBUG_ERROR("%s: Failure to extract http response from header", __FUNCTION__); + return static_cast(rp2040OTAError::ErrorParseHttpHeader); + } + /* Find start of numerical value. */ + char * ptr = const_cast(http_response_ptr); + for (ptr += strlen("HTTP/1.1"); (*ptr != '\0') && !isDigit(*ptr); ptr++) { } + /* Extract numerical value. */ + String http_response_str; + for (; isDigit(*ptr); ptr++) http_response_str += *ptr; + int const http_response = atoi(http_response_str.c_str()); + + if (http_response != 200) { + DEBUG_ERROR("%s: HTTP response status code = %d", __FUNCTION__, http_response); + return static_cast(rp2040OTAError::HttpResponse); + } + /* Extract concent length from HTTP header. A typical entry looks like * "Content-Length: 123456" */ @@ -218,7 +239,7 @@ int rp2040_connect_onOTARequest(char const * ota_url) return static_cast(rp2040OTAError::ErrorParseHttpHeader); } /* Find start of numerical value. */ - char * ptr = const_cast(content_length_ptr); + ptr = const_cast(content_length_ptr); for (; (*ptr != '\0') && !isDigit(*ptr); ptr++) { } /* Extract numerical value. */ String content_length_str;