Compare commits

4 Commits

4 changed files with 16 additions and 21 deletions

View File

@ -15,8 +15,6 @@ set(dependencies
cpputils
espchrono
espcpputils
expected
fmt
)
idf_component_register(
@ -29,6 +27,8 @@ idf_component_register(
${dependencies}
)
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 23)
target_compile_options(${COMPONENT_TARGET}
PRIVATE
-fstack-reuse=all

View File

@ -2,14 +2,12 @@
// system includes
#include <utility>
#include <format>
// esp-idf includes
#include <esp_log.h>
#include <http_parser.h>
// 3rdparty lib includes
#include <fmt/core.h>
// 3rdparty lib includes
#include "espcppmacros.h"
@ -54,15 +52,15 @@ void urldecode(char *dst, const char *src)
*dst++ = '\0';
}
tl::expected<void, std::string> urlverify(std::string_view str)
std::expected<void, std::string> urlverify(std::string_view str)
{
if (str.empty())
return tl::make_unexpected("empty url is not valid");
return std::unexpected("empty url is not valid");
http_parser_url puri;
http_parser_url_init(&puri);
if (const auto result = http_parser_parse_url(str.data(), str.size(), 0, &puri); result != 0)
return tl::make_unexpected(fmt::format("http_parser_parse_url() failed with {}", result));
return std::unexpected(std::format("http_parser_parse_url() failed with {}", result));
return {};
}
@ -83,7 +81,7 @@ esp_err_t webserver_resp_send(httpd_req_t *req, ResponseStatus error, const char
return ESP_OK;
}
tl::expected<std::string, std::string> webserver_get_query(httpd_req_t *req)
std::expected<std::string, std::string> webserver_get_query(httpd_req_t *req)
{
std::string query;
@ -91,7 +89,7 @@ tl::expected<std::string, std::string> webserver_get_query(httpd_req_t *req)
{
query.resize(queryLength);
if (const auto result = httpd_req_get_url_query_str(req, query.data(), query.size() + 1); result != ESP_OK)
return tl::make_unexpected(fmt::format("httpd_req_get_url_query_str() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("httpd_req_get_url_query_str() failed with {}", esp_err_to_name(result)));
}
return query;
@ -107,10 +105,9 @@ std::string toString(httpd_ws_type_t val)
case HTTPD_WS_TYPE_CLOSE: return "HTTPD_WS_TYPE_CLOSE"; break;
case HTTPD_WS_TYPE_PING: return "HTTPD_WS_TYPE_PING"; break;
case HTTPD_WS_TYPE_PONG: return "HTTPD_WS_TYPE_PONG"; break;
default:
ESP_LOGW(TAG, "Unknown httpd_ws_type_t(%i)", std::to_underlying(val));
return fmt::format("Unknown httpd_ws_type_t({})", std::to_underlying(val));
}
ESP_LOGW(TAG, "Unknown httpd_ws_type_t(%i)", std::to_underlying(val));
return std::format("Unknown httpd_ws_type_t({})", std::to_underlying(val));
}
} // namespace esphttpdutils

View File

@ -3,13 +3,11 @@
// system includes
#include <string>
#include <string_view>
#include <expected>
// esp-idf includes
#include <esp_http_server.h>
// 3rdparty lib includes
#include <tl/expected.hpp>
// local includes
#include "esphttpstatuscodes.h"
@ -20,13 +18,13 @@ template<typename T> T htmlentities(T &&val) { return val; } // TODO
void urldecode(char *dst, const char *src);
tl::expected<void, std::string> urlverify(std::string_view str);
std::expected<void, std::string> urlverify(std::string_view str);
esp_err_t webserver_prepare_response(httpd_req_t *req);
esp_err_t webserver_resp_send(httpd_req_t *req, ResponseStatus error, const char *type, std::string_view body);
tl::expected<std::string, std::string> webserver_get_query(httpd_req_t *req);
std::expected<std::string, std::string> webserver_get_query(httpd_req_t *req);
std::string toString(httpd_ws_type_t val);

View File

@ -67,9 +67,6 @@ const char *toString(ResponseStatus status)
case ResponseStatus::TooManyRequests: return "429 Too Many Requests";
case ResponseStatus::RequestHeaderFieldsTooLarge: return "431 Request Header Fields Too Large";
case ResponseStatus::UnavailableForLegalReasons: return "451 Unavailable For Legal Reasons";
default:
ESP_LOGW(TAG, "unknown httpd_err_code_t(%i)", std::to_underlying(status));
[[fallthrough]];
case ResponseStatus::InternalServerError: return "500 Internal Server Error";
case ResponseStatus::NotImplemented: return "501 Not Implemented";
case ResponseStatus::BadGateway: return "502 Bad Gateway";
@ -82,6 +79,9 @@ const char *toString(ResponseStatus status)
case ResponseStatus::NotExtended: return "510 Not Extended";
case ResponseStatus::NetworkAuthenticationRequired: return "511 Network Authentication Required";
}
ESP_LOGW(TAG, "unknown httpd_err_code_t(%i)", std::to_underlying(status));
return "500 Internal Server Error";
}
} // namespace esphttpdutils