Skip to content

View the HTTP user agent and CA certificate instead of copying them - #45

Merged
kahrendt merged 3 commits into
mainfrom
claude/eloquent-galileo-0d27b9
Dec 4, 2026
Merged

kahrendt merged 3 commits into
mainfrom
claude/eloquent-galileo-0d27b9

Conversation

@kahrendt

@kahrendt kahrendt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

The HTTP reader now points at the user agent and CA certificate held in DecoderSource's config instead of copying them, so building an AudioReader makes no heap allocation.

This came out of the Copilot review on #44. The reader thread copied DecoderConfig::http_user_agent and http_ca_certificate into new strings each time it started. The originals already live for the whole life of the object, so a PEM certificate (often several KB) was held twice. On ESP builds without exceptions, a failed copy aborts. The change was left out of #44; this branch has since merged main with #44 in it.

Design

CStringView (new, src/c_string_view.h, internal) is a pointer and a length. It can only be built from a std::string or a const character array whose last element is a NUL (a string literal), so the text always ends in a NUL. Both consumers need that: esp_http_client_config_t::user_agent / cert_pem and libcurl's CURLOPT_USERAGENT take a C string. CURLOPT_CAINFO_BLOB also needs the length. A plain std::string_view makes no NUL promise. The type offers c_str(), size() and empty(). Building one from a temporary std::string doesn't compile, because the view would dangle. Neither does a writable char array, whose text could change under the view. A const array with no NUL fails to compile in a constant expression and aborts at run time, rather than letting the length scan run off its end. const char*, std::string_view and nullptr are refused too, since none promises a NUL. A default-constructed view is empty and points at "", never nullptr. The constructors are implicit, like std::string_view's, so AudioReader still accepts a std::string or a literal.

HttpRequest::user_agent and ca_certificate are now CStringView. The HttpClient contract already required ca_certificate to stay alive and unchanged while the client exists, because the ESP client keeps a pointer to cert_pem. That rule now covers both fields, and the field types show it.

DecoderSource::Impl passes views of this->config.http_user_agent / http_ca_certificate. This is safe for two reasons, both documented at the declaration and at the call site:

  • config is declared first in Impl, so it is destroyed last, after both worker threads are joined.
  • Impl never changes those strings after construction. The constructor only adjusts ring_buffer_size.

Clients. The ESP client's .c_str() / .empty() calls work unchanged on the view. The host client passes c_str() / size() to CURLOPT_CAINFO_BLOB with CURL_BLOB_COPY, so libcurl copies the certificate during the call. libcurl copies CURLOPT_USERAGENT too.

Tests

  • New reader_views_request_text: building an AudioReader with a 512-byte user agent and an 8 KB CA string changes the sanitizer's live-heap count by 0 bytes. That count is of live bytes, so it shows the reader keeps nothing, not that nothing was briefly allocated. The test also checks at compile time which conversions CStringView accepts and refuses, and that it measures a literal up to its first NUL. Checked that it catches a regression: with a copy of the CA string put back into the constructor, it fails (8224 bytes more). The heap check only runs under ASan or TSan, which both CI test jobs use. Without a sanitizer it only checks that the code builds. It uses the heap_bytes() helper from Add DecoderSource::prepare() to allocate kept resources up front #44.
  • New play_url_user_agent: the test server records request heads. A configured user agent must arrive on every request, including the second playback of a persistent reader, which reuses the view taken when its thread started. An empty one must send no User-Agent header (libcurl's default).
  • reader_sniff_cancel still builds AudioReader from literals, unchanged.
  • All 51 tests pass (including Add DecoderSource::prepare() to allocate kept resources up front #44's and its tests_registered check) with ASan/UBSan (-DENABLE_SANITIZERS=ON -DENABLE_WERROR=ON) and with TSan, run through ctest. decode_benchmark builds for esp32s3 with -Werror. cppcheck is clean apart from a __has_feature preprocessor syntaxError on Add DecoderSource::prepare() to allocate kept resources up front #44's sanitizer block, which my local cppcheck 2.21 reports but CI's doesn't.

Not verified

  • Not run on ESP hardware. The ESP client code is unchanged apart from the field types, and cert_pem already relied on the certificate outliving the client.
  • HTTPS isn't covered by the host tests, which only use plain HTTP. The CURLOPT_CAINFO_BLOB path builds but wasn't run against a TLS server.
  • clang-tidy and the include check couldn't run locally (the Homebrew LLVM toolchain can't find standard headers on this machine), so CI is the first run of both.

Possible follow-ups (out of scope)

Other per-playback heap use on the reader path:

  • Impl::url and request_.url each hold a copy of the URL.
  • The ESP client builds a new host_header_for() string on every open.
  • The ESP client stores each redirect target in location_, then copies it again into target before following it.
  • Both clients reset with response_ = HttpResponse{} on open, which drops content_type's capacity, so the next open allocates it again.

HttpRequest::user_agent and ca_certificate become CStringView, a small
non-owning view that can only be built from a std::string or a string
literal, so its text is always NUL-terminated for esp_http_client and
libcurl. The reader views the strings DecoderSource::Impl::config already
holds, which is declared first so it outlives both worker threads and is
never changed after construction. Building an AudioReader no longer
allocates, and a multi-KB PEM certificate is no longer held twice.
…eo-0d27b9

# Conflicts:
#	tests/CMakeLists.txt
#	tests/test_micro_decoder.cpp
A writable char array could change under the view, and a const array with no NUL let the length scan run off its end. The first no longer compiles; the second fails to compile in a constant expression and aborts at run time. The heap test's comment now says it shows nothing is kept, since the sanitizer counts live bytes.
@kahrendt
kahrendt marked this pull request as ready for review December 4, 2026 20:02
Copilot AI adaptive review requested due to automatic review settings December 4, 2026 20:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the HTTP reader’s switch from copied strings to non-owning views.

No open issues 🎉

What changed in this PR

Reduces HTTP reader memory use by viewing the user agent and CA certificate stored in DecoderSource configuration instead of copying them.

Changes:

  • Adds an internal, NUL-terminated CStringView with construction safeguards.
  • Updates HTTP request fields and documents their lifetime requirements.
  • Adds heap-retention and user-agent tests, including repeated persistent playback.
File Description
tests/​test_micro_decoder.cpp Tests retained heap usage and transmitted user agents.
tests/​CMakeLists.txt Registers both new tests.
src/​platform/​http_client.h Uses views for request text and documents lifetimes.
src/​host/​http_client.cpp Adapts certificate access to the view API.
src/​decoder_source.cpp Documents configuration ownership and view safety.
src/​c_string_view.h Introduces the non-owning, NUL-terminated text view.
src/​audio_reader.h Accepts views and documents their lifetime requirements.
src/​audio_reader.cpp Stores request views instead of owned strings.
CLAUDE.md Adds the new header to the file-layout documentation.

Review effort: Balanced (adaptive)

@kahrendt
kahrendt merged commit 95a661b into main Dec 4, 2026
14 checks passed
@kahrendt
kahrendt deleted the claude/eloquent-galileo-0d27b9 branch December 4, 2026 20:06
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.

2 participants