-rw-r--r-- | include/opkele/util.h | 10 | ||||
-rw-r--r-- | lib/util.cc | 28 |
2 files changed, 37 insertions, 1 deletions
diff --git a/include/opkele/util.h b/include/opkele/util.h index fd974a1..0130bff 100644 --- a/include/opkele/util.h +++ b/include/opkele/util.h @@ -18,40 +18,48 @@ namespace opkele { /** * Convert internal time representation to w3c format * @param t internal representation * @return w3c time * @throw failed_conversion in case of error */ string time_to_w3c(time_t t); /** * Convert W3C time representation to internal time_t * @param w w3c representation * @return converted time * @throw failed_conversion in case of error */ time_t w3c_to_time(const string& w); /** - * Encode string to the representation suitable for using in URL. + * Encode string to the representation suitable for using in URL * @param str string to encode * @return encoded string * @throw failed_conversion in case of failure */ string url_encode(const string& str); /** + * Decode url-encoded string back to normal + * @param str url-encoded string + * @return decoded string + * @throw failed_conversion in case of failure + */ + string url_decode(const string& str); + + /** * Make string suitable for using as x(ht)ml attribute. * @param str string to escape * @return escaped string */ string attr_escape(const string& str); /** * Convert number to string * @param l number * @return string representation * @throw failed_conversion in case of failure */ string long_to_string(long l); /** * Convert string to number * @param s string, containing the number diff --git a/lib/util.cc b/lib/util.cc index a6e08e2..3e7f3aa 100644 --- a/lib/util.cc +++ b/lib/util.cc @@ -179,32 +179,60 @@ namespace opkele { else{ char tmp[4]; snprintf(tmp,sizeof(tmp),"%%%02X", (c&0xff)); rv += tmp; } } }; string url_encode(const string& str) { string rv; for_each(str.begin(),str.end(), __url_encoder(rv)); return rv; } + string url_decode(const string& str) { + string rv; + back_insert_iterator<string> ii(rv); + for(string::const_iterator i=str.begin(),ie=str.end(); + i!=ie;++i) { + switch(*i) { + case '+': + *(ii++) = ' '; break; + case '%': + ++i; + static char tmp[3] = {0,0,0}; + if(i==ie) + throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); + tmp[0] == *(i++); + if(i==ie) + throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); + tmp[1] == *i; + if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) + throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); + *(ii++) = strtol(tmp,0,16); + break; + default: + *(ii++) = *i; break; + } + } + return rv; + } + string attr_escape(const string& str) { static const char *unsafechars = "<>&\n\"'"; string rv; string::size_type p=0; while(true) { string::size_type us = str.find_first_of(unsafechars,p); if(us==string::npos) { if(p!=str.length()) rv.append(str,p,str.length()-p); return rv; } rv.append(str,p,us-p); rv += "&#"; rv += long_to_string((long)str[us]); rv += ';'; p = us+1; |