author | Michael Krelin <hacker@klever.net> | 2008-06-27 21:00:41 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-06-27 21:00:41 (UTC) |
commit | 362678728b8232c9490e14ba14ff323d9a92d6be (patch) (unidiff) | |
tree | 105675f96fff87916b2fdea8b51ad9fe98c5531d /lib | |
parent | a344edbdddeac51524001faa10d06f85cfdb041c (diff) | |
download | libopkele-362678728b8232c9490e14ba14ff323d9a92d6be.zip libopkele-362678728b8232c9490e14ba14ff323d9a92d6be.tar.gz libopkele-362678728b8232c9490e14ba14ff323d9a92d6be.tar.bz2 |
made gcc 4.3 a bit happier
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/discovery.cc | 4 | ||||
-rw-r--r-- | lib/message.cc | 2 | ||||
-rw-r--r-- | lib/prequeue_rp.cc | 4 | ||||
-rw-r--r-- | lib/secret.cc | 2 | ||||
-rw-r--r-- | lib/util.cc | 6 |
5 files changed, 9 insertions, 9 deletions
diff --git a/lib/discovery.cc b/lib/discovery.cc index d1989ec..984e308 100644 --- a/lib/discovery.cc +++ b/lib/discovery.cc | |||
@@ -86,499 +86,499 @@ namespace opkele { | |||
86 | string cdata_buf; | 86 | string cdata_buf; |
87 | long status_code; | 87 | long status_code; |
88 | string status_string; | 88 | string status_string; |
89 | 89 | ||
90 | typedef list<string> pt_stack_t; | 90 | typedef list<string> pt_stack_t; |
91 | pt_stack_t pt_stack; | 91 | pt_stack_t pt_stack; |
92 | int skipping; | 92 | int skipping; |
93 | bool parser_choked; | 93 | bool parser_choked; |
94 | string save_html; | 94 | string save_html; |
95 | 95 | ||
96 | XRD_t *xrd; | 96 | XRD_t *xrd; |
97 | service_t *xrd_service; | 97 | service_t *xrd_service; |
98 | string* cdata; | 98 | string* cdata; |
99 | 99 | ||
100 | idigger_t() | 100 | idigger_t() |
101 | : util::curl_t(easy_init()), | 101 | : util::curl_t(easy_init()), |
102 | util::expat_t(0), | 102 | util::expat_t(0), |
103 | xri_proxy(XRI_PROXY_URL) { | 103 | xri_proxy(XRI_PROXY_URL) { |
104 | CURLcode r; | 104 | CURLcode r; |
105 | (r=misc_sets()) | 105 | (r=misc_sets()) |
106 | || (r=set_write()) | 106 | || (r=set_write()) |
107 | || (r=set_header()) | 107 | || (r=set_header()) |
108 | ; | 108 | ; |
109 | if(r) | 109 | if(r) |
110 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); | 110 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); |
111 | } | 111 | } |
112 | ~idigger_t() throw() { } | 112 | ~idigger_t() throw() { } |
113 | 113 | ||
114 | void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) { | 114 | void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) { |
115 | idiscovery_t idis; | 115 | idiscovery_t idis; |
116 | idis.xri_identity = false; | 116 | idis.xri_identity = false; |
117 | discover_at(idis,yurl,xmode_html|xmode_xrd|(redirs?0:xmode_noredirs)); | 117 | discover_at(idis,yurl,xmode_html|xmode_xrd|(redirs?0:xmode_noredirs)); |
118 | if(!xrds_location.empty()) { | 118 | if(!xrds_location.empty()) { |
119 | idis.clear(); | 119 | idis.clear(); |
120 | discover_at(idis,xrds_location,xmode_xrd); | 120 | discover_at(idis,xrds_location,xmode_xrd); |
121 | } | 121 | } |
122 | idis.normalized_id = idis.canonicalized_id = yurl; | 122 | idis.normalized_id = idis.canonicalized_id = yurl; |
123 | service_type_t st; | 123 | service_type_t st; |
124 | for(st.uri=*types;*types;st.uri=*(++types)) | 124 | for(st.uri=*types;*types;st.uri=*(++types)) |
125 | queue_endpoints(oi,idis,&st); | 125 | queue_endpoints(oi,idis,&st); |
126 | } | 126 | } |
127 | 127 | ||
128 | string discover(endpoint_discovery_iterator& oi,const string& identity) { | 128 | string discover(endpoint_discovery_iterator& oi,const string& identity) { |
129 | string rv; | 129 | string rv; |
130 | idiscovery_t idis; | 130 | idiscovery_t idis; |
131 | string::size_type fsc = identity.find_first_not_of(data::_whitespace_chars); | 131 | string::size_type fsc = identity.find_first_not_of(data::_whitespace_chars); |
132 | if(fsc==string::npos) | 132 | if(fsc==string::npos) |
133 | throw bad_input(OPKELE_CP_ "whitespace-only identity"); | 133 | throw bad_input(OPKELE_CP_ "whitespace-only identity"); |
134 | string::size_type lsc = identity.find_last_not_of(data::_whitespace_chars); | 134 | string::size_type lsc = identity.find_last_not_of(data::_whitespace_chars); |
135 | assert(lsc!=string::npos); | 135 | assert(lsc!=string::npos); |
136 | if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) | 136 | if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) |
137 | fsc += sizeof("xri://")-1; | 137 | fsc += sizeof("xri://")-1; |
138 | if((fsc+1)>=lsc) | 138 | if((fsc+1)>=lsc) |
139 | throw bad_input(OPKELE_CP_ "not a character of importance in identity"); | 139 | throw bad_input(OPKELE_CP_ "not a character of importance in identity"); |
140 | string id(identity,fsc,lsc-fsc+1); | 140 | string id(identity,fsc,lsc-fsc+1); |
141 | idis.clear(); | 141 | idis.clear(); |
142 | if(strchr(data::_iname_leaders,id[0])) { | 142 | if(strchr(data::_iname_leaders,id[0])) { |
143 | /* TODO: further normalize xri identity? Like folding case | 143 | /* TODO: further normalize xri identity? Like folding case |
144 | * or whatever... */ | 144 | * or whatever... */ |
145 | rv = id; | 145 | rv = id; |
146 | set<string> cids; | 146 | set<string> cids; |
147 | for(const struct service_type_t *st=op_service_types; | 147 | for(const struct service_type_t *st=op_service_types; |
148 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) { | 148 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) { |
149 | idis.clear(); | 149 | idis.clear(); |
150 | discover_at( idis, | 150 | discover_at( idis, |
151 | xri_proxy + util::url_encode(id)+ | 151 | xri_proxy + util::url_encode(id)+ |
152 | "?_xrd_t="+util::url_encode(st->uri)+ | 152 | "?_xrd_t="+util::url_encode(st->uri)+ |
153 | "&_xrd_r=application/xrd%2Bxml" | 153 | "&_xrd_r=application/xrd%2Bxml" |
154 | ";sep=true;refs=true", | 154 | ";sep=true;refs=true", |
155 | xmode_xrd ); | 155 | xmode_xrd ); |
156 | if(status_code==241) continue; | 156 | if(status_code==241) continue; |
157 | if(status_code!=100) | 157 | if(status_code!=100) |
158 | throw failed_xri_resolution(OPKELE_CP_ | 158 | throw failed_xri_resolution(OPKELE_CP_ |
159 | "XRI resolution failed with '"+status_string+"' message" | 159 | "XRI resolution failed with '"+status_string+"' message" |
160 | ", while looking for SEP with type '"+st->uri+"'", status_code); | 160 | ", while looking for SEP with type '"+st->uri+"'", status_code); |
161 | if(idis.xrd.canonical_ids.empty()) | 161 | if(idis.xrd.canonical_ids.empty()) |
162 | throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found"); | 162 | throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found"); |
163 | string cid = idis.xrd.canonical_ids.begin()->second; | 163 | string cid = idis.xrd.canonical_ids.begin()->second; |
164 | if(cids.find(cid)==cids.end()) { | 164 | if(cids.find(cid)==cids.end()) { |
165 | cids.insert(cid); | 165 | cids.insert(cid); |
166 | idis.clear(); | 166 | idis.clear(); |
167 | discover_at( idis, | 167 | discover_at( idis, |
168 | xri_proxy + util::url_encode(id)+ | 168 | xri_proxy + util::url_encode(id)+ |
169 | "?_xrd_t="+util::url_encode(st->uri)+ | 169 | "?_xrd_t="+util::url_encode(st->uri)+ |
170 | "&_xrd_r=application/xrd%2Bxml" | 170 | "&_xrd_r=application/xrd%2Bxml" |
171 | ";sep=true;refs=true", | 171 | ";sep=true;refs=true", |
172 | xmode_xrd ); | 172 | xmode_xrd ); |
173 | if(status_code==241) continue; | 173 | if(status_code==241) continue; |
174 | if(status_code!=100) | 174 | if(status_code!=100) |
175 | throw failed_xri_resolution(OPKELE_CP_ | 175 | throw failed_xri_resolution(OPKELE_CP_ |
176 | "XRI resolution failed with '"+status_string+"' message" | 176 | "XRI resolution failed with '"+status_string+"' message" |
177 | ", while looking for SEP with type '"+st->uri+"'" | 177 | ", while looking for SEP with type '"+st->uri+"'" |
178 | " on canonical id", status_code); | 178 | " on canonical id", status_code); |
179 | } | 179 | } |
180 | idis.canonicalized_id = cid; | 180 | idis.canonicalized_id = cid; |
181 | idis.normalized_id = rv; idis.xri_identity = true; | 181 | idis.normalized_id = rv; idis.xri_identity = true; |
182 | queue_endpoints(oi,idis,st); | 182 | queue_endpoints(oi,idis,st); |
183 | } | 183 | } |
184 | }else{ | 184 | }else{ |
185 | idis.xri_identity = false; | 185 | idis.xri_identity = false; |
186 | if(id.find("://")==string::npos) | 186 | if(id.find("://")==string::npos) |
187 | id.insert(0,"http://"); | 187 | id.insert(0,"http://"); |
188 | string::size_type fp = id.find('#'); | 188 | string::size_type fp = id.find('#'); |
189 | if(fp!=string::npos) { | 189 | if(fp!=string::npos) { |
190 | string::size_type qp = id.find('?'); | 190 | string::size_type qp = id.find('?'); |
191 | if(qp==string::npos || qp<fp) | 191 | if(qp==string::npos || qp<fp) |
192 | id.erase(fp); | 192 | id.erase(fp); |
193 | else if(qp>fp) | 193 | else if(qp>fp) |
194 | id.erase(fp,qp-fp); | 194 | id.erase(fp,qp-fp); |
195 | } | 195 | } |
196 | rv = idis.normalized_id = util::rfc_3986_normalize_uri(id); | 196 | rv = idis.normalized_id = util::rfc_3986_normalize_uri(id); |
197 | discover_at(idis,id,xmode_html|xmode_xrd); | 197 | discover_at(idis,id,xmode_html|xmode_xrd); |
198 | const char * eu = 0; | 198 | const char * eu = 0; |
199 | CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); | 199 | CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); |
200 | if(r) | 200 | if(r) |
201 | throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); | 201 | throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); |
202 | string cid = util::strip_uri_fragment_part( idis.canonicalized_id = util::rfc_3986_normalize_uri(eu) ); | 202 | string cid = util::strip_uri_fragment_part( idis.canonicalized_id = util::rfc_3986_normalize_uri(eu) ); |
203 | if(xrds_location.empty()) { | 203 | if(xrds_location.empty()) { |
204 | if(idis.xrd.empty()) | 204 | if(idis.xrd.empty()) |
205 | html2xrd(oi,idis); | 205 | html2xrd(oi,idis); |
206 | else{ | 206 | else{ |
207 | for(const service_type_t *st=op_service_types; | 207 | for(const service_type_t *st=op_service_types; |
208 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) | 208 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) |
209 | queue_endpoints(oi,idis,st); | 209 | queue_endpoints(oi,idis,st); |
210 | } | 210 | } |
211 | }else{ | 211 | }else{ |
212 | idis.clear(); | 212 | idis.clear(); |
213 | idis.canonicalized_id = cid; | 213 | idis.canonicalized_id = cid; |
214 | discover_at(idis,xrds_location,xmode_xrd); | 214 | discover_at(idis,xrds_location,xmode_xrd); |
215 | if(idis.xrd.empty()) | 215 | if(idis.xrd.empty()) |
216 | html2xrd(oi,idis); | 216 | html2xrd(oi,idis); |
217 | else{ | 217 | else{ |
218 | for(const service_type_t *st=op_service_types; | 218 | for(const service_type_t *st=op_service_types; |
219 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) | 219 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) |
220 | queue_endpoints(oi,idis,st); | 220 | queue_endpoints(oi,idis,st); |
221 | } | 221 | } |
222 | } | 222 | } |
223 | } | 223 | } |
224 | return rv; | 224 | return rv; |
225 | } | 225 | } |
226 | 226 | ||
227 | void discover_at(idiscovery_t& idis,const string& url,int xm) { | 227 | void discover_at(idiscovery_t& idis,const string& url,int xm) { |
228 | CURLcode r = easy_setopt(CURLOPT_MAXREDIRS, (xm&xmode_noredirs)?0:5); | 228 | CURLcode r = easy_setopt(CURLOPT_MAXREDIRS, (xm&xmode_noredirs)?0:5); |
229 | if(r) | 229 | if(r) |
230 | throw exception_curl(OPKELE_CP_ "failed to set curly maxredirs option"); | 230 | throw exception_curl(OPKELE_CP_ "failed to set curly maxredirs option"); |
231 | if( (r=easy_setopt(CURLOPT_URL,url.c_str())) ) | 231 | if( (r=easy_setopt(CURLOPT_URL,url.c_str())) ) |
232 | throw exception_curl(OPKELE_CP_ "failed to set curly urlie",r); | 232 | throw exception_curl(OPKELE_CP_ "failed to set curly urlie",r); |
233 | 233 | ||
234 | http_content_type.clear(); | 234 | http_content_type.clear(); |
235 | xmode = xm; | 235 | xmode = xm; |
236 | prepare_to_parse(); | 236 | prepare_to_parse(); |
237 | if(xmode&xmode_html) { | 237 | if(xmode&xmode_html) { |
238 | xrds_location.clear(); | 238 | xrds_location.clear(); |
239 | save_html.clear(); | 239 | save_html.clear(); |
240 | save_html.reserve(max_html); | 240 | save_html.reserve(max_html); |
241 | } | 241 | } |
242 | xrd = &idis.xrd; | 242 | xrd = &idis.xrd; |
243 | 243 | ||
244 | r = easy_perform(); | 244 | r = easy_perform(); |
245 | if(r && r!=CURLE_WRITE_ERROR) | 245 | if(r && r!=CURLE_WRITE_ERROR) |
246 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); | 246 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); |
247 | 247 | ||
248 | if(!parser_choked) { | 248 | if(!parser_choked) { |
249 | parse(0,0,true); | 249 | parse(0,0,true); |
250 | }else if(xmode&xmode_html){ | 250 | }else if(xmode&xmode_html){ |
251 | /* TODO: do not bother if we've seen xml */ | 251 | /* TODO: do not bother if we've seen xml */ |
252 | try { | 252 | try { |
253 | util::tidy_doc_t td = util::tidy_doc_t::create(); | 253 | util::tidy_doc_t td = util::tidy_doc_t::create(); |
254 | if(!td) | 254 | if(!td) |
255 | throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document"); | 255 | throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document"); |
256 | #ifndef NDEBUG | 256 | #ifndef NDEBUG |
257 | td.opt_set(TidyQuiet,false); | 257 | td.opt_set(TidyQuiet,false); |
258 | td.opt_set(TidyShowWarnings,false); | 258 | td.opt_set(TidyShowWarnings,false); |
259 | #endif /* NDEBUG */ | 259 | #endif /* NDEBUG */ |
260 | td.opt_set(TidyForceOutput,true); | 260 | td.opt_set(TidyForceOutput,true); |
261 | td.opt_set(TidyXhtmlOut,true); | 261 | td.opt_set(TidyXhtmlOut,true); |
262 | td.opt_set(TidyDoctypeMode,TidyDoctypeOmit); | 262 | td.opt_set(TidyDoctypeMode,TidyDoctypeOmit); |
263 | td.opt_set(TidyMark,false); | 263 | td.opt_set(TidyMark,false); |
264 | td.opt_set(TidyNumEntities,true); | 264 | td.opt_set(TidyNumEntities,true); |
265 | if(td.parse_string(save_html)<=0) | 265 | if(td.parse_string(save_html)<=0) |
266 | throw exception_tidy(OPKELE_CP_ "tidy failed to parse document"); | 266 | throw exception_tidy(OPKELE_CP_ "tidy failed to parse document"); |
267 | if(td.clean_and_repair()<=0) | 267 | if(td.clean_and_repair()<=0) |
268 | throw exception_tidy(OPKELE_CP_ "tidy failed to clean and repair"); | 268 | throw exception_tidy(OPKELE_CP_ "tidy failed to clean and repair"); |
269 | util::tidy_buf_t tide; | 269 | util::tidy_buf_t tide; |
270 | if(td.save_buffer(tide)<=0) | 270 | if(td.save_buffer(tide)<=0) |
271 | throw exception_tidy(OPKELE_CP_ "tidy failed to save buffer"); | 271 | throw exception_tidy(OPKELE_CP_ "tidy failed to save buffer"); |
272 | prepare_to_parse(); | 272 | prepare_to_parse(); |
273 | parse(tide.c_str(),tide.size(),true); | 273 | parse(tide.c_str(),tide.size(),true); |
274 | }catch(exception_tidy& et) { } | 274 | }catch(exception_tidy& et) { } |
275 | } | 275 | } |
276 | save_html.clear(); | 276 | save_html.clear(); |
277 | } | 277 | } |
278 | 278 | ||
279 | void prepare_to_parse() { | 279 | void prepare_to_parse() { |
280 | (*(expat_t*)this) = parser_create_ns(); | 280 | (*(expat_t*)this) = parser_create_ns(); |
281 | set_user_data(); set_element_handler(); | 281 | set_user_data(); set_element_handler(); |
282 | set_character_data_handler(); | 282 | set_character_data_handler(); |
283 | 283 | ||
284 | if(xmode&xmode_html) { | 284 | if(xmode&xmode_html) { |
285 | html_openid1.clear(); html_openid2.clear(); | 285 | html_openid1.clear(); html_openid2.clear(); |
286 | parser_choked = false; | 286 | parser_choked = false; |
287 | } | 287 | } |
288 | 288 | ||
289 | cdata = 0; xrd_service = 0; skipping = 0; | 289 | cdata = 0; xrd_service = 0; skipping = 0; |
290 | pt_stack.clear(); | 290 | pt_stack.clear(); |
291 | status_code = 100; status_string.clear(); | 291 | status_code = 100; status_string.clear(); |
292 | } | 292 | } |
293 | 293 | ||
294 | void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) { | 294 | void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) { |
295 | XRD_t& x = id.xrd; | 295 | XRD_t& x = id.xrd; |
296 | if(!html_openid2.uris.empty()) { | 296 | if(!html_openid2.uris.empty()) { |
297 | html_openid2.types.insert(STURI_OPENID20); | 297 | html_openid2.types.insert(STURI_OPENID20); |
298 | x.services.add(-1,html_openid2); | 298 | x.services.add(-1,html_openid2); |
299 | queue_endpoints(oi,id,&op_service_types[st_index_2]); | 299 | queue_endpoints(oi,id,&op_service_types[st_index_2]); |
300 | } | 300 | } |
301 | if(!html_openid1.uris.empty()) { | 301 | if(!html_openid1.uris.empty()) { |
302 | html_openid1.types.insert(STURI_OPENID11); | 302 | html_openid1.types.insert(STURI_OPENID11); |
303 | x.services.add(-1,html_openid1); | 303 | x.services.add(-1,html_openid1); |
304 | queue_endpoints(oi,id,&op_service_types[st_index_1]); | 304 | queue_endpoints(oi,id,&op_service_types[st_index_1]); |
305 | } | 305 | } |
306 | } | 306 | } |
307 | 307 | ||
308 | size_t write(void *p,size_t s,size_t nm) { | 308 | size_t write(void *p,size_t s,size_t nm) { |
309 | /* TODO: limit total size */ | 309 | /* TODO: limit total size */ |
310 | size_t bytes = s*nm; | 310 | size_t bytes = s*nm; |
311 | const char *inbuf = (const char*)p; | 311 | const char *inbuf = (const char*)p; |
312 | if(xmode&xmode_html) { | 312 | if(xmode&xmode_html) { |
313 | size_t mbts = save_html.capacity()-save_html.size(); | 313 | size_t mbts = save_html.capacity()-save_html.size(); |
314 | size_t bts = 0; | 314 | size_t bts = 0; |
315 | if(mbts>0) { | 315 | if(mbts>0) { |
316 | bts = (bytes>mbts)?mbts:bytes; | 316 | bts = (bytes>mbts)?mbts:bytes; |
317 | save_html.append(inbuf,bts); | 317 | save_html.append(inbuf,bts); |
318 | } | 318 | } |
319 | if(skipping<0) return bts; | 319 | if(skipping<0) return bts; |
320 | } | 320 | } |
321 | if(skipping<0) return 0; | 321 | if(skipping<0) return 0; |
322 | bool rp = parse(inbuf,bytes,false); | 322 | bool rp = parse(inbuf,bytes,false); |
323 | if(!rp) { | 323 | if(!rp) { |
324 | parser_choked = true; | 324 | parser_choked = true; |
325 | skipping = -1; | 325 | skipping = -1; |
326 | if(!(xmode&xmode_html)) | 326 | if(!(xmode&xmode_html)) |
327 | bytes = 0; | 327 | bytes = 0; |
328 | } | 328 | } |
329 | return bytes; | 329 | return bytes; |
330 | } | 330 | } |
331 | size_t header(void *p,size_t s,size_t nm) { | 331 | size_t header(void *p,size_t s,size_t nm) { |
332 | size_t bytes = s*nm; | 332 | size_t bytes = s*nm; |
333 | const char *h = (const char*)p; | 333 | const char *h = (const char*)p; |
334 | const char *colon = (const char*)memchr(p,':',bytes); | 334 | const char *colon = (const char*)memchr(p,':',bytes); |
335 | const char *space = (const char*)memchr(p,' ',bytes); | 335 | const char *space = (const char*)memchr(p,' ',bytes); |
336 | if(space && ( (!colon) || space<colon ) ) { | 336 | if(space && ( (!colon) || space<colon ) ) { |
337 | xrds_location.clear(); http_content_type.clear(); | 337 | xrds_location.clear(); http_content_type.clear(); |
338 | }else if(colon) { | 338 | }else if(colon) { |
339 | const char *hv = ++colon; | 339 | const char *hv = ++colon; |
340 | size_t hnl = colon-h; | 340 | size_t hnl = colon-h; |
341 | int rb; | 341 | int rb; |
342 | for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb); | 342 | for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb) ; |
343 | while(rb>0 && isspace(hv[rb-1])) --rb; | 343 | while(rb>0 && isspace(hv[rb-1])) --rb; |
344 | if(rb) { | 344 | if(rb) { |
345 | if( (hnl>=sizeof(XRDS_HEADER)) | 345 | if( (hnl>=sizeof(XRDS_HEADER)) |
346 | && !strncasecmp(h,XRDS_HEADER":", | 346 | && !strncasecmp(h,XRDS_HEADER":", |
347 | sizeof(XRDS_HEADER)) ) { | 347 | sizeof(XRDS_HEADER)) ) { |
348 | xrds_location.assign(hv,rb); | 348 | xrds_location.assign(hv,rb); |
349 | }else if( (hnl>=sizeof(CT_HEADER)) | 349 | }else if( (hnl>=sizeof(CT_HEADER)) |
350 | && !strncasecmp(h,CT_HEADER":", | 350 | && !strncasecmp(h,CT_HEADER":", |
351 | sizeof(CT_HEADER)) ) { | 351 | sizeof(CT_HEADER)) ) { |
352 | const char *sc = (const char*)memchr( | 352 | const char *sc = (const char*)memchr( |
353 | hv,';',rb); | 353 | hv,';',rb); |
354 | http_content_type.assign(hv,sc?(sc-hv):rb); | 354 | http_content_type.assign(hv,sc?(sc-hv):rb); |
355 | } | 355 | } |
356 | } | 356 | } |
357 | } | 357 | } |
358 | return curl_t::header(p,s,nm); | 358 | return curl_t::header(p,s,nm); |
359 | } | 359 | } |
360 | 360 | ||
361 | void start_element(const XML_Char *n,const XML_Char **a) { | 361 | void start_element(const XML_Char *n,const XML_Char **a) { |
362 | if(skipping<0) return; | 362 | if(skipping<0) return; |
363 | if(skipping) { | 363 | if(skipping) { |
364 | if(xmode&xmode_html) | 364 | if(xmode&xmode_html) |
365 | html_start_element(n,a); | 365 | html_start_element(n,a); |
366 | ++skipping; return; | 366 | ++skipping; return; |
367 | } | 367 | } |
368 | if(pt_stack.empty()) { | 368 | if(pt_stack.empty()) { |
369 | if(is_qelement(n,NSURI_XRDS "\tXRDS")) | 369 | if(is_qelement(n,NSURI_XRDS "\tXRDS")) |
370 | return; | 370 | return; |
371 | if(is_qelement(n,NSURI_XRD "\tXRD")) { | 371 | if(is_qelement(n,NSURI_XRD "\tXRD")) { |
372 | assert(xrd); | 372 | assert(xrd); |
373 | xrd->clear(); | 373 | xrd->clear(); |
374 | pt_stack.push_back(n); | 374 | pt_stack.push_back(n); |
375 | }else if(xmode&xmode_html) { | 375 | }else if(xmode&xmode_html) { |
376 | html_start_element(n,a); | 376 | html_start_element(n,a); |
377 | }else{ | 377 | }else{ |
378 | skipping = -1; | 378 | skipping = -1; |
379 | } | 379 | } |
380 | }else{ | 380 | }else{ |
381 | int pt_s = pt_stack.size(); | 381 | int pt_s = pt_stack.size(); |
382 | if(pt_s==1) { | 382 | if(pt_s==1) { |
383 | if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { | 383 | if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { |
384 | assert(xrd); | 384 | assert(xrd); |
385 | cdata = &(xrd->canonical_ids.add(element_priority(a),string())); | 385 | cdata = &(xrd->canonical_ids.add(element_priority(a),string())); |
386 | }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { | 386 | }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { |
387 | assert(xrd); | 387 | assert(xrd); |
388 | cdata = &(xrd->local_ids.add(element_priority(a),string())); | 388 | cdata = &(xrd->local_ids.add(element_priority(a),string())); |
389 | }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { | 389 | }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { |
390 | assert(xrd); | 390 | assert(xrd); |
391 | cdata = &(xrd->provider_id); | 391 | cdata = &(xrd->provider_id); |
392 | }else if(is_qelement(n,NSURI_XRD "\tService")) { | 392 | }else if(is_qelement(n,NSURI_XRD "\tService")) { |
393 | assert(xrd); | 393 | assert(xrd); |
394 | xrd_service = &(xrd->services.add(element_priority(a), | 394 | xrd_service = &(xrd->services.add(element_priority(a), |
395 | service_t())); | 395 | service_t())); |
396 | pt_stack.push_back(n); | 396 | pt_stack.push_back(n); |
397 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { | 397 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { |
398 | for(;*a;) { | 398 | for(;*a;) { |
399 | if(!strcasecmp(*(a++),"code")) { | 399 | if(!strcasecmp(*(a++),"code")) { |
400 | if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { | 400 | if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { |
401 | cdata = &status_string; | 401 | cdata = &status_string; |
402 | pt_stack.push_back(n); | 402 | pt_stack.push_back(n); |
403 | break; | 403 | break; |
404 | } | 404 | } |
405 | }else | 405 | }else |
406 | ++a; | 406 | ++a; |
407 | } | 407 | } |
408 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { | 408 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { |
409 | assert(xrd); | 409 | assert(xrd); |
410 | cdata_buf.clear(); | 410 | cdata_buf.clear(); |
411 | cdata = &cdata_buf; | 411 | cdata = &cdata_buf; |
412 | }else if(xmode&xmode_html) { | 412 | }else if(xmode&xmode_html) { |
413 | html_start_element(n,a); | 413 | html_start_element(n,a); |
414 | }else{ | 414 | }else{ |
415 | skipping = 1; | 415 | skipping = 1; |
416 | } | 416 | } |
417 | }else if(pt_s==2) { | 417 | }else if(pt_s==2) { |
418 | if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { | 418 | if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { |
419 | if(is_qelement(n,NSURI_XRD "\tType")) { | 419 | if(is_qelement(n,NSURI_XRD "\tType")) { |
420 | assert(xrd); assert(xrd_service); | 420 | assert(xrd); assert(xrd_service); |
421 | cdata_buf.clear(); | 421 | cdata_buf.clear(); |
422 | cdata = &cdata_buf; | 422 | cdata = &cdata_buf; |
423 | }else if(is_qelement(n,NSURI_XRD "\tURI")) { | 423 | }else if(is_qelement(n,NSURI_XRD "\tURI")) { |
424 | assert(xrd); assert(xrd_service); | 424 | assert(xrd); assert(xrd_service); |
425 | const char *append = element_attr(a,"append"); | 425 | const char *append = element_attr(a,"append"); |
426 | xrd::uri_t& uri = xrd_service->uris.add(element_priority(a),xrd::uri_t("",append?append:"")); | 426 | xrd::uri_t& uri = xrd_service->uris.add(element_priority(a),xrd::uri_t("",append?append:"")); |
427 | cdata = &uri.uri; | 427 | cdata = &uri.uri; |
428 | }else if(is_qelement(n,NSURI_XRD "\tLocalID") | 428 | }else if(is_qelement(n,NSURI_XRD "\tLocalID") |
429 | || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { | 429 | || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { |
430 | assert(xrd); assert(xrd_service); | 430 | assert(xrd); assert(xrd_service); |
431 | cdata = &(xrd_service->local_ids.add(element_priority(a),string())); | 431 | cdata = &(xrd_service->local_ids.add(element_priority(a),string())); |
432 | }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { | 432 | }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { |
433 | assert(xrd); assert(xrd_service); | 433 | assert(xrd); assert(xrd_service); |
434 | cdata = &(xrd_service->provider_id); | 434 | cdata = &(xrd_service->provider_id); |
435 | }else{ | 435 | }else{ |
436 | skipping = 1; | 436 | skipping = 1; |
437 | } | 437 | } |
438 | }else | 438 | }else |
439 | skipping = 1; | 439 | skipping = 1; |
440 | }else if(xmode&xmode_html) { | 440 | }else if(xmode&xmode_html) { |
441 | html_start_element(n,a); | 441 | html_start_element(n,a); |
442 | }else{ | 442 | }else{ |
443 | skipping = 1; | 443 | skipping = 1; |
444 | } | 444 | } |
445 | } | 445 | } |
446 | } | 446 | } |
447 | void end_element(const XML_Char *n) { | 447 | void end_element(const XML_Char *n) { |
448 | if(skipping<0) return; | 448 | if(skipping<0) return; |
449 | if(skipping) { | 449 | if(skipping) { |
450 | --skipping; return; | 450 | --skipping; return; |
451 | } | 451 | } |
452 | if(is_qelement(n,NSURI_XRD "\tType")) { | 452 | if(is_qelement(n,NSURI_XRD "\tType")) { |
453 | assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); | 453 | assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); |
454 | xrd_service->types.insert(cdata_buf); | 454 | xrd_service->types.insert(cdata_buf); |
455 | }else if(is_qelement(n,NSURI_XRD "\tService")) { | 455 | }else if(is_qelement(n,NSURI_XRD "\tService")) { |
456 | assert(xrd); assert(xrd_service); | 456 | assert(xrd); assert(xrd_service); |
457 | assert(!pt_stack.empty()); | 457 | assert(!pt_stack.empty()); |
458 | assert(pt_stack.back()==(NSURI_XRD "\tService")); | 458 | assert(pt_stack.back()==(NSURI_XRD "\tService")); |
459 | pt_stack.pop_back(); | 459 | pt_stack.pop_back(); |
460 | xrd_service = 0; | 460 | xrd_service = 0; |
461 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { | 461 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { |
462 | assert(xrd); | 462 | assert(xrd); |
463 | if(is_qelement(pt_stack.back().c_str(),n)) { | 463 | if(is_qelement(pt_stack.back().c_str(),n)) { |
464 | assert(cdata==&status_string); | 464 | assert(cdata==&status_string); |
465 | pt_stack.pop_back(); | 465 | pt_stack.pop_back(); |
466 | if(status_code!=100) | 466 | if(status_code!=100) |
467 | skipping = -1; | 467 | skipping = -1; |
468 | } | 468 | } |
469 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { | 469 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { |
470 | assert(xrd); | 470 | assert(xrd); |
471 | xrd->expires = util::w3c_to_time(cdata_buf); | 471 | xrd->expires = util::w3c_to_time(cdata_buf); |
472 | }else if((xmode&xmode_html) && is_element(n,"head")) { | 472 | }else if((xmode&xmode_html) && is_element(n,"head")) { |
473 | skipping = -1; | 473 | skipping = -1; |
474 | } | 474 | } |
475 | cdata = 0; | 475 | cdata = 0; |
476 | } | 476 | } |
477 | void character_data(const XML_Char *s,int l) { | 477 | void character_data(const XML_Char *s,int l) { |
478 | if(skipping) return; | 478 | if(skipping) return; |
479 | if(cdata) cdata->append(s,l); | 479 | if(cdata) cdata->append(s,l); |
480 | } | 480 | } |
481 | 481 | ||
482 | void html_start_element(const XML_Char *n,const XML_Char **a) { | 482 | void html_start_element(const XML_Char *n,const XML_Char **a) { |
483 | if(is_element(n,"meta")) { | 483 | if(is_element(n,"meta")) { |
484 | bool heq = false; | 484 | bool heq = false; |
485 | string l; | 485 | string l; |
486 | for(;*a;a+=2) { | 486 | for(;*a;a+=2) { |
487 | if(!( strcasecmp(a[0],"http-equiv") | 487 | if(!( strcasecmp(a[0],"http-equiv") |
488 | || strcasecmp(a[1],XRDS_HEADER) )) | 488 | || strcasecmp(a[1],XRDS_HEADER) )) |
489 | heq = true; | 489 | heq = true; |
490 | else if(!strcasecmp(a[0],"content")) | 490 | else if(!strcasecmp(a[0],"content")) |
491 | l.assign(a[1]); | 491 | l.assign(a[1]); |
492 | } | 492 | } |
493 | if(heq) | 493 | if(heq) |
494 | xrds_location = l; | 494 | xrds_location = l; |
495 | }else if(is_element(n,"link")) { | 495 | }else if(is_element(n,"link")) { |
496 | string rels; | 496 | string rels; |
497 | string href; | 497 | string href; |
498 | for(;*a;a+=2) { | 498 | for(;*a;a+=2) { |
499 | if( !strcasecmp(a[0],"rel") ) { | 499 | if( !strcasecmp(a[0],"rel") ) { |
500 | rels.assign(a[1]); | 500 | rels.assign(a[1]); |
501 | }else if( !strcasecmp(a[0],"href") ) { | 501 | }else if( !strcasecmp(a[0],"href") ) { |
502 | const char *ns = a[1]; | 502 | const char *ns = a[1]; |
503 | for(;*ns && isspace(*ns);++ns); | 503 | for(;*ns && isspace(*ns);++ns) ; |
504 | href.assign(ns); | 504 | href.assign(ns); |
505 | string::size_type lns=href.find_last_not_of(data::_whitespace_chars); | 505 | string::size_type lns=href.find_last_not_of(data::_whitespace_chars); |
506 | href.erase(lns+1); | 506 | href.erase(lns+1); |
507 | } | 507 | } |
508 | } | 508 | } |
509 | for(string::size_type ns=rels.find_first_not_of(data::_whitespace_chars); | 509 | for(string::size_type ns=rels.find_first_not_of(data::_whitespace_chars); |
510 | ns!=string::npos; ns=rels.find_first_not_of(data::_whitespace_chars,ns)) { | 510 | ns!=string::npos; ns=rels.find_first_not_of(data::_whitespace_chars,ns)) { |
511 | string::size_type s = rels.find_first_of(data::_whitespace_chars,ns); | 511 | string::size_type s = rels.find_first_of(data::_whitespace_chars,ns); |
512 | string rel; | 512 | string rel; |
513 | if(s==string::npos) { | 513 | if(s==string::npos) { |
514 | rel.assign(rels,ns,string::npos); | 514 | rel.assign(rels,ns,string::npos); |
515 | ns = string::npos; | 515 | ns = string::npos; |
516 | }else{ | 516 | }else{ |
517 | rel.assign(rels,ns,s-ns); | 517 | rel.assign(rels,ns,s-ns); |
518 | ns = s; | 518 | ns = s; |
519 | } | 519 | } |
520 | if(rel=="openid.server") | 520 | if(rel=="openid.server") |
521 | html_openid1.uris.add(-1,xrd::uri_t(href)); | 521 | html_openid1.uris.add(-1,xrd::uri_t(href)); |
522 | else if(rel=="openid.delegate") | 522 | else if(rel=="openid.delegate") |
523 | html_openid1.local_ids.add(-1,href); | 523 | html_openid1.local_ids.add(-1,href); |
524 | else if(rel=="openid2.provider") | 524 | else if(rel=="openid2.provider") |
525 | html_openid2.uris.add(-1,xrd::uri_t(href)); | 525 | html_openid2.uris.add(-1,xrd::uri_t(href)); |
526 | else if(rel=="openid2.local_id") | 526 | else if(rel=="openid2.local_id") |
527 | html_openid2.local_ids.add(-1,href); | 527 | html_openid2.local_ids.add(-1,href); |
528 | } | 528 | } |
529 | }else if(is_element(n,"body")) { | 529 | }else if(is_element(n,"body")) { |
530 | skipping = -1; | 530 | skipping = -1; |
531 | } | 531 | } |
532 | } | 532 | } |
533 | 533 | ||
534 | void queue_endpoints(endpoint_discovery_iterator& oi, | 534 | void queue_endpoints(endpoint_discovery_iterator& oi, |
535 | const idiscovery_t &id, | 535 | const idiscovery_t &id, |
536 | const service_type_t *st) { | 536 | const service_type_t *st) { |
537 | openid_endpoint_t ep; | 537 | openid_endpoint_t ep; |
538 | ep.claimed_id = id.canonicalized_id; | 538 | ep.claimed_id = id.canonicalized_id; |
539 | for(xrd::services_t::const_iterator isvc=id.xrd.services.begin(); | 539 | for(xrd::services_t::const_iterator isvc=id.xrd.services.begin(); |
540 | isvc!=id.xrd.services.end(); ++isvc) { | 540 | isvc!=id.xrd.services.end(); ++isvc) { |
541 | const xrd::service_t svc = isvc->second; | 541 | const xrd::service_t svc = isvc->second; |
542 | if(svc.types.find(st->uri)==svc.types.end()) continue; | 542 | if(svc.types.find(st->uri)==svc.types.end()) continue; |
543 | for(xrd::uris_t::const_iterator iu=svc.uris.begin();iu!=svc.uris.end();++iu) { | 543 | for(xrd::uris_t::const_iterator iu=svc.uris.begin();iu!=svc.uris.end();++iu) { |
544 | ep.uri = iu->second.uri; | 544 | ep.uri = iu->second.uri; |
545 | if(id.xri_identity) { | 545 | if(id.xri_identity) { |
546 | if(iu->second.append=="qxri") { | 546 | if(iu->second.append=="qxri") { |
547 | ep.uri += id.normalized_id; | 547 | ep.uri += id.normalized_id; |
548 | } /* TODO: else handle other append attribute values */ | 548 | } /* TODO: else handle other append attribute values */ |
549 | } | 549 | } |
550 | if(st->forceid) { | 550 | if(st->forceid) { |
551 | ep.local_id = ep.claimed_id = st->forceid; | 551 | ep.local_id = ep.claimed_id = st->forceid; |
552 | *(oi++) = ep; | 552 | *(oi++) = ep; |
553 | }else{ | 553 | }else{ |
554 | if(svc.local_ids.empty()) { | 554 | if(svc.local_ids.empty()) { |
555 | ep.local_id = ep.claimed_id; | 555 | ep.local_id = ep.claimed_id; |
556 | *(oi++) = ep; | 556 | *(oi++) = ep; |
557 | }else{ | 557 | }else{ |
558 | for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin(); | 558 | for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin(); |
559 | ilid!=svc.local_ids.end(); ++ilid) { | 559 | ilid!=svc.local_ids.end(); ++ilid) { |
560 | ep.local_id = ilid->second; | 560 | ep.local_id = ilid->second; |
561 | *(oi++) = ep; | 561 | *(oi++) = ep; |
562 | } | 562 | } |
563 | } | 563 | } |
564 | } | 564 | } |
565 | } | 565 | } |
566 | } | 566 | } |
567 | } | 567 | } |
568 | 568 | ||
569 | }; | 569 | }; |
570 | 570 | ||
571 | string idiscover(endpoint_discovery_iterator oi,const string& identity) { | 571 | string idiscover(endpoint_discovery_iterator oi,const string& identity) { |
572 | idigger_t idigger; | 572 | idigger_t idigger; |
573 | return idigger.discover(oi,identity); | 573 | return idigger.discover(oi,identity); |
574 | } | 574 | } |
575 | 575 | ||
576 | void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) try { | 576 | void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) try { |
577 | idigger_t idigger; | 577 | idigger_t idigger; |
578 | idigger.yadiscover(oi,yurl,types,redirs); | 578 | idigger.yadiscover(oi,yurl,types,redirs); |
579 | }catch(exception_curl& ec) { | 579 | }catch(exception_curl& ec) { |
580 | if(redirs || ec._error!=CURLE_TOO_MANY_REDIRECTS) | 580 | if(redirs || ec._error!=CURLE_TOO_MANY_REDIRECTS) |
581 | throw; | 581 | throw; |
582 | } | 582 | } |
583 | 583 | ||
584 | } | 584 | } |
diff --git a/lib/message.cc b/lib/message.cc index 524946a..c1f8088 100644 --- a/lib/message.cc +++ b/lib/message.cc | |||
@@ -1,196 +1,196 @@ | |||
1 | #include <cassert> | 1 | #include <cassert> |
2 | #include <algorithm> | 2 | #include <algorithm> |
3 | 3 | ||
4 | #include <opkele/types.h> | 4 | #include <opkele/types.h> |
5 | #include <opkele/exception.h> | 5 | #include <opkele/exception.h> |
6 | #include <opkele/util.h> | 6 | #include <opkele/util.h> |
7 | #include <opkele/debug.h> | 7 | #include <opkele/debug.h> |
8 | 8 | ||
9 | #include "config.h" | 9 | #include "config.h" |
10 | 10 | ||
11 | namespace opkele { | 11 | namespace opkele { |
12 | using std::input_iterator_tag; | 12 | using std::input_iterator_tag; |
13 | using std::unary_function; | 13 | using std::unary_function; |
14 | 14 | ||
15 | 15 | ||
16 | struct __om_ns_finder : public unary_function<const string&,bool> { | 16 | struct __om_ns_finder : public unary_function<const string&,bool> { |
17 | public: | 17 | public: |
18 | const basic_openid_message& om; | 18 | const basic_openid_message& om; |
19 | const string& uri; | 19 | const string& uri; |
20 | 20 | ||
21 | __om_ns_finder(const basic_openid_message& m, | 21 | __om_ns_finder(const basic_openid_message& m, |
22 | const string& u) : om(m), uri(u) { } | 22 | const string& u) : om(m), uri(u) { } |
23 | 23 | ||
24 | result_type operator()(argument_type f) { | 24 | result_type operator()(argument_type f) { |
25 | return | 25 | return |
26 | (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1)) | 26 | (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1)) |
27 | && om.get_field(f)==uri ; | 27 | && om.get_field(f)==uri ; |
28 | } | 28 | } |
29 | }; | 29 | }; |
30 | 30 | ||
31 | bool basic_openid_message::has_ns(const string& uri) const { | 31 | bool basic_openid_message::has_ns(const string& uri) const { |
32 | fields_iterator ei = fields_end(); | 32 | fields_iterator ei = fields_end(); |
33 | fields_iterator i = find_if(fields_begin(),fields_end(), | 33 | fields_iterator i = find_if(fields_begin(),fields_end(), |
34 | __om_ns_finder(*this,uri)); | 34 | __om_ns_finder(*this,uri)); |
35 | return !(i==ei); | 35 | return !(i==ei); |
36 | } | 36 | } |
37 | string basic_openid_message::get_ns(const string& uri) const { | 37 | string basic_openid_message::get_ns(const string& uri) const { |
38 | fields_iterator ei = fields_end(); | 38 | fields_iterator ei = fields_end(); |
39 | fields_iterator i = find_if(fields_begin(),fields_end(), | 39 | fields_iterator i = find_if(fields_begin(),fields_end(), |
40 | __om_ns_finder(*this,uri)); | 40 | __om_ns_finder(*this,uri)); |
41 | if(i==ei) | 41 | if(i==ei) |
42 | throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri); | 42 | throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri); |
43 | return i->substr(3); | 43 | return i->substr(3); |
44 | } | 44 | } |
45 | 45 | ||
46 | void basic_openid_message::from_keyvalues(const string& kv) { | 46 | void basic_openid_message::from_keyvalues(const string& kv) { |
47 | reset_fields(); | 47 | reset_fields(); |
48 | string::size_type p = 0; | 48 | string::size_type p = 0; |
49 | while(true) { | 49 | while(true) { |
50 | string::size_type co = kv.find(':',p); | 50 | string::size_type co = kv.find(':',p); |
51 | if(co==string::npos) | 51 | if(co==string::npos) |
52 | break; | 52 | break; |
53 | #ifndef POSTELS_LAW | 53 | #ifndef POSTELS_LAW |
54 | string::size_type nl = kv.find('\n',co+1); | 54 | string::size_type nl = kv.find('\n',co+1); |
55 | if(nl==string::npos) | 55 | if(nl==string::npos) |
56 | throw bad_input(OPKELE_CP_ "malformed input"); | 56 | throw bad_input(OPKELE_CP_ "malformed input"); |
57 | if(nl>co) | 57 | if(nl>co) |
58 | set_field(kv.substr(p,co-p),kv.substr(co+1,nl-co-1)); | 58 | set_field(kv.substr(p,co-p),kv.substr(co+1,nl-co-1)); |
59 | p = nl+1; | 59 | p = nl+1; |
60 | #else /* POSTELS_LAW */ | 60 | #else /* POSTELS_LAW */ |
61 | string::size_type lb = kv.find_first_of("\r\n",co+1); | 61 | string::size_type lb = kv.find_first_of("\r\n",co+1); |
62 | if(lb==string::npos) { | 62 | if(lb==string::npos) { |
63 | set_field(kv.substr(p,co-p),kv.substr(co+1)); | 63 | set_field(kv.substr(p,co-p),kv.substr(co+1)); |
64 | break; | 64 | break; |
65 | } | 65 | } |
66 | if(lb>co) | 66 | if(lb>co) |
67 | set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); | 67 | set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); |
68 | string::size_type nolb = kv.find_first_not_of("\r\n",lb); | 68 | string::size_type nolb = kv.find_first_not_of("\r\n",lb); |
69 | if(nolb==string::npos) | 69 | if(nolb==string::npos) |
70 | break; | 70 | break; |
71 | p = nolb; | 71 | p = nolb; |
72 | #endif /* POSTELS_LAW */ | 72 | #endif /* POSTELS_LAW */ |
73 | } | 73 | } |
74 | } | 74 | } |
75 | 75 | ||
76 | struct __om_kv_outputter : public unary_function<const string&,void> { | 76 | struct __om_kv_outputter : public unary_function<const string&,void> { |
77 | public: | 77 | public: |
78 | const basic_openid_message& om; | 78 | const basic_openid_message& om; |
79 | ostream& os; | 79 | ostream& os; |
80 | 80 | ||
81 | __om_kv_outputter(const basic_openid_message& m,ostream& s) | 81 | __om_kv_outputter(const basic_openid_message& m,ostream& s) |
82 | : om(m), os(s) { } | 82 | : om(m), os(s) { } |
83 | 83 | ||
84 | result_type operator()(argument_type f) { | 84 | result_type operator()(argument_type f) { |
85 | os << f << ':' << om.get_field(f) << '\n'; | 85 | os << f << ':' << om.get_field(f) << '\n'; |
86 | } | 86 | } |
87 | }; | 87 | }; |
88 | 88 | ||
89 | void basic_openid_message::to_keyvalues(ostream& o) const { | 89 | void basic_openid_message::to_keyvalues(ostream& o) const { |
90 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); | 90 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); |
91 | } | 91 | } |
92 | 92 | ||
93 | struct __om_html_outputter : public unary_function<const string&,void> { | 93 | struct __om_html_outputter : public unary_function<const string&,void> { |
94 | public: | 94 | public: |
95 | const basic_openid_message& om; | 95 | const basic_openid_message& om; |
96 | ostream& os; | 96 | ostream& os; |
97 | const char *pfx; | 97 | const char *pfx; |
98 | 98 | ||
99 | __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0) | 99 | __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0) |
100 | : om(m), os(s), pfx(p) { } | 100 | : om(m), os(s), pfx(p) { } |
101 | 101 | ||
102 | result_type operator()(argument_type f) { | 102 | result_type operator()(argument_type f) { |
103 | os << | 103 | os << |
104 | "<input type=\"hidden\"" | 104 | "<input type=\"hidden\"" |
105 | " name=\""; | 105 | " name=\""; |
106 | if(pfx) | 106 | if(pfx) |
107 | os << util::attr_escape(pfx); | 107 | os << util::attr_escape(pfx); |
108 | os << util::attr_escape(f) << "\"" | 108 | os << util::attr_escape(f) << "\"" |
109 | " value=\"" << util::attr_escape(om.get_field(f)) << "\" />"; | 109 | " value=\"" << util::attr_escape(om.get_field(f)) << "\" />"; |
110 | } | 110 | } |
111 | }; | 111 | }; |
112 | 112 | ||
113 | void basic_openid_message::to_htmlhiddens(ostream& o,const char* pfx) const { | 113 | void basic_openid_message::to_htmlhiddens(ostream& o,const char* pfx) const { |
114 | for_each(fields_begin(),fields_end(),__om_html_outputter(*this,o,pfx)); | 114 | for_each(fields_begin(),fields_end(),__om_html_outputter(*this,o,pfx)); |
115 | } | 115 | } |
116 | 116 | ||
117 | void basic_openid_message::add_to_signed(const string& fields) { | 117 | void basic_openid_message::add_to_signed(const string& fields) { |
118 | string::size_type fnc = fields.find_first_not_of(","); | 118 | string::size_type fnc = fields.find_first_not_of(","); |
119 | if(fnc==string::npos) | 119 | if(fnc==string::npos) |
120 | throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields"); | 120 | throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields"); |
121 | string signeds; | 121 | string signeds; |
122 | try { | 122 | try { |
123 | signeds = get_field("signed"); | 123 | signeds = get_field("signed"); |
124 | string::size_type lnc = signeds.find_last_not_of(","); | 124 | string::size_type lnc = signeds.find_last_not_of(","); |
125 | if(lnc==string::npos) | 125 | if(lnc==string::npos) |
126 | signeds.assign(fields,fnc,fields.size()-fnc); | 126 | signeds.assign(fields,fnc,fields.size()-fnc); |
127 | else{ | 127 | else{ |
128 | string::size_type ss = signeds.size(); | 128 | string::size_type ss = signeds.size(); |
129 | if(lnc==(ss-1)) { | 129 | if(lnc==(ss-1)) { |
130 | signeds+= ','; | 130 | signeds+= ','; |
131 | signeds.append(fields,fnc,fields.size()-fnc); | 131 | signeds.append(fields,fnc,fields.size()-fnc); |
132 | }else{ | 132 | }else{ |
133 | if(lnc<(ss-2)) | 133 | if(lnc<(ss-2)) |
134 | signeds.replace(lnc+2,ss-lnc-2, | 134 | signeds.replace(lnc+2,ss-lnc-2, |
135 | fields,fnc,fields.size()-fnc); | 135 | fields,fnc,fields.size()-fnc); |
136 | else | 136 | else |
137 | signeds.append(fields,fnc,fields.size()-fnc); | 137 | signeds.append(fields,fnc,fields.size()-fnc); |
138 | } | 138 | } |
139 | } | 139 | } |
140 | }catch(failed_lookup&) { | 140 | }catch(failed_lookup&) { |
141 | signeds.assign(fields,fnc,fields.size()-fnc); | 141 | signeds.assign(fields,fnc,fields.size()-fnc); |
142 | } | 142 | } |
143 | set_field("signed",signeds); | 143 | set_field("signed",signeds); |
144 | } | 144 | } |
145 | 145 | ||
146 | string basic_openid_message::find_ns(const string& uri,const char *pfx) const { | 146 | string basic_openid_message::find_ns(const string& uri,const char *pfx) const { |
147 | try { | 147 | try { |
148 | return get_ns(uri); | 148 | return get_ns(uri); |
149 | }catch(failed_lookup&) { | 149 | }catch(failed_lookup&) { |
150 | return pfx; | 150 | return pfx; |
151 | } | 151 | } |
152 | } | 152 | } |
153 | string basic_openid_message::allocate_ns(const string& uri,const char *pfx) { | 153 | string basic_openid_message::allocate_ns(const string& uri,const char *pfx) { |
154 | if(!has_field("ns")) | 154 | if(!has_field("ns")) |
155 | return pfx; | 155 | return pfx; |
156 | if(has_ns(uri)) | 156 | if(has_ns(uri)) |
157 | throw bad_input(OPKELE_CP_ "OpenID message already contains namespace"); | 157 | throw bad_input(OPKELE_CP_ "OpenID message already contains namespace"); |
158 | string rv = pfx; | 158 | string rv = pfx; |
159 | if(has_field("ns."+rv)) { | 159 | if(has_field("ns."+rv)) { |
160 | string::reference c=rv[rv.length()]; | 160 | string::reference c=rv[rv.length()]; |
161 | for(c='a';c<='z' && has_field("ns."+rv);++c); | 161 | for(c='a';c<='z' && has_field("ns."+rv);++c) ; |
162 | if(c=='z') | 162 | if(c=='z') |
163 | throw exception(OPKELE_CP_ "Failed to allocate namespace"); | 163 | throw exception(OPKELE_CP_ "Failed to allocate namespace"); |
164 | } | 164 | } |
165 | set_field("ns."+rv,uri); | 165 | set_field("ns."+rv,uri); |
166 | return rv; | 166 | return rv; |
167 | } | 167 | } |
168 | 168 | ||
169 | bool openid_message_t::has_field(const string& n) const { | 169 | bool openid_message_t::has_field(const string& n) const { |
170 | return find(n)!=end(); | 170 | return find(n)!=end(); |
171 | } | 171 | } |
172 | const string& openid_message_t::get_field(const string& n) const { | 172 | const string& openid_message_t::get_field(const string& n) const { |
173 | const_iterator i=find(n); | 173 | const_iterator i=find(n); |
174 | if(i==end()) | 174 | if(i==end()) |
175 | throw failed_lookup(OPKELE_CP_ n+": no such field"); | 175 | throw failed_lookup(OPKELE_CP_ n+": no such field"); |
176 | return i->second; | 176 | return i->second; |
177 | } | 177 | } |
178 | 178 | ||
179 | openid_message_t::fields_iterator openid_message_t::fields_begin() const { | 179 | openid_message_t::fields_iterator openid_message_t::fields_begin() const { |
180 | return util::map_keys_iterator<const_iterator,string,const string&,const string*>(begin(),end()); | 180 | return util::map_keys_iterator<const_iterator,string,const string&,const string*>(begin(),end()); |
181 | } | 181 | } |
182 | openid_message_t::fields_iterator openid_message_t::fields_end() const { | 182 | openid_message_t::fields_iterator openid_message_t::fields_end() const { |
183 | return util::map_keys_iterator<const_iterator,string,const string&,const string*>(end(),end()); | 183 | return util::map_keys_iterator<const_iterator,string,const string&,const string*>(end(),end()); |
184 | } | 184 | } |
185 | 185 | ||
186 | void openid_message_t::reset_fields() { | 186 | void openid_message_t::reset_fields() { |
187 | clear(); | 187 | clear(); |
188 | } | 188 | } |
189 | void openid_message_t::set_field(const string& n,const string& v) { | 189 | void openid_message_t::set_field(const string& n,const string& v) { |
190 | (*this)[n]=v; | 190 | (*this)[n]=v; |
191 | } | 191 | } |
192 | void openid_message_t::reset_field(const string& n) { | 192 | void openid_message_t::reset_field(const string& n) { |
193 | erase(n); | 193 | erase(n); |
194 | } | 194 | } |
195 | 195 | ||
196 | } | 196 | } |
diff --git a/lib/prequeue_rp.cc b/lib/prequeue_rp.cc index e499d08..886efae 100644 --- a/lib/prequeue_rp.cc +++ b/lib/prequeue_rp.cc | |||
@@ -1,86 +1,86 @@ | |||
1 | #include <iostream> | 1 | #include <iostream> |
2 | #include <openssl/sha.h> | 2 | #include <openssl/sha.h> |
3 | #include <openssl/hmac.h> | 3 | #include <openssl/hmac.h> |
4 | #include <opkele/exception.h> | 4 | #include <opkele/exception.h> |
5 | #include <opkele/prequeue_rp.h> | 5 | #include <opkele/prequeue_rp.h> |
6 | #include <opkele/discovery.h> | 6 | #include <opkele/discovery.h> |
7 | #include <opkele/uris.h> | 7 | #include <opkele/uris.h> |
8 | #include <opkele/data.h> | 8 | #include <opkele/data.h> |
9 | #include <opkele/util.h> | 9 | #include <opkele/util.h> |
10 | #include <opkele/curl.h> | 10 | #include <opkele/curl.h> |
11 | #include <opkele/debug.h> | 11 | #include <opkele/debug.h> |
12 | 12 | ||
13 | namespace opkele { | 13 | namespace opkele { |
14 | 14 | ||
15 | class __OP_verifier_good_input : public exception { | 15 | class __OP_verifier_good_input : public exception { |
16 | public: | 16 | public: |
17 | __OP_verifier_good_input(OPKELE_E_PARS) | 17 | __OP_verifier_good_input(OPKELE_E_PARS) |
18 | : exception(OPKELE_E_CONS) { } | 18 | : exception(OPKELE_E_CONS) { } |
19 | }; | 19 | }; |
20 | 20 | ||
21 | class OP_verifier : public iterator<output_iterator_tag,openid_endpoint_t,void> { | 21 | class OP_verifier : public iterator<output_iterator_tag,openid_endpoint_t,void> { |
22 | public: | 22 | public: |
23 | const string& OP; | 23 | const string& OP; |
24 | const string& id; | 24 | const string& id; |
25 | 25 | ||
26 | OP_verifier(const string& o,const string& i) | 26 | OP_verifier(const string& o,const string& i) |
27 | : OP(o), id(i) { } | 27 | : OP(o), id(i) { } |
28 | 28 | ||
29 | OP_verifier& operator*() { return *this; } | 29 | OP_verifier& operator*() { return *this; } |
30 | OP_verifier& operator=(const openid_endpoint_t& oep) { | 30 | OP_verifier& operator=(const openid_endpoint_t& oep) { |
31 | if(oep.uri==OP) { | 31 | if(oep.uri==OP) { |
32 | if(oep.claimed_id==IDURI_SELECT20 | 32 | if(oep.claimed_id==IDURI_SELECT20 |
33 | || oep.local_id==IDURI_SELECT20 ) | 33 | || oep.local_id==IDURI_SELECT20 ) |
34 | throw bad_input(OPKELE_CP_ "claimed_id is an OP-Id"); | 34 | throw bad_input(OPKELE_CP_ "claimed_id is an OP-Id"); |
35 | if(oep.local_id==id) | 35 | if(oep.local_id==id) |
36 | throw __OP_verifier_good_input(OPKELE_CP_ "Found corresponding endpoint"); | 36 | throw __OP_verifier_good_input(OPKELE_CP_ "Found corresponding endpoint"); |
37 | } | 37 | } |
38 | return *this; | 38 | return *this; |
39 | } | 39 | } |
40 | 40 | ||
41 | OP_verifier& operator++() { return *this; } | 41 | OP_verifier& operator++() { return *this; } |
42 | OP_verifier& operator++(int) { return *this; } | 42 | OP_verifier& operator++(int) { return *this; } |
43 | }; | 43 | }; |
44 | 44 | ||
45 | void prequeue_RP::verify_OP(const string& OP,const string& claimed_id,const string& id) const { | 45 | void prequeue_RP::verify_OP(const string& OP,const string& _claimed_id,const string& id) const { |
46 | try { | 46 | try { |
47 | discover(OP_verifier(OP,id),claimed_id); | 47 | discover(OP_verifier(OP,id),_claimed_id); |
48 | throw id_res_unauthorized(OPKELE_CP_ | 48 | throw id_res_unauthorized(OPKELE_CP_ |
49 | "OP is not authorized to make an assertion regarding the identity"); | 49 | "OP is not authorized to make an assertion regarding the identity"); |
50 | }catch(__OP_verifier_good_input& ovgi) { | 50 | }catch(__OP_verifier_good_input& ovgi) { |
51 | } | 51 | } |
52 | } | 52 | } |
53 | 53 | ||
54 | class endpoint_queuer : public iterator<output_iterator_tag,openid_endpoint_t,void> { | 54 | class endpoint_queuer : public iterator<output_iterator_tag,openid_endpoint_t,void> { |
55 | public: | 55 | public: |
56 | prequeue_RP& rp; | 56 | prequeue_RP& rp; |
57 | 57 | ||
58 | endpoint_queuer(prequeue_RP& r) : rp(r) { } | 58 | endpoint_queuer(prequeue_RP& r) : rp(r) { } |
59 | 59 | ||
60 | endpoint_queuer& operator*() { return *this; } | 60 | endpoint_queuer& operator*() { return *this; } |
61 | endpoint_queuer& operator=(const openid_endpoint_t& oep) { | 61 | endpoint_queuer& operator=(const openid_endpoint_t& oep) { |
62 | rp.queue_endpoint(oep); return *this; } | 62 | rp.queue_endpoint(oep); return *this; } |
63 | 63 | ||
64 | endpoint_queuer& operator++() { return *this; } | 64 | endpoint_queuer& operator++() { return *this; } |
65 | endpoint_queuer& operator++(int) { return *this; } | 65 | endpoint_queuer& operator++(int) { return *this; } |
66 | }; | 66 | }; |
67 | 67 | ||
68 | void prequeue_RP::initiate(const string& usi) { | 68 | void prequeue_RP::initiate(const string& usi) { |
69 | begin_queueing(); | 69 | begin_queueing(); |
70 | set_normalized_id( discover(endpoint_queuer(*this),usi) ); | 70 | set_normalized_id( discover(endpoint_queuer(*this),usi) ); |
71 | end_queueing(); | 71 | end_queueing(); |
72 | } | 72 | } |
73 | 73 | ||
74 | void prequeue_RP::set_normalized_id(const string&) { | 74 | void prequeue_RP::set_normalized_id(const string&) { |
75 | } | 75 | } |
76 | 76 | ||
77 | const string prequeue_RP::get_normalized_id() const { | 77 | const string prequeue_RP::get_normalized_id() const { |
78 | throw not_implemented(OPKELE_CP_ "get_normalized_id() is not implemented"); | 78 | throw not_implemented(OPKELE_CP_ "get_normalized_id() is not implemented"); |
79 | } | 79 | } |
80 | 80 | ||
81 | const string prequeue_RP::discover(openid_endpoint_output_iterator it, | 81 | const string prequeue_RP::discover(openid_endpoint_output_iterator it, |
82 | const string& id) const { | 82 | const string& id) const { |
83 | return idiscover(it,id); | 83 | return idiscover(it,id); |
84 | } | 84 | } |
85 | 85 | ||
86 | } | 86 | } |
diff --git a/lib/secret.cc b/lib/secret.cc index d538890..3f1e39c 100644 --- a/lib/secret.cc +++ b/lib/secret.cc | |||
@@ -1,45 +1,45 @@ | |||
1 | #include <algorithm> | 1 | #include <algorithm> |
2 | #include <functional> | 2 | #include <functional> |
3 | #include <opkele/types.h> | 3 | #include <opkele/types.h> |
4 | #include <opkele/exception.h> | 4 | #include <opkele/exception.h> |
5 | #include <opkele/util.h> | 5 | #include <opkele/util.h> |
6 | 6 | ||
7 | namespace opkele { | 7 | namespace opkele { |
8 | using namespace std; | 8 | using namespace std; |
9 | 9 | ||
10 | template<class __a1,class __a2,class __r> | 10 | template<class __a1,class __a2,class __r> |
11 | struct bitwise_xor : public binary_function<__a1,__a2,__r> { | 11 | struct bitwise_xor : public binary_function<__a1,__a2,__r> { |
12 | __r operator() (const __a1& a1,const __a2& a2) const { | 12 | __r operator() (const __a1& a1,const __a2& a2) const { |
13 | return a1^a2; | 13 | return (__r)(a1^a2); |
14 | } | 14 | } |
15 | }; | 15 | }; |
16 | 16 | ||
17 | void secret_t::enxor_to_base64(const unsigned char *key_d,string& rv) const { | 17 | void secret_t::enxor_to_base64(const unsigned char *key_d,string& rv) const { |
18 | vector<unsigned char> tmp; | 18 | vector<unsigned char> tmp; |
19 | transform( | 19 | transform( |
20 | begin(), end(), | 20 | begin(), end(), |
21 | key_d, | 21 | key_d, |
22 | back_insert_iterator<vector<unsigned char> >(tmp), | 22 | back_insert_iterator<vector<unsigned char> >(tmp), |
23 | bitwise_xor<unsigned char,unsigned char,unsigned char>() ); | 23 | bitwise_xor<unsigned char,unsigned char,unsigned char>() ); |
24 | rv = util::encode_base64(&(tmp.front()),tmp.size()); | 24 | rv = util::encode_base64(&(tmp.front()),tmp.size()); |
25 | } | 25 | } |
26 | 26 | ||
27 | void secret_t::enxor_from_base64(const unsigned char *key_d,const string& b64) { | 27 | void secret_t::enxor_from_base64(const unsigned char *key_d,const string& b64) { |
28 | clear(); | 28 | clear(); |
29 | util::decode_base64(b64,*this); | 29 | util::decode_base64(b64,*this); |
30 | transform( | 30 | transform( |
31 | begin(), end(), | 31 | begin(), end(), |
32 | key_d, | 32 | key_d, |
33 | begin(), | 33 | begin(), |
34 | bitwise_xor<unsigned char,unsigned char,unsigned char>() ); | 34 | bitwise_xor<unsigned char,unsigned char,unsigned char>() ); |
35 | } | 35 | } |
36 | 36 | ||
37 | void secret_t::to_base64(string& rv) const { | 37 | void secret_t::to_base64(string& rv) const { |
38 | rv = util::encode_base64(&(front()),size()); | 38 | rv = util::encode_base64(&(front()),size()); |
39 | } | 39 | } |
40 | 40 | ||
41 | void secret_t::from_base64(const string& b64) { | 41 | void secret_t::from_base64(const string& b64) { |
42 | util::decode_base64(b64,*this); | 42 | util::decode_base64(b64,*this); |
43 | } | 43 | } |
44 | 44 | ||
45 | } | 45 | } |
diff --git a/lib/util.cc b/lib/util.cc index b702291..d979502 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,525 +1,525 @@ | |||
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 <opkele/util.h> | 13 | #include <opkele/util.h> |
14 | #include <opkele/exception.h> | 14 | #include <opkele/exception.h> |
15 | #include <opkele/data.h> | 15 | #include <opkele/data.h> |
16 | #include <opkele/debug.h> | 16 | #include <opkele/debug.h> |
17 | 17 | ||
18 | #include <config.h> | 18 | #include <config.h> |
19 | #ifdef HAVE_DEMANGLE | 19 | #ifdef HAVE_DEMANGLE |
20 | # include <cxxabi.h> | 20 | # include <cxxabi.h> |
21 | #endif | 21 | #endif |
22 | 22 | ||
23 | namespace opkele { | 23 | namespace opkele { |
24 | using namespace std; | 24 | using namespace std; |
25 | 25 | ||
26 | namespace util { | 26 | namespace util { |
27 | 27 | ||
28 | /* | 28 | /* |
29 | * base64 | 29 | * base64 |
30 | */ | 30 | */ |
31 | string encode_base64(const void *data,size_t length) { | 31 | string encode_base64(const void *data,size_t length) { |
32 | BIO *b64 = 0, *bmem = 0; | 32 | BIO *b64 = 0, *bmem = 0; |
33 | try { | 33 | try { |
34 | b64 = BIO_new(BIO_f_base64()); | 34 | b64 = BIO_new(BIO_f_base64()); |
35 | if(!b64) | 35 | if(!b64) |
36 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 36 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
37 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 37 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
38 | bmem = BIO_new(BIO_s_mem()); | 38 | bmem = BIO_new(BIO_s_mem()); |
39 | BIO_set_flags(b64,BIO_CLOSE); | 39 | BIO_set_flags(b64,BIO_CLOSE); |
40 | if(!bmem) | 40 | if(!bmem) |
41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
42 | BIO_push(b64,bmem); | 42 | BIO_push(b64,bmem); |
43 | if(((size_t)BIO_write(b64,data,length))!=length) | 43 | if(((size_t)BIO_write(b64,data,length))!=length) |
44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
45 | if(BIO_flush(b64)!=1) | 45 | if(BIO_flush(b64)!=1) |
46 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | 46 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); |
47 | char *rvd; | 47 | char *rvd; |
48 | long rvl = BIO_get_mem_data(bmem,&rvd); | 48 | long rvl = BIO_get_mem_data(bmem,&rvd); |
49 | string rv(rvd,rvl); | 49 | string rv(rvd,rvl); |
50 | BIO_free_all(b64); | 50 | BIO_free_all(b64); |
51 | return rv; | 51 | return rv; |
52 | }catch(...) { | 52 | }catch(...) { |
53 | if(b64) BIO_free_all(b64); | 53 | if(b64) BIO_free_all(b64); |
54 | throw; | 54 | throw; |
55 | } | 55 | } |
56 | } | 56 | } |
57 | 57 | ||
58 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 58 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
59 | BIO *b64 = 0, *bmem = 0; | 59 | BIO *b64 = 0, *bmem = 0; |
60 | rv.clear(); | 60 | rv.clear(); |
61 | try { | 61 | try { |
62 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | 62 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); |
63 | if(!bmem) | 63 | if(!bmem) |
64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
65 | b64 = BIO_new(BIO_f_base64()); | 65 | b64 = BIO_new(BIO_f_base64()); |
66 | if(!b64) | 66 | if(!b64) |
67 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 67 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
68 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 68 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
69 | BIO_push(b64,bmem); | 69 | BIO_push(b64,bmem); |
70 | unsigned char tmp[512]; | 70 | unsigned char tmp[512]; |
71 | size_t rb = 0; | 71 | size_t rb = 0; |
72 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 72 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
73 | rv.insert(rv.end(),tmp,&tmp[rb]); | 73 | rv.insert(rv.end(),tmp,&tmp[rb]); |
74 | BIO_free_all(b64); | 74 | BIO_free_all(b64); |
75 | }catch(...) { | 75 | }catch(...) { |
76 | if(b64) BIO_free_all(b64); | 76 | if(b64) BIO_free_all(b64); |
77 | throw; | 77 | throw; |
78 | } | 78 | } |
79 | } | 79 | } |
80 | 80 | ||
81 | /* | 81 | /* |
82 | * big numerics | 82 | * big numerics |
83 | */ | 83 | */ |
84 | 84 | ||
85 | BIGNUM *base64_to_bignum(const string& b64) { | 85 | BIGNUM *base64_to_bignum(const string& b64) { |
86 | vector<unsigned char> bin; | 86 | vector<unsigned char> bin; |
87 | decode_base64(b64,bin); | 87 | decode_base64(b64,bin); |
88 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 88 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
89 | if(!rv) | 89 | if(!rv) |
90 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 90 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
91 | return rv; | 91 | return rv; |
92 | } | 92 | } |
93 | 93 | ||
94 | BIGNUM *dec_to_bignum(const string& dec) { | 94 | BIGNUM *dec_to_bignum(const string& dec) { |
95 | BIGNUM *rv = 0; | 95 | BIGNUM *rv = 0; |
96 | if(!BN_dec2bn(&rv,dec.c_str())) | 96 | if(!BN_dec2bn(&rv,dec.c_str())) |
97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
98 | return rv; | 98 | return rv; |
99 | } | 99 | } |
100 | 100 | ||
101 | string bignum_to_base64(const BIGNUM *bn) { | 101 | string bignum_to_base64(const BIGNUM *bn) { |
102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
103 | unsigned char *binptr = &(bin.front())+1; | 103 | unsigned char *binptr = &(bin.front())+1; |
104 | int l = BN_bn2bin(bn,binptr); | 104 | int l = BN_bn2bin(bn,binptr); |
105 | if(l && (*binptr)&0x80){ | 105 | if(l && (*binptr)&0x80){ |
106 | (*(--binptr)) = 0; ++l; | 106 | (*(--binptr)) = 0; ++l; |
107 | } | 107 | } |
108 | return encode_base64(binptr,l); | 108 | return encode_base64(binptr,l); |
109 | } | 109 | } |
110 | 110 | ||
111 | /* | 111 | /* |
112 | * w3c times | 112 | * w3c times |
113 | */ | 113 | */ |
114 | 114 | ||
115 | string time_to_w3c(time_t t) { | 115 | string time_to_w3c(time_t t) { |
116 | struct tm tm_t; | 116 | struct tm tm_t; |
117 | if(!gmtime_r(&t,&tm_t)) | 117 | if(!gmtime_r(&t,&tm_t)) |
118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
119 | char rv[25]; | 119 | char rv[25]; |
120 | 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)) |
121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
122 | return rv; | 122 | return rv; |
123 | } | 123 | } |
124 | 124 | ||
125 | time_t w3c_to_time(const string& w) { | 125 | time_t w3c_to_time(const string& w) { |
126 | int fraction; | 126 | int fraction; |
127 | struct tm tm_t; | 127 | struct tm tm_t; |
128 | memset(&tm_t,0,sizeof(tm_t)); | 128 | memset(&tm_t,0,sizeof(tm_t)); |
129 | if( ( | 129 | if( ( |
130 | sscanf( | 130 | sscanf( |
131 | w.c_str(), | 131 | w.c_str(), |
132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
133 | &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, |
134 | &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 |
135 | ) != 6 | 135 | ) != 6 |
136 | ) && ( | 136 | ) && ( |
137 | sscanf( | 137 | sscanf( |
138 | w.c_str(), | 138 | w.c_str(), |
139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
140 | &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, |
141 | &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, |
142 | &fraction | 142 | &fraction |
143 | ) != 7 | 143 | ) != 7 |
144 | ) ) | 144 | ) ) |
145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
146 | tm_t.tm_mon--; | 146 | tm_t.tm_mon--; |
147 | tm_t.tm_year-=1900; | 147 | tm_t.tm_year-=1900; |
148 | time_t rv = mktime(&tm_t); | 148 | time_t rv = mktime(&tm_t); |
149 | if(rv==(time_t)-1) | 149 | if(rv==(time_t)-1) |
150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
151 | return rv-timezone; | 151 | return rv-timezone; |
152 | } | 152 | } |
153 | 153 | ||
154 | /* | 154 | /* |
155 | * | 155 | * |
156 | */ | 156 | */ |
157 | 157 | ||
158 | static inline bool isrfc3986unreserved(int c) { | 158 | static inline bool isrfc3986unreserved(int c) { |
159 | if(c<'-') return false; | 159 | if(c<'-') return false; |
160 | if(c<='.') return true; | 160 | if(c<='.') return true; |
161 | if(c<'0') return false; if(c<='9') return true; | 161 | if(c<'0') return false; if(c<='9') return true; |
162 | if(c<'A') return false; if(c<='Z') return true; | 162 | if(c<'A') return false; if(c<='Z') return true; |
163 | if(c<'_') return false; | 163 | if(c<'_') return false; |
164 | if(c=='_') return true; | 164 | if(c=='_') return true; |
165 | if(c<'a') return false; if(c<='z') return true; | 165 | if(c<'a') return false; if(c<='z') return true; |
166 | if(c=='~') return true; | 166 | if(c=='~') return true; |
167 | return false; | 167 | return false; |
168 | } | 168 | } |
169 | 169 | ||
170 | struct __url_encoder : public unary_function<char,void> { | 170 | struct __url_encoder : public unary_function<char,void> { |
171 | public: | 171 | public: |
172 | string& rv; | 172 | string& rv; |
173 | 173 | ||
174 | __url_encoder(string& r) : rv(r) { } | 174 | __url_encoder(string& r) : rv(r) { } |
175 | 175 | ||
176 | result_type operator()(argument_type c) { | 176 | result_type operator()(argument_type c) { |
177 | if(isrfc3986unreserved(c)) | 177 | if(isrfc3986unreserved(c)) |
178 | rv += c; | 178 | rv += c; |
179 | else{ | 179 | else{ |
180 | char tmp[4]; | 180 | char tmp[4]; |
181 | snprintf(tmp,sizeof(tmp),"%%%02X", | 181 | snprintf(tmp,sizeof(tmp),"%%%02X", |
182 | (c&0xff)); | 182 | (c&0xff)); |
183 | rv += tmp; | 183 | rv += tmp; |
184 | } | 184 | } |
185 | } | 185 | } |
186 | }; | 186 | }; |
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) { | 195 | string url_decode(const string& str) { |
196 | string rv; | 196 | string rv; |
197 | back_insert_iterator<string> ii(rv); | 197 | back_insert_iterator<string> ii(rv); |
198 | for(string::const_iterator i=str.begin(),ie=str.end(); | 198 | for(string::const_iterator i=str.begin(),ie=str.end(); |
199 | i!=ie;++i) { | 199 | i!=ie;++i) { |
200 | switch(*i) { | 200 | switch(*i) { |
201 | case '+': | 201 | case '+': |
202 | *(ii++) = ' '; break; | 202 | *(ii++) = ' '; break; |
203 | case '%': | 203 | case '%': |
204 | ++i; | 204 | ++i; |
205 | static char tmp[3] = {0,0,0}; | 205 | static char tmp[3] = {0,0,0}; |
206 | if(i==ie) | 206 | if(i==ie) |
207 | throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); | 207 | throw failed_conversion(OPKELE_CP_ "trailing percent in the url-encoded string"); |
208 | tmp[0] = *(i++); | 208 | tmp[0] = *(i++); |
209 | if(i==ie) | 209 | if(i==ie) |
210 | throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); | 210 | throw failed_conversion(OPKELE_CP_ "not enough hexadecimals after the percent sign in url-encoded string"); |
211 | tmp[1] = *i; | 211 | tmp[1] = *i; |
212 | if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) | 212 | if(!(isxdigit(tmp[0]) && isxdigit(tmp[1]))) |
213 | throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); | 213 | throw failed_conversion(OPKELE_CP_ "non-hex follows percent in url-encoded string"); |
214 | *(ii++) = strtol(tmp,0,16); | 214 | *(ii++) = (char)strtol(tmp,0,16); |
215 | break; | 215 | break; |
216 | default: | 216 | default: |
217 | *(ii++) = *i; break; | 217 | *(ii++) = *i; break; |
218 | } | 218 | } |
219 | } | 219 | } |
220 | return rv; | 220 | return rv; |
221 | } | 221 | } |
222 | 222 | ||
223 | string attr_escape(const string& str) { | 223 | string attr_escape(const string& str) { |
224 | static const char *unsafechars = "<>&\n\"'"; | 224 | static const char *unsafechars = "<>&\n\"'"; |
225 | string rv; | 225 | string rv; |
226 | string::size_type p=0; | 226 | string::size_type p=0; |
227 | while(true) { | 227 | while(true) { |
228 | string::size_type us = str.find_first_of(unsafechars,p); | 228 | string::size_type us = str.find_first_of(unsafechars,p); |
229 | if(us==string::npos) { | 229 | if(us==string::npos) { |
230 | if(p!=str.length()) | 230 | if(p!=str.length()) |
231 | rv.append(str,p,str.length()-p); | 231 | rv.append(str,p,str.length()-p); |
232 | return rv; | 232 | return rv; |
233 | } | 233 | } |
234 | rv.append(str,p,us-p); | 234 | rv.append(str,p,us-p); |
235 | rv += "&#"; | 235 | rv += "&#"; |
236 | rv += long_to_string((long)str[us]); | 236 | rv += long_to_string((long)str[us]); |
237 | rv += ';'; | 237 | rv += ';'; |
238 | p = us+1; | 238 | p = us+1; |
239 | } | 239 | } |
240 | } | 240 | } |
241 | 241 | ||
242 | string long_to_string(long l) { | 242 | string long_to_string(long l) { |
243 | char rv[32]; | 243 | char rv[32]; |
244 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 244 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
245 | if(r<0 || r>=(int)sizeof(rv)) | 245 | if(r<0 || r>=(int)sizeof(rv)) |
246 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 246 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
247 | return rv; | 247 | return rv; |
248 | } | 248 | } |
249 | 249 | ||
250 | long string_to_long(const string& s) { | 250 | long string_to_long(const string& s) { |
251 | char *endptr = 0; | 251 | char *endptr = 0; |
252 | long rv = strtol(s.c_str(),&endptr,10); | 252 | long rv = strtol(s.c_str(),&endptr,10); |
253 | if((!endptr) || endptr==s.c_str()) | 253 | if((!endptr) || endptr==s.c_str()) |
254 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 254 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
255 | return rv; | 255 | return rv; |
256 | } | 256 | } |
257 | 257 | ||
258 | /* | 258 | /* |
259 | * Normalize URL according to the rules, described in rfc 3986, section 6 | 259 | * Normalize URL according to the rules, described in rfc 3986, section 6 |
260 | * | 260 | * |
261 | * - uppercase hex triplets (e.g. %ab -> %AB) | 261 | * - uppercase hex triplets (e.g. %ab -> %AB) |
262 | * - lowercase scheme and host | 262 | * - lowercase scheme and host |
263 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, | 263 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, |
264 | * that is - [:alpha:][:digit:]._~- | 264 | * that is - [:alpha:][:digit:]._~- |
265 | * - remove dot segments | 265 | * - remove dot segments |
266 | * - remove empty and default ports | 266 | * - remove empty and default ports |
267 | * - if there's no path component, add '/' | 267 | * - if there's no path component, add '/' |
268 | */ | 268 | */ |
269 | string rfc_3986_normalize_uri(const string& uri) { | 269 | string rfc_3986_normalize_uri(const string& uri) { |
270 | string rv; | 270 | string rv; |
271 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); | 271 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); |
272 | if(ns==string::npos) | 272 | if(ns==string::npos) |
273 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); | 273 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); |
274 | string::size_type colon = uri.find(':',ns); | 274 | string::size_type colon = uri.find(':',ns); |
275 | if(colon==string::npos) | 275 | if(colon==string::npos) |
276 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); | 276 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); |
277 | transform( | 277 | transform( |
278 | uri.begin()+ns, uri.begin()+colon+1, | 278 | uri.begin()+ns, uri.begin()+colon+1, |
279 | back_inserter(rv), ::tolower ); | 279 | back_inserter(rv), ::tolower ); |
280 | bool s; | 280 | bool s; |
281 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; | 281 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; |
282 | if(ul <= (colon+3)) | 282 | if(ul <= (colon+3)) |
283 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); | 283 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); |
284 | if(uri[colon+1]!='/' || uri[colon+2]!='/') | 284 | if(uri[colon+1]!='/' || uri[colon+2]!='/') |
285 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); | 285 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); |
286 | if(rv=="http:") | 286 | if(rv=="http:") |
287 | s = false; | 287 | s = false; |
288 | else if(rv=="https:") | 288 | else if(rv=="https:") |
289 | s = true; | 289 | s = true; |
290 | else{ | 290 | else{ |
291 | /* TODO: support more schemes. e.g. xri. How do we normalize | 291 | /* TODO: support more schemes. e.g. xri. How do we normalize |
292 | * xri? | 292 | * xri? |
293 | */ | 293 | */ |
294 | rv.append(uri,colon+1,ul-colon-1); | 294 | rv.append(uri,colon+1,ul-colon-1); |
295 | return rv; | 295 | return rv; |
296 | } | 296 | } |
297 | rv += "//"; | 297 | rv += "//"; |
298 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); | 298 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); |
299 | if(interesting==string::npos) { | 299 | if(interesting==string::npos) { |
300 | transform( | 300 | transform( |
301 | uri.begin()+colon+3,uri.begin()+ul, | 301 | uri.begin()+colon+3,uri.begin()+ul, |
302 | back_inserter(rv), ::tolower ); | 302 | back_inserter(rv), ::tolower ); |
303 | rv += '/'; return rv; | 303 | rv += '/'; return rv; |
304 | } | 304 | } |
305 | transform( | 305 | transform( |
306 | uri.begin()+colon+3,uri.begin()+interesting, | 306 | uri.begin()+colon+3,uri.begin()+interesting, |
307 | back_inserter(rv), ::tolower ); | 307 | back_inserter(rv), ::tolower ); |
308 | bool qf = false; | 308 | bool qf = false; |
309 | char ic = uri[interesting]; | 309 | char ic = uri[interesting]; |
310 | if(ic==':') { | 310 | if(ic==':') { |
311 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 311 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
312 | const char *nptr = uri.data()+interesting+1; | 312 | const char *nptr = uri.data()+interesting+1; |
313 | char *eptr = 0; | 313 | char *eptr = 0; |
314 | long port = strtol(nptr,&eptr,10); | 314 | long port = strtol(nptr,&eptr,10); |
315 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 315 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
316 | char tmp[8]; | 316 | char tmp[8]; |
317 | snprintf(tmp,sizeof(tmp),":%ld",port); | 317 | snprintf(tmp,sizeof(tmp),":%ld",port); |
318 | rv += tmp; | 318 | rv += tmp; |
319 | } | 319 | } |
320 | if(ni==string::npos) { | 320 | if(ni==string::npos) { |
321 | rv += '/'; return rv; | 321 | rv += '/'; return rv; |
322 | } | 322 | } |
323 | interesting = ni; | 323 | interesting = ni; |
324 | }else if(ic!='/') { | 324 | }else if(ic!='/') { |
325 | rv += '/'; rv += ic; | 325 | rv += '/'; rv += ic; |
326 | qf = true; | 326 | qf = true; |
327 | ++interesting; | 327 | ++interesting; |
328 | } | 328 | } |
329 | string::size_type n = interesting; | 329 | string::size_type n = interesting; |
330 | char tmp[3] = { 0,0,0 }; | 330 | char tmp[3] = { 0,0,0 }; |
331 | stack<string::size_type> psegs; psegs.push(rv.length()); | 331 | stack<string::size_type> psegs; psegs.push(rv.length()); |
332 | string pseg; | 332 | string pseg; |
333 | for(;n<ul;) { | 333 | for(;n<ul;) { |
334 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 334 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
335 | if(unsafe==string::npos) { | 335 | if(unsafe==string::npos) { |
336 | pseg.append(uri,n,ul-n-1); n = ul-1; | 336 | pseg.append(uri,n,ul-n-1); n = ul-1; |
337 | }else{ | 337 | }else{ |
338 | pseg.append(uri,n,unsafe-n); | 338 | pseg.append(uri,n,unsafe-n); |
339 | n = unsafe; | 339 | n = unsafe; |
340 | } | 340 | } |
341 | char c = uri[n++]; | 341 | char c = uri[n++]; |
342 | if(c=='%') { | 342 | if(c=='%') { |
343 | if((n+1)>=ul) | 343 | if((n+1)>=ul) |
344 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); | 344 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); |
345 | tmp[0] = uri[n++]; | 345 | tmp[0] = uri[n++]; |
346 | tmp[1] = uri[n++]; | 346 | tmp[1] = uri[n++]; |
347 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) | 347 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) |
348 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 348 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
349 | int cc = strtol(tmp,0,16); | 349 | int cc = strtol(tmp,0,16); |
350 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 350 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
351 | pseg += cc; | 351 | pseg += (char)cc; |
352 | else{ | 352 | else{ |
353 | pseg += '%'; | 353 | pseg += '%'; |
354 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); | 354 | pseg += (char)toupper(tmp[0]); pseg += (char)toupper(tmp[1]); |
355 | } | 355 | } |
356 | }else if(qf) { | 356 | }else if(qf) { |
357 | rv += pseg; rv += c; | 357 | rv += pseg; rv += c; |
358 | pseg.clear(); | 358 | pseg.clear(); |
359 | }else if(n>=ul || strchr("?/#",c)) { | 359 | }else if(n>=ul || strchr("?/#",c)) { |
360 | if(pseg.empty() || pseg==".") { | 360 | if(pseg.empty() || pseg==".") { |
361 | }else if(pseg=="..") { | 361 | }else if(pseg=="..") { |
362 | if(psegs.size()>1) { | 362 | if(psegs.size()>1) { |
363 | rv.resize(psegs.top()); psegs.pop(); | 363 | rv.resize(psegs.top()); psegs.pop(); |
364 | } | 364 | } |
365 | }else{ | 365 | }else{ |
366 | psegs.push(rv.length()); | 366 | psegs.push(rv.length()); |
367 | if(c!='/') { | 367 | if(c!='/') { |
368 | pseg += c; | 368 | pseg += c; |
369 | qf = true; | 369 | qf = true; |
370 | } | 370 | } |
371 | rv += '/'; rv += pseg; | 371 | rv += '/'; rv += pseg; |
372 | } | 372 | } |
373 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 373 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
374 | rv += '/'; | 374 | rv += '/'; |
375 | if(n<ul) | 375 | if(n<ul) |
376 | qf = true; | 376 | qf = true; |
377 | }else if(strchr("?#",c)) { | 377 | }else if(strchr("?#",c)) { |
378 | if(psegs.size()==1 && psegs.top()==rv.length()) | 378 | if(psegs.size()==1 && psegs.top()==rv.length()) |
379 | rv += '/'; | 379 | rv += '/'; |
380 | if(pseg.empty()) | 380 | if(pseg.empty()) |
381 | rv += c; | 381 | rv += c; |
382 | qf = true; | 382 | qf = true; |
383 | } | 383 | } |
384 | pseg.clear(); | 384 | pseg.clear(); |
385 | }else{ | 385 | }else{ |
386 | pseg += c; | 386 | pseg += c; |
387 | } | 387 | } |
388 | } | 388 | } |
389 | if(!pseg.empty()) { | 389 | if(!pseg.empty()) { |
390 | if(!qf) rv += '/'; | 390 | if(!qf) rv += '/'; |
391 | rv += pseg; | 391 | rv += pseg; |
392 | } | 392 | } |
393 | return rv; | 393 | return rv; |
394 | } | 394 | } |
395 | 395 | ||
396 | string& strip_uri_fragment_part(string& u) { | 396 | string& strip_uri_fragment_part(string& u) { |
397 | string::size_type q = u.find('?'), f = u.find('#'); | 397 | string::size_type q = u.find('?'), f = u.find('#'); |
398 | if(q==string::npos) { | 398 | if(q==string::npos) { |
399 | if(f!=string::npos) | 399 | if(f!=string::npos) |
400 | u.erase(f); | 400 | u.erase(f); |
401 | }else{ | 401 | }else{ |
402 | if(f!=string::npos) { | 402 | if(f!=string::npos) { |
403 | if(f<q) | 403 | if(f<q) |
404 | u.erase(f,q-f); | 404 | u.erase(f,q-f); |
405 | else | 405 | else |
406 | u.erase(f); | 406 | u.erase(f); |
407 | } | 407 | } |
408 | } | 408 | } |
409 | return u; | 409 | return u; |
410 | } | 410 | } |
411 | 411 | ||
412 | bool uri_matches_realm(const string& uri,const string& realm) { | 412 | bool uri_matches_realm(const string& uri,const string& realm) { |
413 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | 413 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); |
414 | string nu = opkele::util::rfc_3986_normalize_uri(uri); | 414 | string nu = opkele::util::rfc_3986_normalize_uri(uri); |
415 | string::size_type pr = nrealm.find("://"); | 415 | string::size_type pr = nrealm.find("://"); |
416 | string::size_type pu = nu.find("://"); | 416 | string::size_type pu = nu.find("://"); |
417 | assert(!(pr==string::npos || pu==string::npos)); | 417 | assert(!(pr==string::npos || pu==string::npos)); |
418 | pr += sizeof("://")-1; | 418 | pr += sizeof("://")-1; |
419 | pu += sizeof("://")-1; | 419 | pu += sizeof("://")-1; |
420 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | 420 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { |
421 | pr = nrealm.find('.',pr); | 421 | pr = nrealm.find('.',pr); |
422 | pu = nu.find('.',pu); | 422 | pu = nu.find('.',pu); |
423 | assert(pr!=string::npos); | 423 | assert(pr!=string::npos); |
424 | if(pu==string::npos) | 424 | if(pu==string::npos) |
425 | return false; | 425 | return false; |
426 | // TODO: check for overgeneralized realm | 426 | // TODO: check for overgeneralized realm |
427 | } | 427 | } |
428 | string::size_type lr = nrealm.length(); | 428 | string::size_type lr = nrealm.length(); |
429 | string::size_type lu = nu.length(); | 429 | string::size_type lu = nu.length(); |
430 | if( (lu-pu) < (lr-pr) ) | 430 | if( (lu-pu) < (lr-pr) ) |
431 | return false; | 431 | return false; |
432 | pair<const char*,const char*> mp = mismatch( | 432 | pair<const char*,const char*> mp = mismatch( |
433 | nrealm.c_str()+pr,nrealm.c_str()+lr, | 433 | nrealm.c_str()+pr,nrealm.c_str()+lr, |
434 | nu.c_str()+pu); | 434 | nu.c_str()+pu); |
435 | if( (*(mp.first-1))!='/' | 435 | if( (*(mp.first-1))!='/' |
436 | && !strchr("/?#",*mp.second) ) | 436 | && !strchr("/?#",*mp.second) ) |
437 | return false; | 437 | return false; |
438 | return true; | 438 | return true; |
439 | } | 439 | } |
440 | 440 | ||
441 | string abi_demangle(const char *mn) { | 441 | string abi_demangle(const char *mn) { |
442 | #ifndef HAVE_DEMANGLE | 442 | #ifndef HAVE_DEMANGLE |
443 | return mn; | 443 | return mn; |
444 | #else /* !HAVE_DEMANGLE */ | 444 | #else /* !HAVE_DEMANGLE */ |
445 | int dstat; | 445 | int dstat; |
446 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); | 446 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); |
447 | if(dstat) | 447 | if(dstat) |
448 | return mn; | 448 | return mn; |
449 | string rv = demangled; | 449 | string rv = demangled; |
450 | free(demangled); | 450 | free(demangled); |
451 | return rv; | 451 | return rv; |
452 | #endif /* !HAVE_DEMANGLE */ | 452 | #endif /* !HAVE_DEMANGLE */ |
453 | } | 453 | } |
454 | 454 | ||
455 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { | 455 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { |
456 | const string& slist = om.get_field("signed"); | 456 | const string& slist = om.get_field("signed"); |
457 | string kv; | 457 | string kv; |
458 | string::size_type p=0; | 458 | string::size_type p=0; |
459 | while(true) { | 459 | while(true) { |
460 | string::size_type co = slist.find(',',p); | 460 | string::size_type co = slist.find(',',p); |
461 | string f = (co==string::npos) | 461 | string f = (co==string::npos) |
462 | ?slist.substr(p):slist.substr(p,co-p); | 462 | ?slist.substr(p):slist.substr(p,co-p); |
463 | kv += f; | 463 | kv += f; |
464 | kv += ':'; | 464 | kv += ':'; |
465 | kv += om.get_field(f); | 465 | kv += om.get_field(f); |
466 | kv += '\n'; | 466 | kv += '\n'; |
467 | if(co==string::npos) break; | 467 | if(co==string::npos) break; |
468 | p = co+1; | 468 | p = co+1; |
469 | } | 469 | } |
470 | const secret_t& secret = assoc->secret(); | 470 | const secret_t& secret = assoc->secret(); |
471 | const EVP_MD *evpmd; | 471 | const EVP_MD *evpmd; |
472 | const string& at = assoc->assoc_type(); | 472 | const string& at = assoc->assoc_type(); |
473 | if(at=="HMAC-SHA256") | 473 | if(at=="HMAC-SHA256") |
474 | evpmd = EVP_sha256(); | 474 | evpmd = EVP_sha256(); |
475 | else if(at=="HMAC-SHA1") | 475 | else if(at=="HMAC-SHA1") |
476 | evpmd = EVP_sha1(); | 476 | evpmd = EVP_sha1(); |
477 | else | 477 | else |
478 | throw unsupported(OPKELE_CP_ "unknown association type"); | 478 | throw unsupported(OPKELE_CP_ "unknown association type"); |
479 | unsigned int md_len = 0; | 479 | unsigned int md_len = 0; |
480 | unsigned char md[SHA256_DIGEST_LENGTH]; | 480 | unsigned char md[SHA256_DIGEST_LENGTH]; |
481 | HMAC(evpmd, | 481 | HMAC(evpmd, |
482 | &(secret.front()),secret.size(), | 482 | &(secret.front()),secret.size(), |
483 | (const unsigned char*)kv.data(),kv.length(), | 483 | (const unsigned char*)kv.data(),kv.length(), |
484 | md,&md_len); | 484 | md,&md_len); |
485 | return encode_base64(md,md_len); | 485 | return encode_base64(md,md_len); |
486 | } | 486 | } |
487 | 487 | ||
488 | string normalize_identifier(const string& usi,bool strip_fragment) { | 488 | string normalize_identifier(const string& usi,bool strip_fragment) { |
489 | if(usi.empty()) | 489 | if(usi.empty()) |
490 | return usi; | 490 | return usi; |
491 | string rv; | 491 | string rv; |
492 | string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars); | 492 | string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars); |
493 | if(fsc==string::npos) | 493 | if(fsc==string::npos) |
494 | return rv; | 494 | return rv; |
495 | string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars); | 495 | string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars); |
496 | assert(lsc!=string::npos); | 496 | assert(lsc!=string::npos); |
497 | if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1)) | 497 | if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1)) |
498 | fsc += sizeof("xri://")-1; | 498 | fsc += sizeof("xri://")-1; |
499 | if( (fsc+1) >= lsc ) | 499 | if( (fsc+1) >= lsc ) |
500 | return rv; | 500 | return rv; |
501 | rv.assign(usi,fsc,lsc-fsc+1); | 501 | rv.assign(usi,fsc,lsc-fsc+1); |
502 | if(strchr(data::_iname_leaders,rv[0])) { | 502 | if(strchr(data::_iname_leaders,rv[0])) { |
503 | /* TODO: further normalize xri identity, fold case or | 503 | /* TODO: further normalize xri identity, fold case or |
504 | * whatever... */ | 504 | * whatever... */ |
505 | }else{ | 505 | }else{ |
506 | if(rv.find("://")==string::npos) | 506 | if(rv.find("://")==string::npos) |
507 | rv.insert(0,"http://"); | 507 | rv.insert(0,"http://"); |
508 | if(strip_fragment) { | 508 | if(strip_fragment) { |
509 | string::size_type fp = rv.find('#'); | 509 | string::size_type fp = rv.find('#'); |
510 | if(fp!=string::npos) { | 510 | if(fp!=string::npos) { |
511 | string::size_type qp = rv.find('?'); | 511 | string::size_type qp = rv.find('?'); |
512 | if(qp==string::npos || qp<fp) | 512 | if(qp==string::npos || qp<fp) |
513 | rv.erase(fp); | 513 | rv.erase(fp); |
514 | else if(qp>fp) | 514 | else if(qp>fp) |
515 | rv.erase(fp,qp-fp); | 515 | rv.erase(fp,qp-fp); |
516 | } | 516 | } |
517 | } | 517 | } |
518 | rv = rfc_3986_normalize_uri(rv); | 518 | rv = rfc_3986_normalize_uri(rv); |
519 | } | 519 | } |
520 | return rv; | 520 | return rv; |
521 | } | 521 | } |
522 | 522 | ||
523 | } | 523 | } |
524 | 524 | ||
525 | } | 525 | } |