Loading [MathJax]/jax/output/HTML-CSS/config.js
SensESP 3.1.0
Universal Signal K sensor toolkit ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
http_server.h
Go to the documentation of this file.
1#ifndef SENSESP_NET_HTTP_SERVER_H_
2#define SENSESP_NET_HTTP_SERVER_H_
3
4#include "sensesp.h"
5
6#include <ESPmDNS.h>
7#include <esp_http_server.h>
8#include <functional>
9#include <list>
10#include <memory>
11
12#include "WiFi.h"
15#include "sensesp_base_app.h"
16
17#ifndef HTTP_SERVER_STACK_SIZE
18#define HTTP_SERVER_STACK_SIZE 8192
19#endif
20
21namespace sensesp {
22
23#ifndef HTTP_DEFAULT_PORT
24#define HTTP_DEFAULT_PORT 80
25#endif
26
27#include <ctype.h>
28#include <stdlib.h>
29
30void urldecode2(char* dst, const char* src);
31String get_content_type(httpd_req_t* req);
32esp_err_t call_request_dispatcher(httpd_req_t* req);
33
34class HTTPServer;
35
41 public:
42 HTTPRequestHandler(uint32_t method_mask, String match_uri,
43 std::function<esp_err_t(httpd_req_t*)> handler_func)
44 : method_mask_(method_mask),
45 match_uri_(match_uri),
46 handler_func_(handler_func) {}
47
48 const uint32_t method_mask_;
49 const String match_uri_;
50
51 esp_err_t call(httpd_req_t* req) { return this->handler_func_(req); }
52
53 protected:
54 const std::function<esp_err_t(httpd_req_t*)> handler_func_;
55};
56
62 public:
64 const String& config_path = "/system/httpserver")
65 : FileSystemSaveable(config_path), config_(HTTPD_DEFAULT_CONFIG()) {
66 config_.server_port = port;
68 config_.max_uri_handlers = 20;
69 config_.uri_match_fn = httpd_uri_match_wildcard;
70 String auth_realm_ = "Login required for " + SensESPBaseApp::get_hostname();
71 load();
72 if (auth_required_) {
73 authenticator_ = std::unique_ptr<HTTPDigestAuthenticator>(
75 }
76 event_loop()->onDelay(0, [this]() {
77 esp_err_t error = httpd_start(&server_, &config_);
78 if (error != ESP_OK) {
79 ESP_LOGE(__FILENAME__, "Error starting HTTP server: %s",
80 esp_err_to_name(error));
81 } else {
82 ESP_LOGI(__FILENAME__, "HTTP server started");
83 }
84
85 // register the request dispatcher for all methods and all URIs
86 httpd_uri_t uri = {
87 .uri = "/*",
88 .method = HTTP_GET,
89 .handler = call_request_dispatcher,
90 .user_ctx = this,
91 };
92 httpd_register_uri_handler(server_, &uri);
93 uri.method = HTTP_HEAD;
94 httpd_register_uri_handler(server_, &uri);
95 uri.method = HTTP_POST;
96 httpd_register_uri_handler(server_, &uri);
97 uri.method = HTTP_PUT;
98 httpd_register_uri_handler(server_, &uri);
99 uri.method = HTTP_DELETE;
100 httpd_register_uri_handler(server_, &uri);
101
102 // announce the server over mDNS
103 MDNS.addService("http", "tcp", 80);
104 });
105 };
106
107 void stop() { httpd_stop(server_); }
108
109 void set_auth_credentials(const String& username, const String& password,
110 bool auth_required = true) {
111 username_ = username;
112 password_ = password;
113 auth_required_ = auth_required;
114 }
115
116 void set_captive_portal(bool captive_portal) {
117 captive_portal_ = captive_portal;
118 }
119
120 virtual bool to_json(JsonObject& config) override {
121 config["auth_required"] = auth_required_;
122 config["username"] = username_;
123 config["password"] = password_;
124
125 return true;
126 }
127
128 virtual bool from_json(const JsonObject& config) override {
129 if (config["auth_required"].is<bool>()) {
130 auth_required_ = config["auth_required"];
131 }
132 if (config["username"].is<String>()) {
133 username_ = config["username"].as<String>();
134 }
135 if (config["password"].is<String>()) {
136 password_ = config["password"].as<String>();
137 }
138 return true;
139 }
140
141 void add_handler(std::shared_ptr<HTTPRequestHandler>& handler) {
142 handlers_.push_back(handler);
143 }
144
145 protected:
146 bool captive_portal_ = false;
147 httpd_handle_t server_ = nullptr;
148 httpd_config_t config_;
149 String username_;
150 String password_;
151 bool auth_required_ = false;
152 std::unique_ptr<HTTPAuthenticator> authenticator_;
153
155 std::function<esp_err_t(httpd_req_t*)> handler,
156 httpd_req_t* req);
157
165 esp_err_t dispatch_request(httpd_req_t* req);
166
174 bool handle_captive_portal(httpd_req_t* req);
175
176 std::list<std::shared_ptr<HTTPRequestHandler>> handlers_;
177
178 friend esp_err_t call_request_dispatcher(httpd_req_t* req);
179};
180
181inline const String ConfigSchema(const HTTPServer& obj) {
182 return "null";
183}
184
185inline bool ConfigRequiresRestart(const HTTPServer& obj) { return true; }
186
187} // namespace sensesp
188
189#endif // SENSESP_NET_HTTP_SERVER_H_
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
HTTP Authenticator base class.
HTTP Digest Authenticator class.
HTTP request handler storage class.
Definition http_server.h:40
const uint32_t method_mask_
Definition http_server.h:48
HTTPRequestHandler(uint32_t method_mask, String match_uri, std::function< esp_err_t(httpd_req_t *)> handler_func)
Definition http_server.h:42
const std::function< esp_err_t(httpd_req_t *)> handler_func_
Definition http_server.h:54
esp_err_t call(httpd_req_t *req)
Definition http_server.h:51
HTTP server class wrapping the esp-idf http server.
Definition http_server.h:61
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_
httpd_handle_t server_
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)
httpd_config_t config_
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")
Definition http_server.h:63
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
Definition http_server.h:24
#define HTTP_SERVER_STACK_SIZE
Definition http_server.h:18
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
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)