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.
base_uri.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 * Protocol independent support for 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 <map>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 #include <functional>
33 
34 #include "cpprest/asyncrt_utils.h"
35 #include "cpprest/details/basic_types.h"
36 
37 namespace web {
38  namespace details
39  {
41  {
42  uri_components() : m_path(_XPLATSTR("/")), m_port(-1)
43  {}
44 
45  uri_components(const uri_components &other) :
46  m_scheme(other.m_scheme),
47  m_host(other.m_host),
48  m_user_info(other.m_user_info),
49  m_path(other.m_path),
50  m_query(other.m_query),
51  m_fragment(other.m_fragment),
52  m_port(other.m_port)
53  {}
54 
55  uri_components & operator=(const uri_components &other)
56  {
57  if (this != &other)
58  {
59  m_scheme = other.m_scheme;
60  m_host = other.m_host;
61  m_user_info = other.m_user_info;
62  m_path = other.m_path;
63  m_query = other.m_query;
64  m_fragment = other.m_fragment;
65  m_port = other.m_port;
66  }
67  return *this;
68  }
69 
70  uri_components(uri_components &&other) CPPREST_NOEXCEPT :
71  m_scheme(std::move(other.m_scheme)),
72  m_host(std::move(other.m_host)),
73  m_user_info(std::move(other.m_user_info)),
74  m_path(std::move(other.m_path)),
75  m_query(std::move(other.m_query)),
76  m_fragment(std::move(other.m_fragment)),
77  m_port(other.m_port)
78  {}
79 
80  uri_components & operator=(uri_components &&other) CPPREST_NOEXCEPT
81  {
82  if (this != &other)
83  {
84  m_scheme = std::move(other.m_scheme);
85  m_host = std::move(other.m_host);
86  m_user_info = std::move(other.m_user_info);
87  m_path = std::move(other.m_path);
88  m_query = std::move(other.m_query);
89  m_fragment = std::move(other.m_fragment);
90  m_port = other.m_port;
91  }
92  return *this;
93  }
94 
95  _ASYNCRTIMP utility::string_t join();
96 
97  utility::string_t m_scheme;
98  utility::string_t m_host;
99  utility::string_t m_user_info;
100  utility::string_t m_path;
101  utility::string_t m_query;
102  utility::string_t m_fragment;
103  int m_port;
104  };
105  }
106 
110  class uri_exception : public std::exception
111  {
112  public:
113 
114  uri_exception(std::string msg) : m_msg(std::move(msg)) {}
115 
116  ~uri_exception() CPPREST_NOEXCEPT {}
117 
118  const char* what() const CPPREST_NOEXCEPT
119  {
120  return m_msg.c_str();
121  }
122 
123  private:
124  std::string m_msg;
125  };
126 
151  class uri
152  {
153  public:
154 
163  {
164  public:
165  enum component
166  {
167  user_info,
168  host,
169  path,
170  query,
171  fragment,
172  full_uri
173  };
174  };
175 
183  _ASYNCRTIMP static utility::string_t __cdecl encode_uri(const utility::string_t &raw, uri::components::component = components::full_uri);
184 
191  _ASYNCRTIMP static utility::string_t __cdecl encode_data_string(const utility::string_t &utf8data);
192 
198  _ASYNCRTIMP static utility::string_t __cdecl decode(const utility::string_t &encoded);
199 
205  _ASYNCRTIMP static std::vector<utility::string_t> __cdecl split_path(const utility::string_t &path);
206 
212  _ASYNCRTIMP static std::map<utility::string_t, utility::string_t> __cdecl split_query(const utility::string_t &query);
213 
219  _ASYNCRTIMP static bool __cdecl validate(const utility::string_t &uri_string);
220 
224  uri() { m_uri = _XPLATSTR("/");};
225 
231  _ASYNCRTIMP uri(const utility::char_t *uri_string);
232 
238  _ASYNCRTIMP uri(const utility::string_t &uri_string);
239 
243  uri(const uri &other) :
244  m_uri(other.m_uri),
245  m_components(other.m_components)
246  {}
247 
251  uri & operator=(const uri &other)
252  {
253  if (this != &other)
254  {
255  m_uri = other.m_uri;
256  m_components = other.m_components;
257  }
258  return *this;
259  }
260 
264  uri(uri &&other) CPPREST_NOEXCEPT :
265  m_uri(std::move(other.m_uri)),
266  m_components(std::move(other.m_components))
267  {}
268 
272  uri & operator=(uri &&other) CPPREST_NOEXCEPT
273  {
274  if (this != &other)
275  {
276  m_uri = std::move(other.m_uri);
277  m_components = std::move(other.m_components);
278  }
279  return *this;
280  }
281 
286  const utility::string_t &scheme() const { return m_components.m_scheme; }
287 
292  const utility::string_t &user_info() const { return m_components.m_user_info; }
293 
298  const utility::string_t &host() const { return m_components.m_host; }
299 
304  int port() const { return m_components.m_port; }
305 
310  const utility::string_t &path() const { return m_components.m_path; }
311 
316  const utility::string_t &query() const { return m_components.m_query; }
317 
322  const utility::string_t &fragment() const { return m_components.m_fragment; }
323 
328  _ASYNCRTIMP uri authority() const;
329 
334  _ASYNCRTIMP uri resource() const;
335 
339  bool is_empty() const
340  {
341  return this->m_uri.empty() || this->m_uri == _XPLATSTR("/");
342  }
343 
351  bool is_host_loopback() const
352  {
353  return !is_empty() && ((host() == _XPLATSTR("localhost")) || (host().size() > 4 && host().substr(0,4) == _XPLATSTR("127.")));
354  }
355 
362  bool is_host_wildcard() const
363  {
364  return !is_empty() && (this->host() == _XPLATSTR("*") || this->host() == _XPLATSTR("+"));
365  }
366 
376  bool is_host_portable() const
377  {
378  return !(is_empty() || is_host_loopback() || is_host_wildcard());
379  }
380 
381  // <summary>
386  bool is_port_default() const
387  {
388  return !is_empty() && this->port() == 0;
389  }
390 
395  bool is_authority() const
396  {
397  return !is_empty() && is_path_empty() && query().empty() && fragment().empty();
398  }
399 
405  bool has_same_authority(const uri &other) const
406  {
407  return !is_empty() && this->authority() == other.authority();
408  }
409 
414  bool is_path_empty() const
415  {
416  return path().empty() || path() == _XPLATSTR("/");
417  }
418 
423  utility::string_t to_string() const
424  {
425  return m_uri;
426  }
427 
428  _ASYNCRTIMP bool operator == (const uri &other) const;
429 
430  bool operator < (const uri &other) const
431  {
432  return m_uri < other.m_uri;
433  }
434 
435  bool operator != (const uri &other) const
436  {
437  return !(this->operator == (other));
438  }
439 
440  private:
441  friend class uri_builder;
442 
443  // Encodes all characters not in given set determined by given function.
444  _ASYNCRTIMP static utility::string_t __cdecl encode_impl(const utility::string_t &raw, const std::function<bool __cdecl(int)>& should_encode);
445 
446  utility::string_t m_uri;
447  details::uri_components m_components;
448  };
449 
450 } // namespace web
uri & operator=(const uri &other)
Copy assignment operator.
Definition: base_uri.h:251
const utility::string_t & path() const
Get the path component of the URI as an encoded string.
Definition: base_uri.h:310
_ASYNCRTIMP uri authority() const
Creates a new uri object with the same authority portion as this one, omitting the resource and query...
uri(const uri &other)
Copy constructor.
Definition: base_uri.h:243
bool is_path_empty() const
Returns whether the path portion of this URI is empty
Definition: base_uri.h:414
_ASYNCRTIMP uri resource() const
Gets the path, query, and fragment portion of this uri, which may be empty.
const utility::string_t & query() const
Get the query component of the URI as an encoded string.
Definition: base_uri.h:316
A flexible, protocol independent URI implementation.
Definition: base_uri.h:151
The various components of a URI. This enum is used to indicate which URI component is being encoded t...
Definition: base_uri.h:162
static _ASYNCRTIMP utility::string_t __cdecl decode(const utility::string_t &encoded)
Decodes an encoded string.
The web namespace contains functionality common to multiple protocols like HTTP and WebSockets...
Definition: base_uri.h:37
bool is_host_wildcard() const
A wildcard URI is one which refers to all hostnames that resolve to the local machine (using the * or...
Definition: base_uri.h:362
bool is_host_portable() const
A portable URI is one with a hostname that can be resolved globally (used from another machine)...
Definition: base_uri.h:376
bool is_port_default() const
Definition: base_uri.h:386
int port() const
Get the port component of the URI. Returns -1 if no port is specified.
Definition: base_uri.h:304
uri(uri &&other) CPPREST_NOEXCEPT
Move constructor.
Definition: base_uri.h:264
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...
bool is_host_loopback() const
A loopback URI is one which refers to a hostname or ip address with meaning only on the local machine...
Definition: base_uri.h:351
static _ASYNCRTIMP utility::string_t __cdecl encode_data_string(const utility::string_t &utf8data)
Encodes a string by converting all characters except for RFC 3986 unreserved characters to their hexa...
bool is_authority() const
An "authority" URI is one with only a scheme, optional userinfo, hostname, and (optional) port...
Definition: base_uri.h:395
static _ASYNCRTIMP bool __cdecl validate(const utility::string_t &uri_string)
Validates a string as a URI.
utility::string_t to_string() const
Returns the full (encoded) URI as a string.
Definition: base_uri.h:423
A single exception type to represent errors in parsing, encoding, and decoding URIs.
Definition: base_uri.h:110
const utility::string_t & host() const
Get the host component of the URI as an encoded string.
Definition: base_uri.h:298
uri & operator=(uri &&other) CPPREST_NOEXCEPT
Move assignment operator
Definition: base_uri.h:272
static _ASYNCRTIMP std::vector< utility::string_t > __cdecl split_path(const utility::string_t &path)
Splits a path into its hierarchical components.
bool is_empty() const
An empty URI specifies no components, and serves as a default value
Definition: base_uri.h:339
bool has_same_authority(const uri &other) const
Returns whether the other URI has the same authority as this one
Definition: base_uri.h:405
Definition: base_uri.h:40
const utility::string_t & user_info() const
Get the user information component of the URI as an encoded string.
Definition: base_uri.h:292
const utility::string_t & fragment() const
Get the fragment component of the URI as an encoded string.
Definition: base_uri.h:322
uri()
Creates an empty uri
Definition: base_uri.h:224
static _ASYNCRTIMP std::map< utility::string_t, utility::string_t > __cdecl split_query(const utility::string_t &query)
Splits a query into its key-value components.
const utility::string_t & scheme() const
Get the scheme component of the URI as an encoded string.
Definition: base_uri.h:286