4 #include <esp_http_server.h> 13 namespace web_server_idf {
15 #define F(string_literal) (string_literal) 16 #define PGM_P const char * 17 #define strncpy_P strncpy 38 void addHeader(
const char *
name,
const char *
value);
40 virtual const char *get_content_data()
const = 0;
41 virtual size_t get_content_size()
const = 0;
74 void print(
const char *str) { this->content_.append(str); }
75 void print(
const std::string &str) { this->content_.append(str); }
76 void print(
float value);
77 void printf(
const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
86 : AsyncWebServerResponse(req), data_(data), size_(size) {}
88 const char *
get_content_data()
const override {
return reinterpret_cast<const char *
>(this->data_); };
102 http_method
method()
const {
return static_cast<http_method
>(this->req_->method); }
103 std::string url()
const;
104 std::string host()
const;
108 bool authenticate(
const char *username,
const char *password)
const;
110 void requestAuthentication(
const char *realm =
nullptr)
const;
112 void redirect(
const std::string &url);
115 void send(
int code,
const char *content_type =
nullptr,
const char *content =
nullptr);
119 this->init_response_(res, code, content_type);
125 this->init_response_(res, code, content_type);
132 this->init_response_(res, code, content_type);
138 this->init_response_(res, 200, content_type);
143 bool hasParam(
const std::string &
name) {
return this->getParam(name) !=
nullptr; }
148 bool hasArg(
const char *name) {
return this->hasParam(name); }
149 std::string
arg(
const std::string &name) {
150 auto *param = this->getParam(name);
152 return param->value();
157 operator httpd_req_t *()
const {
return this->req_; }
160 bool hasHeader(
const char *name)
const;
165 std::map<std::string, AsyncWebParameter *>
params_;
187 this->handlers_.push_back(handler);
193 httpd_handle_t server_{};
194 static esp_err_t request_handler(httpd_req_t *r);
195 static esp_err_t request_post_handler(httpd_req_t *r);
198 std::function<void(AsyncWebServerRequest *request)> on_not_found_{};
210 size_t len,
bool final) {}
223 void send(
const char *message,
const char *event =
nullptr, uint32_t
id = 0, uint32_t reconnect = 0);
227 static void destroy(
void *p);
229 httpd_handle_t hd_{};
237 using connect_handler_t = std::function<void(AsyncEventSourceClient *)>;
245 return request->
method() == HTTP_GET && request->
url() == this->url_;
250 void onConnect(connect_handler_t
cb) { this->on_connect_ = std::move(cb); }
252 void send(
const char *message,
const char *event =
nullptr, uint32_t
id = 0, uint32_t reconnect = 0);
254 size_t count()
const {
return this->sessions_.size(); }
259 connect_handler_t on_connect_{};
268 void addHeader(
const char *
name,
const char *value) { this->headers_.emplace_back(name, value); }
277 std::vector<std::pair<std::string, std::string>>
headers_;
285 #endif // !defined(USE_ESP_IDF) AsyncWebServer(uint16_t port)
http_method method() const
size_t get_content_size() const override
bool hasParam(const std::string &name)
virtual void handleRequest(AsyncWebServerRequest *request)
virtual ~AsyncWebServerResponse()
size_t get_content_size() const override
void print(const std::string &str)
AsyncResponseStream * beginResponseStream(const char *content_type)
AsyncWebHandler & addHandler(AsyncWebHandler *handler)
size_t contentLength() const
virtual void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
virtual bool canHandle(AsyncWebServerRequest *request)
virtual ~AsyncWebHandler()
const char * get_content_data() const override
const char * get_content_data() const override
AsyncWebServerRequest(httpd_req_t *req)
const std::string & value() const
const char * get_content_data() const override
virtual bool isRequestHandlerTrivial()
AsyncWebServerResponse(const AsyncWebServerRequest *req)
void onConnect(connect_handler_t cb)
AsyncWebServerResponseProgmem(const AsyncWebServerRequest *req, const uint8_t *data, const size_t size)
const AsyncWebServerRequest * req_
void print(const char *str)
AsyncWebServerResponse * beginResponse(int code, const char *content_type)
AsyncEventSource * server_
bool hasArg(const char *name)
const char * get_content_data() const override
virtual void handleUpload(AsyncWebServerRequest *request, const std::string &filename, size_t index, uint8_t *data, size_t len, bool final)
AsyncEventSource(std::string url)
void onNotFound(std::function< void(AsyncWebServerRequest *request)> fn)
std::vector< AsyncWebHandler * > handlers_
AsyncWebServerResponseContent(const AsyncWebServerRequest *req, std::string content)
AsyncWebServerResponseEmpty(const AsyncWebServerRequest *req)
enum esphome::EntityCategory __attribute__
std::map< std::string, AsyncWebParameter * > params_
std::string arg(const std::string &name)
AsyncWebServerResponse * beginResponse_P(int code, const char *content_type, const uint8_t *data, const size_t data_size)
AsyncWebServerRequest(httpd_req_t *req, std::string post_query)
Implementation of SPI Controller mode.
size_t get_content_size() const override
std::set< AsyncEventSourceResponse * > sessions_
AsyncWebParameter(std::string value)
AsyncWebServerResponse * beginResponse(int code, const char *content_type, const std::string &content)
bool canHandle(AsyncWebServerRequest *request) override
AsyncResponseStream(const AsyncWebServerRequest *req)
size_t get_content_size() const override