author | Michael Krelin <hacker@klever.net> | 2007-12-13 18:33:20 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2007-12-13 18:37:33 (UTC) |
commit | 58383f07af80dfd5b5c18e5158291c3d8caefbe7 (patch) (side-by-side diff) | |
tree | fe7b9bc8da74bce0dcc6d8a033b8a8073f81ad14 | |
parent | 54f9cf0424f47ef6384bc55904097dc4ecb5fc78 (diff) | |
download | libopkele-58383f07af80dfd5b5c18e5158291c3d8caefbe7.zip libopkele-58383f07af80dfd5b5c18e5158291c3d8caefbe7.tar.gz libopkele-58383f07af80dfd5b5c18e5158291c3d8caefbe7.tar.bz2 |
keep track of whether the identity being discovered is XRI
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/discovery.h | 1 | ||||
-rw-r--r-- | lib/discovery.cc | 2 | ||||
-rw-r--r-- | test/idiscover.cc | 1 |
3 files changed, 4 insertions, 0 deletions
diff --git a/include/opkele/discovery.h b/include/opkele/discovery.h index 5d7129b..7865fb2 100644 --- a/include/opkele/discovery.h +++ b/include/opkele/discovery.h @@ -1,33 +1,34 @@ #ifndef __OPKELE_DISCOVERY_H #define __OPKELE_DISCOVERY_H #include <string> #include <opkele/types.h> namespace opkele { using std::string; struct idiscovery_t; void idiscover(idiscovery_t& result,const string& identity); struct idiscovery_t { + bool xri_identity; string normalized_id; string canonicalized_id; xrd::XRD_t xrd; idiscovery_t(const string& i) { idiscover(*this,i); } idiscovery_t(const char *i) { idiscover(*this,i); } void clear() { normalized_id.clear(); canonicalized_id.clear(); xrd.clear(); } }; } #endif /* __OPKELE_DISCOVERY_H */ diff --git a/lib/discovery.cc b/lib/discovery.cc index a35ce32..1f23ff1 100644 --- a/lib/discovery.cc +++ b/lib/discovery.cc @@ -2,203 +2,205 @@ using namespace std; #include <list> #include <opkele/curl.h> #include <opkele/expat.h> #include <opkele/uris.h> #include <opkele/discovery.h> #include <opkele/exception.h> #include <opkele/util.h> #include "config.h" #define XRDS_HEADER "X-XRDS-Location" #define CT_HEADER "Content-Type" namespace opkele { using std::list; using xrd::XRD_t; using xrd::service_t; static const char *whitespace = " \t\r\n"; static const char *i_leaders = "=@+$!("; 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); if( (nl>=(enl+1)) && n[nl-enl-1]=='\t' && !strcasecmp(&n[nl-enl],en) ) return true; return false; } static long element_priority(const XML_Char **a) { for(;*a;++a) if(!strcasecmp(*(a++),"priority")) { long rv; return (sscanf(*a,"%ld",&rv)==1)?rv:-1; } return -1; } class idigger_t : public util::curl_t, public util::expat_t { public: string xri_proxy; enum { xmode_html = 1, xmode_xrd = 2 }; 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; XRD_t *xrd; service_t *xrd_service; 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 discover(idiscovery_t& result,const string& identity) { result.clear(); string::size_type fsc = identity.find_first_not_of(whitespace); if(fsc==string::npos) throw bad_input(OPKELE_CP_ "whtiespace-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); if(strchr(i_leaders,id[0])) { result.normalized_id = id; + result.xri_identity = true; /* TODO: further canonicalize xri identity? Like folding case or whatever... */ discover_at( result, xri_proxy + util::url_encode(id)+ "?_xrd_r=application/xrd+xml;sep=false", xmode_xrd); if(status_code!=100) throw failed_xri_resolution(OPKELE_CP_ "XRI resolution failed with '"+status_string+"' message",status_code); if(result.xrd.canonical_ids.empty()) throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found"); }else{ + result.xri_identity = false; if(id.find("://")==string::npos) id.insert(0,"http://"); string::size_type fp = id.find('#'); if(fp!=string::npos) { string::size_type qp = id.find('?'); if(qp==string::npos || qp<fp) id.erase(fp); else if(qp>fp) id.erase(fp,qp-fp); } result.normalized_id = util::rfc_3986_normalize_uri(id); discover_at(result,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); result.canonicalized_id = util::rfc_3986_normalize_uri(eu); /* XXX: strip fragment part? */ if(xrds_location.empty()) { html2xrd(result.xrd); }else{ discover_at(result,xrds_location,xmode_xrd); if(result.xrd.empty()) html2xrd(result.xrd); } } } void discover_at(idiscovery_t& result,const string& url,int xm) { CURLcode r = easy_setopt(CURLOPT_URL,url.c_str()); if(r) throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r); (*(expat_t*)this) = parser_create_ns(); set_user_data(); set_element_handler(); set_character_data_handler(); xrds_location.clear(); http_content_type.clear(); xmode = xm; if(xmode&xmode_html) { xrds_location.clear(); html_openid1.clear(); html_openid2.clear(); } xrd = &result.xrd; cdata = 0; xrd_service = 0; skipping = 0; status_code = 100; status_string.clear(); r = easy_perform(); if(r && r!=CURLE_WRITE_ERROR) throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); parse(0,0,true); } void html2xrd(XRD_t& x) { if(!html_openid1.uris.empty()) { html_openid1.types.insert(STURI_OPENID11); x.services.add(-1,html_openid1); } if(!html_openid2.uris.empty()) { html_openid2.types.insert(STURI_OPENID20); x.services.add(-1,html_openid2); } } size_t write(void *p,size_t s,size_t nm) { if(skipping<0) return 0; /* TODO: limit total size */ size_t bytes = s*nm; parse((const char *)p,bytes,false); return bytes; } size_t header(void *p,size_t s,size_t nm) { size_t bytes = s*nm; const char *h = (const char*)p; const char *colon = (const char*)memchr(p,':',bytes); const char *space = (const char*)memchr(p,' ',bytes); if(space && ( (!colon) || space<colon ) ) { xrds_location.clear(); http_content_type.clear(); }else if(colon) { const char *hv = ++colon; int hnl = colon-h; int rb; for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb); while(rb>0 && isspace(hv[rb-1])) --rb; if(rb) { if( (hnl>=sizeof(XRDS_HEADER)) && !strncasecmp(h,XRDS_HEADER":", sizeof(XRDS_HEADER)) ) { xrds_location.assign(hv,rb); }else if( (hnl>=sizeof(CT_HEADER)) && !strncasecmp(h,CT_HEADER":", sizeof(CT_HEADER)) ) { const char *sc = (const char*)memchr( hv,';',rb); http_content_type.assign(hv,sc?(sc-hv):rb); } diff --git a/test/idiscover.cc b/test/idiscover.cc index 2abedc9..05cbcae 100644 --- a/test/idiscover.cc +++ b/test/idiscover.cc @@ -1,49 +1,50 @@ #include <iostream> #include <stdexcept> #include <iterator> #include <algorithm> using namespace std; #include <opkele/exception.h> #include <opkele/discovery.h> template<typename _PDT> ostream& operator<<(ostream& o,const opkele::xrd::priority_map<_PDT>& pm) { for(typename opkele::xrd::priority_map<_PDT>::const_iterator i=pm.begin(); i!=pm.end();++i) o << ' ' << i->second << '[' << i->first << ']'; return o; } ostream& operator<<(ostream& o,const opkele::xrd::service_t s) { o << "{" << endl << " Type: "; copy(s.types.begin(),s.types.end(), ostream_iterator<string>(o," ")); o << endl << " URI: " << s.uris << endl << " LocalID: " << s.local_ids << endl; o << "}"; } int main(int argc,char **argv) { try { if(argc<2) throw opkele::exception(OPKELE_CP_ "Please, give me something to resolve"); for(int a=1;a<argc;++a) { opkele::idiscovery_t discovery(argv[a]); clog << "===============================================================" << endl << "User-supplied ID: " << argv[a] << endl << "Normalized ID: " << discovery.normalized_id << endl << "Canonicalized ID: " << discovery.canonicalized_id << endl + << "The identity is " << (discovery.xri_identity?"":"not ") << "an i-name" << endl << endl << "CanonicalID: " << discovery.xrd.canonical_ids << endl << "LocalID: " << discovery.xrd.local_ids << endl << "Services: " << discovery.xrd.services << endl; } }catch(exception& e) { cerr << "oops: " << e.what() << endl; _exit(1); } _exit(0); } |