summaryrefslogtreecommitdiffabout
path: root/lib/discovery.cc
Unidiff
Diffstat (limited to 'lib/discovery.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/discovery.cc29
1 files changed, 22 insertions, 7 deletions
diff --git a/lib/discovery.cc b/lib/discovery.cc
index 93409f4..6e20654 100644
--- a/lib/discovery.cc
+++ b/lib/discovery.cc
@@ -1,537 +1,552 @@
1#include <list> 1#include <list>
2#include <opkele/curl.h> 2#include <opkele/curl.h>
3#include <opkele/expat.h> 3#include <opkele/expat.h>
4#include <opkele/uris.h> 4#include <opkele/uris.h>
5#include <opkele/discovery.h> 5#include <opkele/discovery.h>
6#include <opkele/exception.h> 6#include <opkele/exception.h>
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 } 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
63 * retrieved in one run */
64 static const char *element_attr(const XML_Char **a, const char *at) {
65 for(;*a;++a)
66 if(!strcasecmp(*(a++),at)) {
67 return *a;
68 }
69 return 0;
70 }
62 71
63 class idigger_t : public util::curl_t, public util::expat_t { 72 class idigger_t : public util::curl_t, public util::expat_t {
64 public: 73 public:
65 string xri_proxy; 74 string xri_proxy;
66 75
67 enum { 76 enum {
68 xmode_html = 1, xmode_xrd = 2, xmode_cid = 4 77 xmode_html = 1, xmode_xrd = 2, xmode_cid = 4
69 }; 78 };
70 int xmode; 79 int xmode;
71 80
72 string xrds_location; 81 string xrds_location;
73 string http_content_type; 82 string http_content_type;
74 service_t html_openid1; 83 service_t html_openid1;
75 service_t html_openid2; 84 service_t html_openid2;
76 string cdata_buf; 85 string cdata_buf;
77 long status_code; 86 long status_code;
78 string status_string; 87 string status_string;
79 88
80 typedef list<string> pt_stack_t; 89 typedef list<string> pt_stack_t;
81 pt_stack_t pt_stack; 90 pt_stack_t pt_stack;
82 int skipping; 91 int skipping;
83 bool parser_choked; 92 bool parser_choked;
84 string save_html; 93 string save_html;
85 94
86 XRD_t *xrd; 95 XRD_t *xrd;
87 service_t *xrd_service; 96 service_t *xrd_service;
88 string* cdata; 97 string* cdata;
89 98
90 idigger_t() 99 idigger_t()
91 : util::curl_t(easy_init()), 100 : util::curl_t(easy_init()),
92 util::expat_t(0), 101 util::expat_t(0),
93 xri_proxy(XRI_PROXY_URL) { 102 xri_proxy(XRI_PROXY_URL) {
94 CURLcode r; 103 CURLcode r;
95 (r=misc_sets()) 104 (r=misc_sets())
96 || (r=set_write()) 105 || (r=set_write())
97 || (r=set_header()) 106 || (r=set_header())
98 ; 107 ;
99 if(r) 108 if(r)
100 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 109 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
101 } 110 }
102 ~idigger_t() throw() { } 111 ~idigger_t() throw() { }
103 112
104 string discover(endpoint_discovery_iterator& oi,const string& identity) { 113 string discover(endpoint_discovery_iterator& oi,const string& identity) {
105 string rv; 114 string rv;
106 idiscovery_t idis; 115 idiscovery_t idis;
107 string::size_type fsc = identity.find_first_not_of(whitespace); 116 string::size_type fsc = identity.find_first_not_of(whitespace);
108 if(fsc==string::npos) 117 if(fsc==string::npos)
109 throw bad_input(OPKELE_CP_ "whitespace-only identity"); 118 throw bad_input(OPKELE_CP_ "whitespace-only identity");
110 string::size_type lsc = identity.find_last_not_of(whitespace); 119 string::size_type lsc = identity.find_last_not_of(whitespace);
111 assert(lsc!=string::npos); 120 assert(lsc!=string::npos);
112 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) 121 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1))
113 fsc += sizeof("xri://")-1; 122 fsc += sizeof("xri://")-1;
114 if((fsc+1)>=lsc) 123 if((fsc+1)>=lsc)
115 throw bad_input(OPKELE_CP_ "not a character of importance in identity"); 124 throw bad_input(OPKELE_CP_ "not a character of importance in identity");
116 string id(identity,fsc,lsc-fsc+1); 125 string id(identity,fsc,lsc-fsc+1);
117 idis.clear(); 126 idis.clear();
118 if(strchr(i_leaders,id[0])) { 127 if(strchr(i_leaders,id[0])) {
119 /* TODO: further normalize xri identity? Like folding case 128 /* TODO: further normalize xri identity? Like folding case
120 * or whatever... */ 129 * or whatever... */
121 rv = idis.normalized_id = id; 130 rv = id;
122 idis.xri_identity = true;
123 set<string> cids; 131 set<string> cids;
124 for(const struct service_type_t *st=service_types; 132 for(const struct service_type_t *st=service_types;
125 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) { 133 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) {
126 idis.clear(); 134 idis.clear();
127 discover_at( idis, 135 discover_at( idis,
128 xri_proxy + util::url_encode(id)+ 136 xri_proxy + util::url_encode(id)+
129 "?_xrd_t="+util::url_encode(st->uri)+ 137 "?_xrd_t="+util::url_encode(st->uri)+
130 "&_xrd_r=application/xrd%2Bxml" 138 "&_xrd_r=application/xrd%2Bxml"
131 ";sep=true;refs=true", 139 ";sep=true;refs=true",
132 xmode_xrd ); 140 xmode_xrd );
133 if(status_code==241) continue; 141 if(status_code==241) continue;
134 if(status_code!=100) 142 if(status_code!=100)
135 throw failed_xri_resolution(OPKELE_CP_ 143 throw failed_xri_resolution(OPKELE_CP_
136 "XRI resolution failed with '"+status_string+"' message" 144 "XRI resolution failed with '"+status_string+"' message"
137 ", while looking for SEP with type '"+st->uri+"'", status_code); 145 ", while looking for SEP with type '"+st->uri+"'", status_code);
138 if(idis.xrd.canonical_ids.empty()) 146 if(idis.xrd.canonical_ids.empty())
139 throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID found for XRI identity found"); 147 throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID found for XRI identity found");
140 string cid = idis.xrd.canonical_ids.begin()->second; 148 string cid = idis.xrd.canonical_ids.begin()->second;
141 if(cids.find(cid)==cids.end()) { 149 if(cids.find(cid)==cids.end()) {
142 cids.insert(cid); 150 cids.insert(cid);
143 idis.clear(); 151 idis.clear();
144 discover_at( idis, 152 discover_at( idis,
145 xri_proxy + util::url_encode(id)+ 153 xri_proxy + util::url_encode(id)+
146 "?_xrd_t="+util::url_encode(st->uri)+ 154 "?_xrd_t="+util::url_encode(st->uri)+
147 "&_xrd_r=application/xrd%2Bxml" 155 "&_xrd_r=application/xrd%2Bxml"
148 ";sep=true;refs=true", 156 ";sep=true;refs=true",
149 xmode_xrd ); 157 xmode_xrd );
150 if(status_code==241) continue; 158 if(status_code==241) continue;
151 if(status_code!=100) 159 if(status_code!=100)
152 throw failed_xri_resolution(OPKELE_CP_ 160 throw failed_xri_resolution(OPKELE_CP_
153 "XRI resolution failed with '"+status_string+"' message" 161 "XRI resolution failed with '"+status_string+"' message"
154 ", while looking for SEP with type '"+st->uri+"'" 162 ", while looking for SEP with type '"+st->uri+"'"
155 " on canonical id", status_code); 163 " on canonical id", status_code);
156 } 164 }
157 idis.canonicalized_id = cid; 165 idis.canonicalized_id = cid;
166 idis.normalized_id = rv; idis.xri_identity = true;
158 queue_endpoints(oi,idis,st); 167 queue_endpoints(oi,idis,st);
159 } 168 }
160 }else{ 169 }else{
161 idis.xri_identity = false; 170 idis.xri_identity = false;
162 if(id.find("://")==string::npos) 171 if(id.find("://")==string::npos)
163 id.insert(0,"http://"); 172 id.insert(0,"http://");
164 string::size_type fp = id.find('#'); 173 string::size_type fp = id.find('#');
165 if(fp!=string::npos) { 174 if(fp!=string::npos) {
166 string::size_type qp = id.find('?'); 175 string::size_type qp = id.find('?');
167 if(qp==string::npos || qp<fp) 176 if(qp==string::npos || qp<fp)
168 id.erase(fp); 177 id.erase(fp);
169 else if(qp>fp) 178 else if(qp>fp)
170 id.erase(fp,qp-fp); 179 id.erase(fp,qp-fp);
171 } 180 }
172 rv = idis.normalized_id = util::rfc_3986_normalize_uri(id); 181 rv = idis.normalized_id = util::rfc_3986_normalize_uri(id);
173 discover_at(idis,id,xmode_html|xmode_xrd); 182 discover_at(idis,id,xmode_html|xmode_xrd);
174 const char * eu = 0; 183 const char * eu = 0;
175 CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); 184 CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu);
176 if(r) 185 if(r)
177 throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); 186 throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r);
178 string cid = util::strip_uri_fragment_part( idis.canonicalized_id = util::rfc_3986_normalize_uri(eu) ); 187 string cid = util::strip_uri_fragment_part( idis.canonicalized_id = util::rfc_3986_normalize_uri(eu) );
179 if(xrds_location.empty()) { 188 if(xrds_location.empty()) {
180 html2xrd(oi,idis); 189 html2xrd(oi,idis);
181 }else{ 190 }else{
182 idis.clear(); 191 idis.clear();
183 idis.canonicalized_id = cid; 192 idis.canonicalized_id = cid;
184 discover_at(idis,xrds_location,xmode_xrd); 193 discover_at(idis,xrds_location,xmode_xrd);
185 if(idis.xrd.empty()) 194 if(idis.xrd.empty())
186 html2xrd(oi,idis); 195 html2xrd(oi,idis);
187 else{ 196 else{
188 for(const service_type_t *st=service_types; 197 for(const service_type_t *st=service_types;
189 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) 198 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st)
190 queue_endpoints(oi,idis,st); 199 queue_endpoints(oi,idis,st);
191 } 200 }
192 } 201 }
193 } 202 }
194 return rv; 203 return rv;
195 } 204 }
196 205
197 void discover_at(idiscovery_t& idis,const string& url,int xm) { 206 void discover_at(idiscovery_t& idis,const string& url,int xm) {
198 DOUT_("Doing discovery at " << url);
199 CURLcode r = easy_setopt(CURLOPT_URL,url.c_str()); 207 CURLcode r = easy_setopt(CURLOPT_URL,url.c_str());
200 if(r) 208 if(r)
201 throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r); 209 throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r);
202 210
203 http_content_type.clear(); 211 http_content_type.clear();
204 xmode = xm; 212 xmode = xm;
205 prepare_to_parse(); 213 prepare_to_parse();
206 if(xmode&xmode_html) { 214 if(xmode&xmode_html) {
207 xrds_location.clear(); 215 xrds_location.clear();
208 save_html.clear(); 216 save_html.clear();
209 save_html.reserve(max_html); 217 save_html.reserve(max_html);
210 } 218 }
211 xrd = &idis.xrd; 219 xrd = &idis.xrd;
212 220
213 r = easy_perform(); 221 r = easy_perform();
214 if(r && r!=CURLE_WRITE_ERROR) 222 if(r && r!=CURLE_WRITE_ERROR)
215 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 223 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
216 224
217 if(!parser_choked) { 225 if(!parser_choked) {
218 parse(0,0,true); 226 parse(0,0,true);
219 }else{ 227 }else{
220 /* TODO: do not bother if we've seen xml */ 228 /* TODO: do not bother if we've seen xml */
221 try { 229 try {
222 util::tidy_doc_t td = util::tidy_doc_t::create(); 230 util::tidy_doc_t td = util::tidy_doc_t::create();
223 if(!td) 231 if(!td)
224 throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document"); 232 throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document");
225#ifndef NDEBUG 233#ifndef NDEBUG
226 td.opt_set(TidyQuiet,false); 234 td.opt_set(TidyQuiet,false);
227 td.opt_set(TidyShowWarnings,false); 235 td.opt_set(TidyShowWarnings,false);
228#endif /* NDEBUG */ 236#endif /* NDEBUG */
229 td.opt_set(TidyForceOutput,true); 237 td.opt_set(TidyForceOutput,true);
230 td.opt_set(TidyXhtmlOut,true); 238 td.opt_set(TidyXhtmlOut,true);
231 td.opt_set(TidyDoctypeMode,TidyDoctypeOmit); 239 td.opt_set(TidyDoctypeMode,TidyDoctypeOmit);
232 td.opt_set(TidyMark,false); 240 td.opt_set(TidyMark,false);
233 if(td.parse_string(save_html)<=0) 241 if(td.parse_string(save_html)<=0)
234 throw exception_tidy(OPKELE_CP_ "tidy failed to parse document"); 242 throw exception_tidy(OPKELE_CP_ "tidy failed to parse document");
235 if(td.clean_and_repair()<=0) 243 if(td.clean_and_repair()<=0)
236 throw exception_tidy(OPKELE_CP_ "tidy failed to clean and repair"); 244 throw exception_tidy(OPKELE_CP_ "tidy failed to clean and repair");
237 util::tidy_buf_t tide; 245 util::tidy_buf_t tide;
238 if(td.save_buffer(tide)<=0) 246 if(td.save_buffer(tide)<=0)
239 throw exception_tidy(OPKELE_CP_ "tidy failed to save buffer"); 247 throw exception_tidy(OPKELE_CP_ "tidy failed to save buffer");
240 prepare_to_parse(); 248 prepare_to_parse();
241 parse(tide.c_str(),tide.size(),true); 249 parse(tide.c_str(),tide.size(),true);
242 }catch(exception_tidy& et) { } 250 }catch(exception_tidy& et) { }
243 } 251 }
244 save_html.clear(); 252 save_html.clear();
245 } 253 }
246 254
247 void prepare_to_parse() { 255 void prepare_to_parse() {
248 (*(expat_t*)this) = parser_create_ns(); 256 (*(expat_t*)this) = parser_create_ns();
249 set_user_data(); set_element_handler(); 257 set_user_data(); set_element_handler();
250 set_character_data_handler(); 258 set_character_data_handler();
251 259
252 if(xmode&xmode_html) { 260 if(xmode&xmode_html) {
253 html_openid1.clear(); html_openid2.clear(); 261 html_openid1.clear(); html_openid2.clear();
254 parser_choked = false; 262 parser_choked = false;
255 } 263 }
256 264
257 cdata = 0; xrd_service = 0; skipping = 0; 265 cdata = 0; xrd_service = 0; skipping = 0;
258 pt_stack.clear(); 266 pt_stack.clear();
259 status_code = 100; status_string.clear(); 267 status_code = 100; status_string.clear();
260 } 268 }
261 269
262 void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) { 270 void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) {
263 XRD_t& x = id.xrd; 271 XRD_t& x = id.xrd;
264 if(!html_openid2.uris.empty()) { 272 if(!html_openid2.uris.empty()) {
265 html_openid2.types.insert(STURI_OPENID20); 273 html_openid2.types.insert(STURI_OPENID20);
266 x.services.add(-1,html_openid2); 274 x.services.add(-1,html_openid2);
267 queue_endpoints(oi,id,&service_types[st_index_2]); 275 queue_endpoints(oi,id,&service_types[st_index_2]);
268 } 276 }
269 if(!html_openid1.uris.empty()) { 277 if(!html_openid1.uris.empty()) {
270 html_openid1.types.insert(STURI_OPENID11); 278 html_openid1.types.insert(STURI_OPENID11);
271 x.services.add(-1,html_openid1); 279 x.services.add(-1,html_openid1);
272 queue_endpoints(oi,id,&service_types[st_index_1]); 280 queue_endpoints(oi,id,&service_types[st_index_1]);
273 } 281 }
274 } 282 }
275 283
276 size_t write(void *p,size_t s,size_t nm) { 284 size_t write(void *p,size_t s,size_t nm) {
277 /* TODO: limit total size */ 285 /* TODO: limit total size */
278 size_t bytes = s*nm; 286 size_t bytes = s*nm;
279 const char *inbuf = (const char*)p; 287 const char *inbuf = (const char*)p;
280 if(xmode&xmode_html) { 288 if(xmode&xmode_html) {
281 size_t mbts = save_html.capacity()-save_html.size(); 289 size_t mbts = save_html.capacity()-save_html.size();
282 size_t bts = 0; 290 size_t bts = 0;
283 if(mbts>0) { 291 if(mbts>0) {
284 bts = (bytes>mbts)?mbts:bytes; 292 bts = (bytes>mbts)?mbts:bytes;
285 save_html.append(inbuf,bts); 293 save_html.append(inbuf,bts);
286 } 294 }
287 if(skipping<0) return bts; 295 if(skipping<0) return bts;
288 } 296 }
289 if(skipping<0) return 0; 297 if(skipping<0) return 0;
290 bool rp = parse(inbuf,bytes,false); 298 bool rp = parse(inbuf,bytes,false);
291 if(!rp) { 299 if(!rp) {
292 parser_choked = true; 300 parser_choked = true;
293 skipping = -1; 301 skipping = -1;
294 if(!(xmode&xmode_html)) 302 if(!(xmode&xmode_html))
295 bytes = 0; 303 bytes = 0;
296 } 304 }
297 return bytes; 305 return bytes;
298 } 306 }
299 size_t header(void *p,size_t s,size_t nm) { 307 size_t header(void *p,size_t s,size_t nm) {
300 size_t bytes = s*nm; 308 size_t bytes = s*nm;
301 const char *h = (const char*)p; 309 const char *h = (const char*)p;
302 const char *colon = (const char*)memchr(p,':',bytes); 310 const char *colon = (const char*)memchr(p,':',bytes);
303 const char *space = (const char*)memchr(p,' ',bytes); 311 const char *space = (const char*)memchr(p,' ',bytes);
304 if(space && ( (!colon) || space<colon ) ) { 312 if(space && ( (!colon) || space<colon ) ) {
305 xrds_location.clear(); http_content_type.clear(); 313 xrds_location.clear(); http_content_type.clear();
306 }else if(colon) { 314 }else if(colon) {
307 const char *hv = ++colon; 315 const char *hv = ++colon;
308 int hnl = colon-h; 316 int hnl = colon-h;
309 int rb; 317 int rb;
310 for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb); 318 for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb);
311 while(rb>0 && isspace(hv[rb-1])) --rb; 319 while(rb>0 && isspace(hv[rb-1])) --rb;
312 if(rb) { 320 if(rb) {
313 if( (hnl>=sizeof(XRDS_HEADER)) 321 if( (hnl>=sizeof(XRDS_HEADER))
314 && !strncasecmp(h,XRDS_HEADER":", 322 && !strncasecmp(h,XRDS_HEADER":",
315 sizeof(XRDS_HEADER)) ) { 323 sizeof(XRDS_HEADER)) ) {
316 xrds_location.assign(hv,rb); 324 xrds_location.assign(hv,rb);
317 }else if( (hnl>=sizeof(CT_HEADER)) 325 }else if( (hnl>=sizeof(CT_HEADER))
318 && !strncasecmp(h,CT_HEADER":", 326 && !strncasecmp(h,CT_HEADER":",
319 sizeof(CT_HEADER)) ) { 327 sizeof(CT_HEADER)) ) {
320 const char *sc = (const char*)memchr( 328 const char *sc = (const char*)memchr(
321 hv,';',rb); 329 hv,';',rb);
322 http_content_type.assign(hv,sc?(sc-hv):rb); 330 http_content_type.assign(hv,sc?(sc-hv):rb);
323 } 331 }
324 } 332 }
325 } 333 }
326 return curl_t::header(p,s,nm); 334 return curl_t::header(p,s,nm);
327 } 335 }
328 336
329 void start_element(const XML_Char *n,const XML_Char **a) { 337 void start_element(const XML_Char *n,const XML_Char **a) {
330 if(skipping<0) return; 338 if(skipping<0) return;
331 if(skipping) { 339 if(skipping) {
332 if(xmode&xmode_html) 340 if(xmode&xmode_html)
333 html_start_element(n,a); 341 html_start_element(n,a);
334 ++skipping; return; 342 ++skipping; return;
335 } 343 }
336 if(pt_stack.empty()) { 344 if(pt_stack.empty()) {
337 if(is_qelement(n,NSURI_XRDS "\tXRDS")) 345 if(is_qelement(n,NSURI_XRDS "\tXRDS"))
338 return; 346 return;
339 if(is_qelement(n,NSURI_XRD "\tXRD")) { 347 if(is_qelement(n,NSURI_XRD "\tXRD")) {
340 assert(xrd); 348 assert(xrd);
341 xrd->clear(); 349 xrd->clear();
342 pt_stack.push_back(n); 350 pt_stack.push_back(n);
343 }else if(xmode&xmode_html) { 351 }else if(xmode&xmode_html) {
344 html_start_element(n,a); 352 html_start_element(n,a);
345 }else{ 353 }else{
346 skipping = -1; 354 skipping = -1;
347 } 355 }
348 }else{ 356 }else{
349 int pt_s = pt_stack.size(); 357 int pt_s = pt_stack.size();
350 if(pt_s==1) { 358 if(pt_s==1) {
351 if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { 359 if(is_qelement(n,NSURI_XRD "\tCanonicalID")) {
352 assert(xrd); 360 assert(xrd);
353 cdata = &(xrd->canonical_ids.add(element_priority(a),string())); 361 cdata = &(xrd->canonical_ids.add(element_priority(a),string()));
354 }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { 362 }else if(is_qelement(n,NSURI_XRD "\tLocalID")) {
355 assert(xrd); 363 assert(xrd);
356 cdata = &(xrd->local_ids.add(element_priority(a),string())); 364 cdata = &(xrd->local_ids.add(element_priority(a),string()));
357 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { 365 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) {
358 assert(xrd); 366 assert(xrd);
359 cdata = &(xrd->provider_id); 367 cdata = &(xrd->provider_id);
360 }else if(is_qelement(n,NSURI_XRD "\tService")) { 368 }else if(is_qelement(n,NSURI_XRD "\tService")) {
361 assert(xrd); 369 assert(xrd);
362 xrd_service = &(xrd->services.add(element_priority(a), 370 xrd_service = &(xrd->services.add(element_priority(a),
363 service_t())); 371 service_t()));
364 pt_stack.push_back(n); 372 pt_stack.push_back(n);
365 }else if(is_qelement(n,NSURI_XRD "\tStatus")) { 373 }else if(is_qelement(n,NSURI_XRD "\tStatus")) {
366 for(;*a;) { 374 for(;*a;) {
367 if(!strcasecmp(*(a++),"code")) { 375 if(!strcasecmp(*(a++),"code")) {
368 if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { 376 if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) {
369 cdata = &status_string; 377 cdata = &status_string;
370 pt_stack.push_back(n); 378 pt_stack.push_back(n);
371 break; 379 break;
372 } 380 }
373 }else 381 }else
374 ++a; 382 ++a;
375 } 383 }
376 }else if(is_qelement(n,NSURI_XRD "\tExpires")) { 384 }else if(is_qelement(n,NSURI_XRD "\tExpires")) {
377 assert(xrd); 385 assert(xrd);
378 cdata_buf.clear(); 386 cdata_buf.clear();
379 cdata = &cdata_buf; 387 cdata = &cdata_buf;
380 }else if(xmode&xmode_html) { 388 }else if(xmode&xmode_html) {
381 html_start_element(n,a); 389 html_start_element(n,a);
382 }else{ 390 }else{
383 skipping = 1; 391 skipping = 1;
384 } 392 }
385 }else if(pt_s==2) { 393 }else if(pt_s==2) {
386 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { 394 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) {
387 if(is_qelement(n,NSURI_XRD "\tType")) { 395 if(is_qelement(n,NSURI_XRD "\tType")) {
388 assert(xrd); assert(xrd_service); 396 assert(xrd); assert(xrd_service);
389 cdata_buf.clear(); 397 cdata_buf.clear();
390 cdata = &cdata_buf; 398 cdata = &cdata_buf;
391 }else if(is_qelement(n,NSURI_XRD "\tURI")) { 399 }else if(is_qelement(n,NSURI_XRD "\tURI")) {
392 assert(xrd); assert(xrd_service); 400 assert(xrd); assert(xrd_service);
393 cdata = &(xrd_service->uris.add(element_priority(a),string())); 401 const char *append = element_attr(a,"append");
402 xrd::uri_t& uri = xrd_service->uris.add(element_priority(a),xrd::uri_t("",append?append:""));
403 cdata = &uri.uri;
394 }else if(is_qelement(n,NSURI_XRD "\tLocalID") 404 }else if(is_qelement(n,NSURI_XRD "\tLocalID")
395 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { 405 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) {
396 assert(xrd); assert(xrd_service); 406 assert(xrd); assert(xrd_service);
397 cdata = &(xrd_service->local_ids.add(element_priority(a),string())); 407 cdata = &(xrd_service->local_ids.add(element_priority(a),string()));
398 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { 408 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) {
399 assert(xrd); assert(xrd_service); 409 assert(xrd); assert(xrd_service);
400 cdata = &(xrd_service->provider_id); 410 cdata = &(xrd_service->provider_id);
401 }else{ 411 }else{
402 skipping = 1; 412 skipping = 1;
403 } 413 }
404 }else 414 }else
405 skipping = 1; 415 skipping = 1;
406 }else if(xmode&xmode_html) { 416 }else if(xmode&xmode_html) {
407 html_start_element(n,a); 417 html_start_element(n,a);
408 }else{ 418 }else{
409 skipping = 1; 419 skipping = 1;
410 } 420 }
411 } 421 }
412 } 422 }
413 void end_element(const XML_Char *n) { 423 void end_element(const XML_Char *n) {
414 if(skipping<0) return; 424 if(skipping<0) return;
415 if(skipping) { 425 if(skipping) {
416 --skipping; return; 426 --skipping; return;
417 } 427 }
418 if(is_qelement(n,NSURI_XRD "\tType")) { 428 if(is_qelement(n,NSURI_XRD "\tType")) {
419 assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); 429 assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf);
420 xrd_service->types.insert(cdata_buf); 430 xrd_service->types.insert(cdata_buf);
421 }else if(is_qelement(n,NSURI_XRD "\tService")) { 431 }else if(is_qelement(n,NSURI_XRD "\tService")) {
422 assert(xrd); assert(xrd_service); 432 assert(xrd); assert(xrd_service);
423 assert(!pt_stack.empty()); 433 assert(!pt_stack.empty());
424 assert(pt_stack.back()==(NSURI_XRD "\tService")); 434 assert(pt_stack.back()==(NSURI_XRD "\tService"));
425 pt_stack.pop_back(); 435 pt_stack.pop_back();
426 xrd_service = 0; 436 xrd_service = 0;
427 }else if(is_qelement(n,NSURI_XRD "\tStatus")) { 437 }else if(is_qelement(n,NSURI_XRD "\tStatus")) {
428 assert(xrd); 438 assert(xrd);
429 if(is_qelement(pt_stack.back().c_str(),n)) { 439 if(is_qelement(pt_stack.back().c_str(),n)) {
430 assert(cdata==&status_string); 440 assert(cdata==&status_string);
431 pt_stack.pop_back(); 441 pt_stack.pop_back();
432 if(status_code!=100) 442 if(status_code!=100)
433 skipping = -1; 443 skipping = -1;
434 } 444 }
435 }else if(is_qelement(n,NSURI_XRD "\tExpires")) { 445 }else if(is_qelement(n,NSURI_XRD "\tExpires")) {
436 assert(xrd); 446 assert(xrd);
437 xrd->expires = util::w3c_to_time(cdata_buf); 447 xrd->expires = util::w3c_to_time(cdata_buf);
438 }else if((xmode&xmode_html) && is_element(n,"head")) { 448 }else if((xmode&xmode_html) && is_element(n,"head")) {
439 skipping = -1; 449 skipping = -1;
440 } 450 }
441 cdata = 0; 451 cdata = 0;
442 } 452 }
443 void character_data(const XML_Char *s,int l) { 453 void character_data(const XML_Char *s,int l) {
444 if(skipping) return; 454 if(skipping) return;
445 if(cdata) cdata->append(s,l); 455 if(cdata) cdata->append(s,l);
446 } 456 }
447 457
448 void html_start_element(const XML_Char *n,const XML_Char **a) { 458 void html_start_element(const XML_Char *n,const XML_Char **a) {
449 if(is_element(n,"meta")) { 459 if(is_element(n,"meta")) {
450 bool heq = false; 460 bool heq = false;
451 string l; 461 string l;
452 for(;*a;a+=2) { 462 for(;*a;a+=2) {
453 if(!( strcasecmp(a[0],"http-equiv") 463 if(!( strcasecmp(a[0],"http-equiv")
454 || strcasecmp(a[1],XRDS_HEADER) )) 464 || strcasecmp(a[1],XRDS_HEADER) ))
455 heq = true; 465 heq = true;
456 else if(!strcasecmp(a[0],"content")) 466 else if(!strcasecmp(a[0],"content"))
457 l.assign(a[1]); 467 l.assign(a[1]);
458 } 468 }
459 if(heq) 469 if(heq)
460 xrds_location = l; 470 xrds_location = l;
461 }else if(is_element(n,"link")) { 471 }else if(is_element(n,"link")) {
462 string rels; 472 string rels;
463 string href; 473 string href;
464 for(;*a;a+=2) { 474 for(;*a;a+=2) {
465 if( !strcasecmp(a[0],"rel") ) { 475 if( !strcasecmp(a[0],"rel") ) {
466 rels.assign(a[1]); 476 rels.assign(a[1]);
467 }else if( !strcasecmp(a[0],"href") ) { 477 }else if( !strcasecmp(a[0],"href") ) {
468 const char *ns = a[1]; 478 const char *ns = a[1];
469 for(;*ns && isspace(*ns);++ns); 479 for(;*ns && isspace(*ns);++ns);
470 href.assign(ns); 480 href.assign(ns);
471 string::size_type lns=href.find_last_not_of(whitespace); 481 string::size_type lns=href.find_last_not_of(whitespace);
472 href.erase(lns+1); 482 href.erase(lns+1);
473 } 483 }
474 } 484 }
475 for(string::size_type ns=rels.find_first_not_of(whitespace); 485 for(string::size_type ns=rels.find_first_not_of(whitespace);
476 ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) { 486 ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) {
477 string::size_type s = rels.find_first_of(whitespace,ns); 487 string::size_type s = rels.find_first_of(whitespace,ns);
478 string rel; 488 string rel;
479 if(s==string::npos) { 489 if(s==string::npos) {
480 rel.assign(rels,ns,string::npos); 490 rel.assign(rels,ns,string::npos);
481 ns = string::npos; 491 ns = string::npos;
482 }else{ 492 }else{
483 rel.assign(rels,ns,s-ns); 493 rel.assign(rels,ns,s-ns);
484 ns = s; 494 ns = s;
485 } 495 }
486 if(rel=="openid.server") 496 if(rel=="openid.server")
487 html_openid1.uris.add(-1,href); 497 html_openid1.uris.add(-1,xrd::uri_t(href));
488 else if(rel=="openid.delegate") 498 else if(rel=="openid.delegate")
489 html_openid1.local_ids.add(-1,href); 499 html_openid1.local_ids.add(-1,href);
490 else if(rel=="openid2.provider") 500 else if(rel=="openid2.provider")
491 html_openid2.uris.add(-1,href); 501 html_openid2.uris.add(-1,xrd::uri_t(href));
492 else if(rel=="openid2.local_id") 502 else if(rel=="openid2.local_id")
493 html_openid2.local_ids.add(-1,href); 503 html_openid2.local_ids.add(-1,href);
494 } 504 }
495 }else if(is_element(n,"body")) { 505 }else if(is_element(n,"body")) {
496 skipping = -1; 506 skipping = -1;
497 } 507 }
498 } 508 }
499 509
500 void queue_endpoints(endpoint_discovery_iterator& oi, 510 void queue_endpoints(endpoint_discovery_iterator& oi,
501 const idiscovery_t &id, 511 const idiscovery_t &id,
502 const service_type_t *st) { 512 const service_type_t *st) {
503 openid_endpoint_t ep; 513 openid_endpoint_t ep;
504 ep.claimed_id = id.canonicalized_id; 514 ep.claimed_id = id.canonicalized_id;
505 for(xrd::services_t::const_iterator isvc=id.xrd.services.begin(); 515 for(xrd::services_t::const_iterator isvc=id.xrd.services.begin();
506 isvc!=id.xrd.services.end(); ++isvc) { 516 isvc!=id.xrd.services.end(); ++isvc) {
507 const xrd::service_t svc = isvc->second; 517 const xrd::service_t svc = isvc->second;
508 if(svc.types.find(st->uri)==svc.types.end()) continue; 518 if(svc.types.find(st->uri)==svc.types.end()) continue;
509 for(xrd::uris_t::const_iterator iu=svc.uris.begin();iu!=svc.uris.end();++iu) { 519 for(xrd::uris_t::const_iterator iu=svc.uris.begin();iu!=svc.uris.end();++iu) {
510 ep.uri = iu->second; 520 ep.uri = iu->second.uri;
521 if(id.xri_identity) {
522 if(iu->second.append=="qxri") {
523 ep.uri += id.normalized_id;
524 } /* TODO: else handle other append attribute values */
525 }
511 if(st->forceid) { 526 if(st->forceid) {
512 ep.local_id = ep.claimed_id = st->forceid; 527 ep.local_id = ep.claimed_id = st->forceid;
513 *(oi++) = ep; 528 *(oi++) = ep;
514 }else{ 529 }else{
515 if(svc.local_ids.empty()) { 530 if(svc.local_ids.empty()) {
516 ep.local_id = ep.claimed_id; 531 ep.local_id = ep.claimed_id;
517 *(oi++) = ep; 532 *(oi++) = ep;
518 }else{ 533 }else{
519 for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin(); 534 for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin();
520 ilid!=svc.local_ids.end(); ++ilid) { 535 ilid!=svc.local_ids.end(); ++ilid) {
521 ep.local_id = ilid->second; 536 ep.local_id = ilid->second;
522 *(oi++) = ep; 537 *(oi++) = ep;
523 } 538 }
524 } 539 }
525 } 540 }
526 } 541 }
527 } 542 }
528 } 543 }
529 544
530 }; 545 };
531 546
532 string idiscover(endpoint_discovery_iterator oi,const string& identity) { 547 string idiscover(endpoint_discovery_iterator oi,const string& identity) {
533 idigger_t idigger; 548 idigger_t idigger;
534 return idigger.discover(oi,identity); 549 return idigger.discover(oi,identity);
535 } 550 }
536 551
537} 552}