author | Michael Krelin <hacker@klever.net> | 2008-03-02 20:23:40 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-03-02 20:23:40 (UTC) |
commit | da3f84153be2a93da7ffc49af33b29b9725fac38 (patch) (unidiff) | |
tree | bcacc4c38a53b70c679fa69fbf2577da2802a197 /lib/util.cc | |
parent | f47e336b569739bdde8e9add96ff2c46f97257fb (diff) | |
download | libopkele-da3f84153be2a93da7ffc49af33b29b9725fac38.zip libopkele-da3f84153be2a93da7ffc49af33b29b9725fac38.tar.gz libopkele-da3f84153be2a93da7ffc49af33b29b9725fac38.tar.bz2 |
made util::url_encode refrain from encoding unreserved chars
as per rfc 3986
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/util.cc | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/lib/util.cc b/lib/util.cc index 29e6738..a6e08e2 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,356 +1,383 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <cctype> | 3 | #include <cctype> |
4 | #include <cstring> | 4 | #include <cstring> |
5 | #include <vector> | 5 | #include <vector> |
6 | #include <string> | 6 | #include <string> |
7 | #include <stack> | 7 | #include <stack> |
8 | #include <algorithm> | 8 | #include <algorithm> |
9 | #include <openssl/bio.h> | 9 | #include <openssl/bio.h> |
10 | #include <openssl/evp.h> | 10 | #include <openssl/evp.h> |
11 | #include <openssl/sha.h> | 11 | #include <openssl/sha.h> |
12 | #include <openssl/hmac.h> | 12 | #include <openssl/hmac.h> |
13 | #include <curl/curl.h> | ||
14 | #include <opkele/util.h> | 13 | #include <opkele/util.h> |
15 | #include <opkele/exception.h> | 14 | #include <opkele/exception.h> |
16 | #include <opkele/data.h> | 15 | #include <opkele/data.h> |
17 | #include <opkele/debug.h> | 16 | #include <opkele/debug.h> |
18 | 17 | ||
19 | #include <config.h> | 18 | #include <config.h> |
20 | #ifdef HAVE_DEMANGLE | 19 | #ifdef HAVE_DEMANGLE |
21 | # include <cxxabi.h> | 20 | # include <cxxabi.h> |
22 | #endif | 21 | #endif |
23 | 22 | ||
24 | namespace opkele { | 23 | namespace opkele { |
25 | using namespace std; | 24 | using namespace std; |
26 | 25 | ||
27 | namespace util { | 26 | namespace util { |
28 | 27 | ||
29 | /* | 28 | /* |
30 | * base64 | 29 | * base64 |
31 | */ | 30 | */ |
32 | string encode_base64(const void *data,size_t length) { | 31 | string encode_base64(const void *data,size_t length) { |
33 | BIO *b64 = 0, *bmem = 0; | 32 | BIO *b64 = 0, *bmem = 0; |
34 | try { | 33 | try { |
35 | b64 = BIO_new(BIO_f_base64()); | 34 | b64 = BIO_new(BIO_f_base64()); |
36 | if(!b64) | 35 | if(!b64) |
37 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 36 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
38 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 37 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
39 | bmem = BIO_new(BIO_s_mem()); | 38 | bmem = BIO_new(BIO_s_mem()); |
40 | BIO_set_flags(b64,BIO_CLOSE); | 39 | BIO_set_flags(b64,BIO_CLOSE); |
41 | if(!bmem) | 40 | if(!bmem) |
42 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
43 | BIO_push(b64,bmem); | 42 | BIO_push(b64,bmem); |
44 | if(((size_t)BIO_write(b64,data,length))!=length) | 43 | if(((size_t)BIO_write(b64,data,length))!=length) |
45 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
46 | if(BIO_flush(b64)!=1) | 45 | if(BIO_flush(b64)!=1) |
47 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | 46 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); |
48 | char *rvd; | 47 | char *rvd; |
49 | long rvl = BIO_get_mem_data(bmem,&rvd); | 48 | long rvl = BIO_get_mem_data(bmem,&rvd); |
50 | string rv(rvd,rvl); | 49 | string rv(rvd,rvl); |
51 | BIO_free_all(b64); | 50 | BIO_free_all(b64); |
52 | return rv; | 51 | return rv; |
53 | }catch(...) { | 52 | }catch(...) { |
54 | if(b64) BIO_free_all(b64); | 53 | if(b64) BIO_free_all(b64); |
55 | throw; | 54 | throw; |
56 | } | 55 | } |
57 | } | 56 | } |
58 | 57 | ||
59 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 58 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
60 | BIO *b64 = 0, *bmem = 0; | 59 | BIO *b64 = 0, *bmem = 0; |
61 | rv.clear(); | 60 | rv.clear(); |
62 | try { | 61 | try { |
63 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | 62 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); |
64 | if(!bmem) | 63 | if(!bmem) |
65 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
66 | b64 = BIO_new(BIO_f_base64()); | 65 | b64 = BIO_new(BIO_f_base64()); |
67 | if(!b64) | 66 | if(!b64) |
68 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 67 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
69 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 68 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
70 | BIO_push(b64,bmem); | 69 | BIO_push(b64,bmem); |
71 | unsigned char tmp[512]; | 70 | unsigned char tmp[512]; |
72 | size_t rb = 0; | 71 | size_t rb = 0; |
73 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 72 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
74 | rv.insert(rv.end(),tmp,&tmp[rb]); | 73 | rv.insert(rv.end(),tmp,&tmp[rb]); |
75 | BIO_free_all(b64); | 74 | BIO_free_all(b64); |
76 | }catch(...) { | 75 | }catch(...) { |
77 | if(b64) BIO_free_all(b64); | 76 | if(b64) BIO_free_all(b64); |
78 | throw; | 77 | throw; |
79 | } | 78 | } |
80 | } | 79 | } |
81 | 80 | ||
82 | /* | 81 | /* |
83 | * big numerics | 82 | * big numerics |
84 | */ | 83 | */ |
85 | 84 | ||
86 | BIGNUM *base64_to_bignum(const string& b64) { | 85 | BIGNUM *base64_to_bignum(const string& b64) { |
87 | vector<unsigned char> bin; | 86 | vector<unsigned char> bin; |
88 | decode_base64(b64,bin); | 87 | decode_base64(b64,bin); |
89 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 88 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
90 | if(!rv) | 89 | if(!rv) |
91 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 90 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
92 | return rv; | 91 | return rv; |
93 | } | 92 | } |
94 | 93 | ||
95 | BIGNUM *dec_to_bignum(const string& dec) { | 94 | BIGNUM *dec_to_bignum(const string& dec) { |
96 | BIGNUM *rv = 0; | 95 | BIGNUM *rv = 0; |
97 | if(!BN_dec2bn(&rv,dec.c_str())) | 96 | if(!BN_dec2bn(&rv,dec.c_str())) |
98 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
99 | return rv; | 98 | return rv; |
100 | } | 99 | } |
101 | 100 | ||
102 | string bignum_to_base64(const BIGNUM *bn) { | 101 | string bignum_to_base64(const BIGNUM *bn) { |
103 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
104 | unsigned char *binptr = &(bin.front())+1; | 103 | unsigned char *binptr = &(bin.front())+1; |
105 | int l = BN_bn2bin(bn,binptr); | 104 | int l = BN_bn2bin(bn,binptr); |
106 | if(l && (*binptr)&0x80){ | 105 | if(l && (*binptr)&0x80){ |
107 | (*(--binptr)) = 0; ++l; | 106 | (*(--binptr)) = 0; ++l; |
108 | } | 107 | } |
109 | return encode_base64(binptr,l); | 108 | return encode_base64(binptr,l); |
110 | } | 109 | } |
111 | 110 | ||
112 | /* | 111 | /* |
113 | * w3c times | 112 | * w3c times |
114 | */ | 113 | */ |
115 | 114 | ||
116 | string time_to_w3c(time_t t) { | 115 | string time_to_w3c(time_t t) { |
117 | struct tm tm_t; | 116 | struct tm tm_t; |
118 | if(!gmtime_r(&t,&tm_t)) | 117 | if(!gmtime_r(&t,&tm_t)) |
119 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
120 | char rv[25]; | 119 | char rv[25]; |
121 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 120 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
122 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
123 | return rv; | 122 | return rv; |
124 | } | 123 | } |
125 | 124 | ||
126 | time_t w3c_to_time(const string& w) { | 125 | time_t w3c_to_time(const string& w) { |
127 | int fraction; | 126 | int fraction; |
128 | struct tm tm_t; | 127 | struct tm tm_t; |
129 | memset(&tm_t,0,sizeof(tm_t)); | 128 | memset(&tm_t,0,sizeof(tm_t)); |
130 | if( ( | 129 | if( ( |
131 | sscanf( | 130 | sscanf( |
132 | w.c_str(), | 131 | w.c_str(), |
133 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
134 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 133 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
135 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 134 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
136 | ) != 6 | 135 | ) != 6 |
137 | ) && ( | 136 | ) && ( |
138 | sscanf( | 137 | sscanf( |
139 | w.c_str(), | 138 | w.c_str(), |
140 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
141 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 140 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
142 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, | 141 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
143 | &fraction | 142 | &fraction |
144 | ) != 7 | 143 | ) != 7 |
145 | ) ) | 144 | ) ) |
146 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
147 | tm_t.tm_mon--; | 146 | tm_t.tm_mon--; |
148 | tm_t.tm_year-=1900; | 147 | tm_t.tm_year-=1900; |
149 | time_t rv = mktime(&tm_t); | 148 | time_t rv = mktime(&tm_t); |
150 | if(rv==(time_t)-1) | 149 | if(rv==(time_t)-1) |
151 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
152 | return rv-timezone; | 151 | return rv-timezone; |
153 | } | 152 | } |
154 | 153 | ||
155 | /* | 154 | /* |
156 | * | 155 | * |
157 | */ | 156 | */ |
158 | 157 | ||
158 | static inline bool isrfc3986unreserved(int c) { | ||
159 | if(c<'-') return false; | ||
160 | if(c<='.') return true; | ||
161 | if(c<'0') return false; if(c<='9') return true; | ||
162 | if(c<'A') return false; if(c<='Z') return true; | ||
163 | if(c<'_') return false; | ||
164 | if(c=='_') return true; | ||
165 | if(c<'a') return false; if(c<='z') return true; | ||
166 | if(c=='~') return true; | ||
167 | return false; | ||
168 | } | ||
169 | |||
170 | struct __url_encoder : public unary_function<char,void> { | ||
171 | public: | ||
172 | string& rv; | ||
173 | |||
174 | __url_encoder(string& r) : rv(r) { } | ||
175 | |||
176 | result_type operator()(argument_type c) { | ||
177 | if(isrfc3986unreserved(c)) | ||
178 | rv += c; | ||
179 | else{ | ||
180 | char tmp[4]; | ||
181 | snprintf(tmp,sizeof(tmp),"%%%02X", | ||
182 | (c&0xff)); | ||
183 | rv += tmp; | ||
184 | } | ||
185 | } | ||
186 | }; | ||
187 | |||
159 | string url_encode(const string& str) { | 188 | string url_encode(const string& str) { |
160 | char * t = curl_escape(str.c_str(),str.length()); | 189 | string rv; |
161 | if(!t) | 190 | for_each(str.begin(),str.end(), |
162 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 191 | __url_encoder(rv)); |
163 | string rv(t); | ||
164 | curl_free(t); | ||
165 | return rv; | 192 | return rv; |
166 | } | 193 | } |
167 | 194 | ||
168 | string attr_escape(const string& str) { | 195 | string attr_escape(const string& str) { |
169 | static const char *unsafechars = "<>&\n\"'"; | 196 | static const char *unsafechars = "<>&\n\"'"; |
170 | string rv; | 197 | string rv; |
171 | string::size_type p=0; | 198 | string::size_type p=0; |
172 | while(true) { | 199 | while(true) { |
173 | string::size_type us = str.find_first_of(unsafechars,p); | 200 | string::size_type us = str.find_first_of(unsafechars,p); |
174 | if(us==string::npos) { | 201 | if(us==string::npos) { |
175 | if(p!=str.length()) | 202 | if(p!=str.length()) |
176 | rv.append(str,p,str.length()-p); | 203 | rv.append(str,p,str.length()-p); |
177 | return rv; | 204 | return rv; |
178 | } | 205 | } |
179 | rv.append(str,p,us-p); | 206 | rv.append(str,p,us-p); |
180 | rv += "&#"; | 207 | rv += "&#"; |
181 | rv += long_to_string((long)str[us]); | 208 | rv += long_to_string((long)str[us]); |
182 | rv += ';'; | 209 | rv += ';'; |
183 | p = us+1; | 210 | p = us+1; |
184 | } | 211 | } |
185 | } | 212 | } |
186 | 213 | ||
187 | string long_to_string(long l) { | 214 | string long_to_string(long l) { |
188 | char rv[32]; | 215 | char rv[32]; |
189 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 216 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
190 | if(r<0 || r>=(int)sizeof(rv)) | 217 | if(r<0 || r>=(int)sizeof(rv)) |
191 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 218 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
192 | return rv; | 219 | return rv; |
193 | } | 220 | } |
194 | 221 | ||
195 | long string_to_long(const string& s) { | 222 | long string_to_long(const string& s) { |
196 | char *endptr = 0; | 223 | char *endptr = 0; |
197 | long rv = strtol(s.c_str(),&endptr,10); | 224 | long rv = strtol(s.c_str(),&endptr,10); |
198 | if((!endptr) || endptr==s.c_str()) | 225 | if((!endptr) || endptr==s.c_str()) |
199 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 226 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
200 | return rv; | 227 | return rv; |
201 | } | 228 | } |
202 | 229 | ||
203 | /* | 230 | /* |
204 | * Normalize URL according to the rules, described in rfc 3986, section 6 | 231 | * Normalize URL according to the rules, described in rfc 3986, section 6 |
205 | * | 232 | * |
206 | * - uppercase hex triplets (e.g. %ab -> %AB) | 233 | * - uppercase hex triplets (e.g. %ab -> %AB) |
207 | * - lowercase scheme and host | 234 | * - lowercase scheme and host |
208 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, | 235 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, |
209 | * that is - [:alpha:][:digit:]._~- | 236 | * that is - [:alpha:][:digit:]._~- |
210 | * - remove dot segments | 237 | * - remove dot segments |
211 | * - remove empty and default ports | 238 | * - remove empty and default ports |
212 | * - if there's no path component, add '/' | 239 | * - if there's no path component, add '/' |
213 | */ | 240 | */ |
214 | string rfc_3986_normalize_uri(const string& uri) { | 241 | string rfc_3986_normalize_uri(const string& uri) { |
215 | string rv; | 242 | string rv; |
216 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); | 243 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); |
217 | if(ns==string::npos) | 244 | if(ns==string::npos) |
218 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); | 245 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); |
219 | string::size_type colon = uri.find(':',ns); | 246 | string::size_type colon = uri.find(':',ns); |
220 | if(colon==string::npos) | 247 | if(colon==string::npos) |
221 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); | 248 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); |
222 | transform( | 249 | transform( |
223 | uri.begin()+ns, uri.begin()+colon+1, | 250 | uri.begin()+ns, uri.begin()+colon+1, |
224 | back_inserter(rv), ::tolower ); | 251 | back_inserter(rv), ::tolower ); |
225 | bool s; | 252 | bool s; |
226 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; | 253 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; |
227 | if(ul <= (colon+3)) | 254 | if(ul <= (colon+3)) |
228 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); | 255 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); |
229 | if(uri[colon+1]!='/' || uri[colon+2]!='/') | 256 | if(uri[colon+1]!='/' || uri[colon+2]!='/') |
230 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); | 257 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); |
231 | if(rv=="http:") | 258 | if(rv=="http:") |
232 | s = false; | 259 | s = false; |
233 | else if(rv=="https:") | 260 | else if(rv=="https:") |
234 | s = true; | 261 | s = true; |
235 | else{ | 262 | else{ |
236 | /* TODO: support more schemes. e.g. xri. How do we normalize | 263 | /* TODO: support more schemes. e.g. xri. How do we normalize |
237 | * xri? | 264 | * xri? |
238 | */ | 265 | */ |
239 | rv.append(uri,colon+1,ul-colon-1); | 266 | rv.append(uri,colon+1,ul-colon-1); |
240 | return rv; | 267 | return rv; |
241 | } | 268 | } |
242 | rv += "//"; | 269 | rv += "//"; |
243 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); | 270 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); |
244 | if(interesting==string::npos) { | 271 | if(interesting==string::npos) { |
245 | transform( | 272 | transform( |
246 | uri.begin()+colon+3,uri.begin()+ul, | 273 | uri.begin()+colon+3,uri.begin()+ul, |
247 | back_inserter(rv), ::tolower ); | 274 | back_inserter(rv), ::tolower ); |
248 | rv += '/'; return rv; | 275 | rv += '/'; return rv; |
249 | } | 276 | } |
250 | transform( | 277 | transform( |
251 | uri.begin()+colon+3,uri.begin()+interesting, | 278 | uri.begin()+colon+3,uri.begin()+interesting, |
252 | back_inserter(rv), ::tolower ); | 279 | back_inserter(rv), ::tolower ); |
253 | bool qf = false; | 280 | bool qf = false; |
254 | char ic = uri[interesting]; | 281 | char ic = uri[interesting]; |
255 | if(ic==':') { | 282 | if(ic==':') { |
256 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 283 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
257 | const char *nptr = uri.data()+interesting+1; | 284 | const char *nptr = uri.data()+interesting+1; |
258 | char *eptr = 0; | 285 | char *eptr = 0; |
259 | long port = strtol(nptr,&eptr,10); | 286 | long port = strtol(nptr,&eptr,10); |
260 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 287 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
261 | char tmp[8]; | 288 | char tmp[8]; |
262 | snprintf(tmp,sizeof(tmp),":%ld",port); | 289 | snprintf(tmp,sizeof(tmp),":%ld",port); |
263 | rv += tmp; | 290 | rv += tmp; |
264 | } | 291 | } |
265 | if(ni==string::npos) { | 292 | if(ni==string::npos) { |
266 | rv += '/'; return rv; | 293 | rv += '/'; return rv; |
267 | } | 294 | } |
268 | interesting = ni; | 295 | interesting = ni; |
269 | }else if(ic!='/') { | 296 | }else if(ic!='/') { |
270 | rv += '/'; rv += ic; | 297 | rv += '/'; rv += ic; |
271 | qf = true; | 298 | qf = true; |
272 | ++interesting; | 299 | ++interesting; |
273 | } | 300 | } |
274 | string::size_type n = interesting; | 301 | string::size_type n = interesting; |
275 | char tmp[3] = { 0,0,0 }; | 302 | char tmp[3] = { 0,0,0 }; |
276 | stack<string::size_type> psegs; psegs.push(rv.length()); | 303 | stack<string::size_type> psegs; psegs.push(rv.length()); |
277 | string pseg; | 304 | string pseg; |
278 | for(;n<ul;) { | 305 | for(;n<ul;) { |
279 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 306 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
280 | if(unsafe==string::npos) { | 307 | if(unsafe==string::npos) { |
281 | pseg.append(uri,n,ul-n-1); n = ul-1; | 308 | pseg.append(uri,n,ul-n-1); n = ul-1; |
282 | }else{ | 309 | }else{ |
283 | pseg.append(uri,n,unsafe-n); | 310 | pseg.append(uri,n,unsafe-n); |
284 | n = unsafe; | 311 | n = unsafe; |
285 | } | 312 | } |
286 | char c = uri[n++]; | 313 | char c = uri[n++]; |
287 | if(c=='%') { | 314 | if(c=='%') { |
288 | if((n+1)>=ul) | 315 | if((n+1)>=ul) |
289 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); | 316 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); |
290 | tmp[0] = uri[n++]; | 317 | tmp[0] = uri[n++]; |
291 | tmp[1] = uri[n++]; | 318 | tmp[1] = uri[n++]; |
292 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) | 319 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) |
293 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 320 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
294 | int cc = strtol(tmp,0,16); | 321 | int cc = strtol(tmp,0,16); |
295 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 322 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
296 | pseg += cc; | 323 | pseg += cc; |
297 | else{ | 324 | else{ |
298 | pseg += '%'; | 325 | pseg += '%'; |
299 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); | 326 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); |
300 | } | 327 | } |
301 | }else if(qf) { | 328 | }else if(qf) { |
302 | rv += pseg; rv += c; | 329 | rv += pseg; rv += c; |
303 | pseg.clear(); | 330 | pseg.clear(); |
304 | }else if(n>=ul || strchr("?/#",c)) { | 331 | }else if(n>=ul || strchr("?/#",c)) { |
305 | if(pseg.empty() || pseg==".") { | 332 | if(pseg.empty() || pseg==".") { |
306 | }else if(pseg=="..") { | 333 | }else if(pseg=="..") { |
307 | if(psegs.size()>1) { | 334 | if(psegs.size()>1) { |
308 | rv.resize(psegs.top()); psegs.pop(); | 335 | rv.resize(psegs.top()); psegs.pop(); |
309 | } | 336 | } |
310 | }else{ | 337 | }else{ |
311 | psegs.push(rv.length()); | 338 | psegs.push(rv.length()); |
312 | if(c!='/') { | 339 | if(c!='/') { |
313 | pseg += c; | 340 | pseg += c; |
314 | qf = true; | 341 | qf = true; |
315 | } | 342 | } |
316 | rv += '/'; rv += pseg; | 343 | rv += '/'; rv += pseg; |
317 | } | 344 | } |
318 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 345 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
319 | rv += '/'; | 346 | rv += '/'; |
320 | if(n<ul) | 347 | if(n<ul) |
321 | qf = true; | 348 | qf = true; |
322 | }else if(strchr("?#",c)) { | 349 | }else if(strchr("?#",c)) { |
323 | if(psegs.size()==1 && psegs.top()==rv.length()) | 350 | if(psegs.size()==1 && psegs.top()==rv.length()) |
324 | rv += '/'; | 351 | rv += '/'; |
325 | if(pseg.empty()) | 352 | if(pseg.empty()) |
326 | rv += c; | 353 | rv += c; |
327 | qf = true; | 354 | qf = true; |
328 | } | 355 | } |
329 | pseg.clear(); | 356 | pseg.clear(); |
330 | }else{ | 357 | }else{ |
331 | pseg += c; | 358 | pseg += c; |
332 | } | 359 | } |
333 | } | 360 | } |
334 | if(!pseg.empty()) { | 361 | if(!pseg.empty()) { |
335 | if(!qf) rv += '/'; | 362 | if(!qf) rv += '/'; |
336 | rv += pseg; | 363 | rv += pseg; |
337 | } | 364 | } |
338 | return rv; | 365 | return rv; |
339 | } | 366 | } |
340 | 367 | ||
341 | string& strip_uri_fragment_part(string& u) { | 368 | string& strip_uri_fragment_part(string& u) { |
342 | string::size_type q = u.find('?'), f = u.find('#'); | 369 | string::size_type q = u.find('?'), f = u.find('#'); |
343 | if(q==string::npos) { | 370 | if(q==string::npos) { |
344 | if(f!=string::npos) | 371 | if(f!=string::npos) |
345 | u.erase(f); | 372 | u.erase(f); |
346 | }else{ | 373 | }else{ |
347 | if(f!=string::npos) { | 374 | if(f!=string::npos) { |
348 | if(f<q) | 375 | if(f<q) |
349 | u.erase(f,q-f); | 376 | u.erase(f,q-f); |
350 | else | 377 | else |
351 | u.erase(f); | 378 | u.erase(f); |
352 | } | 379 | } |
353 | } | 380 | } |
354 | return u; | 381 | return u; |
355 | } | 382 | } |
356 | 383 | ||