-rw-r--r-- | lib/discovery.cc | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/discovery.cc b/lib/discovery.cc index ea53786..3b184ee 100644 --- a/lib/discovery.cc +++ b/lib/discovery.cc | |||
@@ -1,26 +1,24 @@ | |||
1 | #include <iostream> | ||
2 | using namespace std; | ||
3 | #include <list> | 1 | #include <list> |
4 | #include <opkele/curl.h> | 2 | #include <opkele/curl.h> |
5 | #include <opkele/expat.h> | 3 | #include <opkele/expat.h> |
6 | #include <opkele/uris.h> | 4 | #include <opkele/uris.h> |
7 | #include <opkele/discovery.h> | 5 | #include <opkele/discovery.h> |
8 | #include <opkele/exception.h> | 6 | #include <opkele/exception.h> |
9 | #include <opkele/util.h> | 7 | #include <opkele/util.h> |
10 | 8 | ||
11 | #include "config.h" | 9 | #include "config.h" |
12 | 10 | ||
13 | #define XRDS_HEADER "X-XRDS-Location" | 11 | #define XRDS_HEADER "X-XRDS-Location" |
14 | #define CT_HEADER "Content-Type" | 12 | #define CT_HEADER "Content-Type" |
15 | 13 | ||
16 | namespace opkele { | 14 | namespace opkele { |
17 | using std::list; | 15 | using std::list; |
18 | using xrd::XRD_t; | 16 | using xrd::XRD_t; |
19 | using xrd::service_t; | 17 | using xrd::service_t; |
20 | 18 | ||
21 | static const char *whitespace = " \t\r\n"; | 19 | static const char *whitespace = " \t\r\n"; |
22 | static const char *i_leaders = "=@+$!("; | 20 | static const char *i_leaders = "=@+$!("; |
23 | 21 | ||
24 | static inline bool is_qelement(const XML_Char *n,const char *qen) { | 22 | static inline bool is_qelement(const XML_Char *n,const char *qen) { |
25 | return !strcasecmp(n,qen); | 23 | return !strcasecmp(n,qen); |
26 | } | 24 | } |
@@ -210,70 +208,73 @@ namespace opkele { | |||
210 | } | 208 | } |
211 | 209 | ||
212 | void start_element(const XML_Char *n,const XML_Char **a) { | 210 | void start_element(const XML_Char *n,const XML_Char **a) { |
213 | if(skipping<0) return; | 211 | if(skipping<0) return; |
214 | if(skipping) { | 212 | if(skipping) { |
215 | if(xmode&xmode_html) | 213 | if(xmode&xmode_html) |
216 | html_start_element(n,a); | 214 | html_start_element(n,a); |
217 | ++skipping; return; | 215 | ++skipping; return; |
218 | } | 216 | } |
219 | if(pt_stack.empty()) { | 217 | if(pt_stack.empty()) { |
220 | if(is_qelement(n,NSURI_XRDS "\tXRDS")) | 218 | if(is_qelement(n,NSURI_XRDS "\tXRDS")) |
221 | return; | 219 | return; |
222 | if(is_qelement(n,NSURI_XRD "\tXRD")) { | 220 | if(is_qelement(n,NSURI_XRD "\tXRD")) { |
223 | assert(xrd); | 221 | assert(xrd); |
224 | xrd->clear(); | 222 | xrd->clear(); |
225 | pt_stack.push_back(n); | 223 | pt_stack.push_back(n); |
226 | }else if(xmode&xmode_html) { | 224 | }else if(xmode&xmode_html) { |
227 | html_start_element(n,a); | 225 | html_start_element(n,a); |
228 | }else{ | 226 | }else{ |
229 | skipping = -1; | 227 | skipping = -1; |
230 | } | 228 | } |
231 | }else{ | 229 | }else{ |
232 | int pt_s = pt_stack.size(); | 230 | int pt_s = pt_stack.size(); |
233 | if(pt_s==1) { | 231 | if(pt_s==1) { |
234 | /* TODO: xrd:XRD/xrd:Expires */ | ||
235 | if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { | 232 | if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { |
236 | assert(xrd); | 233 | assert(xrd); |
237 | cdata = &(xrd->canonical_ids.add(element_priority(a),string())); | 234 | cdata = &(xrd->canonical_ids.add(element_priority(a),string())); |
238 | }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { | 235 | }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { |
239 | assert(xrd); | 236 | assert(xrd); |
240 | cdata = &(xrd->local_ids.add(element_priority(a),string())); | 237 | cdata = &(xrd->local_ids.add(element_priority(a),string())); |
241 | }else if(is_qelement(n,NSURI_XRD "\tService")) { | 238 | }else if(is_qelement(n,NSURI_XRD "\tService")) { |
242 | assert(xrd); | 239 | assert(xrd); |
243 | xrd_service = &(xrd->services.add(element_priority(a), | 240 | xrd_service = &(xrd->services.add(element_priority(a), |
244 | service_t())); | 241 | service_t())); |
245 | pt_stack.push_back(n); | 242 | pt_stack.push_back(n); |
246 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { | 243 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { |
247 | for(;*a;) { | 244 | for(;*a;) { |
248 | if(!strcasecmp(*(a++),"code")) { | 245 | if(!strcasecmp(*(a++),"code")) { |
249 | if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { | 246 | if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { |
250 | cdata = &status_string; | 247 | cdata = &status_string; |
251 | pt_stack.push_back(n); | 248 | pt_stack.push_back(n); |
252 | break; | 249 | break; |
253 | } | 250 | } |
254 | } | 251 | } |
255 | } | 252 | } |
253 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { | ||
254 | assert(xrd); | ||
255 | cdata_buf.clear(); | ||
256 | cdata = &cdata_buf; | ||
256 | }else if(xmode&xmode_html) { | 257 | }else if(xmode&xmode_html) { |
257 | html_start_element(n,a); | 258 | html_start_element(n,a); |
258 | }else{ | 259 | }else{ |
259 | skipping = 1; | 260 | skipping = 1; |
260 | } | 261 | } |
261 | }else if(pt_s==2) { | 262 | }else if(pt_s==2) { |
262 | if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { | 263 | if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { |
263 | if(is_qelement(n,NSURI_XRD "\tType")) { | 264 | if(is_qelement(n,NSURI_XRD "\tType")) { |
264 | assert(xrd); assert(xrd_service); | 265 | assert(xrd); assert(xrd_service); |
265 | cdata_buf.clear(); | 266 | cdata_buf.clear(); |
266 | cdata = &cdata_buf; | 267 | cdata = &cdata_buf; |
267 | }else if(is_qelement(n,NSURI_XRD "\tURI")) { | 268 | }else if(is_qelement(n,NSURI_XRD "\tURI")) { |
268 | assert(xrd); assert(xrd_service); | 269 | assert(xrd); assert(xrd_service); |
269 | cdata = &(xrd_service->uris.add(element_priority(a),string())); | 270 | cdata = &(xrd_service->uris.add(element_priority(a),string())); |
270 | }else if(is_qelement(n,NSURI_XRD "\tLocalID") | 271 | }else if(is_qelement(n,NSURI_XRD "\tLocalID") |
271 | || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { | 272 | || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { |
272 | assert(xrd); assert(xrd_service); | 273 | assert(xrd); assert(xrd_service); |
273 | cdata = &(xrd_service->local_ids.add(element_priority(a),string())); | 274 | cdata = &(xrd_service->local_ids.add(element_priority(a),string())); |
274 | }else{ | 275 | }else{ |
275 | skipping = 1; | 276 | skipping = 1; |
276 | } | 277 | } |
277 | }else | 278 | }else |
278 | skipping = 1; | 279 | skipping = 1; |
279 | }else if(xmode&xmode_html) { | 280 | }else if(xmode&xmode_html) { |
@@ -284,48 +285,51 @@ namespace opkele { | |||
284 | } | 285 | } |
285 | } | 286 | } |
286 | void end_element(const XML_Char *n) { | 287 | void end_element(const XML_Char *n) { |
287 | if(skipping<0) return; | 288 | if(skipping<0) return; |
288 | if(skipping) { | 289 | if(skipping) { |
289 | --skipping; return; | 290 | --skipping; return; |
290 | } | 291 | } |
291 | if(is_qelement(n,NSURI_XRD "\tType")) { | 292 | if(is_qelement(n,NSURI_XRD "\tType")) { |
292 | assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); | 293 | assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); |
293 | xrd_service->types.insert(cdata_buf); | 294 | xrd_service->types.insert(cdata_buf); |
294 | }else if(is_qelement(n,NSURI_XRD "\tService")) { | 295 | }else if(is_qelement(n,NSURI_XRD "\tService")) { |
295 | assert(xrd); assert(xrd_service); | 296 | assert(xrd); assert(xrd_service); |
296 | assert(!pt_stack.empty()); | 297 | assert(!pt_stack.empty()); |
297 | assert(pt_stack.back()==(NSURI_XRD "\tService")); | 298 | assert(pt_stack.back()==(NSURI_XRD "\tService")); |
298 | pt_stack.pop_back(); | 299 | pt_stack.pop_back(); |
299 | xrd_service = 0; | 300 | xrd_service = 0; |
300 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { | 301 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { |
301 | assert(xrd); | 302 | assert(xrd); |
302 | if(is_qelement(pt_stack.back().c_str(),n)) { | 303 | if(is_qelement(pt_stack.back().c_str(),n)) { |
303 | assert(cdata==&status_string); | 304 | assert(cdata==&status_string); |
304 | pt_stack.pop_back(); | 305 | pt_stack.pop_back(); |
305 | if(status_code!=100) | 306 | if(status_code!=100) |
306 | skipping = -1; | 307 | skipping = -1; |
307 | } | 308 | } |
309 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { | ||
310 | assert(xrd); | ||
311 | xrd->expires = util::w3c_to_time(cdata_buf); | ||
308 | }else if((xmode&xmode_html) && is_element(n,"head")) { | 312 | }else if((xmode&xmode_html) && is_element(n,"head")) { |
309 | skipping = -1; | 313 | skipping = -1; |
310 | } | 314 | } |
311 | cdata = 0; | 315 | cdata = 0; |
312 | } | 316 | } |
313 | void character_data(const XML_Char *s,int l) { | 317 | void character_data(const XML_Char *s,int l) { |
314 | if(skipping) return; | 318 | if(skipping) return; |
315 | if(cdata) cdata->append(s,l); | 319 | if(cdata) cdata->append(s,l); |
316 | } | 320 | } |
317 | 321 | ||
318 | void html_start_element(const XML_Char *n,const XML_Char **a) { | 322 | void html_start_element(const XML_Char *n,const XML_Char **a) { |
319 | if(is_element(n,"meta")) { | 323 | if(is_element(n,"meta")) { |
320 | bool heq = false; | 324 | bool heq = false; |
321 | string l; | 325 | string l; |
322 | for(;*a;a+=2) { | 326 | for(;*a;a+=2) { |
323 | if(!( strcasecmp(a[0],"http-equiv") | 327 | if(!( strcasecmp(a[0],"http-equiv") |
324 | || strcasecmp(a[1],XRDS_HEADER) )) | 328 | || strcasecmp(a[1],XRDS_HEADER) )) |
325 | heq = true; | 329 | heq = true; |
326 | else if(!strcasecmp(a[0],"content")) | 330 | else if(!strcasecmp(a[0],"content")) |
327 | l.assign(a[1]); | 331 | l.assign(a[1]); |
328 | } | 332 | } |
329 | if(heq) | 333 | if(heq) |
330 | xrds_location = l; | 334 | xrds_location = l; |
331 | }else if(is_element(n,"link")) { | 335 | }else if(is_element(n,"link")) { |