From 3c9c2036abc444d5f8a9350caa24481ed1f4839d Mon Sep 17 00:00:00 2001 From: chromoxdor <33860956+chromoxdor@users.noreply.github.com> Date: Thu, 26 Jun 2025 12:16:41 +0200 Subject: [PATCH] HttpResponseParser fix:prevent race condition --- src/src/Commands/HTTP.cpp | 6 ------ src/src/Helpers/HTTPResponseParser.cpp | 26 +++++++++++++------------- src/src/Helpers/HTTPResponseParser.h | 15 ++++++++++++--- src/src/Helpers/Networking.cpp | 16 +++++++++++++++- src/src/Helpers/Networking.h | 4 ++++ 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/src/Commands/HTTP.cpp b/src/src/Commands/HTTP.cpp index ff1b358ed..bf1ef851a 100644 --- a/src/src/Commands/HTTP.cpp +++ b/src/src/Commands/HTTP.cpp @@ -105,12 +105,6 @@ const __FlashStringHelper* httpEmitToHTTP(struct EventStruct *event, int httpCode = -1; -# if FEATURE_JSON_EVENT - - // remove the hash property (#json) from the uri like browsers do - path = parseUriPath(path); -# endif // if FEATURE_JSON_EVENT - send_via_http( logIdentifier, timeout, diff --git a/src/src/Helpers/HTTPResponseParser.cpp b/src/src/Helpers/HTTPResponseParser.cpp index 054efdc81..3ff6bbf0b 100644 --- a/src/src/Helpers/HTTPResponseParser.cpp +++ b/src/src/Helpers/HTTPResponseParser.cpp @@ -16,9 +16,8 @@ # endif // if FEATURE_JSON_EVENT -int parseJson = -1; // indicates whether the URI lacks a JSON flag (-1), or if present, specifies the group number (>= 0) -void eventFromResponse(const String& host, const int& httpCode, const String& uri, HTTPClient& http) { +void eventFromResponse(const String& host, const int& httpCode, const String& uri, HTTPClient& http, const int& parseJson) { if ((httpCode == 200)) { if (parseJson == -1) { // -------------------------------------------------------------------------------------------Thingspeak @@ -398,27 +397,28 @@ void readAndProcessJsonKeys(DynamicJsonDocument *root, int numJson) { } // ------------------------------------------------------------------------------------------- JSONevent Helper -String parseUriPath(const String& path) { +UriParseResult parseUriPath(const String& path) { + UriParseResult result; + result.cleanedPath = path; // default + result.parseJson = -1; + int jsonIndex = path.indexOf(F("#json")); - parseJson = -1; if (jsonIndex != -1) { String numberStr = path.substring(jsonIndex + 5); - if (numberStr.length() == 0) { // nothing after #json - parseJson = 0; + if (numberStr.length() == 0) { + result.parseJson = 0; } else if ((numberStr.toInt() > 0) && numberStr.equals(String(numberStr.toInt()))) { - // numberStr is a valid positive integer (no junk after number) - parseJson = numberStr.toInt(); + result.parseJson = numberStr.toInt(); } - if (parseJson > -1) { - return path.substring(0, jsonIndex); + if (result.parseJson > -1) { + result.cleanedPath = path.substring(0, jsonIndex); } - } + } - // no valid "#json" tag found, return original path - return path; + return result; } # endif // if FEATURE_JSON_EVENT diff --git a/src/src/Helpers/HTTPResponseParser.h b/src/src/Helpers/HTTPResponseParser.h index 619bd66c9..93ecec05f 100644 --- a/src/src/Helpers/HTTPResponseParser.h +++ b/src/src/Helpers/HTTPResponseParser.h @@ -23,7 +23,8 @@ void eventFromResponse(const String& host, const int & httpCode, const String& uri, - HTTPClient & http); + HTTPClient & http, + const int & parseJson); # if FEATURE_JSON_EVENT @@ -34,9 +35,17 @@ void readAndProcessJsonKeys(DynamicJsonDocument*root, int numJson); /** - * @brief Parses the URI, detects the JSON flag (#json), extracts the associated number if present and returns the cleaned path. + * @brief Result of parsing a URI for JSON flag. */ -String parseUriPath(const String& path); +struct UriParseResult { + String cleanedPath; + int parseJson; +}; + +/** + * @brief Parses the URI, detects the JSON flag (#json), extracts the associated number if present, and returns both the cleaned path and the parseJson value. + */ +UriParseResult parseUriPath(const String& path); # endif // if FEATURE_JSON_EVENT #endif // RESPONSE_PARSER_SUPPORT diff --git a/src/src/Helpers/Networking.cpp b/src/src/Helpers/Networking.cpp index c91a3aaef..027c0264c 100644 --- a/src/src/Helpers/Networking.cpp +++ b/src/src/Helpers/Networking.cpp @@ -1520,7 +1520,11 @@ int http_authenticate(const String& logIdentifier, const String& pass, const String& host, uint16_t port, + # if FEATURE_JSON_EVENT + String uri, + # else // if FEATURE_JSON_EVENT const String& uri, + # endif // if FEATURE_JSON_EVENT const String& HttpMethod, const String& header, const String& postStr, @@ -1542,6 +1546,16 @@ int http_authenticate(const String& logIdentifier, postStr, must_check_reply); } + +# if RESPONSE_PARSER_SUPPORT + int parseJson = -1; +# if FEATURE_JSON_EVENT + UriParseResult uriResult = parseUriPath(uri); + parseJson = uriResult.parseJson; + uri = uriResult.cleanedPath; +# endif // if FEATURE_JSON_EVENT +# endif // if RESPONSE_PARSER_SUPPORT + int httpCode = 0; const bool hasCredentials = !user.isEmpty() && !pass.isEmpty(); @@ -1666,7 +1680,7 @@ int http_authenticate(const String& logIdentifier, // ----This way to the custom response parser---------------- #if RESPONSE_PARSER_SUPPORT - eventFromResponse(host, httpCode, uri, http); + eventFromResponse(host, httpCode, uri, http, parseJson); #endif } // ----------------------------------------------------------- diff --git a/src/src/Helpers/Networking.h b/src/src/Helpers/Networking.h index ad8e39154..b46da2d73 100644 --- a/src/src/Helpers/Networking.h +++ b/src/src/Helpers/Networking.h @@ -230,7 +230,11 @@ int http_authenticate(const String& logIdentifier, const String& pass, const String& host, uint16_t port, + # if FEATURE_JSON_EVENT + String uri, + # else // if FEATURE_JSON_EVENT const String& uri, + # endif // if FEATURE_JSON_EVENT const String& HttpMethod, const String& header, const String& postStr,