azure-core
url.hpp
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // SPDX-License-Identifier: MIT
3 
9 #pragma once
10 
12 
13 #include <cstdint>
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <unordered_set>
18 
19 namespace Azure { namespace Core {
20  namespace _detail {
21  inline std::string FormatEncodedUrlQueryParameters(
22  std::map<std::string, std::string> const& encodedQueryParameters)
23  {
24  {
25  std::string queryStr;
26  if (!encodedQueryParameters.empty())
27  {
28  auto separator = '?';
29  for (const auto& q : encodedQueryParameters)
30  {
31  queryStr += separator + q.first + '=' + q.second;
32  separator = '&';
33  }
34  }
35 
36  return queryStr;
37  }
38  }
39  } // namespace _detail
40 
47  class Url final {
48  private:
49  std::string m_scheme;
50  std::string m_host;
51  uint16_t m_port{0};
52  std::string m_encodedPath;
53  // query parameters are all encoded
54  std::map<std::string, std::string> m_encodedQueryParameters;
55 
56  // List of default non-URL-encode chars. While URL encoding a string, do not escape any chars in
57  // this set.
58  const static std::unordered_set<unsigned char> defaultNonUrlEncodeChars;
59 
60  std::string GetUrlWithoutQuery(bool relative) const;
61 
68  void AppendQueryParameters(const std::string& encodedQueryParameters);
69 
70  public:
77  static std::string Decode(const std::string& value);
78 
90  static std::string Encode(const std::string& value, const std::string& doNotEncodeSymbols = "");
91 
96  Url() {}
97 
104  explicit Url(const std::string& encodedUrl);
105 
106  /************* Builder Url functions ****************/
107  /******** API for building Url from scratch. Override state ********/
108 
114  void SetScheme(const std::string& scheme) { m_scheme = scheme; }
115 
121  void SetHost(const std::string& encodedHost) { m_host = encodedHost; }
122 
128  void SetPort(uint16_t port) { m_port = port; }
129 
135  void SetPath(const std::string& encodedPath) { m_encodedPath = encodedPath; }
136 
144  void SetQueryParameters(std::map<std::string, std::string> queryParameters)
145  {
146  // creates a copy and discard previous
147  m_encodedQueryParameters = std::move(queryParameters);
148  }
149 
150  // ===== APIs for mutating URL state: ======
151 
157  void AppendPath(const std::string& encodedPath)
158  {
159  if (!m_encodedPath.empty() && m_encodedPath.back() != '/')
160  {
161  m_encodedPath += '/';
162  }
163  m_encodedPath += encodedPath;
164  }
165 
176  void AppendQueryParameter(const std::string& encodedKey, const std::string& encodedValue)
177  {
178  m_encodedQueryParameters[encodedKey] = encodedValue;
179  }
180 
186  void RemoveQueryParameter(const std::string& encodedKey)
187  {
188  m_encodedQueryParameters.erase(encodedKey);
189  }
190 
191  /************** API to read values from Url ***************/
196  const std::string& GetHost() const { return m_host; }
197 
203  const std::string& GetPath() const { return m_encodedPath; }
204 
214  uint16_t GetPort() const { return m_port; }
215 
223  std::map<std::string, std::string> GetQueryParameters() const
224  {
225  return m_encodedQueryParameters;
226  }
227 
232  const std::string& GetScheme() const { return m_scheme; }
233 
239  std::string GetRelativeUrl() const;
240 
246  std::string GetAbsoluteUrl() const;
247  };
248 }} // namespace Azure::Core
Azure::Core::Url::GetHost
const std::string & GetHost() const
Gets URL host.
Definition: url.hpp:196
Azure::Core::Url::GetPort
uint16_t GetPort() const
Gets the port number set for the URL.
Definition: url.hpp:214
Azure::Core::Url::SetQueryParameters
void SetQueryParameters(std::map< std::string, std::string > queryParameters)
Sets the query parameters from an existing query parameter map.
Definition: url.hpp:144
Azure::Core::Url::RemoveQueryParameter
void RemoveQueryParameter(const std::string &encodedKey)
Removes an existing query parameter.
Definition: url.hpp:186
Azure::Core::Url::GetRelativeUrl
std::string GetRelativeUrl() const
Gets the path and query parameters.
Definition: url.cpp:211
Azure::Core::Url::SetScheme
void SetScheme(const std::string &scheme)
Sets URL scheme.
Definition: url.hpp:114
Azure::Core::Url::Encode
static std::string Encode(const std::string &value, const std::string &doNotEncodeSymbols="")
Encodes value by escaping characters to the form of HH where HH are hex digits.
Definition: url.cpp:123
Azure::Core::Url::AppendPath
void AppendPath(const std::string &encodedPath)
Appends an element of URL path.
Definition: url.hpp:157
Azure::Core::Url::SetHost
void SetHost(const std::string &encodedHost)
Sets URL host.
Definition: url.hpp:121
Azure::Core::Url::GetPath
const std::string & GetPath() const
Gets the URL path.
Definition: url.hpp:203
Azure::Core::Url::GetScheme
const std::string & GetScheme() const
Gets the URL scheme.
Definition: url.hpp:232
Azure
Azure SDK abstractions.
Definition: azure_assert.hpp:55
Azure::Core::Url::Decode
static std::string Decode(const std::string &value)
Decodes value by transforming all escaped characters to it's non-encoded value.
Definition: url.cpp:79
Azure::Core::Url::SetPort
void SetPort(uint16_t port)
Sets URL port.
Definition: url.hpp:128
case_insensitive_containers.hpp
A map<string, string> with case-insensitive key comparison.
Azure::Core::Url::Url
Url()
Constructs a new, empty URL object.
Definition: url.hpp:96
Azure::Core::Url::SetPath
void SetPath(const std::string &encodedPath)
Sets URL path.
Definition: url.hpp:135
Azure::Core::Url::AppendQueryParameter
void AppendQueryParameter(const std::string &encodedKey, const std::string &encodedValue)
The value of a query parameter is expected to be non-URL-encoded and, by default, it will be encoded ...
Definition: url.hpp:176
Azure::Core::Url::GetQueryParameters
std::map< std::string, std::string > GetQueryParameters() const
Gets a copy of the list of query parameters from the URL.
Definition: url.hpp:223
Azure::Core::Url
Represents the location where a request will be performed.
Definition: url.hpp:47
Azure::Core::Url::GetAbsoluteUrl
std::string GetAbsoluteUrl() const
Gets Scheme, host, path and query parameters.
Definition: url.cpp:217