aboutsummaryrefslogtreecommitdiff
path: root/web/server/mime-url-encoding.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-03-18 22:17:49 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2020-03-27 17:28:44 +0300
commit35359f038f571dc46de3d14af72a2bc911fb0a24 (patch)
treede3e89d678e78b9efc4d395274fd7ccc68f4a213 /web/server/mime-url-encoding.cxx
parent8ad672cc7211952716ffe1fbf76c179b4f1149e3 (diff)
Implement brep-monitor
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;
+ }
+}