1#ifndef SENSESP_NET_HTTP_SERVER_H_
2#define SENSESP_NET_HTTP_SERVER_H_
7#include <esp_http_server.h>
17#ifndef HTTP_SERVER_STACK_SIZE
18#define HTTP_SERVER_STACK_SIZE 8192
23#ifndef HTTP_DEFAULT_PORT
24#define HTTP_DEFAULT_PORT 80
43 std::function<esp_err_t(httpd_req_t*)> handler_func)
64 const String& config_path =
"/system/httpserver")
69 config_.uri_match_fn = httpd_uri_match_wildcard;
78 if (error != ESP_OK) {
79 ESP_LOGE(__FILENAME__,
"Error starting HTTP server: %s",
80 esp_err_to_name(error));
82 ESP_LOGI(__FILENAME__,
"HTTP server started");
91 .is_websocket =
false,
93 httpd_register_uri_handler(
server_, &uri);
94 uri.method = HTTP_HEAD;
95 httpd_register_uri_handler(
server_, &uri);
96 uri.method = HTTP_POST;
97 httpd_register_uri_handler(
server_, &uri);
98 uri.method = HTTP_PUT;
99 httpd_register_uri_handler(
server_, &uri);
100 uri.method = HTTP_DELETE;
101 httpd_register_uri_handler(
server_, &uri);
104 MDNS.addService(
"http",
"tcp", 80);
111 bool auth_required =
true) {
121 virtual bool to_json(JsonObject& config)
override {
129 virtual bool from_json(
const JsonObject& config)
override {
130 if (config[
"auth_required"].is<bool>()) {
133 if (config[
"username"].is<String>()) {
134 username_ = config[
"username"].as<String>();
136 if (config[
"password"].is<String>()) {
137 password_ = config[
"password"].as<String>();
156 std::function<esp_err_t(httpd_req_t*)> handler,
177 std::list<std::shared_ptr<HTTPRequestHandler>>
handlers_;
virtual bool load() override
Load and populate the object from a persistent storage.
HTTP Authenticator base class.
HTTP Digest Authenticator class.
HTTP request handler storage class.
const uint32_t method_mask_
HTTPRequestHandler(uint32_t method_mask, String match_uri, std::function< esp_err_t(httpd_req_t *)> handler_func)
const std::function< esp_err_t(httpd_req_t *)> handler_func_
esp_err_t call(httpd_req_t *req)
HTTP server class wrapping the esp-idf http server.
esp_err_t dispatch_request(httpd_req_t *req)
Dispatcher method that captures all requests and forwards them to the appropriate handlers.
virtual bool to_json(JsonObject &config) override
void set_captive_portal(bool captive_portal)
std::list< std::shared_ptr< HTTPRequestHandler > > handlers_
void set_auth_credentials(const String &username, const String &password, bool auth_required=true)
bool authenticate_request(HTTPAuthenticator *auth, std::function< esp_err_t(httpd_req_t *)> handler, httpd_req_t *req)
friend esp_err_t call_request_dispatcher(httpd_req_t *req)
virtual bool from_json(const JsonObject &config) override
void add_handler(std::shared_ptr< HTTPRequestHandler > &handler)
HTTPServer(int port=HTTP_DEFAULT_PORT, const String &config_path="/system/httpserver")
std::unique_ptr< HTTPAuthenticator > authenticator_
bool handle_captive_portal(httpd_req_t *req)
Check if the request is for the captive portal and handle it if it.
static String get_hostname()
Get the current hostname.
#define HTTP_DEFAULT_PORT
#define HTTP_SERVER_STACK_SIZE
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
esp_err_t call_request_dispatcher(httpd_req_t *req)
void urldecode2(char *dst, const char *src)
bool ConfigRequiresRestart(const HTTPServer &obj)
String get_content_type(httpd_req_t *req)