author | Michael Krelin <hacker@klever.net> | 2008-03-02 23:52:14 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-03-02 23:52:14 (UTC) |
commit | 5fd5ecad8c2bd1e8846c11fa9b281f0f4ab8a4a7 (patch) (unidiff) | |
tree | 76ba24c589d0ce7de6cab113787cf7a70572f994 | |
parent | f6482fb9003e4953f838ba4ef2c110190355622f (diff) | |
download | libopkele-5fd5ecad8c2bd1e8846c11fa9b281f0f4ab8a4a7.zip libopkele-5fd5ecad8c2bd1e8846c11fa9b281f0f4ab8a4a7.tar.gz libopkele-5fd5ecad8c2bd1e8846c11fa9b281f0f4ab8a4a7.tar.bz2 |
aded util::url_decode()
Signed-off-by: Michael Krelin <hacker@klever.net>
-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 | |||
@@ -26,24 +26,32 @@ namespace opkele { | |||
26 | * Convert W3C time representation to internal time_t | 26 | * Convert W3C time representation to internal time_t |
27 | * @param w w3c representation | 27 | * @param w w3c representation |
28 | * @return converted time | 28 | * @return converted time |
29 | * @throw failed_conversion in case of error | 29 | * @throw failed_conversion in case of error |
30 | */ | 30 | */ |
31 | time_t w3c_to_time(const string& w); | 31 | time_t w3c_to_time(const string& w); |
32 | 32 | ||
33 | /** | 33 | /** |
34 | * Encode string to the representation suitable for using in URL. | 34 | * Encode string to the representation suitable for using in URL |
35 | * @param str string to encode | 35 | * @param str string to encode |
36 | * @return encoded string | 36 | * @return encoded string |
37 | * @throw failed_conversion in case of failure | 37 | * @throw failed_conversion in case of failure |
38 | */ | 38 | */ |
39 | string url_encode(const string& str); | 39 | string url_encode(const string& str); |
40 | 40 | ||
41 | /** | 41 | /** |
42 | * Decode url-encoded string back to normal | ||
43 | * @param str url-encoded string | ||
44 | * @return decoded string | ||
45 | * @throw failed_conversion in case of failure | ||
46 | */ | ||
47 | string url_decode(const string& str); | ||
48 | |||
49 | /** | ||
42 | * Make string suitable for using as x(ht)ml attribute. | 50 | * Make string suitable for using as x(ht)ml attribute. |
43 | * @param str string to escape | 51 | * @param str string to escape |
44 | * @return escaped string | 52 | * @return escaped string |
45 | */ | 53 | */ |
46 | string attr_escape(const string& str); | 54 | string attr_escape(const string& str); |
47 | 55 | ||
48 | /** | 56 | /** |
49 | * Convert number to string | 57 | * Convert number to string |
diff --git a/lib/util.cc b/lib/util.cc index a6e08e2..3e7f3aa 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -187,16 +187,44 @@ namespace opkele { | |||
187 | 187 | ||
188 | string url_encode(const string& str) { | 188 | string url_encode(const string& str) { |
189 | string rv; | 189 | string rv; |
190 | for_each(str.begin(),str.end(), | 190 | for_each(str.begin(),str.end(), |
191 | __url_encoder(rv)); | 191 | __url_encoder(rv)); |
192 | return rv; | 192 | return rv; |
193 | } | 193 | } |
194 | 194 | ||
195 | string url_decode(const string& str) { | ||
196 | string rv; | ||
197 | back_insert_iterator<string> ii(rv); | ||
198 | for(string::const_iterator i=str.begin(),ie=str.end(); | ||
199 | i!=ie;++i) { | ||
200 | switch(*i) { | ||
201 | case '+': | ||
202 | *(ii++) = ' '; break; | ||
203 | case '%': | ||
204 | ++i; | ||
205 | static char tmp[3] = {0,0,0}; | ||
206 | if(i==ie) | ||
207 | throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); | ||
208 | tmp[0] == *(i++); | ||
209 | if(i==ie) | ||
210 | throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); | ||
211 | tmp[1] == *i; | ||
212 | if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) | ||
213 | throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); | ||
214 | *(ii++) = strtol(tmp,0,16); | ||
215 | break; | ||
216 | default: | ||
217 | *(ii++) = *i; break; | ||
218 | } | ||
219 | } | ||
220 | return rv; | ||
221 | } | ||
222 | |||
195 | string attr_escape(const string& str) { | 223 | string attr_escape(const string& str) { |
196 | static const char *unsafechars = "<>&\n\"'"; | 224 | static const char *unsafechars = "<>&\n\"'"; |
197 | string rv; | 225 | string rv; |
198 | string::size_type p=0; | 226 | string::size_type p=0; |
199 | while(true) { | 227 | while(true) { |
200 | string::size_type us = str.find_first_of(unsafechars,p); | 228 | string::size_type us = str.find_first_of(unsafechars,p); |
201 | if(us==string::npos) { | 229 | if(us==string::npos) { |
202 | if(p!=str.length()) | 230 | if(p!=str.length()) |