-rw-r--r-- | lib/consumer.cc | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/lib/consumer.cc b/lib/consumer.cc index 7f31d52..f72ae08 100644 --- a/lib/consumer.cc +++ b/lib/consumer.cc | |||
@@ -320,39 +320,61 @@ namespace opkele { | |||
320 | ; | 320 | ; |
321 | if(r) | 321 | if(r) |
322 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 322 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
323 | r = curl_easy_perform(curl); | 323 | r = curl_easy_perform(curl); |
324 | if(r && r!=CURLE_WRITE_ERROR) | 324 | if(r && r!=CURLE_WRITE_ERROR) |
325 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 325 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
326 | static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", | 326 | static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", |
327 | *re_lre = "<\\s*link\\b([^>]+)>", | 327 | *re_lre = "<\\s*link\\b([^>]+)>", |
328 | *re_rre = "\\brel\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]", | 328 | *re_rre = "\\brel\\s*=\\s*['\"]([^'\"]+)['\"]", |
329 | *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]"; | 329 | *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]"; |
330 | pcre_matches_t m1(3), m2(3); | 330 | pcre_matches_t m1(3), m2(3); |
331 | pcre_t bre(re_bre,PCRE_CASELESS); | 331 | pcre_t bre(re_bre,PCRE_CASELESS); |
332 | if(bre.exec(html,m1)>0) | 332 | if(bre.exec(html,m1)>0) |
333 | html.erase(m1.begin(0)); | 333 | html.erase(m1.begin(0)); |
334 | pcre_t hdre(re_hdre,PCRE_CASELESS); | 334 | pcre_t hdre(re_hdre,PCRE_CASELESS); |
335 | if(hdre.exec(html,m1)<=0) | 335 | if(hdre.exec(html,m1)<=0) |
336 | throw bad_input(OPKELE_CP_ "failed to find <head>"); | 336 | throw bad_input(OPKELE_CP_ "failed to find <head>"); |
337 | html.erase(0,m1.end(0)+1); | 337 | html.erase(0,m1.end(0)+1); |
338 | pcre_t lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS); | 338 | pcre_t lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS); |
339 | while(lre.exec(html,m1)>=2) { | 339 | bool gotit = false; |
340 | while( (!gotit) && lre.exec(html,m1)>=2 ) { | ||
341 | static const char *whitespace = " \t"; | ||
340 | string attrs(html,m1.begin(1),m1.length(1)); | 342 | string attrs(html,m1.begin(1),m1.length(1)); |
341 | html.erase(0,m1.end(0)+1); | 343 | html.erase(0,m1.end(0)+1); |
342 | if(!( rre.exec(attrs,m1)>=2 && hre.exec(attrs,m2)>=2 )) | 344 | if(!( rre.exec(attrs,m1)>=2 && hre.exec(attrs,m2)>=2 )) |
343 | continue; | 345 | continue; |
344 | string rel(attrs,m1.begin(1),m1.length(1)); | 346 | string rels(attrs,m1.begin(1),m1.length(1)); |
345 | if(rel=="openid.server") { | 347 | for(string::size_type ns = rels.find_first_not_of(whitespace); |
346 | server.assign(attrs,m2.begin(1),m2.length(1)); | 348 | ns!=string::npos; |
347 | if(!delegate.empty()) break; | 349 | ns=rels.find_first_not_of(whitespace,ns)) { |
348 | }else if(rel=="openid.delegate") { | 350 | string::size_type s = rels.find_first_of(whitespace,ns); |
349 | delegate.assign(attrs,m2.begin(1),m2.length(1)); | 351 | string rel; |
350 | if(!server.empty()) break; | 352 | if(s==string::npos) { |
353 | rel.assign(rels,ns,string::npos); | ||
354 | ns=string::npos; | ||
355 | }else{ | ||
356 | rel.assign(rels,ns,s-ns); | ||
357 | ns=s; | ||
358 | } | ||
359 | if(rel=="openid.server") { | ||
360 | server.assign(attrs,m2.begin(1),m2.length(1)); | ||
361 | if(!delegate.empty()) { | ||
362 | gotit = true; | ||
363 | break; | ||
364 | } | ||
365 | }else if(rel=="openid.delegate") { | ||
366 | delegate.assign(attrs,m2.begin(1),m2.length(1)); | ||
367 | if(!server.empty()) { | ||
368 | gotit = true; | ||
369 | break; | ||
370 | } | ||
371 | } | ||
372 | if(ns==string::npos) break; | ||
351 | } | 373 | } |
352 | } | 374 | } |
353 | if(server.empty()) | 375 | if(server.empty()) |
354 | throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); | 376 | throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); |
355 | } | 377 | } |
356 | 378 | ||
357 | assoc_t consumer_t::find_assoc(const string& server) { | 379 | assoc_t consumer_t::find_assoc(const string& server) { |
358 | throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); | 380 | throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); |