summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2009-09-12 20:58:06 (UTC)
committer Michael Krelin <hacker@klever.net>2009-09-13 11:44:38 (UTC)
commitc5e2a9ce7d7836adaa284dee30b70e04081b0d01 (patch) (unidiff)
tree1be9052fcc77047d1b387d86a7da0c653db10ce3
parent570ab021e0dcf507ecd6b7118151fb90feeab779 (diff)
downloadlibopkele-c5e2a9ce7d7836adaa284dee30b70e04081b0d01.zip
libopkele-c5e2a9ce7d7836adaa284dee30b70e04081b0d01.tar.gz
libopkele-c5e2a9ce7d7836adaa284dee30b70e04081b0d01.tar.bz2
minor url_decode improvements
Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--lib/util.cc7
1 files changed, 3 insertions, 4 deletions
diff --git a/lib/util.cc b/lib/util.cc
index 4028697..02f9473 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -196,53 +196,52 @@ namespace opkele {
196 snprintf(tmp,sizeof(tmp),"%%%02X", 196 snprintf(tmp,sizeof(tmp),"%%%02X",
197 (c&0xff)); 197 (c&0xff));
198 rv += tmp; 198 rv += tmp;
199 } 199 }
200 } 200 }
201 }; 201 };
202 202
203 string url_encode(const string& str) { 203 string url_encode(const string& str) {
204 string rv; 204 string rv;
205 for_each(str.begin(),str.end(), 205 for_each(str.begin(),str.end(),
206 __url_encoder(rv)); 206 __url_encoder(rv));
207 return rv; 207 return rv;
208 } 208 }
209 209
210 string url_decode(const string& str) { 210 string url_decode(const string& str) {
211 string rv; 211 string rv;
212 back_insert_iterator<string> ii(rv); 212 back_insert_iterator<string> ii(rv);
213 char tmp[3]; tmp[2] = 0; 213 char tmp[3]; tmp[2] = 0;
214 for(string::const_iterator i=str.begin(),ie=str.end(); 214 for(string::const_iterator i=str.begin(),ie=str.end();
215 i!=ie;++i) { 215 i!=ie;++i) {
216 switch(*i) { 216 switch(*i) {
217 case '+': 217 case '+':
218 *(ii++) = ' '; break; 218 *(ii++) = ' '; break;
219 case '%': 219 case '%':
220 ++i; 220 if((++i)==ie)
221 if(i==ie)
222 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); 221 throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string");
223 tmp[0] = *(i++); 222 tmp[0] = *i;
224 if(i==ie) 223 if((++i)==ie)
225 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); 224 throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string");
226 tmp[1] = *i; 225 tmp[1] = *i;
227 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) 226 if(!(isxdigit(tmp[0]) && isxdigit(tmp[1])))
228 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); 227 throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string");
229 *(ii++) = (char)strtol(tmp,0,16); 228 *(ii++) = (char)strtol(tmp,0,16);
230 break; 229 break;
231 default: 230 default:
232 *(ii++) = *i; break; 231 *(ii++) = *i; break;
233 } 232 }
234 } 233 }
235 return rv; 234 return rv;
236 } 235 }
237 236
238 string attr_escape(const string& str) { 237 string attr_escape(const string& str) {
239 static const char *unsafechars = "<>&\n\"'"; 238 static const char *unsafechars = "<>&\n\"'";
240 string rv; 239 string rv;
241 string::size_type p=0; 240 string::size_type p=0;
242 while(true) { 241 while(true) {
243 string::size_type us = str.find_first_of(unsafechars,p); 242 string::size_type us = str.find_first_of(unsafechars,p);
244 if(us==string::npos) { 243 if(us==string::npos) {
245 if(p!=str.length()) 244 if(p!=str.length())
246 rv.append(str,p,str.length()-p); 245 rv.append(str,p,str.length()-p);
247 return rv; 246 return rv;
248 } 247 }