Unverified Commit 0d110fbe authored by Joseph Noir's avatar Joseph Noir Committed by GitHub

Merge pull request #884

Add to_string for authority type of URI class
parents ad35a5c7 14f7489e
......@@ -26,6 +26,7 @@ set(LIBCAF_CORE_SRCS
src/actor_system.cpp
src/actor_system_config.cpp
src/append_hex.cpp
src/append_percent_encoded.cpp
src/atom.cpp
src/attachable.cpp
src/behavior.cpp
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#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_percent_encoded(std::string& str, string_view x,
bool is_path = false);
} // namespace detail
} // namespace caf
......@@ -76,10 +76,6 @@ public:
/// Assembles the human-readable string representation for this URI.
void assemble_str();
// Escapes all reserved characters according to RFC 3986 in `x` and
// adds the encoded string to `str`.
void add_encoded(string_view x, bool is_path = false);
// -- friend functions -------------------------------------------------------
friend void intrusive_ptr_add_ref(const uri_impl* p);
......
......@@ -134,6 +134,9 @@ typename Inspector::result_type inspect(Inspector& f, uri::authority_type& x) {
/// @relates uri
std::string to_string(const uri& x);
/// @relates uri
std::string to_string(const uri::authority_type& x);
/// @relates uri
error parse(string_view str, uri& dest);
......
......@@ -16,7 +16,7 @@
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/detail/stringification_inspector.hpp"
#include "caf/detail/append_hex.hpp"
namespace caf {
namespace detail {
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/detail/append_percent_encoded.hpp"
#include "caf/config.hpp"
#include "caf/detail/append_hex.hpp"
#include "caf/string_view.hpp"
namespace caf {
namespace detail {
void append_percent_encoded(std::string& str, string_view x, bool is_path) {
for (auto ch : x)
switch (ch) {
case '/':
if (is_path) {
str += ch;
break;
}
CAF_ANNOTATE_FALLTHROUGH;
case ' ':
case ':':
case '?':
case '#':
case '[':
case ']':
case '@':
case '!':
case '$':
case '&':
case '\'':
case '"':
case '(':
case ')':
case '*':
case '+':
case ',':
case ';':
case '=':
str += '%';
append_hex(str, reinterpret_cast<uint8_t*>(&ch), 1);
break;
default:
str += ch;
}
}
} // namespace detail
} // namespace caf
......@@ -19,7 +19,9 @@
#include "caf/uri.hpp"
#include "caf/deserializer.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"
......@@ -101,6 +103,33 @@ std::string to_string(const uri& x) {
return result;
}
std::string to_string(const uri::authority_type& x) {
std::string str;
if (!x.userinfo.empty()) {
detail::append_percent_encoded(str, x.userinfo);
str += '@';
}
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 += ']';
}
},
[&](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);
}
return str;
}
error parse(string_view str, uri& dest) {
using namespace detail::parser;
uri_builder builder;
......
......@@ -18,6 +18,7 @@
#include "caf/detail/uri_impl.hpp"
#include "caf/detail/append_percent_encoded.hpp"
#include "caf/detail/parser/read_uri.hpp"
#include "caf/error.hpp"
#include "caf/ip_address.hpp"
......@@ -39,41 +40,26 @@ uri_impl uri_impl::default_instance;
// -- modifiers ----------------------------------------------------------------
void uri_impl::assemble_str() {
add_encoded(scheme);
append_percent_encoded(str, scheme);
str += ':';
if (authority.empty()) {
CAF_ASSERT(!path.empty());
add_encoded(path, true);
append_percent_encoded(str, path, true);
} else {
str += "//";
if (!authority.userinfo.empty()) {
add_encoded(authority.userinfo);
str += '@';
}
auto addr = get_if<ip_address>(&authority.host);
if (addr == nullptr) {
add_encoded(get<std::string>(authority.host));
} else {
str += '[';
str += to_string(*addr);
str += ']';
}
if (authority.port != 0) {
str += ':';
str += std::to_string(authority.port);
}
str += to_string(authority);
if (!path.empty()) {
str += '/';
add_encoded(path, true);
append_percent_encoded(str, path, true);
}
}
if (!query.empty()) {
str += '?';
auto i = query.begin();
auto add_kvp = [&](decltype(*i) kvp) {
add_encoded(kvp.first);
append_percent_encoded(str, kvp.first);
str += '=';
add_encoded(kvp.second);
append_percent_encoded(str, kvp.second);
};
add_kvp(*i);
for (++i; i != query.end(); ++i) {
......@@ -83,43 +69,7 @@ void uri_impl::assemble_str() {
}
if (!fragment.empty()) {
str += '#';
add_encoded(fragment);
}
}
void uri_impl::add_encoded(string_view x, bool is_path) {
for (auto ch : x)
switch (ch) {
case '/':
if (is_path) {
str += ch;
break;
}
CAF_ANNOTATE_FALLTHROUGH;
case ' ':
case ':':
case '?':
case '#':
case '[':
case ']':
case '@':
case '!':
case '$':
case '&':
case '\'':
case '"':
case '(':
case ')':
case '*':
case '+':
case ',':
case ';':
case '=':
str += '%';
detail::append_hex(str, reinterpret_cast<uint8_t*>(&ch), 1);
break;
default:
str += ch;
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