+/* $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