ESPHome  2022.6.3
web_server_base.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ARDUINO
4 
5 #include <memory>
6 #include <utility>
8 
9 #include <ESPAsyncWebServer.h>
10 
11 namespace esphome {
12 namespace web_server_base {
13 
14 namespace internal {
15 
16 class MiddlewareHandler : public AsyncWebHandler {
17  public:
18  MiddlewareHandler(AsyncWebHandler *next) : next_(next) {}
19 
20  bool canHandle(AsyncWebServerRequest *request) override { return next_->canHandle(request); }
21  void handleRequest(AsyncWebServerRequest *request) override { next_->handleRequest(request); }
22  void handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len,
23  bool final) override {
24  next_->handleUpload(request, filename, index, data, len, final);
25  }
26  void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override {
27  next_->handleBody(request, data, len, index, total);
28  }
29  bool isRequestHandlerTrivial() override { return next_->isRequestHandlerTrivial(); }
30 
31  protected:
32  AsyncWebHandler *next_;
33 };
34 
35 struct Credentials {
36  std::string username;
37  std::string password;
38 };
39 
41  public:
42  AuthMiddlewareHandler(AsyncWebHandler *next, Credentials *credentials)
43  : MiddlewareHandler(next), credentials_(credentials) {}
44 
45  bool check_auth(AsyncWebServerRequest *request) {
46  bool success = request->authenticate(credentials_->username.c_str(), credentials_->password.c_str());
47  if (!success) {
48  request->requestAuthentication();
49  }
50  return success;
51  }
52 
53  void handleRequest(AsyncWebServerRequest *request) override {
54  if (!check_auth(request))
55  return;
57  }
58  void handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len,
59  bool final) override {
60  if (!check_auth(request))
61  return;
62  MiddlewareHandler::handleUpload(request, filename, index, data, len, final);
63  }
64  void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override {
65  if (!check_auth(request))
66  return;
67  MiddlewareHandler::handleBody(request, data, len, index, total);
68  }
69 
70  protected:
72 };
73 
74 } // namespace internal
75 
76 class WebServerBase : public Component {
77  public:
78  void init() {
79  if (this->initialized_) {
80  this->initialized_++;
81  return;
82  }
83  this->server_ = std::make_shared<AsyncWebServer>(this->port_);
84  this->server_->begin();
85 
86  for (auto *handler : this->handlers_)
87  this->server_->addHandler(handler);
88 
89  this->initialized_++;
90  }
91  void deinit() {
92  this->initialized_--;
93  if (this->initialized_ == 0) {
94  this->server_ = nullptr;
95  }
96  }
97  std::shared_ptr<AsyncWebServer> get_server() const { return server_; }
98  float get_setup_priority() const override;
99 
100  void set_auth_username(std::string auth_username) { credentials_.username = std::move(auth_username); }
101  void set_auth_password(std::string auth_password) { credentials_.password = std::move(auth_password); }
102 
103  void add_handler(AsyncWebHandler *handler);
104 
105  void add_ota_handler();
106 
107  void set_port(uint16_t port) { port_ = port; }
108  uint16_t get_port() const { return port_; }
109 
110  protected:
111  friend class OTARequestHandler;
112 
113  int initialized_{0};
114  uint16_t port_{80};
115  std::shared_ptr<AsyncWebServer> server_{nullptr};
116  std::vector<AsyncWebHandler *> handlers_;
118 };
119 
120 class OTARequestHandler : public AsyncWebHandler {
121  public:
122  OTARequestHandler(WebServerBase *parent) : parent_(parent) {}
123  void handleRequest(AsyncWebServerRequest *request) override;
124  void handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len,
125  bool final) override;
126  bool canHandle(AsyncWebServerRequest *request) override {
127  return request->url() == "/update" && request->method() == HTTP_POST;
128  }
129 
130  bool isRequestHandlerTrivial() override { return false; }
131 
132  protected:
133  uint32_t last_ota_progress_{0};
134  uint32_t ota_read_length_{0};
136 };
137 
138 } // namespace web_server_base
139 } // namespace esphome
140 
141 #endif // USE_ARDUINO
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
void set_auth_username(std::string auth_username)
bool canHandle(AsyncWebServerRequest *request) override
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override
void handleRequest(AsyncWebServerRequest *request) override
void handleRequest(AsyncWebServerRequest *request) override
std::string size_t len
Definition: helpers.h:278
std::vector< AsyncWebHandler * > handlers_
void set_auth_password(std::string auth_password)
Definition: a4988.cpp:4
std::shared_ptr< AsyncWebServer > get_server() const
void handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) override
void handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) override