Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable HTTP redirect following #3100

Merged
merged 7 commits into from Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions esphome/components/http_request/__init__.py
Expand Up @@ -31,6 +31,8 @@
CONF_JSON = "json"
CONF_VERIFY_SSL = "verify_ssl"
CONF_ON_RESPONSE = "on_response"
CONF_FOLLOW_REDIRECTS = "follow_redirects"
CONF_REDIRECT_LIMIT = "redirect_limit"


def validate_url(value):
Expand Down Expand Up @@ -71,6 +73,8 @@ def validate_secure_url(config):
{
cv.GenerateID(): cv.declare_id(HttpRequestComponent),
cv.Optional(CONF_USERAGENT, "ESPHome"): cv.string,
cv.Optional(CONF_FOLLOW_REDIRECTS, True): cv.boolean,
cv.Optional(CONF_REDIRECT_LIMIT, 3): cv.int_,
cv.Optional(
CONF_TIMEOUT, default="5s"
): cv.positive_time_period_milliseconds,
Expand All @@ -90,6 +94,9 @@ async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_timeout(config[CONF_TIMEOUT]))
cg.add(var.set_useragent(config[CONF_USERAGENT]))
cg.add(var.set_follow_redirects(config[CONF_FOLLOW_REDIRECTS]))
cg.add(var.set_redirect_limit(config[CONF_REDIRECT_LIMIT]))

if CORE.is_esp8266 and not config[CONF_ESP8266_DISABLE_SSL_SUPPORT]:
cg.add_define("USE_HTTP_REQUEST_ESP8266_HTTPS")

Expand Down
25 changes: 15 additions & 10 deletions esphome/components/http_request/http_request.cpp
Expand Up @@ -14,6 +14,8 @@ void HttpRequestComponent::dump_config() {
ESP_LOGCONFIG(TAG, "HTTP Request:");
ESP_LOGCONFIG(TAG, " Timeout: %ums", this->timeout_);
ESP_LOGCONFIG(TAG, " User-Agent: %s", this->useragent_);
ESP_LOGCONFIG(TAG, " Follow Redirects: %d", this->follow_redirects_);
ESP_LOGCONFIG(TAG, " Redirect limit: %d", this->redirect_limit_);
}

void HttpRequestComponent::set_url(std::string url) {
Expand All @@ -38,18 +40,21 @@ void HttpRequestComponent::send(const std::vector<HttpRequestResponseTrigger *>

bool begin_status = false;
const String url = this->url_.c_str();
#ifdef USE_ESP32
begin_status = this->client_.begin(url);
#endif
#ifdef USE_ESP8266
#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0)
this->client_.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
#elif ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
this->client_.setFollowRedirects(true);
#if defined USE_ESP32 || defined ESP8266 && ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
#if defined USE_ESP32 || ARDUINO_VERSION_CODE >= VERSION_CODE(2, 7, 0)
guillempages marked this conversation as resolved.
Show resolved Hide resolved
if (this->follow_redirects_) {
this->client_.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
} else {
this->client_.setFollowRedirects(HTTPC_DISABLE_FOLLOW_REDIRECTS);
}
#else
this->client_.setFollowRedirects(this->follow_redirects_);
#endif
#if ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
this->client_.setRedirectLimit(3);
this->client_.setRedirectLimit(this->redirect_limit_);
#endif
#ifdef USE_ESP32
begin_status = this->client_.begin(url);
#elif USE_ESP8266
guillempages marked this conversation as resolved.
Show resolved Hide resolved
begin_status = this->client_.begin(*this->get_wifi_client_(), url);
#endif

Expand Down
4 changes: 4 additions & 0 deletions esphome/components/http_request/http_request.h
Expand Up @@ -40,6 +40,8 @@ class HttpRequestComponent : public Component {
void set_method(const char *method) { this->method_ = method; }
void set_useragent(const char *useragent) { this->useragent_ = useragent; }
void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
void set_follow_redirects(bool follow_redirects) { this->follow_redirects_ = follow_redirects; }
void set_redirect_limit(uint16_t limit) { this->redirect_limit_ = limit; }
void set_body(const std::string &body) { this->body_ = body; }
void set_headers(std::list<Header> headers) { this->headers_ = std::move(headers); }
void send(const std::vector<HttpRequestResponseTrigger *> &response_triggers);
Expand All @@ -53,6 +55,8 @@ class HttpRequestComponent : public Component {
const char *method_;
const char *useragent_{nullptr};
bool secure_;
bool follow_redirects_;
uint16_t redirect_limit_;
uint16_t timeout_{5000};
std::string body_;
std::list<Header> headers_;
Expand Down