Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions doc/allocation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions doc/guarantees.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 5 additions & 0 deletions doc/names.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<webcpp/asio/ssl.hpp>`, which brings OpenSSL
natively, so that `<webcpp/asio.hpp>` leaves it out.
Expand Down
9 changes: 9 additions & 0 deletions doc/sockets.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<asio-invariant-13>>.
Expand Down
28 changes: 16 additions & 12 deletions include/webcpp/asio/ip/basic_resolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ using basic_resolver = ::webcpp::asio_backend::ip::basic_resolver<InternetProtoc
#include WEBCPP_ASIO_BACKEND_HEADER(async_result.hpp) // lint-world: completion token initiation
#include WEBCPP_ASIO_BACKEND_HEADER(error.hpp) // lint-world: resolver error values
#include WEBCPP_ASIO_BACKEND_HEADER(ip/address_v4.hpp) // lint-world: an unspecified address without DNS
#include WEBCPP_ASIO_BACKEND_HEADER(ip/basic_resolver_results.hpp) // lint-world: Boost resolver result entries
#include WEBCPP_ASIO_BACKEND_HEADER(ip/resolver_base.hpp) // lint-world: Boost resolver flags
#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>
Expand All @@ -41,6 +40,7 @@ using basic_resolver = ::webcpp::asio_backend::ip::basic_resolver<InternetProtoc
#include <type_traits>
#include <utility>
#include <webcpp/asio/detail/post_completion.hpp>
#include <webcpp/asio/ip/basic_resolver_results.hpp>

namespace webcpp::asio::ip::detail {
/** Maps an empty, numeric or supported named service to its port. */
Expand Down Expand Up @@ -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<InternetProtocol>;
/** The resolution's one entry, held in place, under the name of Boost's results. */
using results_type = basic_resolver_results<InternetProtocol>;

/**
Constructs a resolver on executor.
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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.
Expand All @@ -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.
Expand Down
171 changes: 163 additions & 8 deletions include/webcpp/asio/ip/basic_resolver_entry.hpp
Original file line number Diff line number Diff line change
@@ -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 <webcpp/asio/config.hpp>
#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 <algorithm>
#include <array>
#include <cassert>
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>

namespace webcpp::asio::ip {
template <class InternetProtocol>
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 InternetProtocol>
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 <class Allocator>
[[nodiscard]] std::basic_string<char, std::char_traits<char>, Allocator> host_name(
const Allocator& allocator = Allocator()) const {
return std::basic_string<char, std::char_traits<char>, 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<char>()); }

/**
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 <class Allocator>
[[nodiscard]] std::basic_string<char, std::char_traits<char>, Allocator> service_name(
const Allocator& allocator = Allocator()) const {
std::basic_string<char, std::char_traits<char>, 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<InternetProtocol>;

/** 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<std::uint8_t>(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<std::uint8_t>(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<std::uint8_t>(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<char, max_host_name_size> host_{};
std::size_t zeros_ = 0;
std::array<char, max_service_size> 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
Loading
Loading