Skip to content

Commit ec03ccc

Browse files
Add RAII wrapper to HTTPClient (#13)
* Added RAII wrapper * Add comment about non copyability of `HTTPClientRAII`
1 parent 2ae5dc2 commit ec03ccc

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

src/HTTPClientRAII.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "HTTPClientRAII.h"
2+
3+
HTTPClientRAII::HTTPClientRAII() : http(new HTTPClient()) {}
4+
5+
HTTPClientRAII::~HTTPClientRAII()
6+
{
7+
if (http)
8+
{
9+
http->end();
10+
delete http;
11+
http = nullptr;
12+
}
13+
}
14+
15+
bool HTTPClientRAII::begin(const String &url)
16+
{
17+
if (!http)
18+
return false;
19+
20+
http->setUserAgent(userAgent);
21+
return http->begin(url);
22+
}
23+
24+
int HTTPClientRAII::GET()
25+
{
26+
if (!http)
27+
return -1;
28+
29+
return http->GET();
30+
}
31+
32+
size_t HTTPClientRAII::getSize() const
33+
{
34+
if (!http)
35+
return 0;
36+
37+
return http->getSize();
38+
}
39+
40+
WiFiClient *HTTPClientRAII::getStreamPtr()
41+
{
42+
if (!http)
43+
return nullptr;
44+
45+
return http->getStreamPtr();
46+
}
47+
48+
bool HTTPClientRAII::isInitialized() const
49+
{
50+
return http != nullptr;
51+
}

src/HTTPClientRAII.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef HTTPCLIENTRAII_H
2+
#define HTTPCLIENTRAII_H
3+
4+
#include <Arduino.h>
5+
#include <HTTPClient.h>
6+
#include <WiFiClient.h>
7+
8+
class HTTPClientRAII
9+
{
10+
public:
11+
HTTPClientRAII();
12+
// This class manages an HTTPClient and should not be copied.
13+
HTTPClientRAII(const HTTPClientRAII &) = delete;
14+
HTTPClientRAII &operator=(const HTTPClientRAII &) = delete;
15+
HTTPClientRAII(HTTPClientRAII &&) = delete;
16+
HTTPClientRAII &operator=(HTTPClientRAII &&) = delete;
17+
18+
~HTTPClientRAII();
19+
20+
bool begin(const String &url);
21+
int GET();
22+
size_t getSize() const;
23+
WiFiClient *getStreamPtr();
24+
bool isInitialized() const;
25+
26+
private:
27+
HTTPClient *http;
28+
const char *userAgent = "OpenStreetMap-esp32/1.0 (+https://github.com/CelliesProjects/OpenStreetMap-esp32)";
29+
};
30+
31+
#endif // HTTPCLIENTRAII_H

src/OpenStreetMap-esp32.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ bool OpenStreetMap::fillBuffer(WiFiClient *stream, MemoryBuffer &buffer, size_t
324324

325325
std::optional<std::unique_ptr<MemoryBuffer>> OpenStreetMap::urlToBuffer(const String &url, String &result)
326326
{
327-
HTTPClient http;
328-
http.setUserAgent("OpenStreetMap-esp32/1.0 (+https://github.com/CelliesProjects/OpenStreetMap-esp32)");
327+
HTTPClientRAII http;
329328
if (!http.begin(url))
330329
{
331330
result = "Failed to initialize HTTP client";
@@ -335,42 +334,34 @@ std::optional<std::unique_ptr<MemoryBuffer>> OpenStreetMap::urlToBuffer(const St
335334
const int httpCode = http.GET();
336335
if (httpCode != HTTP_CODE_OK)
337336
{
338-
http.end();
339337
result = "HTTP Error: " + String(httpCode);
340338
return std::nullopt;
341339
}
342340

343341
const size_t contentSize = http.getSize();
344342
if (contentSize < 1)
345343
{
346-
http.end();
347344
result = "Empty or chunked response";
348345
return std::nullopt;
349346
}
350347

351348
WiFiClient *stream = http.getStreamPtr();
352349
if (!stream)
353350
{
354-
http.end();
355351
result = "Failed to get HTTP stream";
356352
return std::nullopt;
357353
}
358354

359355
auto buffer = std::make_unique<MemoryBuffer>(contentSize);
360356
if (!buffer->isAllocated())
361357
{
362-
http.end();
363358
result = "Failed to allocate buffer";
364359
return std::nullopt;
365360
}
366361

367362
if (!fillBuffer(stream, *buffer, contentSize, result))
368-
{
369-
http.end();
370363
return std::nullopt;
371-
}
372364

373-
http.end();
374365
result = "Downloaded tile " + url;
375366
return buffer;
376367
}

src/OpenStreetMap-esp32.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "CachedTile.h"
3838
#include "MemoryBuffer.h"
39+
#include "HTTPClientRAII.h"
3940

4041
constexpr uint16_t OSM_TILESIZE = 256;
4142
constexpr uint16_t OSM_TILE_TIMEOUT_MS = 2500;

0 commit comments

Comments
 (0)