24 if (httpd_req_get_hdr_value_len(req,
"Origin") == 0) {
28 char origin[128] = {0};
30 if (httpd_req_get_hdr_value_str(req,
"Origin", origin,
sizeof(origin)) !=
32 httpd_req_get_hdr_value_str(req,
"Host", host,
sizeof(host)) != ESP_OK) {
33 httpd_resp_send_err(req, HTTPD_403_FORBIDDEN,
34 "Cross-origin request rejected");
38 String origin_str(origin);
39 int scheme_end = origin_str.indexOf(
"://");
40 String origin_authority =
41 scheme_end < 0 ? origin_str : origin_str.substring(scheme_end + 3);
43 if (origin_authority == host) {
47 httpd_resp_send_err(req, HTTPD_403_FORBIDDEN,
48 "Cross-origin request rejected");
81 auto info_handler = std::make_shared<HTTPRequestHandler>(
82 1 << HTTP_GET,
"/api/info", [](httpd_req_t* req) {
85 JsonDocument json_doc;
86 JsonArray info_items = json_doc.to<JsonArray>();
88 for (
auto info_item = status_page_items->begin();
89 info_item != status_page_items->end(); ++info_item) {
90 info_items.add(info_item->second->as_json());
97#if defined(CONFIG_FREERTOS_USE_TRACE_FACILITY) && \
98 CONFIG_FREERTOS_USE_TRACE_FACILITY
102 UBaseType_t task_slots = uxTaskGetNumberOfTasks() + 4;
103 std::unique_ptr<TaskStatus_t[]> tasks(
104 new (std::nothrow) TaskStatus_t[task_slots]);
106 UBaseType_t n = uxTaskGetSystemState(tasks.get(), task_slots,
nullptr);
107 for (UBaseType_t i = 0; i < n; i++) {
109 item[
"name"] = tasks[i].pcTaskName;
110 item[
"value"] =
static_cast<uint32_t
>(
111 tasks[i].usStackHighWaterMark *
sizeof(StackType_t));
112 item[
"group"] =
"Task stack free (bytes)";
114 info_items.add(item);
120 serializeJson(json_doc, response);
121 httpd_resp_set_type(req,
"application/json");
122 httpd_resp_sendstr(req, response.c_str());
125 server->add_handler(info_handler);
129 auto log_handler = std::make_shared<HTTPRequestHandler>(
130 1 << HTTP_GET,
"/api/log", [](httpd_req_t* req) {
132 httpd_resp_set_type(req,
"application/json; charset=utf-8");
133 if (log_buffer ==
nullptr) {
135 req,
"{\"session\":0,\"next\":0,\"gap\":false,\"lines\":[]}");
145 bool has_since =
false;
146 size_t query_len = httpd_req_get_url_query_len(req) + 1;
147 if (query_len > 1 && query_len <= 64) {
149 if (httpd_req_get_url_query_str(req, query,
sizeof(query)) == ESP_OK) {
151 if (httpd_query_key_value(query,
"since", value,
sizeof(value)) ==
157 unsigned long parsed = strtoul(value, &end, 10);
158 if (end != value && *end ==
'\0' && errno == 0 &&
159 parsed <= UINT32_MAX) {
160 since =
static_cast<uint32_t
>(parsed);
169 JsonDocument json_doc;
171 json_doc[
"next"] = snapshot.
next;
172 json_doc[
"gap"] = snapshot.
gap;
173 JsonArray lines = json_doc[
"lines"].to<JsonArray>();
174 for (
const auto& line : snapshot.
lines) {
175 lines.add(line.c_str());
179 serializeJson(json_doc, response);
180 httpd_resp_sendstr(req, response.c_str());
183 server->add_handler(log_handler);
187 std::vector<RouteDefinition> routes;
193 routes.push_back(
RouteDefinition(
"Signal K",
"/signalk",
"SignalKPage"));
195 RouteDefinition(
"Configuration",
"/configuration",
"ConfigurationPage"));
196 routes.push_back(
RouteDefinition(
"Control",
"/control",
"ControlPage"));
199 JsonDocument json_doc;
200 JsonArray routes_json = json_doc.to<JsonArray>();
202 for (
auto it = routes.begin(); it != routes.end(); ++it) {
203 routes_json.add(it->as_json());
208 serializeJson(routes_json, response);
210 auto routes_handler = std::make_shared<HTTPRequestHandler>(
211 1 << HTTP_GET,
"/api/routes", [response](httpd_req_t* req) {
212 httpd_resp_set_type(req,
"application/json");
213 httpd_resp_sendstr(req, response.c_str());
216 server->add_handler(routes_handler);
227 if (root_page ==
nullptr) {
228 ESP_LOGE(__FILENAME__,
"Root page not found in kWebUIFiles");
234 for (
auto it = routes.begin(); it != routes.end(); ++it) {
235 String path = it->get_path();
236 auto route_handler = std::make_shared<HTTPRequestHandler>(
237 1 << HTTP_GET, path.c_str(), [root_page](httpd_req_t* req) {
238 httpd_resp_set_type(req, root_page->content_type);
239 if (root_page->content_encoding != nullptr) {
240 httpd_resp_set_hdr(req, kContentEncoding,
241 root_page->content_encoding);
246 server->add_handler(route_handler);
static const std::map< String, StatusPageItemBase * > * get_status_page_items()