Repository navigation
View the HTTP user agent and CA certificate instead of copying them - #45
Merged
Merged
Conversation
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.
There was a problem hiding this comment.
🟢 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
CStringViewwith 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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The HTTP reader now points at the user agent and CA certificate held in
DecoderSource's config instead of copying them, so building anAudioReadermakes no heap allocation.This came out of the Copilot review on #44. The reader thread copied
DecoderConfig::http_user_agentandhttp_ca_certificateinto 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 mergedmainwith #44 in it.Design
CStringView(new,src/c_string_view.h, internal) is a pointer and a length. It can only be built from astd::stringor 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_pemand libcurl'sCURLOPT_USERAGENTtake a C string.CURLOPT_CAINFO_BLOBalso needs the length. A plainstd::string_viewmakes no NUL promise. The type offersc_str(),size()andempty(). Building one from a temporarystd::stringdoesn't compile, because the view would dangle. Neither does a writablechararray, 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_viewandnullptrare refused too, since none promises a NUL. A default-constructed view is empty and points at"", nevernullptr. The constructors are implicit, likestd::string_view's, soAudioReaderstill accepts astd::stringor a literal.HttpRequest::user_agentandca_certificateare nowCStringView. TheHttpClientcontract already requiredca_certificateto stay alive and unchanged while the client exists, because the ESP client keeps a pointer tocert_pem. That rule now covers both fields, and the field types show it.DecoderSource::Implpasses views ofthis->config.http_user_agent/http_ca_certificate. This is safe for two reasons, both documented at the declaration and at the call site:configis declared first inImpl, so it is destroyed last, after both worker threads are joined.Implnever changes those strings after construction. The constructor only adjustsring_buffer_size.Clients. The ESP client's
.c_str()/.empty()calls work unchanged on the view. The host client passesc_str()/size()toCURLOPT_CAINFO_BLOBwithCURL_BLOB_COPY, so libcurl copies the certificate during the call. libcurl copiesCURLOPT_USERAGENTtoo.Tests
reader_views_request_text: building anAudioReaderwith 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 conversionsCStringViewaccepts 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 theheap_bytes()helper from Add DecoderSource::prepare() to allocate kept resources up front #44.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 noUser-Agentheader (libcurl's default).reader_sniff_cancelstill buildsAudioReaderfrom literals, unchanged.tests_registeredcheck) with ASan/UBSan (-DENABLE_SANITIZERS=ON -DENABLE_WERROR=ON) and with TSan, run throughctest.decode_benchmarkbuilds for esp32s3 with-Werror.cppcheckis clean apart from a__has_featurepreprocessorsyntaxErroron 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
cert_pemalready relied on the certificate outliving the client.CURLOPT_CAINFO_BLOBpath builds but wasn't run against a TLS server.Possible follow-ups (out of scope)
Other per-playback heap use on the reader path:
Impl::urlandrequest_.urleach hold a copy of the URL.host_header_for()string on every open.location_, then copies it again intotargetbefore following it.response_ = HttpResponse{}on open, which dropscontent_type's capacity, so the next open allocates it again.