summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/third_party/uri/src/uri_builder.cpp
blob: e47e8acb44608d0d6e949079171ca7fd5b4e4c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) Glyn Matthews 2012-2016.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <locale>
#include "network/uri/uri_builder.hpp"
#include "detail/uri_normalize.hpp"
#include "detail/uri_parse_authority.hpp"
#include "detail/algorithm.hpp"

namespace network {
uri_builder::uri_builder(const network::uri &base_uri) {
  if (base_uri.has_scheme()) {
    scheme_ = base_uri.scheme().to_string();
  }

  if (base_uri.has_user_info()) {
    user_info_ = base_uri.user_info().to_string();
  }

  if (base_uri.has_host()) {
    host_ = base_uri.host().to_string();
  }

  if (base_uri.has_port()) {
    port_ = base_uri.port().to_string();
  }

  if (base_uri.has_path()) {
    path_ = base_uri.path().to_string();
  }

  if (base_uri.has_query()) {
    query_ = base_uri.query().to_string();
  }

  if (base_uri.has_fragment()) {
    fragment_ = base_uri.fragment().to_string();
  }
}

uri_builder::~uri_builder() noexcept {}

network::uri uri_builder::uri() const { return network::uri(*this); }

void uri_builder::set_scheme(string_type &&scheme) {
  // validate scheme is valid and normalize
  scheme_ = scheme;
  detail::transform(*scheme_, std::begin(*scheme_),
                    [](char ch) { return std::tolower(ch, std::locale()); });
}

void uri_builder::set_user_info(string_type &&user_info) {
  user_info_ = string_type();
  network::uri::encode_user_info(std::begin(user_info), std::end(user_info),
                                 std::back_inserter(*user_info_));
}

uri_builder &uri_builder::clear_user_info() {
  user_info_ = network::nullopt;
  return *this;
}

void uri_builder::set_host(string_type &&host) {
  host_ = string_type();
  network::uri::encode_host(std::begin(host), std::end(host),
                            std::back_inserter(*host_));
  detail::transform(*host_, std::begin(*host_),
                    [](char ch) { return std::tolower(ch, std::locale()); });
}

void uri_builder::set_port(string_type &&port) {
  port_ = string_type();
  network::uri::encode_port(std::begin(port), std::end(port),
                            std::back_inserter(*port_));
}

uri_builder &uri_builder::clear_port() {
  port_ = network::nullopt;
  return *this;
}

void uri_builder::set_authority(string_type &&authority) {
  optional<detail::uri_part> user_info, host, port;
  uri::string_view view(authority);
  uri::const_iterator it = std::begin(view), last = std::end(view);
  detail::parse_authority(it, last, user_info, host, port);

  if (user_info) {
    set_user_info(user_info->to_string());
  }

  if (host) {
    set_host(host->to_string());
  }

  if (port) {
    set_port(port->to_string());
  }
}

void uri_builder::set_path(string_type &&path) {
  path_ = string_type();
  network::uri::encode_path(std::begin(path), std::end(path),
                            std::back_inserter(*path_));
}

uri_builder &uri_builder::clear_path() {
  path_ = network::nullopt;
  return *this;
}

void uri_builder::append_query_component(string_type &&name) {
  if (!query_) {
    query_ = string_type();
  } else {
    query_->append("&");
  }
  network::uri::encode_query_component(std::begin(name), std::end(name),
                                       std::back_inserter(*query_));
}

void uri_builder::append_query_key_value_pair(string_type &&key,
                                              string_type &&value) {
  if (!query_) {
    query_ = string_type();
  } else {
    query_->push_back('&');
  }
  network::uri::encode_query_key_value_pair(std::begin(key), std::end(key),
                                            std::begin(value), std::end(value),
                                            std::back_inserter(*query_));
}

uri_builder &uri_builder::clear_query() {
  query_ = network::nullopt;
  return *this;
}

void uri_builder::set_fragment(string_type &&fragment) {
  fragment_ = string_type();
  network::uri::encode_fragment(std::begin(fragment), std::end(fragment),
                                std::back_inserter(*fragment_));
}

uri_builder &uri_builder::clear_fragment() {
  fragment_ = network::nullopt;
  return *this;
}
}  // namespace network