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