Skip to content

fix: Refactor URLService::load() to improve HTTP handling - #362

Merged
mverch67 merged 8 commits into
masterfrom
mverch67-URLservice
Aug 5, 2026
Merged

fix: Refactor URLService::load() to improve HTTP handling#362
mverch67 merged 8 commits into
masterfrom
mverch67-URLservice

Conversation

@mverch67

@mverch67 mverch67 commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

This PR fixes some issues when downloading HTTP map tiles:

  • increased default GET file transfer timeouts/retries to avoid blank tiles
  • seperate WiFi download and SD card write which can cause interference
  • added HTTP header info and corrected other legal stuff

Summary by CodeRabbit

Bug Fixes

  • Improved reliability when downloading, decoding, and saving map tiles.
  • Added better handling for connection timeouts, stalled responses, and failed downloads.
  • Improved Wi-Fi power-mode handling during map downloads.
  • Improved behavior when no map tile providers are configured.

Improvements

  • Added clearer OpenStreetMap attribution and hid attribution when it is not applicable.
  • Improved map provider selection and settings persistence.
  • Updated map service requests for more consistent loading.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@mverch67, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 19 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: af3acbaf-1f04-45a8-b06c-2315fd0b67db

📥 Commits

Reviewing files that changed from the base of the PR and between 780dba0 and 596bb26.

📒 Files selected for processing (4)
  • source/graphics/TFT/TFTView_320x240.cpp
  • source/graphics/map/CURLService.cpp
  • source/graphics/map/TileProvider.cpp
  • source/graphics/map/URLService.cpp
📝 Walkthrough

Walkthrough

URLService::load now configures ESP32 Wi-Fi and HTTP requests, reads responses with bounded idle handling, cleans up HTTP resources, decodes PNG data before saving tiles, and updates provider settings and map attribution handling.

Changes

Map provider settings and UI

Layer / File(s) Summary
Provider settings and map UI
include/graphics/map/MapTileSettings.h, source/graphics/map/MapTileSettings.cpp, source/graphics/map/TileProvider.cpp, source/graphics/TFT/TFTView_320x240.cpp, studio/320x240/TFT320x240.eez-project
Provider IDs now use signed 16-bit values with -1 as the default. Map settings store a unique node ID. Empty provider lists and negative provider IDs are handled. Map attribution displays for OpenStreetMap URLs and hides for other non-Google URLs.

HTTP request and tile response handling

Layer / File(s) Summary
HTTP request and bounded response reading
source/graphics/map/URLService.cpp, source/graphics/map/CURLService.cpp
ESP32 Wi-Fi power saving, HTTP headers, user-agent values, TLS timeouts, bounded reads, and explicit HTTP cleanup are configured. The Google Referer header is removed from CURLService.

PNG decoding and tile saving

Layer / File(s) Summary
PNG decoding and tile saving
source/graphics/map/URLService.cpp
PNG data is decoded before tile saving. Tiles are saved only after successful decoding and image assignment. Save results are logged, and successful saves return immediately.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant MapUI
  participant URLService
  participant HTTPClient
  participant PNGDecoder
  participant TileStorage

  MapUI->>URLService: request map tile
  URLService->>HTTPClient: configure request and timeout
  HTTPClient-->>URLService: return bounded PNG response
  URLService->>HTTPClient: end request
  URLService->>PNGDecoder: decode PNG response
  PNGDecoder-->>URLService: return image
  URLService->>TileStorage: save decoded tile
  TileStorage-->>URLService: return save result
Loading

Possibly related PRs

Poem

A rabbit checks each tile with care,
Signed provider IDs guide it there.
PNGs decode before they store,
Attribution marks the map once more.
Wi-Fi rests; requests close clean.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: refactoring URLService::load() to improve HTTP handling.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
source/graphics/map/URLService.cpp (1)

145-162: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider restoring a scope guard for pngImage.

The four exit paths each free pngImage, and the current code is correct. The manual frees replace the previous RAII guard, so any new early return in this function silently leaks the buffer. A small guard removes the four duplicated calls and keeps the free tied to the scope.

♻️ Proposed refactor

Declare the guard right after the allocation succeeds, then delete the individual lv_free(pngImage) calls at lines 145, 150, 157, and 161.

