Skip to content

Add RAII wrapper to HTTPClient #13

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 6 commits into from
Mar 26, 2025
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
51 changes: 51 additions & 0 deletions src/HTTPClientRAII.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 31 additions & 0 deletions src/HTTPClientRAII.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef HTTPCLIENTRAII_H
#define HTTPCLIENTRAII_H

#include <Arduino.h>
#include <HTTPClient.h>
#include <WiFiClient.h>

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;
HTTPClientRAII &operator=(HTTPClientRAII &&) = delete;

~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
11 changes: 1 addition & 10 deletions src/OpenStreetMap-esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ bool OpenStreetMap::fillBuffer(WiFiClient *stream, MemoryBuffer &buffer, size_t

std::optional<std::unique_ptr<MemoryBuffer>> 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";
Expand All @@ -335,42 +334,34 @@ std::optional<std::unique_ptr<MemoryBuffer>> OpenStreetMap::urlToBuffer(const St
const int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK)
{
http.end();
result = "HTTP Error: " + String(httpCode);
return std::nullopt;
}

const size_t contentSize = http.getSize();
if (contentSize < 1)
{
http.end();
result = "Empty or chunked response";
return std::nullopt;
}

WiFiClient *stream = http.getStreamPtr();
if (!stream)
{
http.end();
result = "Failed to get HTTP stream";
return std::nullopt;
}

auto buffer = std::make_unique<MemoryBuffer>(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;
}
Expand Down
1 change: 1 addition & 0 deletions src/OpenStreetMap-esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down