C++ Rest SDK
The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
uri_builder.h
1 /***
2 * ==++==
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * ==--==
17 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
18 *
19 * Builder style class for creating URIs.
20 *
21 * For the latest on this and related APIs, please see http://casablanca.codeplex.com.
22 *
23 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
24 ****/
25 
26 #pragma once
27 
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 
32 #include "cpprest/base_uri.h"
33 #include "cpprest/details/uri_parser.h"
34 
35 namespace web
36 {
41  {
42  public:
43 
48 
53  uri_builder(const uri &uri_str): m_uri(uri_str.m_components) {}
54 
59  const utility::string_t &scheme() const { return m_uri.m_scheme; }
60 
65  const utility::string_t &user_info() const { return m_uri.m_user_info; }
66 
71  const utility::string_t &host() const { return m_uri.m_host; }
72 
77  int port() const { return m_uri.m_port; }
78 
83  const utility::string_t &path() const { return m_uri.m_path; }
84 
89  const utility::string_t &query() const { return m_uri.m_query; }
90 
95  const utility::string_t &fragment() const { return m_uri.m_fragment; }
96 
102  uri_builder & set_scheme(const utility::string_t &scheme)
103  {
104  m_uri.m_scheme = scheme;
105  return *this;
106  }
107 
114  uri_builder & set_user_info(const utility::string_t &user_info, bool do_encoding = false)
115  {
116  m_uri.m_user_info = do_encoding ? uri::encode_uri(user_info, uri::components::user_info) : user_info;
117  return *this;
118  }
119 
126  uri_builder & set_host(const utility::string_t &host, bool do_encoding = false)
127  {
128  m_uri.m_host = do_encoding ? uri::encode_uri(host, uri::components::host) : host;
129  return *this;
130  }
131 
138  {
139  m_uri.m_port = port;
140  return *this;
141  }
142 
149  uri_builder & set_port(const utility::string_t &port)
150  {
151  utility::istringstream_t portStream(port);
152  int port_tmp;
153  portStream >> port_tmp;
154  if(portStream.fail() || portStream.bad())
155  {
156  throw std::invalid_argument("invalid port argument, must be non empty string containing integer value");
157  }
158  m_uri.m_port = port_tmp;
159  return *this;
160  }
161 
168  uri_builder & set_path(const utility::string_t &path, bool do_encoding = false)
169  {
170  m_uri.m_path = do_encoding ? uri::encode_uri(path, uri::components::path) : path;
171  return *this;
172  }
173 
174 
181  uri_builder & set_query(const utility::string_t &query, bool do_encoding = false)
182  {
183  m_uri.m_query = do_encoding ? uri::encode_uri(query, uri::components::query) : query;
184  return *this;
185  }
186 
193  uri_builder & set_fragment(const utility::string_t &fragment, bool do_encoding = false)
194  {
195  m_uri.m_fragment = do_encoding ? uri::encode_uri(fragment, uri::components::fragment) : fragment;
196  return *this;
197  }
198 
202  void clear()
203  {
204  m_uri = details::uri_components();
205  }
206 
213  _ASYNCRTIMP uri_builder &append_path(const utility::string_t &path, bool do_encoding = false);
214 
221  _ASYNCRTIMP uri_builder &append_query(const utility::string_t &query, bool do_encoding = false);
222 
228  _ASYNCRTIMP uri_builder &append(const uri &relative_uri);
229 
237  template<typename T>
238  uri_builder &append_query(const utility::string_t &name, const T &value, bool do_encoding = true)
239  {
240  auto encodedName = name;
241  auto encodedValue = ::utility::conversions::print_string(value, std::locale::classic());
242 
243  if (do_encoding)
244  {
245  auto encodingCheck = [](int ch)
246  {
247  switch (ch)
248  {
249  // Encode '&', ';', and '=' since they are used
250  // as delimiters in query component.
251  case '&':
252  case ';':
253  case '=':
254  case '%':
255  case '+':
256  return true;
257  default:
258  return !::web::details::uri_parser::is_query_character(ch);
259  }
260  };
261  encodedName = uri::encode_impl(encodedName, encodingCheck);
262  encodedValue = uri::encode_impl(encodedValue, encodingCheck);
263  }
264 
265  auto encodedQuery = encodedName;
266  encodedQuery.append(_XPLATSTR("="));
267  encodedQuery.append(encodedValue);
268  // The query key value pair was already encoded by us or the user separately.
269  return append_query(encodedQuery, false);
270  }
271 
276  _ASYNCRTIMP utility::string_t to_string();
277 
282  _ASYNCRTIMP uri to_uri();
283 
288  _ASYNCRTIMP bool is_valid();
289 
290  private:
292  };
293 } // namespace web
uri_builder & set_port(const utility::string_t &port)
Set the port component of the URI.
Definition: uri_builder.h:149
const utility::string_t & host() const
Get the host component of the URI as an encoded string.
Definition: uri_builder.h:71
const utility::string_t & user_info() const
Get the user information component of the URI as an encoded string.
Definition: uri_builder.h:65
A flexible, protocol independent URI implementation.
Definition: base_uri.h:151
uri_builder & set_host(const utility::string_t &host, bool do_encoding=false)
Set the host component of the URI.
Definition: uri_builder.h:126
void clear()
Clears all components of the underlying URI in this uri_builder.
Definition: uri_builder.h:202
The web namespace contains functionality common to multiple protocols like HTTP and WebSockets...
Definition: base_uri.h:37
int port() const
Get the port component of the URI. Returns -1 if no port is specified.
Definition: uri_builder.h:77
uri_builder & append_query(const utility::string_t &name, const T &value, bool do_encoding=true)
Appends another query to the query of this uri_builder, encoding it first. This overload is useful wh...
Definition: uri_builder.h:238
uri_builder & set_scheme(const utility::string_t &scheme)
Set the scheme of the URI.
Definition: uri_builder.h:102
_ASYNCRTIMP uri_builder & append(const uri &relative_uri)
Appends an relative uri (Path, Query and fragment) at the end of the current uri. ...
static _ASYNCRTIMP utility::string_t __cdecl encode_uri(const utility::string_t &raw, uri::components::component=components::full_uri)
Encodes a URI component according to RFC 3986. Note if a full URI is specified instead of an individu...
uri_builder & set_port(int port)
Set the port component of the URI.
Definition: uri_builder.h:137
_ASYNCRTIMP utility::string_t to_string()
Combine and validate the URI components into a encoded string. An exception will be thrown if the URI...
const utility::string_t & fragment() const
Get the fragment component of the URI as an encoded string.
Definition: uri_builder.h:95
uri_builder & set_path(const utility::string_t &path, bool do_encoding=false)
Set the path component of the URI.
Definition: uri_builder.h:168
const utility::string_t & path() const
Get the path component of the URI as an encoded string.
Definition: uri_builder.h:83
_ASYNCRTIMP bool is_valid()
Validate the generated URI from all existing components of this uri_builder.
uri_builder()
Creates a builder with an initially empty URI.
Definition: uri_builder.h:47
_ASYNCRTIMP uri to_uri()
Combine and validate the URI components into a URI class instance. An exception will be thrown if the...
const utility::string_t & scheme() const
Get the scheme component of the URI as an encoded string.
Definition: uri_builder.h:59
uri_builder & set_query(const utility::string_t &query, bool do_encoding=false)
Set the query component of the URI.
Definition: uri_builder.h:181
Definition: base_uri.h:40
const utility::string_t & query() const
Get the query component of the URI as an encoded string.
Definition: uri_builder.h:89
uri_builder & set_fragment(const utility::string_t &fragment, bool do_encoding=false)
Set the fragment component of the URI.
Definition: uri_builder.h:193
Builder for constructing URIs incrementally.
Definition: uri_builder.h:40
_ASYNCRTIMP uri_builder & append_query(const utility::string_t &query, bool do_encoding=false)
Appends another query to the query of this uri_builder.
_ASYNCRTIMP uri_builder & append_path(const utility::string_t &path, bool do_encoding=false)
Appends another path to the path of this uri_builder.
uri_builder & set_user_info(const utility::string_t &user_info, bool do_encoding=false)
Set the user info component of the URI.
Definition: uri_builder.h:114
uri_builder(const uri &uri_str)
Creates a builder with a existing URI object.
Definition: uri_builder.h:53