author | Michael Krelin <hacker@klever.net> | 2008-02-03 15:29:07 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-02-03 15:29:07 (UTC) |
commit | 23a6d48436e24d3d145b742984ef68ec3bae2bfd (patch) (side-by-side diff) | |
tree | 9cd085f583f4235e4f21e640cfc1b1ab1fb04b06 | |
parent | d1c45af16b3bd31f65d03eec0fcd1c61b4d69fb0 (diff) | |
download | libopkele-23a6d48436e24d3d145b742984ef68ec3bae2bfd.zip libopkele-23a6d48436e24d3d145b742984ef68ec3bae2bfd.tar.gz libopkele-23a6d48436e24d3d145b742984ef68ec3bae2bfd.tar.bz2 |
added provisions for discovery on RP
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/discovery.h | 5 | ||||
-rw-r--r-- | lib/discovery.cc | 45 |
2 files changed, 39 insertions, 11 deletions
diff --git a/include/opkele/discovery.h b/include/opkele/discovery.h index 677d7bb..4471597 100644 --- a/include/opkele/discovery.h +++ b/include/opkele/discovery.h @@ -1,22 +1,21 @@ #ifndef __OPKELE_DISCOVERY_H #define __OPKELE_DISCOVERY_H #include <string> #include <opkele/types.h> -#include <opkele/basic_rp.h> namespace opkele { using std::string; namespace xrd { struct priority_compare { inline bool operator()(long a,long b) const { return (a<0) ? false : (b<0) ? true : (a<b); } }; template <typename _DT> class priority_map : public multimap<long,_DT,priority_compare> { typedef multimap<long,_DT,priority_compare> map_type; public: @@ -79,32 +78,36 @@ namespace opkele { return canonical_ids.empty() && local_ids.empty() && services.empty(); } }; } typedef util::output_iterator_proxy<openid_endpoint_t> endpoint_discovery_iterator; string idiscover( endpoint_discovery_iterator oi, const string& identity); + void yadiscover( + endpoint_discovery_iterator oi, + const string& yurl, + const char **types, bool redirs=false); struct idiscovery_t { bool xri_identity; string normalized_id; string canonicalized_id; xrd::XRD_t xrd; idiscovery_t() { } void clear() { normalized_id.clear(); canonicalized_id.clear(); xrd.clear(); } }; } diff --git a/lib/discovery.cc b/lib/discovery.cc index 6e20654..6f58339 100644 --- a/lib/discovery.cc +++ b/lib/discovery.cc @@ -15,33 +15,33 @@ namespace opkele { using std::list; using xrd::XRD_t; using xrd::service_t; /* TODO: the whole discovery thing needs cleanup and optimization due to * many changes of concept. */ 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 }; static inline bool is_qelement(const XML_Char *n,const char *qen) { return !strcasecmp(n,qen); } static inline bool is_element(const XML_Char *n,const char *en) { if(!strcasecmp(n,en)) return true; int nl = strlen(n), enl = strlen(en); @@ -61,33 +61,34 @@ namespace opkele { } /* TODO: ideally all attributes should be * retrieved in one run */ static const char *element_attr(const XML_Char **a, const char *at) { for(;*a;++a) if(!strcasecmp(*(a++),at)) { return *a; } 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; long status_code; string status_string; typedef list<string> pt_stack_t; pt_stack_t pt_stack; int skipping; bool parser_choked; string save_html; @@ -97,53 +98,67 @@ namespace opkele { string* cdata; idigger_t() : util::curl_t(easy_init()), util::expat_t(0), xri_proxy(XRI_PROXY_URL) { CURLcode r; (r=misc_sets()) || (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); if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) fsc += sizeof("xri://")-1; if((fsc+1)>=lsc) 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; if(status_code!=100) throw failed_xri_resolution(OPKELE_CP_ "XRI resolution failed with '"+status_string+"' message" ", while looking for SEP with type '"+st->uri+"'", status_code); if(idis.xrd.canonical_ids.empty()) throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID found for XRI identity found"); string cid = idis.xrd.canonical_ids.begin()->second; if(cids.find(cid)==cids.end()) { @@ -181,45 +196,47 @@ namespace opkele { rv = idis.normalized_id = util::rfc_3986_normalize_uri(id); discover_at(idis,id,xmode_html|xmode_xrd); const char * eu = 0; CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); if(r) throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); string cid = util::strip_uri_fragment_part( idis.canonicalized_id = util::rfc_3986_normalize_uri(eu) ); if(xrds_location.empty()) { 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); } xrd = &idis.xrd; r = easy_perform(); if(r && r!=CURLE_WRITE_ERROR) throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); if(!parser_choked) { @@ -259,38 +276,38 @@ namespace opkele { if(xmode&xmode_html) { html_openid1.clear(); html_openid2.clear(); parser_choked = false; } cdata = 0; xrd_service = 0; skipping = 0; pt_stack.clear(); 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) { size_t mbts = save_html.capacity()-save_html.size(); size_t bts = 0; if(mbts>0) { bts = (bytes>mbts)?mbts:bytes; save_html.append(inbuf,bts); } if(skipping<0) return bts; } @@ -536,17 +553,25 @@ namespace opkele { ep.local_id = ilid->second; *(oi++) = ep; } } } } } } }; 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; + } + } |