HttpResponseParser fix:prevent race condition

This commit is contained in:
chromoxdor
2025-06-26 12:16:41 +02:00
parent 50cec921da
commit 3c9c2036ab
5 changed files with 44 additions and 23 deletions
-6
View File
@@ -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,
+13 -13
View File
@@ -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
+12 -3
View File
@@ -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
+15 -1
View File
@@ -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
}
// -----------------------------------------------------------
+4
View File
@@ -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,