Skip to content

Commit 9d7d379

Browse files
Use HTTPClient (#115)
1 parent 9f0cbaf commit 9d7d379

File tree

4 files changed

+100
-32
lines changed

4 files changed

+100
-32
lines changed

src/OpenStreetMap-esp32.cpp

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void OpenStreetMap::makeJobList(const tileList &requiredTiles, std::vector<TileJ
250250

251251
void OpenStreetMap::runJobs(const std::vector<TileJob> &jobs)
252252
{
253-
log_i("submitting %i jobs", (int)jobs.size());
253+
log_d("submitting %i jobs", (int)jobs.size());
254254

255255
pendingJobs.store(jobs.size());
256256
for (const TileJob &job : jobs)
@@ -380,9 +380,46 @@ bool OpenStreetMap::fillBuffer(WiFiClient *stream, MemoryBuffer &buffer, size_t
380380
return true;
381381
}
382382

383-
std::unique_ptr<MemoryBuffer> OpenStreetMap::urlToBuffer(const char *url, String &result, ReusableTileFetcher &fetcher)
383+
std::unique_ptr<MemoryBuffer> OpenStreetMap::urlToBuffer(const char *url, String &result)
384384
{
385-
auto buffer = fetcher.fetchToBuffer(url, result);
385+
HTTPClientRAII http;
386+
if (!http.begin(url))
387+
{
388+
result = "Failed to initialize HTTP client";
389+
return nullptr;
390+
}
391+
392+
const int httpCode = http.GET();
393+
if (httpCode != HTTP_CODE_OK)
394+
{
395+
result = "HTTP Error: " + String(httpCode);
396+
return nullptr;
397+
}
398+
399+
const size_t contentSize = http.getSize();
400+
if (contentSize < 1)
401+
{
402+
result = "Empty or chunked response";
403+
return nullptr;
404+
}
405+
406+
WiFiClient *stream = http.getStreamPtr();
407+
if (!stream)
408+
{
409+
result = "Failed to get HTTP stream";
410+
return nullptr;
411+
}
412+
413+
auto buffer = std::make_unique<MemoryBuffer>(contentSize);
414+
if (!buffer->isAllocated())
415+
{
416+
result = "Failed to allocate buffer";
417+
return nullptr;
418+
}
419+
420+
if (!fillBuffer(stream, *buffer, contentSize, result))
421+
return nullptr;
422+
386423
return buffer;
387424
}
388425

@@ -392,7 +429,7 @@ void OpenStreetMap::PNGDraw(PNGDRAW *pDraw)
392429
getPNGCurrentCore()->getLineAsRGB565(pDraw, destRow, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
393430
}
394431

395-
bool OpenStreetMap::fetchTile(ReusableTileFetcher &fetcher, CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result)
432+
bool OpenStreetMap::fetchTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result)
396433
{
397434
String url = currentProvider->urlTemplate;
398435
url.replace("{x}", String(x));
@@ -401,7 +438,7 @@ bool OpenStreetMap::fetchTile(ReusableTileFetcher &fetcher, CachedTile &tile, ui
401438
if (currentProvider->requiresApiKey && strstr(url.c_str(), "{apiKey}"))
402439
url.replace("{apiKey}", currentProvider->apiKey);
403440

404-
const std::unique_ptr<MemoryBuffer> buffer = urlToBuffer(url.c_str(), result, fetcher);
441+
const std::unique_ptr<MemoryBuffer> buffer = urlToBuffer(url.c_str(), result);
405442
if (!buffer)
406443
return false;
407444

@@ -439,7 +476,6 @@ bool OpenStreetMap::fetchTile(ReusableTileFetcher &fetcher, CachedTile &tile, ui
439476
void OpenStreetMap::tileFetcherTask(void *param)
440477
{
441478
OpenStreetMap *osm = static_cast<OpenStreetMap *>(param);
442-
ReusableTileFetcher fetcher;
443479
while (true)
444480
{
445481
TileJob job;
@@ -450,7 +486,7 @@ void OpenStreetMap::tileFetcherTask(void *param)
450486
break;
451487

452488
String result;
453-
if (!osm->fetchTile(fetcher, *job.tile, job.x, job.y, job.z, result))
489+
if (!osm->fetchTile(*job.tile, job.x, job.y, job.z, result))
454490
{
455491
const size_t tileByteCount = osm->currentProvider->tileSize * osm->currentProvider->tileSize * 2;
456492
memset(job.tile->buffer, 0, tileByteCount);

src/OpenStreetMap-esp32.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@
3737
#include "TileJob.hpp"
3838
#include "MemoryBuffer.hpp"
3939
#include "HTTPClientRAII.hpp"
40-
#include "ReusableTileFetcher.hpp"
4140
#include "fonts/DejaVu9-modded.h"
4241

4342
constexpr uint16_t OSM_BGCOLOR = lgfx::color565(32, 32, 128);
44-
constexpr uint16_t OSM_TILE_TIMEOUT_MS = 500;
43+
constexpr uint16_t OSM_TILE_TIMEOUT_MS = 1000;
4544
constexpr UBaseType_t OSM_TASK_PRIORITY = 1;
4645
constexpr uint32_t OSM_TASK_STACKSIZE = 5120;
4746
constexpr uint32_t OSM_JOB_QUEUE_SIZE = 50;
@@ -109,8 +108,8 @@ class OpenStreetMap
109108
void runJobs(const std::vector<TileJob> &jobs);
110109
CachedTile *findUnusedTile(const tileList &requiredTiles, uint8_t zoom);
111110
CachedTile *isTileCached(uint32_t x, uint32_t y, uint8_t z);
112-
bool fetchTile(ReusableTileFetcher &fetcher, CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result);
113-
std::unique_ptr<MemoryBuffer> urlToBuffer(const char *url, String &result, ReusableTileFetcher &fetcher);
111+
std::unique_ptr<MemoryBuffer> urlToBuffer(const char *url, String &result);
112+
bool fetchTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result);
114113
bool fillBuffer(WiFiClient *stream, MemoryBuffer &buffer, size_t contentSize, String &result);
115114
bool composeMap(LGFX_Sprite &mapSprite, TileBufferList &tilePointers);
116115
static void tileFetcherTask(void *param);

src/ReusableTileFetcher.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/*
2+
Copyright (c) 2025 Cellie https://github.com/CelliesProjects/OpenStreetMap-esp32
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
SPDX-License-Identifier: MIT
22+
*/
23+
124
#include "ReusableTileFetcher.hpp"
225

326
ReusableTileFetcher::ReusableTileFetcher() {}
@@ -16,12 +39,7 @@ std::unique_ptr<MemoryBuffer> ReusableTileFetcher::fetchToBuffer(const String &u
1639
if (!ensureConnection(host, port, result))
1740
return nullptr;
1841

19-
if (!sendHttpRequest(host, path))
20-
{
21-
result = "Failed to send HTTP GET request";
22-
return nullptr;
23-
}
24-
42+
sendHttpRequest(host, path);
2543
size_t contentLength = 0;
2644
if (!readHttpHeaders(contentLength, result))
2745
return nullptr;
@@ -42,12 +60,8 @@ std::unique_ptr<MemoryBuffer> ReusableTileFetcher::fetchToBuffer(const String &u
4260
bool ReusableTileFetcher::parseUrl(const String &url, String &host, String &path, uint16_t &port)
4361
{
4462
port = 80;
45-
4663
if (url.startsWith("https://"))
47-
{
48-
// Not supported in this fetcher (requires WiFiClientSecure)
4964
return false;
50-
}
5165

5266
if (!url.startsWith("http://"))
5367
return false;
@@ -78,14 +92,13 @@ bool ReusableTileFetcher::ensureConnection(const String &host, uint16_t port, St
7892
return true;
7993
}
8094

81-
bool ReusableTileFetcher::sendHttpRequest(const String &host, const String &path)
95+
void ReusableTileFetcher::sendHttpRequest(const String &host, const String &path)
8296
{
8397
client.print(String("GET ") + path + " HTTP/1.1\r\n");
8498
client.print(String("Host: ") + host + "\r\n");
8599
client.print("User-Agent: OpenStreetMap-esp32/1.0 (+https://github.com/CelliesProjects/OpenStreetMap-esp32)\r\n");
86100
client.print("Connection: keep-alive\r\n");
87101
client.print("\r\n");
88-
return true;
89102
}
90103

91104
bool ReusableTileFetcher::readHttpHeaders(size_t &contentLength, String &result)
@@ -100,9 +113,8 @@ bool ReusableTileFetcher::readHttpHeaders(size_t &contentLength, String &result)
100113
break; // End of headers
101114

102115
if (line.startsWith("Content-Length:"))
103-
{
104116
contentLength = line.substring(15).toInt();
105-
}
117+
106118
else if (line.startsWith("HTTP/1.1"))
107119
{
108120
if (!line.startsWith("HTTP/1.1 200"))
@@ -143,9 +155,7 @@ bool ReusableTileFetcher::readBody(MemoryBuffer &buffer, size_t contentLength, S
143155
return false;
144156
}
145157
else
146-
{
147-
delay(1);
148-
}
158+
taskYIELD();
149159
}
150160

151161
if (remaining > 0)

src/ReusableTileFetcher.hpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1+
/*
2+
Copyright (c) 2025 Cellie https://github.com/CelliesProjects/OpenStreetMap-esp32
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
SPDX-License-Identifier: MIT
22+
*/
23+
124
#pragma once
225

326
#include <WiFiClient.h>
427
#include <memory>
5-
#include "MemoryBuffer.hpp" // your existing class
28+
#include "MemoryBuffer.hpp"
629

7-
class ReusableTileFetcher {
30+
class ReusableTileFetcher
31+
{
832
public:
933
ReusableTileFetcher();
1034
~ReusableTileFetcher();
1135

12-
// Not copyable/movable
1336
ReusableTileFetcher(const ReusableTileFetcher &) = delete;
1437
ReusableTileFetcher &operator=(const ReusableTileFetcher &) = delete;
1538

@@ -22,7 +45,7 @@ class ReusableTileFetcher {
2245

2346
bool parseUrl(const String &url, String &host, String &path, uint16_t &port);
2447
bool ensureConnection(const String &host, uint16_t port, String &result);
25-
bool sendHttpRequest(const String &host, const String &path);
48+
void sendHttpRequest(const String &host, const String &path);
2649
bool readHttpHeaders(size_t &contentLength, String &result);
2750
bool readBody(MemoryBuffer &buffer, size_t contentLength, String &result);
2851
};

0 commit comments

Comments
 (0)