summaryrefslogtreecommitdiff
path: root/src/urlutil.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/urlutil.hh')
-rw-r--r--src/urlutil.hh60
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