Skip to content

httpclient: remove deprecated API #7617

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

Merged
merged 2 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 0 additions & 177 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,62 +28,12 @@
#include <StreamString.h>
#include <base64.h>

class TransportTraits
{
public:
virtual ~TransportTraits()
{
}

virtual std::unique_ptr<WiFiClient> create()
{
return std::unique_ptr<WiFiClient>(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<WiFiClient> create() override
{
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure();
client->setFingerprint(_fingerprint);
return std::unique_ptr<WiFiClient>(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);
}

/**
Expand Down Expand Up @@ -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:)
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
18 changes: 6 additions & 12 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,12 @@ class HTTPClient
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);

Expand All @@ -183,7 +181,6 @@ class HTTPClient
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

Expand Down Expand Up @@ -237,9 +234,6 @@ class HTTPClient
int handleHeaderResponse();
int writeToStreamDataBlock(Stream * stream, int len);


TransportTraitsPtr _transportTraits;
std::unique_ptr<WiFiClient> _tcpDeprecated;
WiFiClient* _client;

/// request handling
Expand Down