struct LvBufferGuard {
    uint8_t *ptr;
    ~LvBufferGuard()
    {
        if (ptr)
            lv_free(ptr);
    }
};
LvBufferGuard pngGuard{pngImage};
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@source/graphics/map/URLService.cpp` around lines 145 - 162, Restore an RAII
scope guard for the allocated pngImage immediately after successful allocation
in the tile-decoding function, using the shown LvBufferGuard pattern and keeping
ownership tied to the local scope. Remove the individual lv_free(pngImage) calls
from all success and failure exits while preserving their existing return
behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@source/graphics/map/URLService.cpp`:
- Around line 91-98: Validate the result of HTTPClient::getStreamPtr() before
entering the read loop. In the surrounding image-fetch function, handle a null
stream by releasing the already-allocated pngImage and returning through the
existing failure path, preventing dereferences of stream when the connection
drops after http.GET().
- Line 96: Update maxIdleSpins and the nearby idleSpins variable in the
tile-read loop to use an unsigned type capable of representing the full
MUI_MAX_IDLE_SPINS configuration range, avoiding truncation for values above 255
while preserving the existing loop behavior.

---

Nitpick comments:
In `@source/graphics/map/URLService.cpp`:
- Around line 145-162: Restore an RAII scope guard for the allocated pngImage
immediately after successful allocation in the tile-decoding function, using the
shown LvBufferGuard pattern and keeping ownership tied to the local scope.
Remove the individual lv_free(pngImage) calls from all success and failure exits
while preserving their existing return behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 268cc3c8-3f6d-4cf8-90fb-b9780f7576a0

📥 Commits

Reviewing files that changed from the base of the PR and between 95483d8 and 59ce9ea.

📒 Files selected for processing (1)
  • source/graphics/map/URLService.cpp

Comment thread source/graphics/map/URLService.cpp
Comment thread source/graphics/map/URLService.cpp Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@source/graphics/map/TileProvider.cpp`:
- Around line 50-53: Normalize provider selection through the existing
bounds-checked path in both TileProvider::url() overloads, preserving -1 as
unselected instead of converting it to size_t or resetting it to zero; update
source/graphics/map/TileProvider.cpp lines 50-53 and the other url() overload
accordingly. In source/graphics/TFT/TFTView_320x240.cpp lines 2686-2693, only
set the dropdown selection when TileProvider::selectedTemplate() is nonnegative
and below the provider count.

In `@source/graphics/map/URLService.cpp`:
- Around line 92-95: Move the LvFreeGuard for pngImage out of the HTTP scope and
into URLService::load() so it remains active through decoding, saving, and the
function return. Preserve the existing cleanup behavior while ensuring pngImage
is not freed before the later reads.

In `@source/graphics/TFT/TFTView_320x240.cpp`:
- Around line 2795-2803: Update the provider handling around the URL checks so
each recognized-provider branch hides the other provider’s attribution element:
hide map_attribution_label when showing google_logo_image, and hide
google_logo_image when showing the OpenStreetMap label. Preserve the existing
fallback behavior that hides both elements.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7bf3c9ce-d73d-4b07-bd0e-7d8a7d96406f

📥 Commits

Reviewing files that changed from the base of the PR and between 262876e and df9e0dd.

⛔ Files ignored due to path filters (2)
  • generated/ui_320x240/screens.c is excluded by !**/generated/**
  • generated/ui_320x240/screens.h is excluded by !**/generated/**
📒 Files selected for processing (7)
  • include/graphics/map/MapTileSettings.h
  • source/graphics/TFT/TFTView_320x240.cpp
  • source/graphics/map/CURLService.cpp
  • source/graphics/map/MapTileSettings.cpp
  • source/graphics/map/TileProvider.cpp
  • source/graphics/map/URLService.cpp
  • studio/320x240/TFT320x240.eez-project

Comment thread source/graphics/map/TileProvider.cpp
Comment thread source/graphics/map/URLService.cpp Outdated
Comment thread source/graphics/TFT/TFTView_320x240.cpp
@mverch67
mverch67 merged commit 9aa0ff6 into master Aug 5, 2026
6 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant