]> git.tdb.fi Git - libs/net.git/blobdiff - source/utils.cpp
Support handling requests asynchronously
[libs/net.git] / source / utils.cpp
diff --git a/source/utils.cpp b/source/utils.cpp
new file mode 100644 (file)
index 0000000..d550259
--- /dev/null
@@ -0,0 +1,65 @@
+/* $Id$
+
+This file is part of libmsphttp
+Copyright © 2009  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <msp/strings/formatter.h>
+#include "utils.h"
+
+using namespace std;
+
+namespace {
+
+const char reserved[]=" :/?#[]@!$&'()*+,;=%";
+
+bool is_reserved(char c)
+{
+       for(const char *r=reserved; *r; ++r)
+               if(c==*r)
+                       return true;
+       return false;
+}
+
+}
+
+namespace Msp {
+namespace Http {
+
+string urlencode(const string &str)
+{
+       string result;
+       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
+       {
+               if(is_reserved(*i))
+                       result+=format("%%%02X", *i);
+               else
+                       result+=*i;
+       }
+       return result;
+}
+
+string urldecode(const string &str)
+{
+       string result;
+       for(unsigned i=0; i<str.size(); ++i)
+       {
+               char c=str[i];
+               if(c=='%')
+               {
+                       if(i+3>str.size())
+                               throw InvalidParameterValue("Malformed data");
+                       result+=lexical_cast<unsigned char>(str.substr(i+1, 2), "x");
+                       i+=2;
+               }
+               else if(c=='+')
+                       result+=' ';
+               else
+                       result+=c;
+       }
+       return result;
+}
+
+} // namespace Http
+} // namespace Msp