| 1 |
// Copyright (c) Glyn Matthews 2012-2016. |
| 2 |
// Distributed under the Boost Software License, Version 1.0. |
| 3 |
// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
#include <locale> |
| 7 |
#include "network/uri/uri_builder.hpp" |
| 8 |
#include "detail/uri_normalize.hpp" |
| 9 |
#include "detail/uri_parse_authority.hpp" |
| 10 |
#include "detail/algorithm.hpp" |
| 11 |
|
| 12 |
namespace network { |
| 13 |
uri_builder::uri_builder(const network::uri &base_uri) { |
| 14 |
if (base_uri.has_scheme()) { |
| 15 |
scheme_ = base_uri.scheme().to_string(); |
| 16 |
} |
| 17 |
|
| 18 |
if (base_uri.has_user_info()) { |
| 19 |
user_info_ = base_uri.user_info().to_string(); |
| 20 |
} |
| 21 |
|
| 22 |
if (base_uri.has_host()) { |
| 23 |
host_ = base_uri.host().to_string(); |
| 24 |
} |
| 25 |
|
| 26 |
if (base_uri.has_port()) { |
| 27 |
port_ = base_uri.port().to_string(); |
| 28 |
} |
| 29 |
|
| 30 |
if (base_uri.has_path()) { |
| 31 |
path_ = base_uri.path().to_string(); |
| 32 |
} |
| 33 |
|
| 34 |
if (base_uri.has_query()) { |
| 35 |
query_ = base_uri.query().to_string(); |
| 36 |
} |
| 37 |
|
| 38 |
if (base_uri.has_fragment()) { |
| 39 |
fragment_ = base_uri.fragment().to_string(); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
uri_builder::~uri_builder() noexcept {} |
| 44 |
|
| 45 |
network::uri uri_builder::uri() const { return network::uri(*this); } |
| 46 |
|
| 47 |
void uri_builder::set_scheme(string_type &&scheme) { |
| 48 |
// validate scheme is valid and normalize |
| 49 |
scheme_ = scheme; |
| 50 |
detail::transform(*scheme_, std::begin(*scheme_), |
| 51 |
[](char ch) { return std::tolower(ch, std::locale()); }); |
| 52 |
} |
| 53 |
|
| 54 |
void uri_builder::set_user_info(string_type &&user_info) { |
| 55 |
user_info_ = string_type(); |
| 56 |
network::uri::encode_user_info(std::begin(user_info), std::end(user_info), |
| 57 |
std::back_inserter(*user_info_)); |
| 58 |
} |
| 59 |
|
| 60 |
uri_builder &uri_builder::clear_user_info() { |
| 61 |
user_info_ = network::nullopt; |
| 62 |
return *this; |
| 63 |
} |
| 64 |
|
| 65 |
void uri_builder::set_host(string_type &&host) { |
| 66 |
host_ = string_type(); |
| 67 |
network::uri::encode_host(std::begin(host), std::end(host), |
| 68 |
std::back_inserter(*host_)); |
| 69 |
detail::transform(*host_, std::begin(*host_), |
| 70 |
[](char ch) { return std::tolower(ch, std::locale()); }); |
| 71 |
} |
| 72 |
|
| 73 |
void uri_builder::set_port(string_type &&port) { |
| 74 |
port_ = string_type(); |
| 75 |
network::uri::encode_port(std::begin(port), std::end(port), |
| 76 |
std::back_inserter(*port_)); |
| 77 |
} |
| 78 |
|
| 79 |
uri_builder &uri_builder::clear_port() { |
| 80 |
port_ = network::nullopt; |
| 81 |
return *this; |
| 82 |
} |
| 83 |
|
| 84 |
void uri_builder::set_authority(string_type &&authority) { |
| 85 |
optional<detail::uri_part> user_info, host, port; |
| 86 |
uri::string_view view(authority); |
| 87 |
uri::const_iterator it = std::begin(view), last = std::end(view); |
| 88 |
detail::parse_authority(it, last, user_info, host, port); |
| 89 |
|
| 90 |
if (user_info) { |
| 91 |
set_user_info(user_info->to_string()); |
| 92 |
} |
| 93 |
|
| 94 |
if (host) { |
| 95 |
set_host(host->to_string()); |
| 96 |
} |
| 97 |
|
| 98 |
if (port) { |
| 99 |
set_port(port->to_string()); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void uri_builder::set_path(string_type &&path) { |
| 104 |
path_ = string_type(); |
| 105 |
network::uri::encode_path(std::begin(path), std::end(path), |
| 106 |
std::back_inserter(*path_)); |
| 107 |
} |
| 108 |
|
| 109 |
uri_builder &uri_builder::clear_path() { |
| 110 |
path_ = network::nullopt; |
| 111 |
return *this; |
| 112 |
} |
| 113 |
|
| 114 |
void uri_builder::append_query_component(string_type &&name) { |
| 115 |
if (!query_) { |
| 116 |
query_ = string_type(); |
| 117 |
} else { |
| 118 |
query_->append("&"); |
| 119 |
} |
| 120 |
network::uri::encode_query_component(std::begin(name), std::end(name), |
| 121 |
std::back_inserter(*query_)); |
| 122 |
} |
| 123 |
|
| 124 |
void uri_builder::append_query_key_value_pair(string_type &&key, |
| 125 |
string_type &&value) { |
| 126 |
if (!query_) { |
| 127 |
query_ = string_type(); |
| 128 |
} else { |
| 129 |
query_->push_back('&'); |
| 130 |
} |
| 131 |
network::uri::encode_query_key_value_pair(std::begin(key), std::end(key), |
| 132 |
std::begin(value), std::end(value), |
| 133 |
std::back_inserter(*query_)); |
| 134 |
} |
| 135 |
|
| 136 |
uri_builder &uri_builder::clear_query() { |
| 137 |
query_ = network::nullopt; |
| 138 |
return *this; |
| 139 |
} |
| 140 |
|
| 141 |
void uri_builder::set_fragment(string_type &&fragment) { |
| 142 |
fragment_ = string_type(); |
| 143 |
network::uri::encode_fragment(std::begin(fragment), std::end(fragment), |
| 144 |
std::back_inserter(*fragment_)); |
| 145 |
} |
| 146 |
|
| 147 |
uri_builder &uri_builder::clear_fragment() { |
| 148 |
fragment_ = network::nullopt; |
| 149 |
return *this; |
| 150 |
} |
| 151 |
} // namespace network |