-rw-r--r-- | lib/discovery.cc | 45 |
1 files changed, 35 insertions, 10 deletions
diff --git a/lib/discovery.cc b/lib/discovery.cc index 6e20654..6f58339 100644 --- a/lib/discovery.cc +++ b/lib/discovery.cc @@ -23,17 +23,17 @@ namespace opkele { static const char *whitespace = " \t\r\n"; static const char *i_leaders = "=@+$!("; static const size_t max_html = 16384; static const struct service_type_t { const char *uri; const char *forceid; - } service_types[] = { + } op_service_types[] = { { STURI_OPENID20_OP, IDURI_SELECT20 }, { STURI_OPENID20, 0 }, { STURI_OPENID11, 0 }, { STURI_OPENID10, 0 } }; enum { st_index_1 = 2, st_index_2 = 1 }; @@ -69,17 +69,18 @@ namespace opkele { return 0; } class idigger_t : public util::curl_t, public util::expat_t { public: string xri_proxy; enum { - xmode_html = 1, xmode_xrd = 2, xmode_cid = 4 + xmode_html = 1, xmode_xrd = 2, xmode_cid = 4, + xmode_noredirs = 8 }; int xmode; string xrds_location; string http_content_type; service_t html_openid1; service_t html_openid2; string cdata_buf; @@ -105,16 +106,30 @@ namespace opkele { || (r=set_write()) || (r=set_header()) ; if(r) throw exception_curl(OPKELE_CP_ "failed to set curly options",r); } ~idigger_t() throw() { } + void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) { + idiscovery_t idis; + idis.xri_identity = false; + discover_at(idis,yurl,xmode_html|xmode_xrd|(redirs?0:xmode_noredirs)); + if(!xrds_location.empty()) { + idis.clear(); + discover_at(idis,xrds_location,xmode_xrd); + } + idis.normalized_id = idis.canonicalized_id = yurl; + service_type_t st; + for(st.uri=*types;*types;st.uri=*(++types)) + queue_endpoints(oi,idis,&st); + } + string discover(endpoint_discovery_iterator& oi,const string& identity) { string rv; idiscovery_t idis; string::size_type fsc = identity.find_first_not_of(whitespace); if(fsc==string::npos) throw bad_input(OPKELE_CP_ "whitespace-only identity"); string::size_type lsc = identity.find_last_not_of(whitespace); assert(lsc!=string::npos); @@ -124,18 +139,18 @@ namespace opkele { throw bad_input(OPKELE_CP_ "not a character of importance in identity"); string id(identity,fsc,lsc-fsc+1); idis.clear(); if(strchr(i_leaders,id[0])) { /* TODO: further normalize xri identity? Like folding case * or whatever... */ rv = id; set<string> cids; - for(const struct service_type_t *st=service_types; - st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) { + for(const struct service_type_t *st=op_service_types; + st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) { idis.clear(); discover_at( idis, xri_proxy + util::url_encode(id)+ "?_xrd_t="+util::url_encode(st->uri)+ "&_xrd_r=application/xrd%2Bxml" ";sep=true;refs=true", xmode_xrd ); if(status_code==241) continue; @@ -189,29 +204,31 @@ namespace opkele { html2xrd(oi,idis); }else{ idis.clear(); idis.canonicalized_id = cid; discover_at(idis,xrds_location,xmode_xrd); if(idis.xrd.empty()) html2xrd(oi,idis); else{ - for(const service_type_t *st=service_types; - st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) + for(const service_type_t *st=op_service_types; + st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) queue_endpoints(oi,idis,st); } } } return rv; } void discover_at(idiscovery_t& idis,const string& url,int xm) { - CURLcode r = easy_setopt(CURLOPT_URL,url.c_str()); + CURLcode r = easy_setopt(CURLOPT_MAXREDIRS, (xm&xmode_noredirs)?0:5); if(r) - throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r); + throw exception_curl(OPKELE_CP_ "failed to set curly maxredirs option"); + if( (r=easy_setopt(CURLOPT_URL,url.c_str())) ) + throw exception_curl(OPKELE_CP_ "failed to set curly urlie",r); http_content_type.clear(); xmode = xm; prepare_to_parse(); if(xmode&xmode_html) { xrds_location.clear(); save_html.clear(); save_html.reserve(max_html); @@ -267,22 +284,22 @@ namespace opkele { status_code = 100; status_string.clear(); } void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) { XRD_t& x = id.xrd; if(!html_openid2.uris.empty()) { html_openid2.types.insert(STURI_OPENID20); x.services.add(-1,html_openid2); - queue_endpoints(oi,id,&service_types[st_index_2]); + queue_endpoints(oi,id,&op_service_types[st_index_2]); } if(!html_openid1.uris.empty()) { html_openid1.types.insert(STURI_OPENID11); x.services.add(-1,html_openid1); - queue_endpoints(oi,id,&service_types[st_index_1]); + queue_endpoints(oi,id,&op_service_types[st_index_1]); } } size_t write(void *p,size_t s,size_t nm) { /* TODO: limit total size */ size_t bytes = s*nm; const char *inbuf = (const char*)p; if(xmode&xmode_html) { @@ -544,9 +561,17 @@ namespace opkele { }; string idiscover(endpoint_discovery_iterator oi,const string& identity) { idigger_t idigger; return idigger.discover(oi,identity); } + void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) try { + idigger_t idigger; + idigger.yadiscover(oi,yurl,types,redirs); + }catch(exception_curl& ec) { + if(redirs || ec._error!=CURLE_TOO_MANY_REDIRECTS) + throw; + } + } |