author | Michael Krelin <hacker@klever.net> | 2008-02-19 23:48:32 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-02-19 23:48:32 (UTC) |
commit | daf2d4bcb4a31df6b46d3da7a33ee3f98d85e464 (patch) (unidiff) | |
tree | 7d929285bc296777c63d4f482c7bb07f8541bce2 /lib/util.cc | |
parent | 42e4fb613d190508b3e8b8993d233044eeea4d20 (diff) | |
download | libopkele-daf2d4bcb4a31df6b46d3da7a33ee3f98d85e464.zip libopkele-daf2d4bcb4a31df6b46d3da7a33ee3f98d85e464.tar.gz libopkele-daf2d4bcb4a31df6b46d3da7a33ee3f98d85e464.tar.bz2 |
added an identifier normalization utility function
* moved iname leader characters and whitespace characters strings to
opkele::data namespace
* added opkele::util::normalize_identifier() function
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/util.cc | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/lib/util.cc b/lib/util.cc index bb8a2e8..29e6738 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,435 +1,470 @@ | |||
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> | 13 | #include <curl/curl.h> |
14 | #include <opkele/util.h> | 14 | #include <opkele/util.h> |
15 | #include <opkele/exception.h> | 15 | #include <opkele/exception.h> |
16 | #include <opkele/data.h> | ||
16 | #include <opkele/debug.h> | 17 | #include <opkele/debug.h> |
17 | 18 | ||
18 | #include <config.h> | 19 | #include <config.h> |
19 | #ifdef HAVE_DEMANGLE | 20 | #ifdef HAVE_DEMANGLE |
20 | # include <cxxabi.h> | 21 | # include <cxxabi.h> |
21 | #endif | 22 | #endif |
22 | 23 | ||
23 | namespace opkele { | 24 | namespace opkele { |
24 | using namespace std; | 25 | using namespace std; |
25 | 26 | ||
26 | namespace util { | 27 | namespace util { |
27 | 28 | ||
28 | /* | 29 | /* |
29 | * base64 | 30 | * base64 |
30 | */ | 31 | */ |
31 | string encode_base64(const void *data,size_t length) { | 32 | string encode_base64(const void *data,size_t length) { |
32 | BIO *b64 = 0, *bmem = 0; | 33 | BIO *b64 = 0, *bmem = 0; |
33 | try { | 34 | try { |
34 | b64 = BIO_new(BIO_f_base64()); | 35 | b64 = BIO_new(BIO_f_base64()); |
35 | if(!b64) | 36 | if(!b64) |
36 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 37 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
37 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 38 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
38 | bmem = BIO_new(BIO_s_mem()); | 39 | bmem = BIO_new(BIO_s_mem()); |
39 | BIO_set_flags(b64,BIO_CLOSE); | 40 | BIO_set_flags(b64,BIO_CLOSE); |
40 | if(!bmem) | 41 | if(!bmem) |
41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 42 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
42 | BIO_push(b64,bmem); | 43 | BIO_push(b64,bmem); |
43 | if(((size_t)BIO_write(b64,data,length))!=length) | 44 | if(((size_t)BIO_write(b64,data,length))!=length) |
44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 45 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
45 | if(BIO_flush(b64)!=1) | 46 | if(BIO_flush(b64)!=1) |
46 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | 47 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); |
47 | char *rvd; | 48 | char *rvd; |
48 | long rvl = BIO_get_mem_data(bmem,&rvd); | 49 | long rvl = BIO_get_mem_data(bmem,&rvd); |
49 | string rv(rvd,rvl); | 50 | string rv(rvd,rvl); |
50 | BIO_free_all(b64); | 51 | BIO_free_all(b64); |
51 | return rv; | 52 | return rv; |
52 | }catch(...) { | 53 | }catch(...) { |
53 | if(b64) BIO_free_all(b64); | 54 | if(b64) BIO_free_all(b64); |
54 | throw; | 55 | throw; |
55 | } | 56 | } |
56 | } | 57 | } |
57 | 58 | ||
58 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 59 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
59 | BIO *b64 = 0, *bmem = 0; | 60 | BIO *b64 = 0, *bmem = 0; |
60 | rv.clear(); | 61 | rv.clear(); |
61 | try { | 62 | try { |
62 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | 63 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); |
63 | if(!bmem) | 64 | if(!bmem) |
64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 65 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
65 | b64 = BIO_new(BIO_f_base64()); | 66 | b64 = BIO_new(BIO_f_base64()); |
66 | if(!b64) | 67 | if(!b64) |
67 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 68 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
68 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 69 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
69 | BIO_push(b64,bmem); | 70 | BIO_push(b64,bmem); |
70 | unsigned char tmp[512]; | 71 | unsigned char tmp[512]; |
71 | size_t rb = 0; | 72 | size_t rb = 0; |
72 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 73 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
73 | rv.insert(rv.end(),tmp,&tmp[rb]); | 74 | rv.insert(rv.end(),tmp,&tmp[rb]); |
74 | BIO_free_all(b64); | 75 | BIO_free_all(b64); |
75 | }catch(...) { | 76 | }catch(...) { |
76 | if(b64) BIO_free_all(b64); | 77 | if(b64) BIO_free_all(b64); |
77 | throw; | 78 | throw; |
78 | } | 79 | } |
79 | } | 80 | } |
80 | 81 | ||
81 | /* | 82 | /* |
82 | * big numerics | 83 | * big numerics |
83 | */ | 84 | */ |
84 | 85 | ||
85 | BIGNUM *base64_to_bignum(const string& b64) { | 86 | BIGNUM *base64_to_bignum(const string& b64) { |
86 | vector<unsigned char> bin; | 87 | vector<unsigned char> bin; |
87 | decode_base64(b64,bin); | 88 | decode_base64(b64,bin); |
88 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 89 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
89 | if(!rv) | 90 | if(!rv) |
90 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 91 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
91 | return rv; | 92 | return rv; |
92 | } | 93 | } |
93 | 94 | ||
94 | BIGNUM *dec_to_bignum(const string& dec) { | 95 | BIGNUM *dec_to_bignum(const string& dec) { |
95 | BIGNUM *rv = 0; | 96 | BIGNUM *rv = 0; |
96 | if(!BN_dec2bn(&rv,dec.c_str())) | 97 | if(!BN_dec2bn(&rv,dec.c_str())) |
97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 98 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
98 | return rv; | 99 | return rv; |
99 | } | 100 | } |
100 | 101 | ||
101 | string bignum_to_base64(const BIGNUM *bn) { | 102 | string bignum_to_base64(const BIGNUM *bn) { |
102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 103 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
103 | unsigned char *binptr = &(bin.front())+1; | 104 | unsigned char *binptr = &(bin.front())+1; |
104 | int l = BN_bn2bin(bn,binptr); | 105 | int l = BN_bn2bin(bn,binptr); |
105 | if(l && (*binptr)&0x80){ | 106 | if(l && (*binptr)&0x80){ |
106 | (*(--binptr)) = 0; ++l; | 107 | (*(--binptr)) = 0; ++l; |
107 | } | 108 | } |
108 | return encode_base64(binptr,l); | 109 | return encode_base64(binptr,l); |
109 | } | 110 | } |
110 | 111 | ||
111 | /* | 112 | /* |
112 | * w3c times | 113 | * w3c times |
113 | */ | 114 | */ |
114 | 115 | ||
115 | string time_to_w3c(time_t t) { | 116 | string time_to_w3c(time_t t) { |
116 | struct tm tm_t; | 117 | struct tm tm_t; |
117 | if(!gmtime_r(&t,&tm_t)) | 118 | if(!gmtime_r(&t,&tm_t)) |
118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 119 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
119 | char rv[25]; | 120 | char rv[25]; |
120 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 121 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 122 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
122 | return rv; | 123 | return rv; |
123 | } | 124 | } |
124 | 125 | ||
125 | time_t w3c_to_time(const string& w) { | 126 | time_t w3c_to_time(const string& w) { |
126 | int fraction; | 127 | int fraction; |
127 | struct tm tm_t; | 128 | struct tm tm_t; |
128 | memset(&tm_t,0,sizeof(tm_t)); | 129 | memset(&tm_t,0,sizeof(tm_t)); |
129 | if( ( | 130 | if( ( |
130 | sscanf( | 131 | sscanf( |
131 | w.c_str(), | 132 | w.c_str(), |
132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 133 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
133 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 134 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
134 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 135 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
135 | ) != 6 | 136 | ) != 6 |
136 | ) && ( | 137 | ) && ( |
137 | sscanf( | 138 | sscanf( |
138 | w.c_str(), | 139 | w.c_str(), |
139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 140 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
140 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 141 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
141 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, | 142 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
142 | &fraction | 143 | &fraction |
143 | ) != 7 | 144 | ) != 7 |
144 | ) ) | 145 | ) ) |
145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 146 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
146 | tm_t.tm_mon--; | 147 | tm_t.tm_mon--; |
147 | tm_t.tm_year-=1900; | 148 | tm_t.tm_year-=1900; |
148 | time_t rv = mktime(&tm_t); | 149 | time_t rv = mktime(&tm_t); |
149 | if(rv==(time_t)-1) | 150 | if(rv==(time_t)-1) |
150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 151 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
151 | return rv-timezone; | 152 | return rv-timezone; |
152 | } | 153 | } |
153 | 154 | ||
154 | /* | 155 | /* |
155 | * | 156 | * |
156 | */ | 157 | */ |
157 | 158 | ||
158 | string url_encode(const string& str) { | 159 | string url_encode(const string& str) { |
159 | char * t = curl_escape(str.c_str(),str.length()); | 160 | char * t = curl_escape(str.c_str(),str.length()); |
160 | if(!t) | 161 | if(!t) |
161 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 162 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
162 | string rv(t); | 163 | string rv(t); |
163 | curl_free(t); | 164 | curl_free(t); |
164 | return rv; | 165 | return rv; |
165 | } | 166 | } |
166 | 167 | ||
167 | string attr_escape(const string& str) { | 168 | string attr_escape(const string& str) { |
168 | static const char *unsafechars = "<>&\n\"'"; | 169 | static const char *unsafechars = "<>&\n\"'"; |
169 | string rv; | 170 | string rv; |
170 | string::size_type p=0; | 171 | string::size_type p=0; |
171 | while(true) { | 172 | while(true) { |
172 | string::size_type us = str.find_first_of(unsafechars,p); | 173 | string::size_type us = str.find_first_of(unsafechars,p); |
173 | if(us==string::npos) { | 174 | if(us==string::npos) { |
174 | if(p!=str.length()) | 175 | if(p!=str.length()) |
175 | rv.append(str,p,str.length()-p); | 176 | rv.append(str,p,str.length()-p); |
176 | return rv; | 177 | return rv; |
177 | } | 178 | } |
178 | rv.append(str,p,us-p); | 179 | rv.append(str,p,us-p); |
179 | rv += "&#"; | 180 | rv += "&#"; |
180 | rv += long_to_string((long)str[us]); | 181 | rv += long_to_string((long)str[us]); |
181 | rv += ';'; | 182 | rv += ';'; |
182 | p = us+1; | 183 | p = us+1; |
183 | } | 184 | } |
184 | } | 185 | } |
185 | 186 | ||
186 | string long_to_string(long l) { | 187 | string long_to_string(long l) { |
187 | char rv[32]; | 188 | char rv[32]; |
188 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 189 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
189 | if(r<0 || r>=(int)sizeof(rv)) | 190 | if(r<0 || r>=(int)sizeof(rv)) |
190 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 191 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
191 | return rv; | 192 | return rv; |
192 | } | 193 | } |
193 | 194 | ||
194 | long string_to_long(const string& s) { | 195 | long string_to_long(const string& s) { |
195 | char *endptr = 0; | 196 | char *endptr = 0; |
196 | long rv = strtol(s.c_str(),&endptr,10); | 197 | long rv = strtol(s.c_str(),&endptr,10); |
197 | if((!endptr) || endptr==s.c_str()) | 198 | if((!endptr) || endptr==s.c_str()) |
198 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 199 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
199 | return rv; | 200 | return rv; |
200 | } | 201 | } |
201 | 202 | ||
202 | /* | 203 | /* |
203 | * Normalize URL according to the rules, described in rfc 3986, section 6 | 204 | * Normalize URL according to the rules, described in rfc 3986, section 6 |
204 | * | 205 | * |
205 | * - uppercase hex triplets (e.g. %ab -> %AB) | 206 | * - uppercase hex triplets (e.g. %ab -> %AB) |
206 | * - lowercase scheme and host | 207 | * - lowercase scheme and host |
207 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, | 208 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, |
208 | * that is - [:alpha:][:digit:]._~- | 209 | * that is - [:alpha:][:digit:]._~- |
209 | * - remove dot segments | 210 | * - remove dot segments |
210 | * - remove empty and default ports | 211 | * - remove empty and default ports |
211 | * - if there's no path component, add '/' | 212 | * - if there's no path component, add '/' |
212 | */ | 213 | */ |
213 | string rfc_3986_normalize_uri(const string& uri) { | 214 | string rfc_3986_normalize_uri(const string& uri) { |
214 | static const char *whitespace = " \t\r\n"; | ||
215 | string rv; | 215 | string rv; |
216 | string::size_type ns = uri.find_first_not_of(whitespace); | 216 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); |
217 | if(ns==string::npos) | 217 | if(ns==string::npos) |
218 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); | 218 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); |
219 | string::size_type colon = uri.find(':',ns); | 219 | string::size_type colon = uri.find(':',ns); |
220 | if(colon==string::npos) | 220 | if(colon==string::npos) |
221 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); | 221 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); |
222 | transform( | 222 | transform( |
223 | uri.begin()+ns, uri.begin()+colon+1, | 223 | uri.begin()+ns, uri.begin()+colon+1, |
224 | back_inserter(rv), ::tolower ); | 224 | back_inserter(rv), ::tolower ); |
225 | bool s; | 225 | bool s; |
226 | string::size_type ul = uri.find_last_not_of(whitespace)+1; | 226 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; |
227 | if(ul <= (colon+3)) | 227 | if(ul <= (colon+3)) |
228 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); | 228 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); |
229 | if(uri[colon+1]!='/' || uri[colon+2]!='/') | 229 | if(uri[colon+1]!='/' || uri[colon+2]!='/') |
230 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); | 230 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); |
231 | if(rv=="http:") | 231 | if(rv=="http:") |
232 | s = false; | 232 | s = false; |
233 | else if(rv=="https:") | 233 | else if(rv=="https:") |
234 | s = true; | 234 | s = true; |
235 | else{ | 235 | else{ |
236 | /* TODO: support more schemes. e.g. xri. How do we normalize | 236 | /* TODO: support more schemes. e.g. xri. How do we normalize |
237 | * xri? | 237 | * xri? |
238 | */ | 238 | */ |
239 | rv.append(uri,colon+1,ul-colon-1); | 239 | rv.append(uri,colon+1,ul-colon-1); |
240 | return rv; | 240 | return rv; |
241 | } | 241 | } |
242 | rv += "//"; | 242 | rv += "//"; |
243 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); | 243 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); |
244 | if(interesting==string::npos) { | 244 | if(interesting==string::npos) { |
245 | transform( | 245 | transform( |
246 | uri.begin()+colon+3,uri.begin()+ul, | 246 | uri.begin()+colon+3,uri.begin()+ul, |
247 | back_inserter(rv), ::tolower ); | 247 | back_inserter(rv), ::tolower ); |
248 | rv += '/'; return rv; | 248 | rv += '/'; return rv; |
249 | } | 249 | } |
250 | transform( | 250 | transform( |
251 | uri.begin()+colon+3,uri.begin()+interesting, | 251 | uri.begin()+colon+3,uri.begin()+interesting, |
252 | back_inserter(rv), ::tolower ); | 252 | back_inserter(rv), ::tolower ); |
253 | bool qf = false; | 253 | bool qf = false; |
254 | char ic = uri[interesting]; | 254 | char ic = uri[interesting]; |
255 | if(ic==':') { | 255 | if(ic==':') { |
256 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 256 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
257 | const char *nptr = uri.data()+interesting+1; | 257 | const char *nptr = uri.data()+interesting+1; |
258 | char *eptr = 0; | 258 | char *eptr = 0; |
259 | long port = strtol(nptr,&eptr,10); | 259 | long port = strtol(nptr,&eptr,10); |
260 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 260 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
261 | char tmp[8]; | 261 | char tmp[8]; |
262 | snprintf(tmp,sizeof(tmp),":%ld",port); | 262 | snprintf(tmp,sizeof(tmp),":%ld",port); |
263 | rv += tmp; | 263 | rv += tmp; |
264 | } | 264 | } |
265 | if(ni==string::npos) { | 265 | if(ni==string::npos) { |
266 | rv += '/'; return rv; | 266 | rv += '/'; return rv; |
267 | } | 267 | } |
268 | interesting = ni; | 268 | interesting = ni; |
269 | }else if(ic!='/') { | 269 | }else if(ic!='/') { |
270 | rv += '/'; rv += ic; | 270 | rv += '/'; rv += ic; |
271 | qf = true; | 271 | qf = true; |
272 | ++interesting; | 272 | ++interesting; |
273 | } | 273 | } |
274 | string::size_type n = interesting; | 274 | string::size_type n = interesting; |
275 | char tmp[3] = { 0,0,0 }; | 275 | char tmp[3] = { 0,0,0 }; |
276 | stack<string::size_type> psegs; psegs.push(rv.length()); | 276 | stack<string::size_type> psegs; psegs.push(rv.length()); |
277 | string pseg; | 277 | string pseg; |
278 | for(;n<ul;) { | 278 | for(;n<ul;) { |
279 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 279 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
280 | if(unsafe==string::npos) { | 280 | if(unsafe==string::npos) { |
281 | pseg.append(uri,n,ul-n-1); n = ul-1; | 281 | pseg.append(uri,n,ul-n-1); n = ul-1; |
282 | }else{ | 282 | }else{ |
283 | pseg.append(uri,n,unsafe-n); | 283 | pseg.append(uri,n,unsafe-n); |
284 | n = unsafe; | 284 | n = unsafe; |
285 | } | 285 | } |
286 | char c = uri[n++]; | 286 | char c = uri[n++]; |
287 | if(c=='%') { | 287 | if(c=='%') { |
288 | if((n+1)>=ul) | 288 | if((n+1)>=ul) |
289 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); | 289 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); |
290 | tmp[0] = uri[n++]; | 290 | tmp[0] = uri[n++]; |
291 | tmp[1] = uri[n++]; | 291 | tmp[1] = uri[n++]; |
292 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) | 292 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) |
293 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 293 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
294 | int cc = strtol(tmp,0,16); | 294 | int cc = strtol(tmp,0,16); |
295 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 295 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
296 | pseg += cc; | 296 | pseg += cc; |
297 | else{ | 297 | else{ |
298 | pseg += '%'; | 298 | pseg += '%'; |
299 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); | 299 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); |
300 | } | 300 | } |
301 | }else if(qf) { | 301 | }else if(qf) { |
302 | rv += pseg; rv += c; | 302 | rv += pseg; rv += c; |
303 | pseg.clear(); | 303 | pseg.clear(); |
304 | }else if(n>=ul || strchr("?/#",c)) { | 304 | }else if(n>=ul || strchr("?/#",c)) { |
305 | if(pseg.empty() || pseg==".") { | 305 | if(pseg.empty() || pseg==".") { |
306 | }else if(pseg=="..") { | 306 | }else if(pseg=="..") { |
307 | if(psegs.size()>1) { | 307 | if(psegs.size()>1) { |
308 | rv.resize(psegs.top()); psegs.pop(); | 308 | rv.resize(psegs.top()); psegs.pop(); |
309 | } | 309 | } |
310 | }else{ | 310 | }else{ |
311 | psegs.push(rv.length()); | 311 | psegs.push(rv.length()); |
312 | if(c!='/') { | 312 | if(c!='/') { |
313 | pseg += c; | 313 | pseg += c; |
314 | qf = true; | 314 | qf = true; |
315 | } | 315 | } |
316 | rv += '/'; rv += pseg; | 316 | rv += '/'; rv += pseg; |
317 | } | 317 | } |
318 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 318 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
319 | rv += '/'; | 319 | rv += '/'; |
320 | if(n<ul) | 320 | if(n<ul) |
321 | qf = true; | 321 | qf = true; |
322 | }else if(strchr("?#",c)) { | 322 | }else if(strchr("?#",c)) { |
323 | if(psegs.size()==1 && psegs.top()==rv.length()) | 323 | if(psegs.size()==1 && psegs.top()==rv.length()) |
324 | rv += '/'; | 324 | rv += '/'; |
325 | if(pseg.empty()) | 325 | if(pseg.empty()) |
326 | rv += c; | 326 | rv += c; |
327 | qf = true; | 327 | qf = true; |
328 | } | 328 | } |
329 | pseg.clear(); | 329 | pseg.clear(); |
330 | }else{ | 330 | }else{ |
331 | pseg += c; | 331 | pseg += c; |
332 | } | 332 | } |
333 | } | 333 | } |
334 | if(!pseg.empty()) { | 334 | if(!pseg.empty()) { |
335 | if(!qf) rv += '/'; | 335 | if(!qf) rv += '/'; |
336 | rv += pseg; | 336 | rv += pseg; |
337 | } | 337 | } |
338 | return rv; | 338 | return rv; |
339 | } | 339 | } |
340 | 340 | ||
341 | string& strip_uri_fragment_part(string& u) { | 341 | string& strip_uri_fragment_part(string& u) { |
342 | string::size_type q = u.find('?'), f = u.find('#'); | 342 | string::size_type q = u.find('?'), f = u.find('#'); |
343 | if(q==string::npos) { | 343 | if(q==string::npos) { |
344 | if(f!=string::npos) | 344 | if(f!=string::npos) |
345 | u.erase(f); | 345 | u.erase(f); |
346 | }else{ | 346 | }else{ |
347 | if(f!=string::npos) { | 347 | if(f!=string::npos) { |
348 | if(f<q) | 348 | if(f<q) |
349 | u.erase(f,q-f); | 349 | u.erase(f,q-f); |
350 | else | 350 | else |
351 | u.erase(f); | 351 | u.erase(f); |
352 | } | 352 | } |
353 | } | 353 | } |
354 | return u; | 354 | return u; |
355 | } | 355 | } |
356 | 356 | ||
357 | bool uri_matches_realm(const string& uri,const string& realm) { | 357 | bool uri_matches_realm(const string& uri,const string& realm) { |
358 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | 358 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); |
359 | string nu = opkele::util::rfc_3986_normalize_uri(uri); | 359 | string nu = opkele::util::rfc_3986_normalize_uri(uri); |
360 | string::size_type pr = nrealm.find("://"); | 360 | string::size_type pr = nrealm.find("://"); |
361 | string::size_type pu = nu.find("://"); | 361 | string::size_type pu = nu.find("://"); |
362 | assert(!(pr==string::npos || pu==string::npos)); | 362 | assert(!(pr==string::npos || pu==string::npos)); |
363 | pr += sizeof("://")-1; | 363 | pr += sizeof("://")-1; |
364 | pu += sizeof("://")-1; | 364 | pu += sizeof("://")-1; |
365 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | 365 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { |
366 | pr = nrealm.find('.',pr); | 366 | pr = nrealm.find('.',pr); |
367 | pu = nu.find('.',pu); | 367 | pu = nu.find('.',pu); |
368 | assert(pr!=string::npos); | 368 | assert(pr!=string::npos); |
369 | if(pu==string::npos) | 369 | if(pu==string::npos) |
370 | return false; | 370 | return false; |
371 | // TODO: check for overgeneralized realm | 371 | // TODO: check for overgeneralized realm |
372 | } | 372 | } |
373 | string::size_type lr = nrealm.length(); | 373 | string::size_type lr = nrealm.length(); |
374 | string::size_type lu = nu.length(); | 374 | string::size_type lu = nu.length(); |
375 | if( (lu-pu) < (lr-pr) ) | 375 | if( (lu-pu) < (lr-pr) ) |
376 | return false; | 376 | return false; |
377 | pair<const char*,const char*> mp = mismatch( | 377 | pair<const char*,const char*> mp = mismatch( |
378 | nrealm.c_str()+pr,nrealm.c_str()+lr, | 378 | nrealm.c_str()+pr,nrealm.c_str()+lr, |
379 | nu.c_str()+pu); | 379 | nu.c_str()+pu); |
380 | if( (*(mp.first-1))!='/' | 380 | if( (*(mp.first-1))!='/' |
381 | && !strchr("/?#",*mp.second) ) | 381 | && !strchr("/?#",*mp.second) ) |
382 | return false; | 382 | return false; |
383 | return true; | 383 | return true; |
384 | } | 384 | } |
385 | 385 | ||
386 | string abi_demangle(const char *mn) { | 386 | string abi_demangle(const char *mn) { |
387 | #ifndef HAVE_DEMANGLE | 387 | #ifndef HAVE_DEMANGLE |
388 | return mn; | 388 | return mn; |
389 | #else /* !HAVE_DEMANGLE */ | 389 | #else /* !HAVE_DEMANGLE */ |
390 | int dstat; | 390 | int dstat; |
391 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); | 391 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); |
392 | if(dstat) | 392 | if(dstat) |
393 | return mn; | 393 | return mn; |
394 | string rv = demangled; | 394 | string rv = demangled; |
395 | free(demangled); | 395 | free(demangled); |
396 | return rv; | 396 | return rv; |
397 | #endif /* !HAVE_DEMANGLE */ | 397 | #endif /* !HAVE_DEMANGLE */ |
398 | } | 398 | } |
399 | 399 | ||
400 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { | 400 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { |
401 | const string& slist = om.get_field("signed"); | 401 | const string& slist = om.get_field("signed"); |
402 | string kv; | 402 | string kv; |
403 | string::size_type p=0; | 403 | string::size_type p=0; |
404 | while(true) { | 404 | while(true) { |
405 | string::size_type co = slist.find(',',p); | 405 | string::size_type co = slist.find(',',p); |
406 | string f = (co==string::npos) | 406 | string f = (co==string::npos) |
407 | ?slist.substr(p):slist.substr(p,co-p); | 407 | ?slist.substr(p):slist.substr(p,co-p); |
408 | kv += f; | 408 | kv += f; |
409 | kv += ':'; | 409 | kv += ':'; |
410 | kv += om.get_field(f); | 410 | kv += om.get_field(f); |
411 | kv += '\n'; | 411 | kv += '\n'; |
412 | if(co==string::npos) break; | 412 | if(co==string::npos) break; |
413 | p = co+1; | 413 | p = co+1; |
414 | } | 414 | } |
415 | const secret_t& secret = assoc->secret(); | 415 | const secret_t& secret = assoc->secret(); |
416 | const EVP_MD *evpmd; | 416 | const EVP_MD *evpmd; |
417 | const string& at = assoc->assoc_type(); | 417 | const string& at = assoc->assoc_type(); |
418 | if(at=="HMAC-SHA256") | 418 | if(at=="HMAC-SHA256") |
419 | evpmd = EVP_sha256(); | 419 | evpmd = EVP_sha256(); |
420 | else if(at=="HMAC-SHA1") | 420 | else if(at=="HMAC-SHA1") |
421 | evpmd = EVP_sha1(); | 421 | evpmd = EVP_sha1(); |
422 | else | 422 | else |
423 | throw unsupported(OPKELE_CP_ "unknown association type"); | 423 | throw unsupported(OPKELE_CP_ "unknown association type"); |
424 | unsigned int md_len = 0; | 424 | unsigned int md_len = 0; |
425 | unsigned char md[SHA256_DIGEST_LENGTH]; | 425 | unsigned char md[SHA256_DIGEST_LENGTH]; |
426 | HMAC(evpmd, | 426 | HMAC(evpmd, |
427 | &(secret.front()),secret.size(), | 427 | &(secret.front()),secret.size(), |
428 | (const unsigned char*)kv.data(),kv.length(), | 428 | (const unsigned char*)kv.data(),kv.length(), |
429 | md,&md_len); | 429 | md,&md_len); |
430 | return encode_base64(md,md_len); | 430 | return encode_base64(md,md_len); |
431 | } | 431 | } |
432 | 432 | ||
433 | string normalize_identifier(const string& usi,bool strip_fragment) { | ||
434 | if(usi.empty()) | ||
435 | return usi; | ||
436 | string rv; | ||
437 | string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars); | ||
438 | if(fsc==string::npos) | ||
439 | return rv; | ||
440 | string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars); | ||
441 | assert(lsc!=string::npos); | ||
442 | if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1)) | ||
443 | fsc += sizeof("xri://")-1; | ||
444 | if( (fsc+1) >= lsc ) | ||
445 | return rv; | ||
446 | rv.assign(usi,fsc,lsc-fsc+1); | ||
447 | if(strchr(data::_iname_leaders,rv[0])) { | ||
448 | /* TODO: further normalize xri identity, fold case or | ||
449 | * whatever... */ | ||
450 | }else{ | ||
451 | if(rv.find("://")==string::npos) | ||
452 | rv.insert(0,"http://"); | ||
453 | if(strip_fragment) { | ||
454 | string::size_type fp = rv.find('#'); | ||
455 | if(fp!=string::npos) { | ||
456 | string::size_type qp = rv.find('?'); | ||
457 | if(qp==string::npos || qp<fp) | ||
458 | rv.erase(fp); | ||
459 | else if(qp>fp) | ||
460 | rv.erase(fp,qp-fp); | ||
461 | } | ||
462 | } | ||
463 | rv = rfc_3986_normalize_uri(rv); | ||
464 | } | ||
465 | return rv; | ||
466 | } | ||
467 | |||
433 | } | 468 | } |
434 | 469 | ||
435 | } | 470 | } |