mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-18 23:03:34 +03:00
Movable HTTPClient and fixing WiFiClient copy (#8237)
- =default for default ctor, destructor, move ctor and the assignment move - use `std::unique_ptr<WiFiClient>` instead of raw pointer to the client - implement `virtual std::unique_ptr<WiFiClient> WiFiClient::clone()` to safely copy the WiFiClientSecure instance, without accidentally slicing it (i.e. using the pointer with incorrect type, calling base WiFiClient virtual methods) - replace headers pointer array with `std::unique_ptr<T[]>` to simplify the move operations - substitute userAgent with the default one when it is empty (may be a subject to change though, b/c now there is a global static `String`) Allow HTTPClient to be placed inside of movable classes (e.g. std::optional, requested in the linked issue) or to be returned from functions. Class logic stays as-is, only the underlying member types are changed. Notice that WiFiClient connection object is now copied, and the internal ClientContext will be preserved even after the original WiFiClient object was destroyed. replaces #8236 resolves #8231 and, possibly #5734
This commit is contained in:
@ -101,6 +101,10 @@ WiFiClient::~WiFiClient()
|
||||
_client->unref();
|
||||
}
|
||||
|
||||
std::unique_ptr<WiFiClient> WiFiClient::clone() const {
|
||||
return std::make_unique<WiFiClient>(*this);
|
||||
}
|
||||
|
||||
WiFiClient::WiFiClient(const WiFiClient& other)
|
||||
{
|
||||
_client = other._client;
|
||||
|
@ -52,6 +52,18 @@ public:
|
||||
WiFiClient(const WiFiClient&);
|
||||
WiFiClient& operator=(const WiFiClient&);
|
||||
|
||||
// b/c this is both a real class and a virtual parent of the secure client, make sure
|
||||
// there's a safe way to copy from the pointer without 'slicing' it; i.e. only the base
|
||||
// portion of a derived object will be copied, and the polymorphic behavior will be corrupted.
|
||||
//
|
||||
// this class still implements the copy and assignment though, so this is not yet enforced
|
||||
// (but, *should* be inside the Core itself, see httpclient & server)
|
||||
//
|
||||
// ref.
|
||||
// - https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-copy-virtual
|
||||
// - https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-copy
|
||||
virtual std::unique_ptr<WiFiClient> clone() const;
|
||||
|
||||
virtual uint8_t status();
|
||||
virtual int connect(IPAddress ip, uint16_t port) override;
|
||||
virtual int connect(const char *host, uint16_t port) override;
|
||||
|
@ -39,6 +39,13 @@ class WiFiClientSecureCtx : public WiFiClient {
|
||||
|
||||
WiFiClientSecureCtx& operator=(const WiFiClientSecureCtx&) = delete;
|
||||
|
||||
// TODO: usage is invalid b/c of deleted copy, but this will only trigger an error when it is actually used by something
|
||||
// TODO: don't remove just yet to avoid including the WiFiClient default implementation and unintentionally causing
|
||||
// a 'slice' that this method tries to avoid in the first place
|
||||
std::unique_ptr<WiFiClient> clone() const override {
|
||||
return std::unique_ptr<WiFiClient>(new WiFiClientSecureCtx(*this));
|
||||
}
|
||||
|
||||
int connect(IPAddress ip, uint16_t port) override;
|
||||
int connect(const String& host, uint16_t port) override;
|
||||
int connect(const char* name, uint16_t port) override;
|
||||
@ -231,13 +238,23 @@ class WiFiClientSecure : public WiFiClient {
|
||||
// Instead, all virtual functions call their counterpart in "WiFiClientecureCtx* _ctx"
|
||||
// which also derives from WiFiClient (this parent is the one which is eventually used)
|
||||
|
||||
// TODO: notice that this complicates the implementation by having two distinct ways the client connection is managed, consider:
|
||||
// - implementing the secure connection details in the ClientContext
|
||||
// (i.e. delegate the write & read functions there)
|
||||
// - simplify the inheritance chain by implementing base wificlient class and inherit the original wificlient and wificlientsecure from it
|
||||
// - abstract internals so it's possible to seamlessly =default copy and move with the instance *without* resorting to manual copy and initialization of each member
|
||||
|
||||
// TODO: prefer implementing virtual overrides in the .cpp (or, at least one of them)
|
||||
|
||||
public:
|
||||
|
||||
WiFiClientSecure():_ctx(new WiFiClientSecureCtx()) { _owned = _ctx.get(); }
|
||||
WiFiClientSecure(const WiFiClientSecure &rhs): WiFiClient(), _ctx(rhs._ctx) { if (_ctx) _owned = _ctx.get(); }
|
||||
~WiFiClientSecure() override { _ctx = nullptr; }
|
||||
|
||||
WiFiClientSecure& operator=(const WiFiClientSecure&) = default; // The shared-ptrs handle themselves automatically
|
||||
WiFiClientSecure& operator=(const WiFiClientSecure&) = default;
|
||||
|
||||
std::unique_ptr<WiFiClient> clone() const override { return std::unique_ptr<WiFiClient>(new WiFiClientSecure(*this)); }
|
||||
|
||||
uint8_t status() override { return _ctx->status(); }
|
||||
int connect(IPAddress ip, uint16_t port) override { return _ctx->connect(ip, port); }
|
||||
|
Reference in New Issue
Block a user