8000 Movable HTTPClient and fixing WiFiClient copy by mcspr · Pull Request #8237 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Movable HTTPClient and fixing WiFiClient copy #8237

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 10 commits into from
Oct 13, 2021
Prev Previous commit
Next Next commit
handle wificlient lifetime through the wificlient itself
  • Loading branch information
mcspr committed Sep 6, 2021
commit bf8d804d14638e04c4c8fc0bcaf4ab32c26f2b36
6 changes: 3 additions & 3 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ void HTTPClient::clear()
* @return success bool
*/
bool HTTPClient::begin(WiFiClient &client, const String& url) {
_client = &client;

// check for : (http: or https:)
int index = url.indexOf(':');
if(index < 0) {
Expand All @@ -86,6 +84,8 @@ bool HTTPClient::begin(WiFiClient &client, const String& url) {
}

_port = (protocol == "https" ? 443 : 80);
_client = std::make_unique<WiFiClient>(client);

return beginInternal(url, protocol.c_str());
}

Expand All @@ -101,7 +101,7 @@ bool HTTPClient::begin(WiFiClient &client, const String& url) {
*/
bool HTTPClient::begin(WiFiClient &client, const String& host, uint16_t port, const String& uri, bool https)
{
_client = &client;
_client = std::make_unique<WiFiClient>(client);

clear();
_host = host;
Expand Down
56 changes: 2 additions & 54 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ class HTTPClient
HTTPClient(HTTPClient&&) = default;
HTTPClient& operator=(HTTPClient&&) = default;

/*
* Since both begin() functions take a reference to client as a parameter, you need to
* ensure the client object lives the entire time of the HTTPClient
*/
// Note that WiFiClient instance *will* be captured by the internal handler.
bool begin(WiFiClient &client, const String& url);
bool begin(WiFiClient &client, const String& host, uint16_t port, const String& uri = "/", bool https = false);

Expand Down Expand Up @@ -235,55 +232,6 @@ class HTTPClient
// in case wificlient supports seamless ref() / unref() of the underlying connection
// for both wificlient and wificlientsecure, this may be removed in favour of that approach.

struct NonOwningClientPtr {
NonOwningClientPtr() = default;
NonOwningClientPtr(const NonOwningClientPtr&) = delete;
NonOwningClientPtr(NonOwningClientPtr&& other) noexcept {
*this = std::move(other);
}

~NonOwningClientPtr() noexcept {
if (_ptr) {
_ptr->stop();
}
}

explicit NonOwningClientPtr(WiFiClient* ptr) noexcept :
_ptr(ptr)
{}

explicit operator bool() const {
return _ptr != nullptr;
}

WiFiClient* get() const noexcept {
return _ptr;
}

WiFiClient& operator*() const {
return *_ptr;
}

WiFiClient* operator->() const noexcept {
return _ptr;
}

NonOwningClientPtr& operator=(WiFiClient* ptr) noexcept {
_ptr = ptr;
return *this;
}

NonOwningClientPtr& operator=(const NonOwningClientPtr&) = delete;
NonOwningClientPtr& operator=(NonOwningClientPtr&& other) noexcept {
_ptr = other._ptr;
other._ptr = nullptr;
return *this;
}

private:
WiFiClient* _ptr = nullptr;
};

bool beginInternal(const String& url, const char* expectedProtocol);
void disconnect(bool preserveClient = false);
void clear();
Expand All @@ -293,7 +241,7 @@ class HTTPClient
int handleHeaderResponse();
int writeToStreamDataBlock(Stream * stream, int len);

NonOwningClientPtr _client;
std::unique_ptr<WiFiClient> _client;

/// request handling
String _host;
Expand Down
0