1#ifndef SENSESP_NET_HTTP_SERVER_H_
2#define SENSESP_NET_HTTP_SERVER_H_
8#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;
80 if (error != ESP_OK) {
81 ESP_LOGE(__FILENAME__,
"Error starting HTTP server: %s",
82 esp_err_to_name(error));
84 ESP_LOGI(__FILENAME__,
"HTTP server started");
94 httpd_register_uri_handler(
server_, &uri);
95 uri.method = HTTP_HEAD;
96 httpd_register_uri_handler(
server_, &uri);
97 uri.method = HTTP_POST;
98 httpd_register_uri_handler(
server_, &uri);
99 uri.method = HTTP_PUT;
100 httpd_register_uri_handler(
server_, &uri);
101 uri.method = HTTP_DELETE;
102 httpd_register_uri_handler(
server_, &uri);
105 MDNS.addService(
"http",
"tcp", 80);
112 bool auth_required =
true) {
115 ha1_ =
MD5(username +
":" + realm +
":" + password);
128 IPAddress ap_ip = IPAddress()) {
133 virtual bool to_json(JsonObject& config)
override {
136 config[
"ha1"] =
ha1_;
141 virtual bool from_json(
const JsonObject& config)
override {
142 if (config[
"auth_required"].is<bool>()) {
145 if (config[
"username"].is<String>()) {
146 username_ = config[
"username"].as<String>();
148 if (config[
"ha1"].is<String>()) {
149 ha1_ = config[
"ha1"].as<String>();
150 }
else if (config[
"password"].is<String>()) {
152 String password = config[
"password"].as<String>();
175 std::function<esp_err_t(httpd_req_t*)> handler,
196 std::list<std::shared_ptr<HTTPRequestHandler>>
handlers_;
virtual bool load() override
Load and populate the object from a persistent storage.
virtual bool save() override
Save the object to a persistent storage.
FileSystemSaveable(const String &config_path)
HTTP Authenticator base class.
HTTP Digest Authenticator 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.
String ha1_
Pre-computed MD5(username:realm:password).
virtual bool to_json(JsonObject &config) override
std::list< std::shared_ptr< HTTPRequestHandler > > handlers_
void set_auth_credentials(const String &username, const String &password, bool auth_required=true)
IPAddress captive_portal_ap_ip_
bool authenticate_request(HTTPAuthenticator *auth, std::function< esp_err_t(httpd_req_t *)> handler, httpd_req_t *req)
void set_captive_portal(bool captive_portal, IPAddress ap_ip=IPAddress())
Enable or disable the captive-portal redirect path.
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()
String MD5(const String &payload_str)
MD5 hash function.
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)
Construct with a pre-computed HA1 hash (avoids storing plaintext password).