X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Futils.cpp;fp=source%2Futils.cpp;h=d550259984f79357ca94449df00a4a53ce8dbdcc;hb=077408f98f08fac1a098a501fffdb22728a57a46;hp=0000000000000000000000000000000000000000;hpb=b06cf07513fdd85e249398ebfce500fbf3dfb6a7;p=libs%2Fnet.git diff --git a/source/utils.cpp b/source/utils.cpp new file mode 100644 index 0000000..d550259 --- /dev/null +++ b/source/utils.cpp @@ -0,0 +1,65 @@ +/* $Id$ + +This file is part of libmsphttp +Copyright © 2009 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include +#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; istr.size()) + throw InvalidParameterValue("Malformed data"); + result+=lexical_cast(str.substr(i+1, 2), "x"); + i+=2; + } + else if(c=='+') + result+=' '; + else + result+=c; + } + return result; +} + +} // namespace Http +} // namespace Msp