summaryrefslogtreecommitdiffabout
path: root/lib
Unidiff
Diffstat (limited to 'lib') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/discovery.cc45
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
@@ -7,151 +7,166 @@
7#include <opkele/util.h> 7#include <opkele/util.h>
8#include <opkele/tidy.h> 8#include <opkele/tidy.h>
9#include <opkele/debug.h> 9#include <opkele/debug.h>
10 10
11#include "config.h" 11#include "config.h"
12 12
13#define XRDS_HEADER "X-XRDS-Location" 13#define XRDS_HEADER "X-XRDS-Location"
14#define CT_HEADER "Content-Type" 14#define CT_HEADER "Content-Type"
15 15
16namespace opkele { 16namespace opkele {
17 using std::list; 17 using std::list;
18 using xrd::XRD_t; 18 using xrd::XRD_t;
19 using xrd::service_t; 19 using xrd::service_t;
20 20
21 /* TODO: the whole discovery thing needs cleanup and optimization due to 21 /* TODO: the whole discovery thing needs cleanup and optimization due to
22 * many changes of concept. */ 22 * many changes of concept. */
23 23
24 static const char *whitespace = " \t\r\n"; 24 static const char *whitespace = " \t\r\n";
25 static const char *i_leaders = "=@+$!("; 25 static const char *i_leaders = "=@+$!(";
26 static const size_t max_html = 16384; 26 static const size_t max_html = 16384;
27 27
28 static const struct service_type_t { 28 static const struct service_type_t {
29 const char *uri; 29 const char *uri;
30 const char *forceid; 30 const char *forceid;
31 } service_types[] = { 31 } op_service_types[] = {
32 { STURI_OPENID20_OP, IDURI_SELECT20 }, 32 { STURI_OPENID20_OP, IDURI_SELECT20 },
33 { STURI_OPENID20, 0 }, 33 { STURI_OPENID20, 0 },
34 { STURI_OPENID11, 0 }, 34 { STURI_OPENID11, 0 },
35 { STURI_OPENID10, 0 } 35 { STURI_OPENID10, 0 }
36 }; 36 };
37 enum { 37 enum {
38 st_index_1 = 2, st_index_2 = 1 38 st_index_1 = 2, st_index_2 = 1
39 }; 39 };
40 40
41 41
42 static inline bool is_qelement(const XML_Char *n,const char *qen) { 42 static inline bool is_qelement(const XML_Char *n,const char *qen) {
43 return !strcasecmp(n,qen); 43 return !strcasecmp(n,qen);
44 } 44 }
45 static inline bool is_element(const XML_Char *n,const char *en) { 45 static inline bool is_element(const XML_Char *n,const char *en) {
46 if(!strcasecmp(n,en)) return true; 46 if(!strcasecmp(n,en)) return true;
47 int nl = strlen(n), enl = strlen(en); 47 int nl = strlen(n), enl = strlen(en);
48 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t' 48 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t'
49 && !strcasecmp(&n[nl-enl],en) ) 49 && !strcasecmp(&n[nl-enl],en) )
50 return true; 50 return true;
51 return false; 51 return false;
52 } 52 }
53 53
54 static long element_priority(const XML_Char **a) { 54 static long element_priority(const XML_Char **a) {
55 for(;*a;++a) 55 for(;*a;++a)
56 if(!strcasecmp(*(a++),"priority")) { 56 if(!strcasecmp(*(a++),"priority")) {
57 long rv; 57 long rv;
58 return (sscanf(*a,"%ld",&rv)==1)?rv:-1; 58 return (sscanf(*a,"%ld",&rv)==1)?rv:-1;
59 } 59 }
60 return -1; 60 return -1;
61 } 61 }
62 /* TODO: ideally all attributes should be 62 /* TODO: ideally all attributes should be
63 * retrieved in one run */ 63 * retrieved in one run */
64 static const char *element_attr(const XML_Char **a, const char *at) { 64 static const char *element_attr(const XML_Char **a, const char *at) {
65 for(;*a;++a) 65 for(;*a;++a)
66 if(!strcasecmp(*(a++),at)) { 66 if(!strcasecmp(*(a++),at)) {
67 return *a; 67 return *a;
68 } 68 }
69 return 0; 69 return 0;
70 } 70 }
71 71
72 class idigger_t : public util::curl_t, public util::expat_t { 72 class idigger_t : public util::curl_t, public util::expat_t {
73 public: 73 public:
74 string xri_proxy; 74 string xri_proxy;
75 75
76 enum { 76 enum {
77 xmode_html = 1, xmode_xrd = 2, xmode_cid = 4 77 xmode_html = 1, xmode_xrd = 2, xmode_cid = 4,
78 xmode_noredirs = 8
78 }; 79 };
79 int xmode; 80 int xmode;
80 81
81 string xrds_location; 82 string xrds_location;
82 string http_content_type; 83 string http_content_type;
83 service_t html_openid1; 84 service_t html_openid1;
84 service_t html_openid2; 85 service_t html_openid2;
85 string cdata_buf; 86 string cdata_buf;
86 long status_code; 87 long status_code;
87 string status_string; 88 string status_string;
88 89
89 typedef list<string> pt_stack_t; 90 typedef list<string> pt_stack_t;
90 pt_stack_t pt_stack; 91 pt_stack_t pt_stack;
91 int skipping; 92 int skipping;
92 bool parser_choked; 93 bool parser_choked;
93 string save_html; 94 string save_html;
94 95
95 XRD_t *xrd; 96 XRD_t *xrd;
96 service_t *xrd_service; 97 service_t *xrd_service;
97 string* cdata; 98 string* cdata;
98 99
99 idigger_t() 100 idigger_t()
100 : util::curl_t(easy_init()), 101 : util::curl_t(easy_init()),
101 util::expat_t(0), 102 util::expat_t(0),
102 xri_proxy(XRI_PROXY_URL) { 103 xri_proxy(XRI_PROXY_URL) {
103 CURLcode r; 104 CURLcode r;
104 (r=misc_sets()) 105 (r=misc_sets())
105 || (r=set_write()) 106 || (r=set_write())
106 || (r=set_header()) 107 || (r=set_header())
107 ; 108 ;
108 if(r) 109 if(r)
109 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 110 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
110 } 111 }
111 ~idigger_t() throw() { } 112 ~idigger_t() throw() { }
112 113
114 void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) {
115 idiscovery_t idis;
116 idis.xri_identity = false;
117 discover_at(idis,yurl,xmode_html|xmode_xrd|(redirs?0:xmode_noredirs));
118 if(!xrds_location.empty()) {
119 idis.clear();
120 discover_at(idis,xrds_location,xmode_xrd);
121 }
122 idis.normalized_id = idis.canonicalized_id = yurl;
123 service_type_t st;
124 for(st.uri=*types;*types;st.uri=*(++types))
125 queue_endpoints(oi,idis,&st);
126 }
127
113 string discover(endpoint_discovery_iterator& oi,const string& identity) { 128 string discover(endpoint_discovery_iterator& oi,const string& identity) {
114 string rv; 129 string rv;
115 idiscovery_t idis; 130 idiscovery_t idis;
116 string::size_type fsc = identity.find_first_not_of(whitespace); 131 string::size_type fsc = identity.find_first_not_of(whitespace);
117 if(fsc==string::npos) 132 if(fsc==string::npos)
118 throw bad_input(OPKELE_CP_ "whitespace-only identity"); 133 throw bad_input(OPKELE_CP_ "whitespace-only identity");
119 string::size_type lsc = identity.find_last_not_of(whitespace); 134 string::size_type lsc = identity.find_last_not_of(whitespace);
120 assert(lsc!=string::npos); 135 assert(lsc!=string::npos);
121 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) 136 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1))
122 fsc += sizeof("xri://")-1; 137 fsc += sizeof("xri://")-1;
123 if((fsc+1)>=lsc) 138 if((fsc+1)>=lsc)
124 throw bad_input(OPKELE_CP_ "not a character of importance in identity"); 139 throw bad_input(OPKELE_CP_ "not a character of importance in identity");
125 string id(identity,fsc,lsc-fsc+1); 140 string id(identity,fsc,lsc-fsc+1);
126 idis.clear(); 141 idis.clear();
127 if(strchr(i_leaders,id[0])) { 142 if(strchr(i_leaders,id[0])) {
128 /* TODO: further normalize xri identity? Like folding case 143 /* TODO: further normalize xri identity? Like folding case
129 * or whatever... */ 144 * or whatever... */
130 rv = id; 145 rv = id;
131 set<string> cids; 146 set<string> cids;
132 for(const struct service_type_t *st=service_types; 147 for(const struct service_type_t *st=op_service_types;
133 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) { 148 st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) {
134 idis.clear(); 149 idis.clear();
135 discover_at( idis, 150 discover_at( idis,
136 xri_proxy + util::url_encode(id)+ 151 xri_proxy + util::url_encode(id)+
137 "?_xrd_t="+util::url_encode(st->uri)+ 152 "?_xrd_t="+util::url_encode(st->uri)+
138 "&_xrd_r=application/xrd%2Bxml" 153 "&_xrd_r=application/xrd%2Bxml"
139 ";sep=true;refs=true", 154 ";sep=true;refs=true",
140 xmode_xrd ); 155 xmode_xrd );
141 if(status_code==241) continue; 156 if(status_code==241) continue;
142 if(status_code!=100) 157 if(status_code!=100)
143 throw failed_xri_resolution(OPKELE_CP_ 158 throw failed_xri_resolution(OPKELE_CP_
144 "XRI resolution failed with '"+status_string+"' message" 159 "XRI resolution failed with '"+status_string+"' message"
145 ", while looking for SEP with type '"+st->uri+"'", status_code); 160 ", while looking for SEP with type '"+st->uri+"'", status_code);
146 if(idis.xrd.canonical_ids.empty()) 161 if(idis.xrd.canonical_ids.empty())
147 throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID found for XRI identity found"); 162 throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID found for XRI identity found");
148 string cid = idis.xrd.canonical_ids.begin()->second; 163 string cid = idis.xrd.canonical_ids.begin()->second;
149 if(cids.find(cid)==cids.end()) { 164 if(cids.find(cid)==cids.end()) {
150 cids.insert(cid); 165 cids.insert(cid);
151 idis.clear(); 166 idis.clear();
152 discover_at( idis, 167 discover_at( idis,
153 xri_proxy + util::url_encode(id)+ 168 xri_proxy + util::url_encode(id)+
154 "?_xrd_t="+util::url_encode(st->uri)+ 169 "?_xrd_t="+util::url_encode(st->uri)+
155 "&_xrd_r=application/xrd%2Bxml" 170 "&_xrd_r=application/xrd%2Bxml"
156 ";sep=true;refs=true", 171 ";sep=true;refs=true",
157 xmode_xrd ); 172 xmode_xrd );
@@ -173,61 +188,63 @@ namespace opkele {
173 string::size_type fp = id.find('#'); 188 string::size_type fp = id.find('#');
174 if(fp!=string::npos) { 189 if(fp!=string::npos) {
175 string::size_type qp = id.find('?'); 190 string::size_type qp = id.find('?');
176 if(qp==string::npos || qp<fp) 191 if(qp==string::npos || qp<fp)
177 id.erase(fp); 192 id.erase(fp);
178 else if(qp>fp) 193 else if(qp>fp)
179 id.erase(fp,qp-fp); 194 id.erase(fp,qp-fp);
180 } 195 }
181 rv = idis.normalized_id = util::rfc_3986_normalize_uri(id); 196 rv = idis.normalized_id = util::rfc_3986_normalize_uri(id);
182 discover_at(idis,id,xmode_html|xmode_xrd); 197 discover_at(idis,id,xmode_html|xmode_xrd);
183 const char * eu = 0; 198 const char * eu = 0;
184 CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); 199 CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu);
185 if(r) 200 if(r)
186 throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); 201 throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r);
187 string cid = util::strip_uri_fragment_part( idis.canonicalized_id = util::rfc_3986_normalize_uri(eu) ); 202 string cid = util::strip_uri_fragment_part( idis.canonicalized_id = util::rfc_3986_normalize_uri(eu) );
188 if(xrds_location.empty()) { 203 if(xrds_location.empty()) {
189 html2xrd(oi,idis); 204 html2xrd(oi,idis);
190 }else{ 205 }else{
191 idis.clear(); 206 idis.clear();
192 idis.canonicalized_id = cid; 207 idis.canonicalized_id = cid;
193 discover_at(idis,xrds_location,xmode_xrd); 208 discover_at(idis,xrds_location,xmode_xrd);
194 if(idis.xrd.empty()) 209 if(idis.xrd.empty())
195 html2xrd(oi,idis); 210 html2xrd(oi,idis);
196 else{ 211 else{
197 for(const service_type_t *st=service_types; 212 for(const service_type_t *st=op_service_types;
198 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) 213 st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st)
199 queue_endpoints(oi,idis,st); 214 queue_endpoints(oi,idis,st);
200 } 215 }
201 } 216 }
202 } 217 }
203 return rv; 218 return rv;
204 } 219 }
205 220
206 void discover_at(idiscovery_t& idis,const string& url,int xm) { 221 void discover_at(idiscovery_t& idis,const string& url,int xm) {
207 CURLcode r = easy_setopt(CURLOPT_URL,url.c_str()); 222 CURLcode r = easy_setopt(CURLOPT_MAXREDIRS, (xm&xmode_noredirs)?0:5);
208 if(r) 223 if(r)
209 throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r); 224 throw exception_curl(OPKELE_CP_ "failed to set curly maxredirs option");
225 if( (r=easy_setopt(CURLOPT_URL,url.c_str())) )
226 throw exception_curl(OPKELE_CP_ "failed to set curly urlie",r);
210 227
211 http_content_type.clear(); 228 http_content_type.clear();
212 xmode = xm; 229 xmode = xm;
213 prepare_to_parse(); 230 prepare_to_parse();
214 if(xmode&xmode_html) { 231 if(xmode&xmode_html) {
215 xrds_location.clear(); 232 xrds_location.clear();
216 save_html.clear(); 233 save_html.clear();
217 save_html.reserve(max_html); 234 save_html.reserve(max_html);
218 } 235 }
219 xrd = &idis.xrd; 236 xrd = &idis.xrd;
220 237
221 r = easy_perform(); 238 r = easy_perform();
222 if(r && r!=CURLE_WRITE_ERROR) 239 if(r && r!=CURLE_WRITE_ERROR)
223 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 240 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
224 241
225 if(!parser_choked) { 242 if(!parser_choked) {
226 parse(0,0,true); 243 parse(0,0,true);
227 }else{ 244 }else{
228 /* TODO: do not bother if we've seen xml */ 245 /* TODO: do not bother if we've seen xml */
229 try { 246 try {
230 util::tidy_doc_t td = util::tidy_doc_t::create(); 247 util::tidy_doc_t td = util::tidy_doc_t::create();
231 if(!td) 248 if(!td)
232 throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document"); 249 throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document");
233#ifndef NDEBUG 250#ifndef NDEBUG
@@ -251,54 +268,54 @@ namespace opkele {
251 } 268 }
252 save_html.clear(); 269 save_html.clear();
253 } 270 }
254 271
255 void prepare_to_parse() { 272 void prepare_to_parse() {
256 (*(expat_t*)this) = parser_create_ns(); 273 (*(expat_t*)this) = parser_create_ns();
257 set_user_data(); set_element_handler(); 274 set_user_data(); set_element_handler();
258 set_character_data_handler(); 275 set_character_data_handler();
259 276
260 if(xmode&xmode_html) { 277 if(xmode&xmode_html) {
261 html_openid1.clear(); html_openid2.clear(); 278 html_openid1.clear(); html_openid2.clear();
262 parser_choked = false; 279 parser_choked = false;
263 } 280 }
264 281
265 cdata = 0; xrd_service = 0; skipping = 0; 282 cdata = 0; xrd_service = 0; skipping = 0;
266 pt_stack.clear(); 283 pt_stack.clear();
267 status_code = 100; status_string.clear(); 284 status_code = 100; status_string.clear();
268 } 285 }
269 286
270 void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) { 287 void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) {
271 XRD_t& x = id.xrd; 288 XRD_t& x = id.xrd;
272 if(!html_openid2.uris.empty()) { 289 if(!html_openid2.uris.empty()) {
273 html_openid2.types.insert(STURI_OPENID20); 290 html_openid2.types.insert(STURI_OPENID20);
274 x.services.add(-1,html_openid2); 291 x.services.add(-1,html_openid2);
275 queue_endpoints(oi,id,&service_types[st_index_2]); 292 queue_endpoints(oi,id,&op_service_types[st_index_2]);
276 } 293 }
277 if(!html_openid1.uris.empty()) { 294 if(!html_openid1.uris.empty()) {
278 html_openid1.types.insert(STURI_OPENID11); 295 html_openid1.types.insert(STURI_OPENID11);
279 x.services.add(-1,html_openid1); 296 x.services.add(-1,html_openid1);
280 queue_endpoints(oi,id,&service_types[st_index_1]); 297 queue_endpoints(oi,id,&op_service_types[st_index_1]);
281 } 298 }
282 } 299 }
283 300
284 size_t write(void *p,size_t s,size_t nm) { 301 size_t write(void *p,size_t s,size_t nm) {
285 /* TODO: limit total size */ 302 /* TODO: limit total size */
286 size_t bytes = s*nm; 303 size_t bytes = s*nm;
287 const char *inbuf = (const char*)p; 304 const char *inbuf = (const char*)p;
288 if(xmode&xmode_html) { 305 if(xmode&xmode_html) {
289 size_t mbts = save_html.capacity()-save_html.size(); 306 size_t mbts = save_html.capacity()-save_html.size();
290 size_t bts = 0; 307 size_t bts = 0;
291 if(mbts>0) { 308 if(mbts>0) {
292 bts = (bytes>mbts)?mbts:bytes; 309 bts = (bytes>mbts)?mbts:bytes;
293 save_html.append(inbuf,bts); 310 save_html.append(inbuf,bts);
294 } 311 }
295 if(skipping<0) return bts; 312 if(skipping<0) return bts;
296 } 313 }
297 if(skipping<0) return 0; 314 if(skipping<0) return 0;
298 bool rp = parse(inbuf,bytes,false); 315 bool rp = parse(inbuf,bytes,false);
299 if(!rp) { 316 if(!rp) {
300 parser_choked = true; 317 parser_choked = true;
301 skipping = -1; 318 skipping = -1;
302 if(!(xmode&xmode_html)) 319 if(!(xmode&xmode_html))
303 bytes = 0; 320 bytes = 0;
304 } 321 }
@@ -528,25 +545,33 @@ namespace opkele {
528 *(oi++) = ep; 545 *(oi++) = ep;
529 }else{ 546 }else{
530 if(svc.local_ids.empty()) { 547 if(svc.local_ids.empty()) {
531 ep.local_id = ep.claimed_id; 548 ep.local_id = ep.claimed_id;
532 *(oi++) = ep; 549 *(oi++) = ep;
533 }else{ 550 }else{
534 for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin(); 551 for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin();
535 ilid!=svc.local_ids.end(); ++ilid) { 552 ilid!=svc.local_ids.end(); ++ilid) {
536 ep.local_id = ilid->second; 553 ep.local_id = ilid->second;
537 *(oi++) = ep; 554 *(oi++) = ep;
538 } 555 }
539 } 556 }
540 } 557 }
541 } 558 }
542 } 559 }
543 } 560 }
544 561
545 }; 562 };
546 563
547 string idiscover(endpoint_discovery_iterator oi,const string& identity) { 564 string idiscover(endpoint_discovery_iterator oi,const string& identity) {
548 idigger_t idigger; 565 idigger_t idigger;
549 return idigger.discover(oi,identity); 566 return idigger.discover(oi,identity);
550 } 567 }
551 568
569 void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) try {
570 idigger_t idigger;
571 idigger.yadiscover(oi,yurl,types,redirs);
572 }catch(exception_curl& ec) {
573 if(redirs || ec._error!=CURLE_TOO_MANY_REDIRECTS)
574 throw;
575 }
576
552} 577}