Commit 61f3da06 authored by Joseph Noir's avatar Joseph Noir

Rename append_uri, add type safe handling of host

parent d91ff8e8
......@@ -26,7 +26,7 @@ set(LIBCAF_CORE_SRCS
src/actor_system.cpp
src/actor_system_config.cpp
src/append_hex.cpp
src/append_uri.cpp
src/append_percent_encoded.cpp
src/atom.cpp
src/attachable.cpp
src/behavior.cpp
......
......@@ -19,13 +19,14 @@
#pragma once
#include "caf/detail/append_hex.hpp"
#include "caf/fwd.hpp"
namespace caf {
namespace detail {
// Escapes all reserved characters according to RFC 3986 in `x` and
// adds the encoded string to `str`.
void append_uri(std::string& str, string_view x, bool is_path = false);
void append_percent_encoded(std::string& str, string_view x, bool is_path = false);
} // namespacvec detail
} // namespace detail
} // namespace caf
......@@ -16,7 +16,7 @@
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/detail/append_uri.hpp"
#include "caf/detail/append_percent_encoded.hpp"
#include "caf/config.hpp"
#include "caf/detail/append_hex.hpp"
......@@ -25,7 +25,7 @@
namespace caf {
namespace detail {
void append_uri(std::string& str, string_view x, bool is_path) {
void append_percent_encoded(std::string& str, string_view x, bool is_path) {
for (auto ch : x)
switch (ch) {
case '/':
......@@ -61,5 +61,5 @@ void append_uri(std::string& str, string_view x, bool is_path) {
}
}
} // namespacvec detail
} // namespace detail
} // namespace caf
......@@ -19,8 +19,9 @@
#include "caf/uri.hpp"
#include "caf/deserializer.hpp"
#include "caf/detail/append_uri.hpp"
#include "caf/detail/append_percent_encoded.hpp"
#include "caf/detail/fnv_hash.hpp"
#include "caf/detail/overload.hpp"
#include "caf/detail/parser/read_uri.hpp"
#include "caf/detail/uri_impl.hpp"
#include "caf/error.hpp"
......@@ -105,17 +106,24 @@ std::string to_string(const uri& x) {
std::string to_string(const uri::authority_type& x) {
std::string str;
if (!x.userinfo.empty()) {
detail::append_uri(str, x.userinfo);
detail::append_percent_encoded(str, x.userinfo);
str += '@';
}
auto addr = get_if<ip_address>(&x.host);
if (addr == nullptr) {
detail::append_uri(str, get<std::string>(x.host));
auto f = caf::detail::make_overload(
[&](const ip_address& addr) {
if (addr.embeds_v4()) {
str += to_string(addr);
} else {
str += '[';
str += to_string(*addr);
str += to_string(addr);
str += ']';
}
},
[&](const std::string& host) {
detail::append_percent_encoded(str, host);
}
);
visit(f, x.host);
if (x.port != 0) {
str += ':';
str += std::to_string(x.port);
......
......@@ -18,7 +18,7 @@
#include "caf/detail/uri_impl.hpp"
#include "caf/detail/append_uri.hpp"
#include "caf/detail/append_percent_encoded.hpp"
#include "caf/detail/parser/read_uri.hpp"
#include "caf/error.hpp"
#include "caf/ip_address.hpp"
......@@ -40,26 +40,26 @@ uri_impl uri_impl::default_instance;
// -- modifiers ----------------------------------------------------------------
void uri_impl::assemble_str() {
append_uri(str, scheme);
append_percent_encoded(str, scheme);
str += ':';
if (authority.empty()) {
CAF_ASSERT(!path.empty());
append_uri(str, path, true);
append_percent_encoded(str, path, true);
} else {
str += "//";
str += to_string(authority);
if (!path.empty()) {
str += '/';
append_uri(str, path, true);
append_percent_encoded(str, path, true);
}
}
if (!query.empty()) {
str += '?';
auto i = query.begin();
auto add_kvp = [&](decltype(*i) kvp) {
append_uri(str, kvp.first);
append_percent_encoded(str, kvp.first);
str += '=';
append_uri(str, kvp.second);
append_percent_encoded(str, kvp.second);
};
add_kvp(*i);
for (++i; i != query.end(); ++i) {
......@@ -69,7 +69,7 @@ void uri_impl::assemble_str() {
}
if (!fragment.empty()) {
str += '#';
append_uri(str, fragment);
append_percent_encoded(str, fragment);
}
}
......
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