From 96ef6ba6313a15d6ed82ce8a3560ba8b78fceba2 Mon Sep 17 00:00:00 2001 From: Cellie Date: Wed, 26 Mar 2025 12:22:34 +0100 Subject: [PATCH 1/6] Added RAII wrapper and adjusted code --- src/HTTPClientRAII.cpp | 51 +++++++++++++++++++++++++++++++++++++ src/HTTPClientRAII.h | 25 ++++++++++++++++++ src/OpenStreetMap-esp32.cpp | 11 +------- src/OpenStreetMap-esp32.h | 1 + 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 src/HTTPClientRAII.cpp create mode 100644 src/HTTPClientRAII.h diff --git a/src/HTTPClientRAII.cpp b/src/HTTPClientRAII.cpp new file mode 100644 index 0000000..edabf54 --- /dev/null +++ b/src/HTTPClientRAII.cpp @@ -0,0 +1,51 @@ +#include "HTTPClientRAII.h" + +HTTPClientRAII::HTTPClientRAII() : http(new HTTPClient()) {} + +HTTPClientRAII::~HTTPClientRAII() +{ + if (http) + { + http->end(); + delete http; + http = nullptr; + } +} + +bool HTTPClientRAII::begin(const String &url) +{ + if (!http) + return false; + + http->setUserAgent(userAgent); + return http->begin(url); +} + +int HTTPClientRAII::GET() +{ + if (!http) + return -1; + + return http->GET(); +} + +size_t HTTPClientRAII::getSize() const +{ + if (!http) + return 0; + + return http->getSize(); +} + +WiFiClient *HTTPClientRAII::getStreamPtr() +{ + if (!http) + return nullptr; + + return http->getStreamPtr(); +} + +bool HTTPClientRAII::isInitialized() const +{ + return http != nullptr; +} \ No newline at end of file diff --git a/src/HTTPClientRAII.h b/src/HTTPClientRAII.h new file mode 100644 index 0000000..5202617 --- /dev/null +++ b/src/HTTPClientRAII.h @@ -0,0 +1,25 @@ +#ifndef HTTPCLIENTRAII_H +#define HTTPCLIENTRAII_H + +#include +#include +#include + +class HTTPClientRAII +{ +public: + HTTPClientRAII(); + ~HTTPClientRAII(); + + bool begin(const String &url); + int GET(); + size_t getSize() const; + WiFiClient *getStreamPtr(); + bool isInitialized() const; + +private: + HTTPClient *http; + const char *userAgent = "OpenStreetMap-esp32/1.0 (+https://github.com/CelliesProjects/OpenStreetMap-esp32)"; +}; + +#endif // HTTPCLIENTRAII_H \ No newline at end of file diff --git a/src/OpenStreetMap-esp32.cpp b/src/OpenStreetMap-esp32.cpp index 4180327..852641c 100644 --- a/src/OpenStreetMap-esp32.cpp +++ b/src/OpenStreetMap-esp32.cpp @@ -324,8 +324,7 @@ bool OpenStreetMap::fillBuffer(WiFiClient *stream, MemoryBuffer &buffer, size_t std::optional> OpenStreetMap::urlToBuffer(const String &url, String &result) { - HTTPClient http; - http.setUserAgent("OpenStreetMap-esp32/1.0 (+https://github.com/CelliesProjects/OpenStreetMap-esp32)"); + HTTPClientRAII http; if (!http.begin(url)) { result = "Failed to initialize HTTP client"; @@ -335,7 +334,6 @@ std::optional> OpenStreetMap::urlToBuffer(const St const int httpCode = http.GET(); if (httpCode != HTTP_CODE_OK) { - http.end(); result = "HTTP Error: " + String(httpCode); return std::nullopt; } @@ -343,7 +341,6 @@ std::optional> OpenStreetMap::urlToBuffer(const St const size_t contentSize = http.getSize(); if (contentSize < 1) { - http.end(); result = "Empty or chunked response"; return std::nullopt; } @@ -351,7 +348,6 @@ std::optional> OpenStreetMap::urlToBuffer(const St WiFiClient *stream = http.getStreamPtr(); if (!stream) { - http.end(); result = "Failed to get HTTP stream"; return std::nullopt; } @@ -359,18 +355,13 @@ std::optional> OpenStreetMap::urlToBuffer(const St auto buffer = std::make_unique(contentSize); if (!buffer->isAllocated()) { - http.end(); result = "Failed to allocate buffer"; return std::nullopt; } if (!fillBuffer(stream, *buffer, contentSize, result)) - { - http.end(); return std::nullopt; - } - http.end(); result = "Downloaded tile " + url; return buffer; } diff --git a/src/OpenStreetMap-esp32.h b/src/OpenStreetMap-esp32.h index 8c4a83b..5d16e4a 100644 --- a/src/OpenStreetMap-esp32.h +++ b/src/OpenStreetMap-esp32.h @@ -36,6 +36,7 @@ #include "CachedTile.h" #include "MemoryBuffer.h" +#include "HTTPClientRAII.h" constexpr uint16_t OSM_TILESIZE = 256; constexpr uint16_t OSM_TILE_TIMEOUT_MS = 2500; From fdd94bdfe57ddfad3c1ade61a6ccdded72995117 Mon Sep 17 00:00:00 2001 From: Cellie Date: Wed, 26 Mar 2025 12:23:41 +0100 Subject: [PATCH 2/6] Cleanup --- src/HTTPClientRAII.cpp | 2 +- src/HTTPClientRAII.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HTTPClientRAII.cpp b/src/HTTPClientRAII.cpp index edabf54..4cd88b4 100644 --- a/src/HTTPClientRAII.cpp +++ b/src/HTTPClientRAII.cpp @@ -48,4 +48,4 @@ WiFiClient *HTTPClientRAII::getStreamPtr() bool HTTPClientRAII::isInitialized() const { return http != nullptr; -} \ No newline at end of file +} diff --git a/src/HTTPClientRAII.h b/src/HTTPClientRAII.h index 5202617..4ebd5da 100644 --- a/src/HTTPClientRAII.h +++ b/src/HTTPClientRAII.h @@ -22,4 +22,4 @@ class HTTPClientRAII const char *userAgent = "OpenStreetMap-esp32/1.0 (+https://github.com/CelliesProjects/OpenStreetMap-esp32)"; }; -#endif // HTTPCLIENTRAII_H \ No newline at end of file +#endif // HTTPCLIENTRAII_H From e4ca94182564a3a61c9d9f84a11e675b697a7065 Mon Sep 17 00:00:00 2001 From: Cellie Date: Wed, 26 Mar 2025 12:49:39 +0100 Subject: [PATCH 3/6] Delete copy consructor --- src/HTTPClientRAII.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/HTTPClientRAII.h b/src/HTTPClientRAII.h index 4ebd5da..dda6cf2 100644 --- a/src/HTTPClientRAII.h +++ b/src/HTTPClientRAII.h @@ -17,6 +17,8 @@ class HTTPClientRAII WiFiClient *getStreamPtr(); bool isInitialized() const; + HTTPClientRAII(HTTPClient& other) = delete; + private: HTTPClient *http; const char *userAgent = "OpenStreetMap-esp32/1.0 (+https://github.com/CelliesProjects/OpenStreetMap-esp32)"; From 77836e862d38992a25d582202f32f9b3e4402df2 Mon Sep 17 00:00:00 2001 From: Cellie Date: Wed, 26 Mar 2025 13:07:48 +0100 Subject: [PATCH 4/6] Deleted copy and move ctors --- src/HTTPClientRAII.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/HTTPClientRAII.h b/src/HTTPClientRAII.h index dda6cf2..83fe77b 100644 --- a/src/HTTPClientRAII.h +++ b/src/HTTPClientRAII.h @@ -17,7 +17,10 @@ class HTTPClientRAII WiFiClient *getStreamPtr(); bool isInitialized() const; - HTTPClientRAII(HTTPClient& other) = delete; + HTTPClientRAII(const HTTPClientRAII &) = delete; + HTTPClientRAII &operator=(const HTTPClientRAII &) = delete; + HTTPClientRAII(HTTPClientRAII &&) = delete; + HTTPClientRAII &operator=(HTTPClientRAII &&) = delete; private: HTTPClient *http; From 07ce7810729b498a37be6480292b3ab929b7133f Mon Sep 17 00:00:00 2001 From: Cellie Date: Wed, 26 Mar 2025 13:16:30 +0100 Subject: [PATCH 5/6] cleanup --- src/HTTPClientRAII.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/HTTPClientRAII.h b/src/HTTPClientRAII.h index 83fe77b..cb64684 100644 --- a/src/HTTPClientRAII.h +++ b/src/HTTPClientRAII.h @@ -9,6 +9,11 @@ class HTTPClientRAII { public: HTTPClientRAII(); + HTTPClientRAII(const HTTPClientRAII &) = delete; + HTTPClientRAII &operator=(const HTTPClientRAII &) = delete; + HTTPClientRAII(HTTPClientRAII &&) = delete; + HTTPClientRAII &operator=(HTTPClientRAII &&) = delete; + ~HTTPClientRAII(); bool begin(const String &url); @@ -17,11 +22,6 @@ class HTTPClientRAII WiFiClient *getStreamPtr(); bool isInitialized() const; - HTTPClientRAII(const HTTPClientRAII &) = delete; - HTTPClientRAII &operator=(const HTTPClientRAII &) = delete; - HTTPClientRAII(HTTPClientRAII &&) = delete; - HTTPClientRAII &operator=(HTTPClientRAII &&) = delete; - private: HTTPClient *http; const char *userAgent = "OpenStreetMap-esp32/1.0 (+https://github.com/CelliesProjects/OpenStreetMap-esp32)"; From d3e7bd64381daeb11e979b351176062a58421b11 Mon Sep 17 00:00:00 2001 From: Cellie Date: Wed, 26 Mar 2025 13:23:32 +0100 Subject: [PATCH 6/6] Add comment about non copyability of `HTTPClientRAII` --- src/HTTPClientRAII.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/HTTPClientRAII.h b/src/HTTPClientRAII.h index cb64684..fb90c8c 100644 --- a/src/HTTPClientRAII.h +++ b/src/HTTPClientRAII.h @@ -9,6 +9,7 @@ class HTTPClientRAII { public: HTTPClientRAII(); + // This class manages an HTTPClient and should not be copied. HTTPClientRAII(const HTTPClientRAII &) = delete; HTTPClientRAII &operator=(const HTTPClientRAII &) = delete; HTTPClientRAII(HTTPClientRAII &&) = delete;