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