summaryrefslogtreecommitdiffabout
path: root/src/util.cc
authorMichael Krelin <hacker@klever.net>2005-01-29 20:14:37 (UTC)
committer Michael Krelin <hacker@klever.net>2005-01-29 20:14:37 (UTC)
commitff4b919683537625f693eedf53006364d0f8444d (patch) (unidiff)
tree4c19e38c0832b16b4ca98ae5af6542d932373eb1 /src/util.cc
parentf9a64a67c89a7566e63ed66c3a69c359abea4dfd (diff)
downloadkingate-0.0.zip
kingate-0.0.tar.gz
kingate-0.0.tar.bz2
initial commit into repository0.0
Diffstat (limited to 'src/util.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--src/util.cc53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc
new file mode 100644
index 0000000..2e2d305
--- a/dev/null
+++ b/src/util.cc
@@ -0,0 +1,53 @@
1#include "kingate/util.h"
2#include "kingate/exception.h"
3
4namespace kingate {
5
6 static const char *safeChars =
7 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8 "abcdefghijklmnopqrstuvwxyz"
9 "0123456789"
10 "_-" ;
11
12 string url_escape(const string& str) {
13 string rv = str;
14 string::size_type screwed = 0;
15 for(;;) {
16 screwed = rv.find_first_not_of(safeChars,screwed);
17 if(screwed == string::npos)
18 break;
19 while(screwed<rv.length() && !strchr(safeChars,rv.at(screwed))) {
20 char danger = rv.at(screwed);
21 if(danger==' ') {
22 rv.replace(screwed++,1,1,'+');
23 }else{
24 static char tmp[4] = {'%',0,0,0};
25 snprintf(&tmp[1],3,"%02X",0xFF&(int)danger);
26 rv.replace(screwed,1,tmp,3);
27 screwed+=3;
28 }
29 }
30 }
31 return rv;
32 }
33 string url_unescape(const string& str) {
34 string rv = str;
35 string::size_type unscrewed = 0;
36 for(;;) {
37 unscrewed = rv.find_first_of("%+",unscrewed);
38 if(unscrewed == string::npos)
39 break;
40 if(rv.at(unscrewed)=='+') {
41 rv.replace(unscrewed++,1,1,' ');
42 }else{
43 if((rv.length()-unscrewed)<3)
44 throw exception(CODEPOINT,"incorrectly escaped string");
45 // XXX: ensure it's hex?
46 int danger = strtol(rv.substr(unscrewed+1,2).c_str(),NULL,16);
47 rv.replace(unscrewed,3,1,danger);
48 unscrewed++;
49 }
50 }
51 return rv;
52 }
53}