azure-core
Loading...
Searching...
No Matches
url.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
9#pragma once
10
12
13#include <cstdint>
14#include <map>
15#include <memory>
16#include <string>
17
18namespace Azure { namespace Core {
19 namespace _detail {
20 inline std::string FormatEncodedUrlQueryParameters(
21 std::map<std::string, std::string> const& encodedQueryParameters)
22 {
23 {
24 std::string queryStr;
25 if (!encodedQueryParameters.empty())
26 {
27 auto separator = '?';
28 for (const auto& q : encodedQueryParameters)
29 {
30 queryStr += separator + q.first + '=' + q.second;
31 separator = '&';
32 }
33 }
34
35 return queryStr;
36 }
37 }
38 } // namespace _detail
39
46 class Url final {
47 private:
48 std::string m_scheme;
49 std::string m_host;
50 uint16_t m_port{0};
51 std::string m_encodedPath;
52 // query parameters are all encoded
53 std::map<std::string, std::string> m_encodedQueryParameters;
54
55 std::string GetUrlWithoutQuery(bool relative) const;
56
63 void AppendQueryParameters(const std::string& encodedQueryParameters);
64
65 public:
72 static std::string Decode(const std::string& value);
73
85 static std::string Encode(const std::string& value, const std::string& doNotEncodeSymbols = "");
86
91 Url() = default;
92
99 explicit Url(const std::string& encodedUrl);
100
101 /************* Builder Url functions ****************/
102 /******** API for building Url from scratch. Override state ********/
103
109 void SetScheme(const std::string& scheme) { m_scheme = scheme; }
110
116 void SetHost(const std::string& encodedHost) { m_host = encodedHost; }
117
123 void SetPort(uint16_t port) { m_port = port; }
124
130 void SetPath(const std::string& encodedPath) { m_encodedPath = encodedPath; }
131
139 void SetQueryParameters(std::map<std::string, std::string> queryParameters)
140 {
141 // creates a copy and discard previous
142 m_encodedQueryParameters = std::move(queryParameters);
143 }
144
145 // ===== APIs for mutating URL state: ======
146
152 void AppendPath(const std::string& encodedPath)
153 {
154 if (!m_encodedPath.empty() && m_encodedPath.back() != '/')
155 {
156 m_encodedPath += '/';
157 }
158 m_encodedPath += encodedPath;
159 }
160
171 void AppendQueryParameter(const std::string& encodedKey, const std::string& encodedValue)
172 {
173 m_encodedQueryParameters[encodedKey] = encodedValue;
174 }
175
181 void RemoveQueryParameter(const std::string& encodedKey)
182 {
183 m_encodedQueryParameters.erase(encodedKey);
184 }
185
186 /************** API to read values from Url ***************/
191 const std::string& GetHost() const { return m_host; }
192
198 const std::string& GetPath() const { return m_encodedPath; }
199
209 uint16_t GetPort() const { return m_port; }
210
218 std::map<std::string, std::string> GetQueryParameters() const
219 {
220 return m_encodedQueryParameters;
221 }
222
227 const std::string& GetScheme() const { return m_scheme; }
228
234 std::string GetRelativeUrl() const;
235
241 std::string GetAbsoluteUrl() const;
242 };
243}} // namespace Azure::Core
A map<string, string> with case-insensitive key comparison.
Represents the location where a request will be performed.
Definition url.hpp:46
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:141
Url()=default
Constructs a new, empty URL object.
void SetPort(uint16_t port)
Sets URL port.
Definition url.hpp:123
std::string GetAbsoluteUrl() const
Gets Scheme, host, path and query parameters.
Definition url.cpp:240
void RemoveQueryParameter(const std::string &encodedKey)
Removes an existing query parameter.
Definition url.hpp:181
const std::string & GetHost() const
Gets URL host.
Definition url.hpp:191
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:171
void SetScheme(const std::string &scheme)
Sets URL scheme.
Definition url.hpp:109
const std::string & GetScheme() const
Gets the URL scheme.
Definition url.hpp:227
void SetPath(const std::string &encodedPath)
Sets URL path.
Definition url.hpp:130
uint16_t GetPort() const
Gets the port number set for the URL.
Definition url.hpp:209
const std::string & GetPath() const
Gets the URL path.
Definition url.hpp:198
void SetQueryParameters(std::map< std::string, std::string > queryParameters)
Sets the query parameters from an existing query parameter map.
Definition url.hpp:139
std::string GetRelativeUrl() const
Gets the path and query parameters.
Definition url.cpp:234
std::map< std::string, std::string > GetQueryParameters() const
Gets a copy of the list of query parameters from the URL.
Definition url.hpp:218
static std::string Decode(const std::string &value)
Decodes value by transforming all escaped characters to it's non-encoded value.
Definition url.cpp:97
void SetHost(const std::string &encodedHost)
Sets URL host.
Definition url.hpp:116
void AppendPath(const std::string &encodedPath)
Appends an element of URL path.
Definition url.hpp:152
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57