mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 05:24:53 +01:00
HTTP Request fix reusing connections. (#1383)
This commit is contained in:
parent
b3169deda7
commit
c12c9e97c2
2 changed files with 18 additions and 6 deletions
|
@ -12,9 +12,20 @@ void HttpRequestComponent::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, " User-Agent: %s", this->useragent_);
|
ESP_LOGCONFIG(TAG, " User-Agent: %s", this->useragent_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HttpRequestComponent::set_url(std::string url) {
|
||||||
|
this->url_ = url;
|
||||||
|
this->secure_ = url.compare(0, 6, "https:") == 0;
|
||||||
|
|
||||||
|
if (!this->last_url_.empty() && this->url_ != this->last_url_) {
|
||||||
|
// Close connection if url has been changed
|
||||||
|
this->client_.setReuse(false);
|
||||||
|
this->client_.end();
|
||||||
|
}
|
||||||
|
this->client_.setReuse(true);
|
||||||
|
}
|
||||||
|
|
||||||
void HttpRequestComponent::send() {
|
void HttpRequestComponent::send() {
|
||||||
bool begin_status = false;
|
bool begin_status = false;
|
||||||
this->client_.setReuse(true);
|
|
||||||
const String url = this->url_.c_str();
|
const String url = this->url_.c_str();
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
begin_status = this->client_.begin(url);
|
begin_status = this->client_.begin(url);
|
||||||
|
@ -78,7 +89,10 @@ WiFiClient *HttpRequestComponent::get_wifi_client_() {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void HttpRequestComponent::close() { this->client_.end(); }
|
void HttpRequestComponent::close() {
|
||||||
|
this->last_url_ = this->url_;
|
||||||
|
this->client_.end();
|
||||||
|
}
|
||||||
|
|
||||||
const char *HttpRequestComponent::get_string() {
|
const char *HttpRequestComponent::get_string() {
|
||||||
static const String STR = this->client_.getString();
|
static const String STR = this->client_.getString();
|
||||||
|
|
|
@ -27,10 +27,7 @@ class HttpRequestComponent : public Component {
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||||
|
|
||||||
void set_url(std::string url) {
|
void set_url(std::string url);
|
||||||
this->url_ = url;
|
|
||||||
this->secure_ = url.compare(0, 6, "https:") == 0;
|
|
||||||
}
|
|
||||||
void set_method(const char *method) { this->method_ = method; }
|
void set_method(const char *method) { this->method_ = method; }
|
||||||
void set_useragent(const char *useragent) { this->useragent_ = useragent; }
|
void set_useragent(const char *useragent) { this->useragent_ = useragent; }
|
||||||
void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
|
void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
|
||||||
|
@ -43,6 +40,7 @@ class HttpRequestComponent : public Component {
|
||||||
protected:
|
protected:
|
||||||
HTTPClient client_{};
|
HTTPClient client_{};
|
||||||
std::string url_;
|
std::string url_;
|
||||||
|
std::string last_url_;
|
||||||
const char *method_;
|
const char *method_;
|
||||||
const char *useragent_{nullptr};
|
const char *useragent_{nullptr};
|
||||||
bool secure_;
|
bool secure_;
|
||||||
|
|
Loading…
Reference in a new issue