149static constexpr int HTTP_ERROR_CONNECTION_CLOSED = -1;
180 bool is_read_complete) {
181 if (bytes_read_or_error > 0) {
182 last_data_time =
millis();
185 if (bytes_read_or_error < 0) {
189 if (is_read_complete) {
192 if (
millis() - last_data_time >= timeout_ms) {
199class HttpRequestComponent;
235 virtual int read(uint8_t *buf,
size_t max_len) = 0;
286 uint32_t timeout_ms) {
287 size_t read_index = 0;
288 uint32_t last_data_time =
millis();
290 while (read_index < total_size) {
291 int read_bytes_or_error = container->
read(buffer + read_index, std::min(chunk_size, total_size - read_index));
306 read_index += read_bytes_or_error;
313 void process(
const std::shared_ptr<HttpContainer> &container, std::string &response_body) {
314 this->
trigger(container, response_body);
331 std::shared_ptr<HttpContainer>
get(
const std::string &url) {
return this->
start(url,
"GET",
"", {}); }
332 std::shared_ptr<HttpContainer>
get(
const std::string &url,
const std::list<Header> &request_headers) {
333 return this->
start(url,
"GET",
"", request_headers);
335 std::shared_ptr<HttpContainer>
get(
const std::string &url,
const std::list<Header> &request_headers,
336 const std::set<std::string> &collect_headers) {
337 return this->
start(url,
"GET",
"", request_headers, collect_headers);
339 std::shared_ptr<HttpContainer>
post(
const std::string &url,
const std::string &body) {
340 return this->
start(url,
"POST", body, {});
342 std::shared_ptr<HttpContainer>
post(
const std::string &url,
const std::string &body,
343 const std::list<Header> &request_headers) {
344 return this->
start(url,
"POST", body, request_headers);
346 std::shared_ptr<HttpContainer>
post(
const std::string &url,
const std::string &body,
347 const std::list<Header> &request_headers,
348 const std::set<std::string> &collect_headers) {
349 return this->
start(url,
"POST", body, request_headers, collect_headers);
352 std::shared_ptr<HttpContainer>
start(
const std::string &url,
const std::string &method,
const std::string &body,
353 const std::list<Header> &request_headers) {
354 return this->
start(url, method, body, request_headers, {});
357 std::shared_ptr<HttpContainer>
start(
const std::string &url,
const std::string &method,
const std::string &body,
358 const std::list<Header> &request_headers,
359 const std::set<std::string> &collect_headers) {
360 std::set<std::string> lower_case_collect_headers;
361 for (
const std::string &collect_header : collect_headers) {
362 lower_case_collect_headers.insert(
str_lower_case(collect_header));
364 return this->
perform(url, method, body, request_headers, lower_case_collect_headers);
368 virtual std::shared_ptr<HttpContainer>
perform(
const std::string &url,
const std::string &method,
369 const std::string &body,
const std::list<Header> &request_headers,
370 const std::set<std::string> &collect_headers) = 0;
384#ifdef USE_HTTP_REQUEST_RESPONSE
398#ifdef USE_HTTP_REQUEST_RESPONSE
411 void play(
const Ts &...
x)
override {
413 if (this->body_.has_value()) {
414 body = this->body_.value(
x...);
416 if (!this->
json_.empty()) {
424 std::list<Header> request_headers;
426 auto val = item.second;
428 header.
name = item.first;
430 request_headers.push_back(header);
433 auto container = this->
parent_->
start(this->url_.value(
x...), this->method_.value(
x...), body, request_headers,
434 this->collect_headers_);
436 auto captured_args = std::make_tuple(
x...);
438 if (container ==
nullptr) {
439 std::apply([
this](Ts... captured_args_inner) { this->error_trigger_.trigger(captured_args_inner...); },
445#ifdef USE_HTTP_REQUEST_RESPONSE
446 if (this->capture_response_.value(
x...)) {
447 std::string response_body;
449 uint8_t *buf = allocator.
allocate(max_length);
450 if (buf !=
nullptr) {
453 size_t read_index = 0;
454 uint32_t last_data_time =
millis();
456 while (container->get_bytes_read() < max_length) {
457 int read_or_error = container->read(buf + read_index, std::min<size_t>(max_length - read_index, 512));
466 read_index += read_or_error;
468 response_body.reserve(read_index);
469 response_body.assign((
char *) buf, read_index);
473 [
this, &container, &response_body](Ts... captured_args_inner) {
474 this->success_trigger_with_response_.trigger(container, response_body, captured_args_inner...);
480 std::apply([
this, &container](
481 Ts... captured_args_inner) { this->success_trigger_.trigger(container, captured_args_inner...); },
489 for (
const auto &item : this->
json_) {
490 auto val = item.second;
491 root[item.first] =
val.value(
x...);
500#ifdef USE_HTTP_REQUEST_RESPONSE
void feed_wdt(uint32_t time=0)
Helper class to easily give an object a parent of type T.
An STL allocator that uses SPI or internal RAM.
void deallocate(T *p, size_t n)
void trigger(const Ts &...x)
std::string get_response_header(const std::string &header_name)
std::map< std::string, std::list< std::string > > get_response_headers()
Get response headers.
virtual int read(uint8_t *buf, size_t max_len)=0
Read data from the HTTP response body.
virtual bool is_read_complete() const
Check if all expected content has been read.
virtual ~HttpContainer()=default
int status_code
-1 indicates no response received yet
void set_chunked(bool chunked)
std::map< std::string, std::list< std::string > > response_headers_
size_t get_bytes_read() const
void set_secure(bool secure)
bool is_chunked_
True if response uses chunked transfer encoding.
float get_setup_priority() const override
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body, const std::list< Header > &request_headers, const std::set< std::string > &collect_headers)
uint32_t get_watchdog_timeout() const
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body)
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body, const std::list< Header > &request_headers)
void set_useragent(const char *useragent)
virtual std::shared_ptr< HttpContainer > perform(const std::string &url, const std::string &method, const std::string &body, const std::list< Header > &request_headers, const std::set< std::string > &collect_headers)=0
std::shared_ptr< HttpContainer > start(const std::string &url, const std::string &method, const std::string &body, const std::list< Header > &request_headers, const std::set< std::string > &collect_headers)
std::shared_ptr< HttpContainer > get(const std::string &url, const std::list< Header > &request_headers)
uint32_t watchdog_timeout_
void set_follow_redirects(bool follow_redirects)
std::shared_ptr< HttpContainer > start(const std::string &url, const std::string &method, const std::string &body, const std::list< Header > &request_headers)
uint32_t get_timeout() const
void set_redirect_limit(uint16_t limit)
void set_watchdog_timeout(uint32_t watchdog_timeout)
void set_timeout(uint32_t timeout)
std::shared_ptr< HttpContainer > get(const std::string &url, const std::list< Header > &request_headers, const std::set< std::string > &collect_headers)
std::shared_ptr< HttpContainer > get(const std::string &url)
void dump_config() override
void process(const std::shared_ptr< HttpContainer > &container, std::string &response_body)
void set_max_response_buffer_size(size_t max_response_buffer_size)
void play(const Ts &...x) override
std::set< std::string > collect_headers_
HttpRequestComponent * parent_
std::map< const char *, TemplatableValue< std::string, Ts... > > json_
method capture_response void add_request_header(const char *key, TemplatableValue< const char *, Ts... > value)
TEMPLATABLE_VALUE(std::string, url) TEMPLATABLE_VALUE(const char *
Trigger< std::shared_ptr< HttpContainer >, std::string &, Ts... > success_trigger_with_response_
void add_collect_header(const char *value)
Trigger< Ts... > error_trigger_
Trigger< Ts... > * get_error_trigger()
std::function< void(Ts..., JsonObject)> json_func_
Trigger< std::shared_ptr< HttpContainer >, Ts... > success_trigger_
void encode_json_func_(Ts... x, JsonObject root)
void set_json(std::function< void(Ts..., JsonObject)> json_func)
void add_json(const char *key, TemplatableValue< std::string, Ts... > value)
Trigger< std::shared_ptr< HttpContainer >, std::string &, Ts... > * get_success_trigger_with_response()
void encode_json_(Ts... x, JsonObject root)
size_t max_response_buffer_size_
std::map< const char *, TemplatableValue< const char *, Ts... > > request_headers_
Trigger< std::shared_ptr< HttpContainer >, Ts... > * get_success_trigger()
HttpRequestSendAction(HttpRequestComponent *parent)
@ HTTP_STATUS_PARTIAL_CONTENT
@ HTTP_STATUS_LENGTH_REQUIRED
@ HTTP_STATUS_PERMANENT_REDIRECT
@ HTTP_STATUS_RESET_CONTENT
@ HTTP_STATUS_METHOD_NOT_ALLOWED
@ HTTP_STATUS_NOT_MODIFIED
@ HTTP_STATUS_INTERNAL_ERROR
@ HTTP_STATUS_UNAUTHORIZED
@ HTTP_STATUS_MULTIPLE_CHOICES
@ HTTP_STATUS_NOT_ACCEPTABLE
@ HTTP_STATUS_TEMPORARY_REDIRECT
@ HTTP_STATUS_BAD_REQUEST
@ HTTP_STATUS_MOVED_PERMANENTLY
HttpReadLoopResult
Result of processing a non-blocking read with timeout (for manual loops)
@ TIMEOUT
Timeout waiting for data, caller should exit loop.
@ COMPLETE
All content has been read, caller should exit loop.
@ ERROR
Read error, caller should exit loop.
@ RETRY
No data yet, already delayed, caller should continue loop.
@ DATA
Data was read, process it.
bool is_success(int const status)
Checks if the given HTTP status code indicates a successful request.
HttpReadLoopResult http_read_loop_result(int bytes_read_or_error, uint32_t &last_data_time, uint32_t timeout_ms, bool is_read_complete)
Process a read result with timeout tracking and delay handling.
bool is_redirect(int const status)
Returns true if the HTTP status code is a redirect.
HttpReadResult http_read_fully(HttpContainer *container, uint8_t *buffer, size_t total_size, size_t chunk_size, uint32_t timeout_ms)
Read data from HTTP container into buffer with timeout handling Handles feed_wdt, yield,...
HttpReadStatus
Status of a read operation.
@ TIMEOUT
Timeout waiting for data.
@ ERROR
Read error occurred.
@ OK
Read completed successfully.
std::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
void IRAM_ATTR HOT yield()
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
Result of an HTTP read operation.
HttpReadStatus status
Status of the read operation.
int error_code
Error code from read() on failure, 0 on success.