From f542175009b29b2e148853dce5c413060e18e6db Mon Sep 17 00:00:00 2001 From: david gauchard Date: Wed, 30 Sep 2020 18:59:46 +0200 Subject: [PATCH] httpclient: remove deprecated API (#7617) --- .../src/ESP8266HTTPClient.cpp | 177 ------------------ .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 18 +- 2 files changed, 6 insertions(+), 189 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 580964da1..7ec3ab86f 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -28,62 +28,12 @@ #include #include -class TransportTraits -{ -public: - virtual ~TransportTraits() - { - } - - virtual std::unique_ptr create() - { - return std::unique_ptr(new WiFiClient()); - } - - virtual bool verify(WiFiClient& client, const char* host) - { - (void)client; - (void)host; - return true; - } -}; - - -class BearSSLTraits : public TransportTraits -{ -public: - BearSSLTraits(const uint8_t fingerprint[20]) - { - memcpy(_fingerprint, fingerprint, sizeof(_fingerprint)); - } - - std::unique_ptr create() override - { - BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure(); - client->setFingerprint(_fingerprint); - return std::unique_ptr(client); - } - - bool verify(WiFiClient& client, const char* host) override - { - // No-op. BearSSL will not connect if the fingerprint doesn't match. - // So if you get to here you've already connected and it matched - (void) client; - (void) host; - return true; - } - -protected: - uint8_t _fingerprint[20]; -}; - /** * constructor */ HTTPClient::HTTPClient() : _client(nullptr), _userAgent(F("ESP8266HTTPClient")) { - _tcpDeprecated.reset(nullptr); } /** @@ -117,12 +67,6 @@ void HTTPClient::clear() * @return success bool */ bool HTTPClient::begin(WiFiClient &client, const String& url) { - if(_tcpDeprecated) { - DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); - _canReuse = false; - end(); - } - _client = &client; // check for : (http: or https:) @@ -154,12 +98,6 @@ bool HTTPClient::begin(WiFiClient &client, const String& url) { */ bool HTTPClient::begin(WiFiClient &client, const String& host, uint16_t port, const String& uri, bool https) { - if(_tcpDeprecated) { - DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); - _canReuse = false; - end(); - } - _client = &client; clear(); @@ -171,52 +109,6 @@ bool HTTPClient::begin(WiFiClient &client, const String& host, uint16_t port, co } -bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) -{ - if(_client && !_tcpDeprecated) { - DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); - _canReuse = false; - end(); - } - - if (!beginInternal(url, "https")) { - return false; - } - _transportTraits = TransportTraitsPtr(new (std::nothrow) BearSSLTraits(httpsFingerprint)); - if(!_transportTraits) { - DEBUG_HTTPCLIENT("[HTTP-Client][begin] could not create transport traits\n"); - return false; - } - - DEBUG_HTTPCLIENT("[HTTP-Client][begin] BearSSL-httpsFingerprint:"); - for (size_t i=0; i < 20; i++) { - DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]); - } - DEBUG_HTTPCLIENT("\n"); - return true; -} - - -/** - * parsing the url for all needed parameters - * @param url String - */ -bool HTTPClient::begin(String url) -{ - if(_client && !_tcpDeprecated) { - DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); - _canReuse = false; - end(); - } - - if (!beginInternal(url, "http")) { - return false; - } - _transportTraits = TransportTraitsPtr(new TransportTraits()); - return true; -} - - bool HTTPClient::beginInternal(const String& __url, const char* expectedProtocol) { String url(__url); @@ -278,47 +170,6 @@ bool HTTPClient::beginInternal(const String& __url, const char* expectedProtocol } -bool HTTPClient::begin(String host, uint16_t port, String uri) -{ - if(_client && !_tcpDeprecated) { - DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); - _canReuse = false; - end(); - } - - clear(); - _host = host; - _port = port; - _uri = uri; - _transportTraits = TransportTraitsPtr(new TransportTraits()); - DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d uri: %s\n", host.c_str(), port, uri.c_str()); - return true; -} - - -bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]) -{ - if(_client && !_tcpDeprecated) { - DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); - _canReuse = false; - end(); - } - - clear(); - _host = host; - _port = port; - _uri = uri; - - _transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint)); - DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s BearSSL-httpsFingerprint:", host.c_str(), port, uri.c_str()); - for (size_t i=0; i < 20; i++) { - DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]); - } - DEBUG_HTTPCLIENT("\n"); - return true; -} - - /** * end * called after the payload is handled @@ -353,10 +204,6 @@ void HTTPClient::disconnect(bool preserveClient) _client = nullptr; } } - if(_tcpDeprecated) { - _transportTraits.reset(nullptr); - _tcpDeprecated.reset(nullptr); - } } } else { if (!preserveClient && _client) { // Also destroy _client if not connected() @@ -460,15 +307,6 @@ bool HTTPClient::setURL(const String& url) return beginInternal(url, nullptr); } -/** - * set true to follow redirects. - * @param follow - * @deprecated - */ -void HTTPClient::setFollowRedirects(bool follow) -{ - _followRedirects = follow ? HTTPC_STRICT_FOLLOW_REDIRECTS : HTTPC_DISABLE_FOLLOW_REDIRECTS; -} /** * set redirect follow mode. See `followRedirects_t` enum for avaliable modes. * @param follow @@ -1116,15 +954,6 @@ bool HTTPClient::connect(void) return true; } - if(!_client && _transportTraits) { - _tcpDeprecated = _transportTraits->create(); - if(!_tcpDeprecated) { - DEBUG_HTTPCLIENT("[HTTP-Client] connect: could not create tcp\n"); - return false; - } - _client = _tcpDeprecated.get(); - } - if(!_client) { DEBUG_HTTPCLIENT("[HTTP-Client] connect: HTTPClient::begin was not called or returned error\n"); return false; @@ -1139,12 +968,6 @@ bool HTTPClient::connect(void) DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port); - if (_tcpDeprecated && !_transportTraits->verify(*_tcpDeprecated, _host.c_str())) { - DEBUG_HTTPCLIENT("[HTTP-Client] transport level verify failed\n"); - _client->stop(); - return false; - } - #ifdef ESP8266 _client->setNoDelay(true); #endif diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 5dd3df5fb..9699b92b5 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -163,14 +163,12 @@ public: bool begin(WiFiClient &client, const String& url); bool begin(WiFiClient &client, const String& host, uint16_t port, const String& uri = "/", bool https = false); - // Plain HTTP connection, unencrypted - bool begin(String url) __attribute__ ((deprecated)); - bool begin(String host, uint16_t port, String uri = "/") __attribute__ ((deprecated)); - // Use BearSSL for secure HTTPS connection - bool begin(String url, const uint8_t httpsFingerprint[20]) __attribute__ ((deprecated)); - bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]) __attribute__ ((deprecated)); - // deprecated, use the overload above instead - bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) __attribute__ ((deprecated)); + // old API is now explicitely forbidden + bool begin(String url) __attribute__ ((error("obsolete API, use ::begin(WiFiClient, url)"))); + bool begin(String host, uint16_t port, String uri = "/") __attribute__ ((error("obsolete API, use ::begin(WiFiClient, host, port, uri)"))); + bool begin(String url, const uint8_t httpsFingerprint[20]) __attribute__ ((error("obsolete API, use ::begin(WiFiClientSecure, ...)"))); + bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]) __attribute__ ((error("obsolete API, use ::begin(WiFiClientSecure, ...)"))); + bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) __attribute__ ((error("obsolete API, use ::begin(WiFiClientSecure, ...)"))); void end(void); @@ -183,7 +181,6 @@ public: void setTimeout(uint16_t timeout); // Redirections - void setFollowRedirects(bool follow) __attribute__ ((deprecated)); void setFollowRedirects(followRedirects_t follow); void setRedirectLimit(uint16_t limit); // max redirects to follow for a single request @@ -237,9 +234,6 @@ protected: int handleHeaderResponse(); int writeToStreamDataBlock(Stream * stream, int len); - - TransportTraitsPtr _transportTraits; - std::unique_ptr _tcpDeprecated; WiFiClient* _client; /// request handling