diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2021-11-17 22:34:57 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2021-11-17 22:34:57 +0100 |
| commit | 6232d13f5321b87ddf12a1aa36b4545da45f173d (patch) | |
| tree | 23f3316470a14136debd9d02f9e920ca2b06f4cc /src/urlutil.hh | |
Travel3: Simple image and video display site
Reads the images and videos from filesystem and builds a site in
memroy.
Diffstat (limited to 'src/urlutil.hh')
| -rw-r--r-- | src/urlutil.hh | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/urlutil.hh b/src/urlutil.hh new file mode 100644 index 0000000..5ae170c --- /dev/null +++ b/src/urlutil.hh @@ -0,0 +1,60 @@ +#ifndef URLUTIL_HH +#define URLUTIL_HH + +#include <string> +#include <string_view> +#include <unordered_map> + +namespace url { + +enum class EscapeFlags : unsigned { + // Default encodes all non-unreserved characters + // (not the same as all reserved) to be safe. + DEFAULT = 0, + // Same as DEFAULT but doesn't encode SLASH, useful when the in data + // is a path. + KEEP_SLASH = 1, +}; + +std::string escape(std::string_view str, + EscapeFlags flags = EscapeFlags::DEFAULT); +void escape(std::string_view str, std::string& out, + EscapeFlags flags = EscapeFlags::DEFAULT); + +std::string unescape(std::string_view str); +void unescape(std::string_view str, std::string& out); + +constexpr EscapeFlags operator&(EscapeFlags a, EscapeFlags b) noexcept { + using utype = typename std::underlying_type<EscapeFlags>::type; + return static_cast<EscapeFlags>( + static_cast<utype>(a) & static_cast<utype>(b)); +} + +constexpr EscapeFlags operator|(EscapeFlags a, EscapeFlags b) noexcept { + using utype = typename std::underlying_type<EscapeFlags>::type; + return static_cast<EscapeFlags>( + static_cast<utype>(a) | static_cast<utype>(b)); +} + +constexpr EscapeFlags operator^(EscapeFlags a, EscapeFlags b) noexcept { + using utype = typename std::underlying_type<EscapeFlags>::type; + return static_cast<EscapeFlags>( + static_cast<utype>(a) ^ static_cast<utype>(b)); +} + +constexpr EscapeFlags operator~(EscapeFlags a) noexcept { + using utype = typename std::underlying_type<EscapeFlags>::type; + return static_cast<EscapeFlags>(~static_cast<utype>(a)); +} + +void split_and_unescape_path_and_query( + std::string_view url, + std::string& path, + std::unordered_map<std::string, std::string>& query); + +std::unordered_map<std::string, std::string> expand_and_unescape_query( + std::string_view query); + +} // namespace url + +#endif // URLUTIL_HH |
