aboutsummaryrefslogtreecommitdiff
path: root/web/server/mime-url-encoding.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'web/server/mime-url-encoding.cxx')
-rw-r--r--web/server/mime-url-encoding.cxx66
1 files changed, 66 insertions, 0 deletions
diff --git a/web/server/mime-url-encoding.cxx b/web/server/mime-url-encoding.cxx
new file mode 100644
index 0000000..fd1e4e8
--- /dev/null
+++ b/web/server/mime-url-encoding.cxx
@@ -0,0 +1,66 @@
+// file : web/server/mime-url-encoding.cxx -*- C++ -*-
+// license : MIT; see accompanying LICENSE file
+
+#include <web/server/mime-url-encoding.hxx>
+
+#include <string>
+#include <iterator> // back_inserter
+
+#include <libbutl/url.mxx>
+
+using namespace std;
+using namespace butl;
+
+namespace web
+{
+ inline static bool
+ encode_query (char& c)
+ {
+ if (c == ' ')
+ {
+ c = '+';
+ return false;
+ }
+
+ return !url::unreserved (c);
+ }
+
+ string
+ mime_url_encode (const char* v, bool query)
+ {
+ return query ? url::encode (v, encode_query) : url::encode (v);
+ }
+
+ string
+ mime_url_encode (const string& v, bool query)
+ {
+ return query ? url::encode (v, encode_query) : url::encode (v);
+ }
+
+ string
+ mime_url_decode (const char* b, const char* e, bool trim, bool query)
+ {
+ if (trim)
+ {
+ for (; b != e && *b == ' '; ++b) ;
+
+ if (b == e)
+ return string ();
+
+ while (*--e == ' ');
+ ++e;
+ }
+
+ string r;
+ if (!query)
+ url::decode (b, e, back_inserter (r));
+ else
+ url::decode (b, e, back_inserter (r),
+ [] (char& c)
+ {
+ if (c == '+')
+ c = ' ';
+ });
+ return r;
+ }
+}