Commit a5aa1dac authored by Dominik Charousset's avatar Dominik Charousset

Store hosts as IP address when parsing URIs

parent 72a16f8f
......@@ -170,6 +170,9 @@ is based on [Keep a Changelog](https://keepachangelog.com).
two. This should have no implications for real-world applications, because
actors that produce a signaling NaN trigger trap handlers before sending
the result to another actor.
- The URI parser stored IPv4 addresses as strings (#1123). Users can now safely
assume that the parsed URI for `tcp://127.0.0.1:8080` returns an IP address
when calling `authority().host`.
## [0.17.6] - 2020-07-24
......
......@@ -19,6 +19,7 @@
#include "caf/uri_builder.hpp"
#include "caf/detail/uri_impl.hpp"
#include "caf/ipv4_address.hpp"
#include "caf/make_counted.hpp"
namespace caf {
......@@ -38,7 +39,15 @@ uri_builder& uri_builder::userinfo(std::string str) {
}
uri_builder& uri_builder::host(std::string str) {
impl_->authority.host = std::move(str);
// IPv6 addresses get special attention from URIs, i.e., they go in between
// square brackets. However, the parser does not catch IPv4 addresses. Hence,
// we do a quick check here whether `str` contains a valid IPv4 address and
// store it as IP address if possible.
ipv4_address addr;
if (auto err = parse(str, addr))
impl_->authority.host = std::move(str);
else
impl_->authority.host = ip_address{addr};
return *this;
}
......
......@@ -218,12 +218,21 @@ bool operator "" _i(const char* cstr, size_t cstr_len) {
CAF_TEST_FIXTURE_SCOPE(uri_tests, fixture)
CAF_TEST(constructing) {
CAF_TEST(default URIs are empty) {
uri x;
CAF_CHECK_EQUAL(x.empty(), true);
CAF_CHECK_EQUAL(x.str(), "");
}
CAF_TEST(URIs recognize IP addresses while parsing) {
auto v6_localhost = "tcp://[::1]:8080"_u;
CAF_CHECK(holds_alternative<ip_address>(v6_localhost.authority().host));
auto v4_localhost = "tcp://127.0.0.1:8080"_u;
CAF_CHECK(holds_alternative<ip_address>(v4_localhost.authority().host));
auto str_localhost = "tcp://localhost:8080"_u;
CAF_CHECK(holds_alternative<std::string>(str_localhost.authority().host));
}
#define BUILD(components) \
CAF_CHECK_EQUAL(*(http << components), *(http_str << components))
......@@ -325,7 +334,7 @@ CAF_TEST(from string) {
ROUNDTRIP("http://me@node:80/file?a=1&b=2");
ROUNDTRIP("http://me@node:80/file#42");
ROUNDTRIP("http://me@node:80/file?a=1&b=2#42");
// all combinations of with IPv6 host
// all combinations of components with IPv6 host
ROUNDTRIP("http://[::1]");
ROUNDTRIP("http://[::1]?a=1&b=2");
ROUNDTRIP("http://[::1]#42");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment