summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/third_party/uri/include/network/uri/uri_builder.hpp
blob: 87f0b79221fe88c438d1d35279d65756d2501c8a (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) Glyn Matthews 2012-2016.
// Copyright 2012 Dean Michael Berris <dberris@google.com>
// Copyright 2012 Google, Inc.
// 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)

/**
 * \file
 * \brief Contains the definition of the uri_builder.
 */

#ifndef NETWORK_URI_BUILDER_INC
#define NETWORK_URI_BUILDER_INC

#include <cstdint>
#include <utility>
#include <type_traits>
#include <network/uri/uri.hpp>

#ifdef NETWORK_URI_MSVC
#pragma warning(push)
#pragma warning(disable : 4251 4231 4660)
#endif

namespace network {
#if !defined(DOXYGEN_SHOULD_SKIP_THIS)
namespace detail {

template <class T>
struct host_converter {
  uri::string_type operator()(const T &host) const {
    return detail::translate(host);
  }
};

template <class T, class Enable = void>
struct port_converter {
  uri::string_type operator()(const T &port) const {
    return detail::translate(port);
  }
};

template <class T>
struct port_converter<T, typename std::enable_if<std::is_integral<
                             typename std::decay<T>::type>::value>::type> {
  uri::string_type operator()(std::uint16_t port) const {
    return std::to_string(port);
  }
};

template <class T>
struct path_converter {
  uri::string_type operator()(const T &path) const {
    return detail::translate(path);
  }
};
}  // namespace detail
#endif  // !defined(DOXYGEN_SHOULD_SKIP_THIS)

/**
 * \ingroup uri
 * \class uri_builder network/uri/uri_builder.hpp network/uri.hpp
 * \brief A class that allows complex uri objects to be constructed.
 * \sa uri
 */
class uri_builder {
#if !defined(DOXYGEN_SHOULD_SKIP_THIS)
  friend class uri;
#endif  // !defined(DOXYGEN_SHOULD_SKIP_THIS)

  uri_builder(const uri_builder &) = delete;
  uri_builder &operator=(const uri_builder &) = delete;

 public:
  /**
   * \brief The uri_builder string_type.
   */
  using string_type = network::uri::string_type;

  /**
   * \brief Constructor.
   */
  uri_builder() = default;

  /**
   * \brief Constructor.
   * \param base A URI that is the base on which a new URI is built.
   */
  explicit uri_builder(const uri &base);

  /**
   * \brief Destructor.
   */
  ~uri_builder() noexcept;

  /**
   * \brief Adds a new scheme to the uri_builder.
   * \param scheme The scheme.
   * \returns \c *this
   */
  template <typename Source>
  uri_builder &scheme(const Source &scheme) {
    set_scheme(detail::translate(scheme));
    return *this;
  }

  /**
   * \brief Adds a new user info to the uri_builder.
   * \param user_info The user info.
   * \returns \c *this
   */
  template <typename Source>
  uri_builder &user_info(const Source &user_info) {
    set_user_info(detail::translate(user_info));
    return *this;
  }

  /**
   * \brief Clears the URI user_info part.
   * \returns \c *this
   */
  uri_builder &clear_user_info();

  /**
   * \brief Adds a new host to the uri_builder.
   * \param host The host.
   * \returns \c *this
   */
  template <typename Source>
  uri_builder &host(const Source &host) {
    detail::host_converter<Source> convert;
    set_host(convert(host));
    return *this;
  }

  /**
   * \brief Adds a new port to the uri_builder.
   * \param port The port.
   * \returns \c *this
   */
  template <typename Source>
  uri_builder &port(const Source &port) {
    detail::port_converter<Source> convert;
    set_port(convert(port));
    return *this;
  }

  /**
   * \brief Clears the URI port part.
   * \returns \c *this
   */
  uri_builder &clear_port();

  /**
   * \brief Adds a new authority to the uri_builder.
   * \param authority The authority.
   * \returns \c *this
   */
  template <typename Source>
  uri_builder &authority(const Source &authority) {
    set_authority(detail::translate(authority));
    return *this;
  }

  /**
   * \brief Sets a new path to the uri_builder.
   * \param path The path.
   * \returns \c *this
   */
  template <typename Source>
  uri_builder &path(const Source &path) {
    detail::path_converter<Source> convert;
    set_path(convert(path));
    return *this;
  }

  /**
   * \brief Clears the URI path part.
   * \returns \c *this
   */
  uri_builder &clear_path();

  /**
   * \deprecated Use append_query_component
   * \warning This function's behaviour has changed and percent encoding
   *          of the '=' character is not ignored.
   * \brief Adds a new query to the uri_builder.
   * \param query The query.
   * \returns \c *this
   * \sa append_query_parameter
   */
  template <typename Source>
  uri_builder &append_query(const Source &query) {
    return append_query_component(query);
  }

  /**
   * \brief Appends a new query component to the uri_builder. The
   *        '=' symbol is percent encoded.
   * \param component The query component.
   * \returns \c *this
   * \sa append_query_key_value_pair
   */
  template <typename Source>
  uri_builder &append_query_component(const Source &component) {
    append_query_component(detail::translate(component));
    return *this;
  }

  /**
   * \brief Adds a new query key/value pair to the uri_builder.
   * \param key The query parameter key.
   * \param value The query parameter value.
   * \returns \c *this
   */
  template <typename Key, typename Value>
  uri_builder &append_query_key_value_pair(const Key &key, const Value &value) {
    append_query_key_value_pair(detail::translate(key),
                                detail::translate(value));
    return *this;
  }

  /**
   * \brief Clears the URI query part.
   * \returns \c *this
   */
  uri_builder &clear_query();

  /**
   * \brief Adds a new fragment to the uri_builder.
   * \param fragment The fragment.
   * \returns \c *this
   */
  template <typename Source>
  uri_builder &fragment(const Source &fragment) {
    set_fragment(detail::translate(fragment));
    return *this;
  }

  /**
   * \brief Clears the URI fragment part.
   * \returns \c *this
   */
  uri_builder &clear_fragment();

  /**
   * \brief Builds a new uri object.
   * \returns A valid uri object.
   * \throws uri_builder_error if the uri_builder is unable to build
   *         a valid URI.
   * \throws std::bad_alloc If the underlying string cannot be
   *         allocated.
   */
  network::uri uri() const;

 private:
  void set_scheme(string_type &&scheme);
  void set_user_info(string_type &&user_info);
  void set_host(string_type &&host);
  void set_port(string_type &&port);
  void set_authority(string_type &&authority);
  void set_path(string_type &&path);
  void append_query_component(string_type &&name);
  void append_query_key_value_pair(string_type &&key, string_type &&value);
  void set_fragment(string_type &&fragment);

  optional<string_type> scheme_, user_info_, host_, port_, path_, query_,
      fragment_;
};
}  // namespace network

#ifdef NETWORK_URI_MSVC
#pragma warning(pop)
#endif

#endif  // NETWORK_URI_BUILDER_INC