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:
Yurii committed 2026-11-29 05:43:51 +03:00
1 parent cc6131eac9
commit 8fba88636e
3 files changed
+148 -54

No files matched your search

+56
View File
@@ -5,11 +5,67 @@
#include <string.h>
#include <time.h>
#include <cerrno>
#include <cmath>
#include <cstdlib>
#include <optional>
#include <string>
#include <string_view>
#include "types.h"
inline 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);
}
// text must be NUL-terminated; length may exclude trailing whitespace.
inline std::optional<float> parseNumber(const char* text, size_t length) {
if (text == nullptr || length == 0) {
return std::nullopt;
}
const auto payload = std::string_view(text, length);
const char first = payload.front();
// strtof also accepts leading whitespace, '+' and hexadecimal numbers.
if ((first < '0' || first > '9') && first != '-' && first != '.') {
return std::nullopt;
}
if (payload.find_first_of("xX") != std::string_view::npos) {
return std::nullopt;
}
char* end;
errno = 0;
const float parsed = std::strtof(text, &end);
if (end != text + length || !std::isfinite(parsed) || (errno == ERANGE && parsed == 0.0f)) {
return std::nullopt;
}
return parsed;
}
inline 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);
char buffer[bufferSize];
memcpy(buffer, payload.data(), payload.size());
buffer[payload.size()] = '\0';
return parseNumber(buffer, payload.size());
}
inline std::string getChipId(const char* prefix = nullptr, const char* suffix = nullptr) {
std::string chipId;
chipId.reserve(