mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-30 10:13:04 +05:00
fix: accept form posts for manual sensor values
Accept form submissions alongside the existing JSON sensor value endpoint for compatibility with the manual sensor use case discussed in PR #270. Support named value and unnamed body parameters with strict numeric parsing and bounded input lengths. Preserve authentication, unit conversion, MQTT publication and noresponse handling. Share payload trimming and numeric parsing between HTTP and MQTT instead of maintaining separate parser implementations. Refs #270
This commit is contained in:
1 parent
cc6131eac9
commit
8fba88636e
3 files changed
+148
-54
No files matched your search
@@ -6,10 +6,8 @@
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <bitset>
|
||||
#include <cerrno>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <list>
|
||||
@@ -308,46 +306,6 @@ static void publishState() {
|
||||
publishMasterState();
|
||||
}
|
||||
|
||||
static std::string_view trimPayload(std::string_view payload) {
|
||||
const auto first = payload.find_first_not_of(" \t\r\n");
|
||||
if (first == std::string_view::npos) {
|
||||
return {};
|
||||
}
|
||||
return payload.substr(first, payload.find_last_not_of(" \t\r\n") - first + 1);
|
||||
}
|
||||
|
||||
static std::optional<float> parseNumber(std::string_view payload, uint8_t bufferSize = 17) {
|
||||
if (payload.empty() || bufferSize < 2) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Long input is intentionally truncated to leave room for the terminating NUL.
|
||||
payload = payload.substr(0, bufferSize - 1);
|
||||
|
||||
// strtof also accepts leading whitespace, '+' and hexadecimal numbers.
|
||||
const char first = payload.front();
|
||||
if ((first < '0' || first > '9') && first != '-' && first != '.') {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (payload.find_first_of("xX") != std::string_view::npos) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
char buffer[bufferSize];
|
||||
memcpy(buffer, payload.data(), payload.size());
|
||||
buffer[payload.size()] = '\0';
|
||||
|
||||
char* end;
|
||||
errno = 0;
|
||||
const float parsed = std::strtof(buffer, &end);
|
||||
if (end != buffer + payload.size() || !std::isfinite(parsed) || (errno == ERANGE && parsed == 0.0f)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
static bool isCommandValid(const std::string& topic, std::string_view payload) {
|
||||
if (payload.empty()) {
|
||||
return false;
|
||||
|
||||
Reference in new issue
Block a user