77 lines
2.4 KiB
C++
77 lines
2.4 KiB
C++
#include "app.h"
|
|
|
|
static HTTPMethod httpMethod(const char *method) {
|
|
if (strcmp(method, "GET") == 0) return HTTP_GET;
|
|
if (strcmp(method, "POST") == 0) return HTTP_POST;
|
|
if (strcmp(method, "DELETE") == 0) return HTTP_DELETE;
|
|
if (strcmp(method, "PUT") == 0) return HTTP_PUT;
|
|
if (strcmp(method, "PATCH") == 0) return HTTP_PATCH;
|
|
return HTTP_ANY;
|
|
}
|
|
|
|
static void registerApiDefs(ApiDef *defs, size_t count) {
|
|
for (size_t i = 0; i < count; i++) {
|
|
ApiDef &api = defs[i];
|
|
if (api.uploadHandler) {
|
|
server.on(api.path, httpMethod(api.method), api.handler, api.uploadHandler);
|
|
} else {
|
|
server.on(api.path, httpMethod(api.method), api.handler);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void registerApiRoutes() {
|
|
registerApiDefs(apiDefs, API_DEF_COUNT);
|
|
registerApiDefs(customApiDefs, CUSTOM_API_DEF_COUNT);
|
|
}
|
|
|
|
static String requestMethodName() {
|
|
switch (server.method()) {
|
|
case HTTP_GET:
|
|
return "GET";
|
|
case HTTP_POST:
|
|
return "POST";
|
|
case HTTP_DELETE:
|
|
return "DELETE";
|
|
case HTTP_PUT:
|
|
return "PUT";
|
|
case HTTP_PATCH:
|
|
return "PATCH";
|
|
default:
|
|
return "UNKNOWN";
|
|
}
|
|
}
|
|
|
|
void registerRoutes() {
|
|
static const char *headers[] = {"Authorization", "X-Auth-Token"};
|
|
server.collectHeaders(headers, 2);
|
|
|
|
server.on("/", HTTP_GET, setupMode ? handleSetupPage : handleAdminPage);
|
|
server.on("/favicon.ico", HTTP_GET, handleFavicon);
|
|
server.on("/generate_204", HTTP_GET, handleCaptiveProbe);
|
|
server.on("/gen_204", HTTP_GET, handleCaptiveProbe);
|
|
server.on("/hotspot-detect.html", HTTP_GET, handleCaptiveProbe);
|
|
server.on("/library/test/success.html", HTTP_GET, handleCaptiveProbe);
|
|
server.on("/connecttest.txt", HTTP_GET, handleCaptiveProbe);
|
|
server.on("/ncsi.txt", HTTP_GET, handleCaptiveProbe);
|
|
server.on("/fwlink", HTTP_GET, handleCaptiveProbe);
|
|
|
|
registerApiRoutes();
|
|
|
|
server.onNotFound([]() {
|
|
if (setupMode) {
|
|
if (server.uri().startsWith("/api/")) {
|
|
appendLog(LOG_SECURITY_AUDIT, "undefined URL or method " + requestMethodName() + " " + server.uri());
|
|
sendJson(404, jsonError("Not found"));
|
|
} else if (!handleHtmlFileRequest()) {
|
|
redirectToSetupPage();
|
|
}
|
|
} else if (server.method() == HTTP_GET && !server.uri().startsWith("/api/")) {
|
|
if (!handleHtmlFileRequest()) handleAdminPage();
|
|
} else {
|
|
appendLog(LOG_SECURITY_AUDIT, "undefined URL or method " + requestMethodName() + " " + server.uri());
|
|
sendJson(404, jsonError("Not found"));
|
|
}
|
|
});
|
|
}
|