Skip to content

Commit

Permalink
feat: add compatibility with esp_web_server.h
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanzyTHEbar committed Jun 22, 2023
1 parent 1b0dc24 commit ade38aa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
35 changes: 18 additions & 17 deletions NetworkManager/include/api/base/base_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
# include <data/webpage.h>
#endif

#define WEBSERVER_H
//#define WEBSERVER_H

constexpr int HTTP_GET = 0b00000001;
constexpr int HTTP_POST = 0b00000010;
constexpr int HTTP_DELETE = 0b00000100;
constexpr int HTTP_PUT = 0b00001000;
constexpr int HTTP_PATCH = 0b00010000;
constexpr int HTTP_HEAD = 0b00100000;
constexpr int HTTP_OPTIONS = 0b01000000;
constexpr int HTTP_ANY = 0b01111111;
constexpr int XHTTP_GET = 0b00000001;
constexpr int XHTTP_POST = 0b00000010;
constexpr int XHTTP_DELETE = 0b00000100;
constexpr int XHTTP_PUT = 0b00001000;
constexpr int XHTTP_PATCH = 0b00010000;
constexpr int XHTTP_HEAD = 0b00100000;
constexpr int XHTTP_OPTIONS = 0b01000000;
constexpr int XHTTP_ANY = 0b01111111;

#include <Update.h>
#include <esp_int_wdt.h>
Expand Down Expand Up @@ -81,24 +81,25 @@ class BaseAPI : public API_Utilities {
public:
std::unordered_map<std::string, WebRequestMethodComposite>
_networkMethodsMap_inv = {
{"GET", HTTP_GET}, {"POST", HTTP_POST},
{"PUT", HTTP_PUT}, {"DELETE", HTTP_DELETE},
{"PATCH", HTTP_PATCH}, {"OPTIONS", HTTP_OPTIONS},
{"GET", XHTTP_GET}, {"POST", XHTTP_POST},
{"PUT", XHTTP_PUT}, {"DELETE", XHTTP_DELETE},
{"PATCH", XHTTP_PATCH}, {"OPTIONS", XHTTP_OPTIONS},
};

std::unordered_map<WebRequestMethodComposite, std::string>
_networkMethodsMap = {
{HTTP_GET, "GET"}, {HTTP_POST, "POST"},
{HTTP_PUT, "PUT"}, {HTTP_DELETE, "DELETE"},
{HTTP_PATCH, "PATCH"}, {HTTP_OPTIONS, "OPTIONS"},
{XHTTP_GET, "GET"}, {XHTTP_POST, "POST"},
{XHTTP_PUT, "PUT"}, {XHTTP_DELETE, "DELETE"},
{XHTTP_PATCH, "PATCH"}, {XHTTP_OPTIONS, "OPTIONS"},
};

enum RequestMethods { GET, POST, PUT, DELETE, PATCH, OPTIONS };

std::unordered_map<WebRequestMethodComposite, RequestMethods>
_networkMethodsMap_enum = {
{HTTP_GET, GET}, {HTTP_POST, POST}, {HTTP_PUT, PUT},
{HTTP_DELETE, DELETE}, {HTTP_PATCH, PATCH}, {HTTP_OPTIONS, OPTIONS},
{XHTTP_GET, GET}, {XHTTP_POST, POST},
{XHTTP_PUT, PUT}, {XHTTP_DELETE, DELETE},
{XHTTP_PATCH, PATCH}, {XHTTP_OPTIONS, OPTIONS},
};

public:
Expand Down
8 changes: 4 additions & 4 deletions NetworkManager/src/api/base/base_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ BaseAPI::~BaseAPI() {}
void BaseAPI::begin() {
//! i have changed this to use lambdas instead of std::bind to avoid the
//! overhead. Lambdas are always more preferable.
server.on("/", HTTP_GET,
server.on("/", XHTTP_GET,
[&](AsyncWebServerRequest* request) { request->send(200); });

#ifdef USE_WEBMANAGER
server.on(
wifimanager_url.c_str(), HTTP_GET, [&](AsyncWebServerRequest* request) {
wifimanager_url.c_str(), XHTTP_GET, [&](AsyncWebServerRequest* request) {
// TODO: add authentication support
/* if (_authRequired) {
if (!request->authenticate(_username.c_str(),
Expand Down Expand Up @@ -65,7 +65,7 @@ void BaseAPI::begin() {
}

// preflight cors check
server.on("/", HTTP_OPTIONS, [&](AsyncWebServerRequest* request) {
server.on("/", XHTTP_OPTIONS, [&](AsyncWebServerRequest* request) {
AsyncWebServerResponse* response = request->beginResponse(204);
response->addHeader("Access-Control-Allow-Methods",
"PUT,POST,GET,OPTIONS");
Expand Down Expand Up @@ -389,7 +389,7 @@ void BaseAPI::beginOTA() {
const char* password = device_config.ota_password.c_str();
log_d("[DEBUG] Free Heap: %d", ESP.getFreeHeap());

// Note: HTTP_GET
// Note: XHTTP_GET
server.on(
"/update/identity", 0b00000001, [&](AsyncWebServerRequest* request) {
checkAuthentication(request, login, password);
Expand Down
2 changes: 1 addition & 1 deletion NetworkManager/src/api/rest_api_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void APIServer::begin() {
"^\\%s\\/([a-zA-Z0-9]+)\\/command\\/([a-zA-Z0-9]+)$",
this->api_url.c_str());
log_d("API URL: %s", buffer);
server.on(buffer, HTTP_ANY,
server.on(buffer, XHTTP_ANY,
[&](AsyncWebServerRequest* request) { handleRequest(request); });

char buf[1000];
Expand Down
4 changes: 2 additions & 2 deletions NetworkManager/wokwi.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[wokwi]
version = 1
elf = ".pio/build/esp32dev_debug/EasyNetworkManager-v4.1.1-esp32dev_debug-e87210d-main.elf"
firmware = ".pio/build/esp32dev_debug/EasyNetworkManager-v4.1.1-esp32dev_debug-e87210d-main.bin"
elf = ".pio/build/esp32dev_debug/EasyNetworkManager-v4.1.1-esp32dev_debug-fe8a2bd-main.elf"
firmware = ".pio/build/esp32dev_debug/EasyNetworkManager-v4.1.1-esp32dev_debug-fe8a2bd-main.bin"
[[net.forward]]
from = "localhost:8180"
to = "target:80"

0 comments on commit ade38aa

Please sign in to comment.