1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-09-09 18:40:33 +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:
Max Prokhorov
2021-10-13 04:19:51 +03:00
committed by GitHub
parent b7a2f44b50
commit 40b26b769c
5 changed files with 73 additions and 46 deletions

View File

@@ -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;