diff --git a/doc/allocation.adoc b/doc/allocation.adoc index 447b9a8..47e8b5d 100644 --- a/doc/allocation.adoc +++ b/doc/allocation.adoc @@ -67,8 +67,10 @@ operation's associated allocator: above. A macOS allocation result does not validate the Windows backend. * Emscripten's `select` allocates its poll array with `calloc` on each call. * Asyncify allocates its unwind data when a call suspends. -* Boost.Asio's `basic_resolver_results` allocates its result entries with - `new`. The measured operation scenario above does not resolve a name. +* Boost.Asio's `basic_resolver_results`, natively and on WASI, allocates its + result entries with `new`. The measured operation scenario above does not + resolve a name. A page's results hold their one entry in place and allocate + nothing, which the browser allocation test measures. Forward asynchronous resolution owns copies of its input names through the initiating token's associated allocator, preserving temporary input lifetimes. Bind the allocator when creating a deferred operation; binding one only when diff --git a/doc/changelog.adoc b/doc/changelog.adoc index 507ca50..e4ea6ca 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -14,6 +14,7 @@ Changes prepared for the first WebCPP release. === Unreleased // tag::unreleased[] +* *Breaking change:* Changed a page's resolver results into webcpp's own, holding their one entry in place and allocating nothing; a host over 254 bytes reports `host_not_found`. * Fixed three diagnostic tests whose expected error clang's `-verify` never read, so they checked nothing. * *Breaking change:* Removed `/webcpp/asio//cobalt` and `cobalt_ssl`; link `/webcpp/cobalt//cobalt` or `cobalt_ssl`. * Fixed browser turn budgets across timer waits so worker proxy messages can run. diff --git a/doc/guarantees.adoc b/doc/guarantees.adoc index 5e51d97..b4ac578 100644 --- a/doc/guarantees.adoc +++ b/doc/guarantees.adoc @@ -71,6 +71,8 @@ these guarantees describe their behaviour together. === 13. Resolver entries preserve host and service On Emscripten, forward resolution returns one entry containing the original -host and service, an unspecified address and the service port. A plain socket -cannot connect to it. `resolver_test.cpp` checks service parsing and posted -completion, including failures. +host and service, an unspecified address and the service port, held in the +results, which allocate nothing. A plain socket cannot connect to it. +`resolver_test.cpp` checks service parsing, the results' interface and posted +completion, including failures; `allocation_test.cpp` checks that resolving, +copying and posting the results allocate nothing. diff --git a/doc/names.adoc b/doc/names.adoc index 8f4c9fd..eb0b199 100644 --- a/doc/names.adoc +++ b/doc/names.adoc @@ -46,6 +46,11 @@ the shipped implementation, with browser-specific extensions where required: transport provides it, and the resolver completes with the entries the browser streams of webcpp's beast library accept. +| `ip::basic_resolver_results`, `ip::basic_resolver_entry` +| Aliases of Boost's. +| Class templates of asio's with Boost's interfaces: the results hold their + one entry in place, and allocate nothing. + | `ssl`: `context`, `stream_base`, `verify_*`, `host_name_verification` | Aliases of Boost's, in ``, which brings OpenSSL natively, so that `` leaves it out. diff --git a/doc/sockets.adoc b/doc/sockets.adoc index d8e4718..66e4e61 100644 --- a/doc/sockets.adoc +++ b/doc/sockets.adoc @@ -70,9 +70,18 @@ and service, an unspecified IPv4 address and the service port. This is not a DNS lookup. A browser stream can retain the name without rewriting a client's resolve step. A plain socket still refuses connecting to this entry. +The results hold that entry in place, so that resolving, copying and posting +them allocate nothing. Their type is the page's own, under Boost's names: +`ip::basic_resolver_results`, a forward range of at most one +`ip::basic_resolver_entry`, with Boost's members, whose names are returned as +copies, in an allocator's string if one is given. Two results are equal when +their entries are. A host longer than 254 bytes, DNS's 253 and a final dot, +names nothing DNS would find, and reports `host_not_found`. + An empty service means port 0. Decimal services accept 0 through 65535; `http` and `ws` select 80, and `https` and `wss` select 443. Named services are refused with `numeric_service`. Other services report `service_not_found`. +A decimal service keeps its leading zeros, however many. Reverse resolution reports `operation_not_supported`. Async completion is posted; cancel does not retract a result already computed at initiation. See <>. diff --git a/include/webcpp/asio/ip/basic_resolver.hpp b/include/webcpp/asio/ip/basic_resolver.hpp index 6849bc6..ed0aa3a 100644 --- a/include/webcpp/asio/ip/basic_resolver.hpp +++ b/include/webcpp/asio/ip/basic_resolver.hpp @@ -28,7 +28,6 @@ using basic_resolver = ::webcpp::asio_backend::ip::basic_resolver #include @@ -41,6 +40,7 @@ using basic_resolver = ::webcpp::asio_backend::ip::basic_resolver #include #include +#include namespace webcpp::asio::ip::detail { /** Maps an empty, numeric or supported named service to its port. */ @@ -89,8 +89,8 @@ class basic_resolver : public ::webcpp::asio_backend::ip::resolver_base { using protocol_type = InternetProtocol; /** The protocol endpoint type. */ using endpoint_type = InternetProtocol::endpoint; - /** Boost's sequence of resolver entries. */ - using results_type = ::webcpp::asio_backend::ip::basic_resolver_results; + /** The resolution's one entry, held in place, under the name of Boost's results. */ + using results_type = basic_resolver_results; /** Constructs a resolver on executor. @@ -120,9 +120,9 @@ class basic_resolver : public ::webcpp::asio_backend::ip::resolver_base { /** Constructs an entry retaining host and service, with an unspecified address. - @param host The host name retained verbatim. + @param host The host name retained verbatim, of at most 254 bytes. @param service An empty, numeric or supported named service. - @param ec Receives success or service_not_found. + @param ec Receives success, service_not_found, or host_not_found for a longer host. @return One entry on success, otherwise an empty sequence. */ results_type resolve(std::string_view host, std::string_view service, @@ -132,10 +132,10 @@ class basic_resolver : public ::webcpp::asio_backend::ip::resolver_base { /** Constructs a forward result under the service parsing flags. - @param host The host name retained verbatim. + @param host The host name retained verbatim, of at most 254 bytes: DNS's 253 and a final dot. @param service The service retained verbatim. @param resolve_flags Only numeric_service affects parsing. - @param ec Receives success or service_not_found. + @param ec Receives success, service_not_found, or host_not_found for a longer host. @return One entry on success, otherwise an empty sequence. */ // Preserve Boost's non-static member interface. @@ -147,10 +147,14 @@ class basic_resolver : public ::webcpp::asio_backend::ip::resolver_base { ec = ::webcpp::asio_backend::error::service_not_found; return {}; } + if (host.size() > results_type::value_type::max_host_name_size) { + // No name DNS knows is longer; the results hold the host in place. + ec = ::webcpp::asio_backend::error::host_not_found; + return {}; + } ec.clear(); - return results_type::create( - endpoint_type(::webcpp::asio_backend::ip::address_v4::any(), *port), std::string(host), - std::string(service)); + return results_type(endpoint_type(::webcpp::asio_backend::ip::address_v4::any(), *port), + host, service); } /** @@ -166,7 +170,7 @@ class basic_resolver : public ::webcpp::asio_backend::ip::resolver_base { } #ifndef WEBCPP_ASIO_NO_EXCEPTIONS /** - Constructs a forward result, throwing on an unsupported service. + Constructs a forward result, throwing on an unsupported service or a longer host. @param host The original host. @param service The original service. @return One resolver entry. @@ -176,7 +180,7 @@ class basic_resolver : public ::webcpp::asio_backend::ip::resolver_base { } /** - Constructs a forward result with flags, throwing on an unsupported service. + Constructs a forward result with flags, throwing on an unsupported service or a longer host. @param host The original host. @param service The original service. @param resolve_flags The service parsing flags. diff --git a/include/webcpp/asio/ip/basic_resolver_entry.hpp b/include/webcpp/asio/ip/basic_resolver_entry.hpp index 3b9a12a..9480234 100644 --- a/include/webcpp/asio/ip/basic_resolver_entry.hpp +++ b/include/webcpp/asio/ip/basic_resolver_entry.hpp @@ -1,12 +1,167 @@ // Copyright (c) 2026 WebCpp.org -// Distributed under the Boost Software License, Version 1.0. -// See accompanying file LICENSE_1_0.txt or https://www.boost.org/LICENSE_1_0.txt. -// Generated by upstream/sync.py; do not edit. +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) -#ifndef WEBCPP_ASIO_FORWARD_IP_BASIC_RESOLVER_ENTRY_HPP -#define WEBCPP_ASIO_FORWARD_IP_BASIC_RESOLVER_ENTRY_HPP +#ifndef WEBCPP_ASIO_IP_BASIC_RESOLVER_ENTRY_HPP +#define WEBCPP_ASIO_IP_BASIC_RESOLVER_ENTRY_HPP #include -#include WEBCPP_ASIO_BACKEND_HEADER(ip/basic_resolver_entry.hpp) -namespace webcpp::asio::ip { using namespace ::webcpp::asio_backend::ip; } -#endif // WEBCPP_ASIO_FORWARD_IP_BASIC_RESOLVER_ENTRY_HPP +#ifndef WEBCPP_TARGET_EMSCRIPTEN +#include WEBCPP_ASIO_BACKEND_HEADER(ip/basic_resolver_entry.hpp) // lint-world: native resolver entries + +namespace webcpp::asio::ip { +using namespace ::webcpp::asio_backend::ip; +} // namespace webcpp::asio::ip +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace webcpp::asio::ip { +template +class basic_resolver_results; + +/** + An entry of a page's resolution: an endpoint, and the host and service names it was resolved + from, which the entry holds in place. + + A page looks up no name: its resolver keeps the host and the service for the page's transport, + which connects to them, with an unspecified address. The entry allocates nothing, nor do the + results that hold it: the host is at most `max_host_name_size` bytes, and the service, empty, + one of the names the resolver knows or digits, is kept as the digits of its port and the count + of its leading zeros, which give it back as it was. + @tparam InternetProtocol The internet protocol. + @see basic_resolver_results, which holds the resolution's one entry. +*/ +template +class basic_resolver_entry { +public: + /** The internet protocol type. */ + using protocol_type = InternetProtocol; + /** The protocol endpoint type. */ + using endpoint_type = InternetProtocol::endpoint; + + /** The longest host an entry holds: a name of DNS's 253 bytes, and its final dot. */ + static constexpr std::size_t max_host_name_size = 254; + + /** Constructs an entry with an unspecified endpoint and empty names. */ + basic_resolver_entry() noexcept = default; + + /** + Returns the endpoint. + @return The endpoint, whose address is unspecified. + */ + [[nodiscard]] endpoint_type endpoint() const noexcept { return endpoint_; } + + /** + Converts to the endpoint. + @return The endpoint, whose address is unspecified. + */ + // Boost's entry converts implicitly, which the range algorithms rely on. + // NOLINTNEXTLINE(misc-explicit-constructor) + operator endpoint_type() const noexcept { return endpoint_; } + + /** + Returns the host name as it was resolved. + @return A copy of the host name. + */ + [[nodiscard]] std::string host_name() const { return std::string(host_view()); } + + /** + Returns the host name as it was resolved, in an allocator's string. + @tparam Allocator The string's allocator type. + @param allocator The allocator of the returned string. + @return A copy of the host name. + */ + template + [[nodiscard]] std::basic_string, Allocator> host_name( + const Allocator& allocator = Allocator()) const { + return std::basic_string, Allocator>(host_view(), allocator); + } + + /** + Returns the service name as it was resolved. + @return A copy of the service name. + */ + [[nodiscard]] std::string service_name() const { return service_name(std::allocator()); } + + /** + Returns the service name as it was resolved, in an allocator's string. + @tparam Allocator The string's allocator type. + @param allocator The allocator of the returned string. + @return A copy of the service name. + */ + template + [[nodiscard]] std::basic_string, Allocator> service_name( + const Allocator& allocator = Allocator()) const { + std::basic_string, Allocator> name(allocator); + name.reserve(zeros_ + service_size_); + name.append(zeros_, '0'); + name.append(service_.data(), service_size_); + return name; + } + + /** + Compares two entries: their endpoints, and their names as they were resolved. + @param a The first entry. + @param b The second entry. + @return Whether they are equal. + */ + friend bool operator==(const basic_resolver_entry& a, const basic_resolver_entry& b) noexcept { + return a.endpoint_ == b.endpoint_ && a.host_view() == b.host_view() && + a.zeros_ == b.zeros_ && a.service_view() == b.service_view(); + } + +private: + friend class basic_resolver_results; + + /** The longest service kept as it is: `https`, or the five digits of a port. */ + static constexpr std::size_t max_service_size = 5; + + /** + Constructs an entry from an endpoint, a host of at most `max_host_name_size` bytes, and a + service that is empty, one of the resolver's names, or digits whose value is the port. + */ + basic_resolver_entry(const endpoint_type& endpoint, std::string_view host, + std::string_view service) noexcept + : endpoint_(endpoint), host_size_(static_cast(host.size())) { + assert(host.size() <= max_host_name_size); + std::ranges::copy(host, host_.begin()); + if (!service.empty() && service.find_first_not_of("0123456789") == std::string_view::npos) { + // Digits: those of the port, after the zeros that lead them. + const auto converted = + std::to_chars(service_.data(), service_.data() + service_.size(), endpoint.port()); + service_size_ = static_cast(converted.ptr - service_.data()); + assert(service.size() >= service_size_); + zeros_ = service.size() - service_size_; + } else { + assert(service.size() <= max_service_size); + std::ranges::copy(service, service_.begin()); + service_size_ = static_cast(service.size()); + } + } + + [[nodiscard]] std::string_view host_view() const noexcept { return {host_.data(), host_size_}; } + + [[nodiscard]] std::string_view service_view() const noexcept { + return {service_.data(), service_size_}; + } + + endpoint_type endpoint_{}; + std::array host_{}; + std::size_t zeros_ = 0; + std::array service_{}; + std::uint8_t host_size_ = 0; + std::uint8_t service_size_ = 0; +}; +} // namespace webcpp::asio::ip +#endif +#endif // WEBCPP_ASIO_IP_BASIC_RESOLVER_ENTRY_HPP diff --git a/include/webcpp/asio/ip/basic_resolver_results.hpp b/include/webcpp/asio/ip/basic_resolver_results.hpp index e96aba7..736d750 100644 --- a/include/webcpp/asio/ip/basic_resolver_results.hpp +++ b/include/webcpp/asio/ip/basic_resolver_results.hpp @@ -1,12 +1,141 @@ // Copyright (c) 2026 WebCpp.org -// Distributed under the Boost Software License, Version 1.0. -// See accompanying file LICENSE_1_0.txt or https://www.boost.org/LICENSE_1_0.txt. -// Generated by upstream/sync.py; do not edit. +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) -#ifndef WEBCPP_ASIO_FORWARD_IP_BASIC_RESOLVER_RESULTS_HPP -#define WEBCPP_ASIO_FORWARD_IP_BASIC_RESOLVER_RESULTS_HPP +#ifndef WEBCPP_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP +#define WEBCPP_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP #include -#include WEBCPP_ASIO_BACKEND_HEADER(ip/basic_resolver_results.hpp) -namespace webcpp::asio::ip { using namespace ::webcpp::asio_backend::ip; } -#endif // WEBCPP_ASIO_FORWARD_IP_BASIC_RESOLVER_RESULTS_HPP +#ifndef WEBCPP_TARGET_EMSCRIPTEN +#include WEBCPP_ASIO_BACKEND_HEADER(ip/basic_resolver_results.hpp) // lint-world: native resolver results + +namespace webcpp::asio::ip { +using namespace ::webcpp::asio_backend::ip; +} // namespace webcpp::asio::ip +#else +#include +#include +#include +#include + +namespace webcpp::asio::ip { +template +class basic_resolver; + +/** + The results of a page's resolution: none, or the one entry it makes, held in place. + + A page's resolver keeps a host and a service for the page's transport and looks up no name, so + that it makes at most one entry, which these results hold in the object itself: they allocate + nothing when they are made, copied or posted with a completion, unlike Boost's results, which + allocate their list of entries and its shared count. They are a forward range of + `basic_resolver_entry`, as Boost's are; two results are equal when they hold equal entries. + @tparam InternetProtocol The internet protocol. + @see "Sockets and transports", in the guide. +*/ +template +class basic_resolver_results { +public: + /** The internet protocol type. */ + using protocol_type = InternetProtocol; + /** The protocol endpoint type. */ + using endpoint_type = InternetProtocol::endpoint; + /** The entry type. */ + using value_type = basic_resolver_entry; + /** A reference to a constant entry. */ + using const_reference = const value_type&; + /** A reference to an entry. */ + using reference = value_type&; + /** An iterator over the constant entries. */ + using const_iterator = const value_type*; + /** An iterator over the entries, which are constant. */ + using iterator = const_iterator; + /** The iterators' difference type. */ + using difference_type = std::ptrdiff_t; + /** The size type. */ + using size_type = std::size_t; + + /** Constructs empty results. */ + basic_resolver_results() noexcept = default; + + /** + Returns the number of entries. + @return One, or zero for empty results. + */ + [[nodiscard]] size_type size() const noexcept { return size_; } + + /** + Returns the greatest number of entries results may hold. + @return One. + */ + // Boost's results expose max_size as a non-static member. + // NOLINTNEXTLINE(readability-convert-member-functions-to-static) + [[nodiscard]] size_type max_size() const noexcept { return 1; } + + /** + Returns whether the results hold no entry. + @return Whether they are empty. + */ + [[nodiscard]] bool empty() const noexcept { return size_ == 0; } + + /** + Returns an iterator to the first entry. + @return The iterator, equal to `end()` for empty results. + */ + [[nodiscard]] const_iterator begin() const noexcept { return &entry_; } + + /** + Returns an iterator past the last entry. + @return The iterator. + */ + [[nodiscard]] const_iterator end() const noexcept { return &entry_ + size_; } + + /** + Returns an iterator to the first entry. + @return The iterator, equal to `cend()` for empty results. + */ + [[nodiscard]] const_iterator cbegin() const noexcept { return begin(); } + + /** + Returns an iterator past the last entry. + @return The iterator. + */ + [[nodiscard]] const_iterator cend() const noexcept { return end(); } + + /** + Exchanges the entries of two results. + @param that The other results. + */ + void swap(basic_resolver_results& that) noexcept { + std::swap(entry_, that.entry_); + std::swap(size_, that.size_); + } + + /** + Compares two results by their entries. + @param a The first results. + @param b The second results. + @return Whether both are empty, or both hold equal entries. + */ + friend bool operator==(const basic_resolver_results& a, + const basic_resolver_results& b) noexcept { + return a.size_ == b.size_ && (a.size_ == 0 || a.entry_ == b.entry_); + } + +private: + template + friend class basic_resolver; + + /** Constructs the results of one entry, whose names the resolver has checked. */ + basic_resolver_results(const endpoint_type& endpoint, std::string_view host, + std::string_view service) noexcept + : entry_(endpoint, host, service), size_(1) {} + + value_type entry_{}; + size_type size_ = 0; +}; +} // namespace webcpp::asio::ip +#endif +#endif // WEBCPP_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP diff --git a/test/allocation_test.cpp b/test/allocation_test.cpp index a5e5d43..2e6a28f 100644 --- a/test/allocation_test.cpp +++ b/test/allocation_test.cpp @@ -645,7 +645,7 @@ void resolver_inputs_use_the_associated_allocator() { webcpp::asio::bind_allocator(counted(&given), webcpp::asio::deferred)); }(); report("resolver deferred captures", given - before); - // No result entry exists yet: Boost's result storage would allocate globally. + // No result entry exists yet: the deferred operation holds its copied inputs alone. BOOST_TEST_GT(given, before); BOOST_TEST_EQ(operator_new_calls, 0U); BOOST_TEST_EQ(c_allocations, 0U); @@ -669,6 +669,50 @@ void resolver_inputs_use_the_associated_allocator() { BOOST_TEST_EQ(completions, 2U); } +/** A page's results hold their entry: resolving, copying and posting them allocate nothing. */ +void page_resolver_results_allocate_nothing() { + using resolver = webcpp::asio::ip::tcp::resolver; + const std::string host = std::string(253, 'h') + '.'; + const std::string service = std::string(100, '0') + "443"; + webcpp::asio::io_context io; + resolver names(io); + bool correct = false; + { + const allocation_window window; + boost::system::error_code ec; + const auto results = names.resolve(host, service, ec); + const auto copy = results; + correct = + !ec && copy == results && copy.size() == 1 && copy.begin()->endpoint().port() == 443; + } + report("page resolver results"); + BOOST_TEST(correct); + BOOST_TEST_EQ(operator_new_calls, 0U); + BOOST_TEST_EQ(c_allocations, 0U); + BOOST_TEST_EQ(aligned_allocations, 0U); + + // Posted, the results travel in the handler, whose storage is the bound allocator's. + std::size_t given = 0; + webcpp::asio::io_context counted_io(std::allocator_arg, counted(&given)); + resolver posted(counted_io); + correct = false; + { + const allocation_window window; + auto check = [&](boost::system::error_code ec, const resolver::results_type& results) { + correct = !ec && results.size() == 1 && results.begin()->endpoint().port() == 443; + }; + posted.async_resolve(host, service, + webcpp::asio::bind_allocator(counted(&given), check)); + counted_io.run(); // lint-run: observes the posted results inside the window + } + report("page resolver posted results", given); + BOOST_TEST(correct); + BOOST_TEST_GT(given, 0U); + BOOST_TEST_EQ(operator_new_calls, 0U); + BOOST_TEST_EQ(c_allocations, 0U); + BOOST_TEST_EQ(aligned_allocations, 0U); +} + /** Each delivered event posts through its associated allocator, never runs inline. */ class counted_registration final : public webcpp::asio::browser::registration { public: @@ -1068,6 +1112,7 @@ int main() { #ifdef __EMSCRIPTEN__ tls_host_storage_uses_the_resource(); resolver_inputs_use_the_associated_allocator(); + page_resolver_results_allocate_nothing(); #endif the_counters_see_an_unbound_handler(); #if defined(WEBCPP_TEST_ASIO_COUNTS_C_ALLOCATIONS) && defined(__APPLE__) && !defined(__EMSCRIPTEN__) diff --git a/test/resolver_test.cpp b/test/resolver_test.cpp index 67d9ba4..713b8e5 100644 --- a/test/resolver_test.cpp +++ b/test/resolver_test.cpp @@ -7,12 +7,19 @@ #include #include #include +#include #include +#include +#include +#include #include #include +#include #include #include #include +#include +#include #include #include #include @@ -135,10 +142,93 @@ void asynchronous_results() { io.run(); // lint-run: the resolver test drains posted completions BOOST_TEST_EQ(calls, 4U); } + +/** The page's results hold their one entry, under the names Boost gives the native ones. */ +void results_hold_their_entry() { + using results_type = resolver::results_type; + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::forward_iterator); + static_assert(std::ranges::forward_range); + static_assert(std::is_nothrow_copy_constructible_v); + webcpp::asio::io_context io; + resolver r(io); + const results_type none; + BOOST_TEST(none.empty()); + BOOST_TEST_EQ(none.size(), 0U); + BOOST_TEST_EQ(none.max_size(), 1U); + BOOST_TEST(none.begin() == none.end()); + + // The longest name DNS has, 253 bytes and the final dot, and a service of any length. + boost::system::error_code ec; + const std::string longest = std::string(253, 'a') + '.'; + const std::string padded = std::string(100, '0') + "443"; + auto results = r.resolve(longest, padded, ec); + BOOST_TEST(!ec); + check_entry(results, longest, padded, 443); + check_entry(r.resolve("h", "000", ec), "h", "000", 0); + BOOST_TEST(!ec); + std::size_t visited = 0; + for (const auto& entry : results) { + const webcpp::asio::ip::tcp::endpoint endpoint = entry; + BOOST_TEST_EQ(endpoint.port(), 443); + ++visited; + } + BOOST_TEST_EQ(visited, 1U); + BOOST_TEST_EQ(std::distance(results.cbegin(), results.cend()), 1); + + // A copy is its own: equal to its source, and unchanged when the source changes. + auto copy = results; + BOOST_TEST(copy == results); + results = r.resolve("other.example", "80", ec); + BOOST_TEST(copy != results); + check_entry(copy, longest, padded, 443); + auto moved = std::move(copy); + check_entry(moved, longest, padded, 443); + moved.swap(results); + check_entry(moved, "other.example", "80", 80); + check_entry(results, longest, padded, 443); + + // The names are copied into the caller's allocator. + std::array storage{}; + std::pmr::monotonic_buffer_resource arena(storage.data(), storage.size(), + std::pmr::null_memory_resource()); + const std::pmr::polymorphic_allocator allocator(&arena); + BOOST_TEST(results.begin()->host_name(allocator) == std::string_view(longest)); + BOOST_TEST(results.begin()->service_name(allocator) == std::string_view(padded)); + + // A longer host is no name DNS would find, whichever way it is resolved. + const std::string too_long = longest + 'a'; + BOOST_TEST(r.resolve(too_long, "80", ec).empty()); + BOOST_TEST(ec == webcpp::asio::error::host_not_found); + unsigned calls = 0; + r.async_resolve(too_long, "80", + [&](boost::system::error_code posted, const results_type& found) { + BOOST_TEST(posted == webcpp::asio::error::host_not_found); + BOOST_TEST(found.empty()); + ++calls; + }); + io.run(); // lint-run: the resolver test drains the posted refusal + BOOST_TEST_EQ(calls, 1U); +#ifndef WEBCPP_ASIO_NO_EXCEPTIONS + bool caught = false; + try { + (void)r.resolve(too_long, "80"); + } catch (const boost::system::system_error& error) { + caught = error.code() == webcpp::asio::error::host_not_found; + } + BOOST_TEST(caught); +#endif +} } // namespace int main() { services_and_errors(); asynchronous_results(); + results_hold_their_entry(); return webcpp::test::exit_status(boost::report_errors()); } diff --git a/upstream/generated.json b/upstream/generated.json index ee63bfe..a196ab4 100644 --- a/upstream/generated.json +++ b/upstream/generated.json @@ -753,10 +753,8 @@ "include/webcpp/asio/ip/address_v6_range.hpp": "5038e000bb0f25d2cdbda8f3ec50af7cc9f4e2d57a840021bda8109cb2f3ecdf", "include/webcpp/asio/ip/bad_address_cast.hpp": "87e0568c10c5e36580785c92da51de543abb8f9a70f27562c15a1704839365a9", "include/webcpp/asio/ip/basic_endpoint.hpp": "79fb2096365e1c377d8dad00ddcb7eb89b7ab497a80d3df4d2864d604345fb5f", - "include/webcpp/asio/ip/basic_resolver_entry.hpp": "051e8a0ffd025e1ec5df491735e3db9596fc49b5f8b20a1f10dc1f0f387781d1", "include/webcpp/asio/ip/basic_resolver_iterator.hpp": "3569e1e08a4164405f70885adba4deb4a64fe1f6b63375f70e1f0ecb8cdcc3ce", "include/webcpp/asio/ip/basic_resolver_query.hpp": "bc4843c0c7aab1c1e3208d90f1ebe67ee1b94e96229ebaa6512e58b52e9f6a70", - "include/webcpp/asio/ip/basic_resolver_results.hpp": "75be69bae8cc6985a0e94903e4db26cce726cfd34edbba4f347554451d23ec5b", "include/webcpp/asio/ip/host_name.hpp": "217bcaac8e120801f9ff4c6b4e72b48ee1ec84313721c40b0a0c1de17603457d", "include/webcpp/asio/ip/icmp.hpp": "0eaf6c7656680ea0200309b30fe69b7122602dbf8ab7cdf2a5d63bb939770413", "include/webcpp/asio/ip/multicast.hpp": "f3b60697ab363ae6dec2f14dbd555c170e43b5f651b701a53fe146306367ca66", diff --git a/upstream/pin.json b/upstream/pin.json index 6fc9b01..f7103db 100644 --- a/upstream/pin.json +++ b/upstream/pin.json @@ -652,6 +652,8 @@ "detail/wasi_pollable_descriptor.hpp", "io_context.hpp", "ip/basic_resolver.hpp", + "ip/basic_resolver_entry.hpp", + "ip/basic_resolver_results.hpp", "ip/tcp.hpp", "ip/udp.hpp", "ssl.hpp",