cesium-native 0.48.0
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
95 std::string_view getFileName() const;
96
105 std::string_view getStem() const;
106
115 std::string_view getExtension() const;
116
122 std::string_view getQuery() const;
123
130 void setPath(const std::string_view& path);
131
138 void setQuery(const std::string_view& queryString);
139
157 static std::string resolve(
158 const std::string& base,
159 const std::string& relative,
160 bool useBaseQuery = false,
161 bool assumeHttpsDefault = true);
180 static std::string addQuery(
181 const std::string& uri,
182 const std::string& key,
183 const std::string& value);
203 static std::string
204 getQueryValue(const std::string& uri, const std::string& key);
205
213 typedef std::string
214 SubstitutionCallbackSignature(const std::string& placeholder);
215
231 const std::string& templateUri,
232 const std::function<SubstitutionCallbackSignature>& substitutionCallback);
233
240 static std::string escape(const std::string& s);
241
249 static std::string unescape(const std::string& s);
250
262 static std::string unixPathToUriPath(const std::string& unixPath);
263
278 static std::string windowsPathToUriPath(const std::string& windowsPath);
279
293 static std::string nativePathToUriPath(const std::string& nativePath);
294
305 static std::string uriPathToUnixPath(const std::string& uriPath);
306
317 static std::string uriPathToWindowsPath(const std::string& uriPath);
318
331 static std::string uriPathToNativePath(const std::string& uriPath);
332
342 static std::string getPath(const std::string& uri);
343
356 static std::string
357 setPath(const std::string& uri, const std::string& newPath);
358
359private:
360 std::optional<ada::url_aggregator> _url = std::nullopt;
361 bool _hasScheme = false;
362};
363
367class UriQuery final {
368public:
378 UriQuery(const std::string_view& queryString) : _params(queryString) {}
386 UriQuery(const Uri& uri) : _params(uri.getQuery()) {}
390 UriQuery() = default;
391
404 std::optional<std::string_view> getValue(const std::string& key) {
405 return this->_params.get(key);
406 }
407
416 void setValue(const std::string& key, const std::string& value) {
417 this->_params.set(key, value);
418 }
419
426 bool hasValue(const std::string& key) { return this->_params.has(key); }
427
433 std::string toQueryString() const { return this->_params.to_string(); }
434
437 inline auto begin() const { return this->_params.begin(); }
439 inline auto end() const { return this->_params.end(); }
441 inline auto front() const { return this->_params.front(); }
443 inline auto back() const { return this->_params.back(); }
444
445private:
446 ada::url_search_params _params;
447};
448
449} // namespace CesiumUtility
auto back() const
Returns the last element in the query parameters.
Definition Uri.h:443
UriQuery()=default
Creates an empty UriQuery object.
auto end() const
Returns an iterator pointing to the end of the query parameters.
Definition Uri.h:439
auto begin() const
Returns an iterator pointing to the beginning of the query parameters.
Definition Uri.h:437
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:404
UriQuery(const std::string_view &queryString)
Creates a UriQuery object from a query string.
Definition Uri.h:378
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:416
UriQuery(const Uri &uri)
Creates a UriQuery object from a Uri instance.
Definition Uri.h:386
std::string toQueryString() const
Converts this object back into a query string, including all modifications that have been made....
Definition Uri.h:433
auto front() const
Returns the first element in the query parameters.
Definition Uri.h:441
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:426
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...
std::string_view getStem() const
Returns the filename portion of the URI without any extension.
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.
std::string_view getExtension() const
Returns the extension portion of the URI, if present.
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_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 SubstitutionCallbackSignature(const std::string &placeholder)
A callback to fill-in a placeholder value in a URL.
Definition Uri.h:214
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...
std::string_view getFileName() const
Returns the filename portion of the URI.
Utility classes for Cesium.