11 #include <ESPAsyncWebServer.h> 18 namespace web_server_base {
26 bool canHandle(AsyncWebServerRequest *request)
override {
return next_->canHandle(request); }
28 void handleUpload(AsyncWebServerRequest *request,
const String &filename,
size_t index, uint8_t *data,
size_t len,
29 bool final)
override {
30 next_->handleUpload(request, filename, index, data, len,
final);
32 void handleBody(AsyncWebServerRequest *request, uint8_t *data,
size_t len,
size_t index,
size_t total)
override {
33 next_->handleBody(request, data, len, index, total);
52 bool success = request->authenticate(credentials_->username.c_str(), credentials_->password.c_str());
54 request->requestAuthentication();
60 if (!check_auth(request))
64 void handleUpload(AsyncWebServerRequest *request,
const String &filename,
size_t index, uint8_t *data,
size_t len,
65 bool final)
override {
66 if (!check_auth(request))
70 void handleBody(AsyncWebServerRequest *request, uint8_t *data,
size_t len,
size_t index,
size_t total)
override {
71 if (!check_auth(request))
85 if (this->initialized_) {
89 this->server_ = std::make_shared<AsyncWebServer>(this->port_);
91 DefaultHeaders::Instance().addHeader(
"Access-Control-Allow-Origin",
"*");
92 this->server_->begin();
94 for (
auto *handler : this->handlers_)
95 this->server_->addHandler(handler);
100 this->initialized_--;
101 if (this->initialized_ == 0) {
102 this->server_ =
nullptr;
105 std::shared_ptr<AsyncWebServer>
get_server()
const {
return server_; }
106 float get_setup_priority()
const override;
108 void set_auth_username(std::string auth_username) { credentials_.username = std::move(auth_username); }
109 void set_auth_password(std::string auth_password) { credentials_.password = std::move(auth_password); }
111 void add_handler(AsyncWebHandler *handler);
113 void add_ota_handler();
123 std::shared_ptr<AsyncWebServer> server_{
nullptr};
132 void handleUpload(AsyncWebServerRequest *request,
const String &filename,
size_t index, uint8_t *data,
size_t len,
133 bool final)
override;
134 bool canHandle(AsyncWebServerRequest *request)
override {
135 return request->url() ==
"/update" && request->method() == HTTP_POST;
142 uint32_t last_ota_progress_{0};
143 uint32_t ota_read_length_{0};
MiddlewareHandler(AsyncWebHandler *next)
bool check_auth(AsyncWebServerRequest *request)
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override
AuthMiddlewareHandler(AsyncWebHandler *next, Credentials *credentials)
bool canHandle(AsyncWebServerRequest *request) override
Credentials * credentials_
void set_auth_username(std::string auth_username)
uint16_t get_port() const
void set_port(uint16_t port)
bool canHandle(AsyncWebServerRequest *request) override
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override
internal::Credentials credentials_
void handleRequest(AsyncWebServerRequest *request) override
void handleRequest(AsyncWebServerRequest *request) override
bool isRequestHandlerTrivial() override
std::vector< AsyncWebHandler * > handlers_
void set_auth_password(std::string auth_password)
Implementation of SPI Controller mode.
std::shared_ptr< AsyncWebServer > get_server() const
OTARequestHandler(WebServerBase *parent)
void handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) override
bool isRequestHandlerTrivial() override
void handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) override