cesium-native 0.44.2
Loading...
Searching...
No Matches
Uri.h
1#pragma once
2
3#include <ada.h>
4#include <ada/url_search_params.h>
5
6#include <functional>
7#include <optional>
8#include <string>
9#include <string_view>
10
11namespace CesiumUtility {
12
22class Uri final {
23public:
30 Uri(const std::string& uri);
42 Uri(const Uri& base, const std::string& relative, bool useBaseQuery = false);
43
48 std::string_view toString() const;
49
53 bool isValid() const;
54
58 operator bool() const { return this->isValid(); }
59
67 std::string_view getScheme() const;
68
77 std::string_view getHost() const;
78
85 std::string_view getPath() const;
86
92 std::string_view getQuery() const;
93
100 void setPath(const std::string_view& path);
101
108 void setQuery(const std::string_view& queryString);
109
127 static std::string resolve(
128 const std::string& base,
129 const std::string& relative,
130 bool useBaseQuery = false,
131 bool assumeHttpsDefault = true);
150 static std::string addQuery(
151 const std::string& uri,
152 const std::string& key,
153 const std::string& value);
173 static std::string
174 getQueryValue(const std::string& uri, const std::string& key);
175
183 typedef std::string
184 SubstitutionCallbackSignature(const std::string& placeholder);
185
201 const std::string& templateUri,
202 const std::function<SubstitutionCallbackSignature>& substitutionCallback);
203
210 static std::string escape(const std::string& s);
211
219 static std::string unescape(const std::string& s);
220
232 static std::string unixPathToUriPath(const std::string& unixPath);
233
248 static std::string windowsPathToUriPath(const std::string& windowsPath);
249
263 static std::string nativePathToUriPath(const std::string& nativePath);
264
275 static std::string uriPathToUnixPath(const std::string& uriPath);
276
287 static std::string uriPathToWindowsPath(const std::string& uriPath);
288
301 static std::string uriPathToNativePath(const std::string& uriPath);
302
312 static std::string getPath(const std::string& uri);
313
326 static std::string
327 setPath(const std::string& uri, const std::string& newPath);
328
329private:
330 std::optional<ada::url_aggregator> _url = std::nullopt;
331 bool _hasScheme = false;
332};
333
337class UriQuery final {
338public:
348 UriQuery(const std::string_view& queryString) : _params(queryString) {}
356 UriQuery(const Uri& uri) : _params(uri.getQuery()) {}
357
370 std::optional<std::string_view> getValue(const std::string& key) {
371 return this->_params.get(key);
372 }
373
382 void setValue(const std::string& key, const std::string& value) {
383 this->_params.set(key, value);
384 }
385
392 bool hasValue(const std::string& key) { return this->_params.has(key); }
393
399 std::string toQueryString() const { return this->_params.to_string(); }
400
403 inline auto begin() const { return this->_params.begin(); }
405 inline auto end() const { return this->_params.end(); }
407 inline auto front() const { return this->_params.front(); }
409 inline auto back() const { return this->_params.back(); }
410
411private:
412 ada::url_search_params _params;
413};
414
415} // namespace CesiumUtility
A class for parsing and manipulating the query string of a URI.
Definition Uri.h:337
auto back() const
Returns the last element in the query parameters.
Definition Uri.h:409
auto end() const
Returns an iterator pointing to the end of the query parameters.
Definition Uri.h:405
auto begin() const
Returns an iterator pointing to the beginning of the query parameters.
Definition Uri.h:403
std::optional< std::string_view > getValue(const std::string &key)
Obtains the value of the given key from the query parameters, if possible.
Definition Uri.h:370
UriQuery(const std::string_view &queryString)
Creates a UriQuery object from a query string.
Definition Uri.h:348
void setValue(const std::string &key, const std::string &value)
Sets the given key in the query parameters to the given value. If the key doesn't exist already,...
Definition Uri.h:382
UriQuery(const Uri &uri)
Creates a UriQuery object from a Uri instance.
Definition Uri.h:356
std::string toQueryString() const
Converts this object back into a query string, including all modifications that have been made....
Definition Uri.h:399
auto front() const
Returns the first element in the query parameters.
Definition Uri.h:407
bool hasValue(const std::string &key)
Returns true if this query string contains a value for the given key, or false otherwise.
Definition Uri.h:392
A class for parsing and manipulating Uniform Resource Identifiers (URIs).
Definition Uri.h:22
static std::string getPath(const std::string &uri)
Gets the path portion of the URI. This will not include path parameters, if present.
static std::string uriPathToWindowsPath(const std::string &uriPath)
Converts the path portion of a URI to a Windows file system path. Percent-encoded characters in the U...
static std::string resolve(const std::string &base, const std::string &relative, bool useBaseQuery=false, bool assumeHttpsDefault=true)
Attempts to resolve a relative URI using a base URI.
static std::string addQuery(const std::string &uri, const std::string &key, const std::string &value)
Adds the given key and value to the query string of a URI. For example, addQuery("https://api....
std::string_view getQuery() const
Gets the query portion of the URI.
static std::string setPath(const std::string &uri, const std::string &newPath)
Sets the path portion of a URI to a new value. The other portions of the URI are left unmodified,...
std::string_view getPath() const
Gets the path portion of the URI. This will not include query parameters, if present.
static std::string nativePathToUriPath(const std::string &nativePath)
Converts a file system path on the current system to a string suitable for use as the path portion of...
static std::string getQueryValue(const std::string &uri, const std::string &key)
Obtains the value of the given key from the query string of the URI, if possible.
static std::string windowsPathToUriPath(const std::string &windowsPath)
Converts a Windows file system path to a string suitable for use as the path portion of a URI....
void setPath(const std::string_view &path)
Sets the path portion of a URI to a new value. The other portions of the URI are left unmodified,...
static std::string substituteTemplateParameters(const std::string &templateUri, const std::function< SubstitutionCallbackSignature > &substitutionCallback)
Substitutes the placeholders in a templated URI with their appropriate values obtained using a specif...
static std::string uriPathToUnixPath(const std::string &uriPath)
Converts the path portion of a URI to a Unix file system path. Percent-encoded characters in the URI ...
std::string_view toString() const
Returns a string representation of the entire URI including path and query parameters.
static std::string escape(const std::string &s)
Escapes a portion of a URI, percent-encoding disallowed characters.
Uri(const Uri &base, const std::string &relative, bool useBaseQuery=false)
Attempts to create a new Uri from a base URI and a relative URI. If the base URI is invalid,...
std::string SubstitutionCallbackSignature(const std::string &placeholder)
A callback to fill-in a placeholder value in a URL.
Definition Uri.h:184
std::string_view getScheme() const
Gets the scheme portion of the URI. If the URI was created without a scheme, this will return an empt...
static std::string unescape(const std::string &s)
Unescapes a portion of a URI, decoding any percent-encoded characters.
void setQuery(const std::string_view &queryString)
Sets the query portion of a URI to a new value. The other portions of the URI are left unmodified.
bool isValid() const
Returns true if this URI has been successfully parsed.
Uri(const std::string &uri)
Attempts to create a new Uri by parsing the given string. If the string fails to parse,...
static std::string uriPathToNativePath(const std::string &uriPath)
Converts the path portion of a URI to a file system path on the current system. Percent-encoded chara...
static std::string unixPathToUriPath(const std::string &unixPath)
Converts a Unix file system path to a string suitable for use as the path portion of a URI....
std::string_view getHost() const
Gets the host portion of the URI. If the URI also specifies a non-default port, it will be included i...
Utility classes for Cesium.