summaryrefslogtreecommitdiffabout
path: root/lib/discovery.cc
Unidiff
Diffstat (limited to 'lib/discovery.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/discovery.cc10
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/discovery.cc b/lib/discovery.cc
index ea53786..3b184ee 100644
--- a/lib/discovery.cc
+++ b/lib/discovery.cc
@@ -1,50 +1,48 @@
1#include <iostream>
2using namespace std;
3#include <list> 1#include <list>
4#include <opkele/curl.h> 2#include <opkele/curl.h>
5#include <opkele/expat.h> 3#include <opkele/expat.h>
6#include <opkele/uris.h> 4#include <opkele/uris.h>
7#include <opkele/discovery.h> 5#include <opkele/discovery.h>
8#include <opkele/exception.h> 6#include <opkele/exception.h>
9#include <opkele/util.h> 7#include <opkele/util.h>
10 8
11#include "config.h" 9#include "config.h"
12 10
13#define XRDS_HEADER "X-XRDS-Location" 11#define XRDS_HEADER "X-XRDS-Location"
14#define CT_HEADER "Content-Type" 12#define CT_HEADER "Content-Type"
15 13
16namespace opkele { 14namespace opkele {
17 using std::list; 15 using std::list;
18 using xrd::XRD_t; 16 using xrd::XRD_t;
19 using xrd::service_t; 17 using xrd::service_t;
20 18
21 static const char *whitespace = " \t\r\n"; 19 static const char *whitespace = " \t\r\n";
22 static const char *i_leaders = "=@+$!("; 20 static const char *i_leaders = "=@+$!(";
23 21
24 static inline bool is_qelement(const XML_Char *n,const char *qen) { 22 static inline bool is_qelement(const XML_Char *n,const char *qen) {
25 return !strcasecmp(n,qen); 23 return !strcasecmp(n,qen);
26 } 24 }
27 static inline bool is_element(const XML_Char *n,const char *en) { 25 static inline bool is_element(const XML_Char *n,const char *en) {
28 if(!strcasecmp(n,en)) return true; 26 if(!strcasecmp(n,en)) return true;
29 int nl = strlen(n), enl = strlen(en); 27 int nl = strlen(n), enl = strlen(en);
30 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t' 28 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t'
31 && !strcasecmp(&n[nl-enl],en) ) 29 && !strcasecmp(&n[nl-enl],en) )
32 return true; 30 return true;
33 return false; 31 return false;
34 } 32 }
35 33
36 static long element_priority(const XML_Char **a) { 34 static long element_priority(const XML_Char **a) {
37 for(;*a;++a) 35 for(;*a;++a)
38 if(!strcasecmp(*(a++),"priority")) { 36 if(!strcasecmp(*(a++),"priority")) {
39 long rv; 37 long rv;
40 return (sscanf(*a,"%ld",&rv)==1)?rv:-1; 38 return (sscanf(*a,"%ld",&rv)==1)?rv:-1;
41 } 39 }
42 return -1; 40 return -1;
43 } 41 }
44 42
45 class idigger_t : public util::curl_t, public util::expat_t { 43 class idigger_t : public util::curl_t, public util::expat_t {
46 public: 44 public:
47 string xri_proxy; 45 string xri_proxy;
48 46
49 enum { 47 enum {
50 xmode_html = 1, xmode_xrd = 2 48 xmode_html = 1, xmode_xrd = 2
@@ -186,170 +184,176 @@ namespace opkele {
186 const char *space = (const char*)memchr(p,' ',bytes); 184 const char *space = (const char*)memchr(p,' ',bytes);
187 if(space && ( (!colon) || space<colon ) ) { 185 if(space && ( (!colon) || space<colon ) ) {
188 xrds_location.clear(); http_content_type.clear(); 186 xrds_location.clear(); http_content_type.clear();
189 }else if(colon) { 187 }else if(colon) {
190 const char *hv = ++colon; 188 const char *hv = ++colon;
191 int hnl = colon-h; 189 int hnl = colon-h;
192 int rb; 190 int rb;
193 for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb); 191 for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb);
194 while(rb>0 && isspace(hv[rb-1])) --rb; 192 while(rb>0 && isspace(hv[rb-1])) --rb;
195 if(rb) { 193 if(rb) {
196 if( (hnl>=sizeof(XRDS_HEADER)) 194 if( (hnl>=sizeof(XRDS_HEADER))
197 && !strncasecmp(h,XRDS_HEADER":", 195 && !strncasecmp(h,XRDS_HEADER":",
198 sizeof(XRDS_HEADER)) ) { 196 sizeof(XRDS_HEADER)) ) {
199 xrds_location.assign(hv,rb); 197 xrds_location.assign(hv,rb);
200 }else if( (hnl>=sizeof(CT_HEADER)) 198 }else if( (hnl>=sizeof(CT_HEADER))
201 && !strncasecmp(h,CT_HEADER":", 199 && !strncasecmp(h,CT_HEADER":",
202 sizeof(CT_HEADER)) ) { 200 sizeof(CT_HEADER)) ) {
203 const char *sc = (const char*)memchr( 201 const char *sc = (const char*)memchr(
204 hv,';',rb); 202 hv,';',rb);
205 http_content_type.assign(hv,sc?(sc-hv):rb); 203 http_content_type.assign(hv,sc?(sc-hv):rb);
206 } 204 }
207 } 205 }
208 } 206 }
209 return curl_t::header(p,s,nm); 207 return curl_t::header(p,s,nm);
210 } 208 }
211 209
212 void start_element(const XML_Char *n,const XML_Char **a) { 210 void start_element(const XML_Char *n,const XML_Char **a) {
213 if(skipping<0) return; 211 if(skipping<0) return;
214 if(skipping) { 212 if(skipping) {
215 if(xmode&xmode_html) 213 if(xmode&xmode_html)
216 html_start_element(n,a); 214 html_start_element(n,a);
217 ++skipping; return; 215 ++skipping; return;
218 } 216 }
219 if(pt_stack.empty()) { 217 if(pt_stack.empty()) {
220 if(is_qelement(n,NSURI_XRDS "\tXRDS")) 218 if(is_qelement(n,NSURI_XRDS "\tXRDS"))
221 return; 219 return;
222 if(is_qelement(n,NSURI_XRD "\tXRD")) { 220 if(is_qelement(n,NSURI_XRD "\tXRD")) {
223 assert(xrd); 221 assert(xrd);
224 xrd->clear(); 222 xrd->clear();
225 pt_stack.push_back(n); 223 pt_stack.push_back(n);
226 }else if(xmode&xmode_html) { 224 }else if(xmode&xmode_html) {
227 html_start_element(n,a); 225 html_start_element(n,a);
228 }else{ 226 }else{
229 skipping = -1; 227 skipping = -1;
230 } 228 }
231 }else{ 229 }else{
232 int pt_s = pt_stack.size(); 230 int pt_s = pt_stack.size();
233 if(pt_s==1) { 231 if(pt_s==1) {
234 /* TODO: xrd:XRD/xrd:Expires */
235 if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { 232 if(is_qelement(n,NSURI_XRD "\tCanonicalID")) {
236 assert(xrd); 233 assert(xrd);
237 cdata = &(xrd->canonical_ids.add(element_priority(a),string())); 234 cdata = &(xrd->canonical_ids.add(element_priority(a),string()));
238 }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { 235 }else if(is_qelement(n,NSURI_XRD "\tLocalID")) {
239 assert(xrd); 236 assert(xrd);
240 cdata = &(xrd->local_ids.add(element_priority(a),string())); 237 cdata = &(xrd->local_ids.add(element_priority(a),string()));
241 }else if(is_qelement(n,NSURI_XRD "\tService")) { 238 }else if(is_qelement(n,NSURI_XRD "\tService")) {
242 assert(xrd); 239 assert(xrd);
243 xrd_service = &(xrd->services.add(element_priority(a), 240 xrd_service = &(xrd->services.add(element_priority(a),
244 service_t())); 241 service_t()));
245 pt_stack.push_back(n); 242 pt_stack.push_back(n);
246 }else if(is_qelement(n,NSURI_XRD "\tStatus")) { 243 }else if(is_qelement(n,NSURI_XRD "\tStatus")) {
247 for(;*a;) { 244 for(;*a;) {
248 if(!strcasecmp(*(a++),"code")) { 245 if(!strcasecmp(*(a++),"code")) {
249 if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { 246 if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) {
250 cdata = &status_string; 247 cdata = &status_string;
251 pt_stack.push_back(n); 248 pt_stack.push_back(n);
252 break; 249 break;
253 } 250 }
254 } 251 }
255 } 252 }
253 }else if(is_qelement(n,NSURI_XRD "\tExpires")) {
254 assert(xrd);
255 cdata_buf.clear();
256 cdata = &cdata_buf;
256 }else if(xmode&xmode_html) { 257 }else if(xmode&xmode_html) {
257 html_start_element(n,a); 258 html_start_element(n,a);
258 }else{ 259 }else{
259 skipping = 1; 260 skipping = 1;
260 } 261 }
261 }else if(pt_s==2) { 262 }else if(pt_s==2) {
262 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { 263 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) {
263 if(is_qelement(n,NSURI_XRD "\tType")) { 264 if(is_qelement(n,NSURI_XRD "\tType")) {
264 assert(xrd); assert(xrd_service); 265 assert(xrd); assert(xrd_service);
265 cdata_buf.clear(); 266 cdata_buf.clear();
266 cdata = &cdata_buf; 267 cdata = &cdata_buf;
267 }else if(is_qelement(n,NSURI_XRD "\tURI")) { 268 }else if(is_qelement(n,NSURI_XRD "\tURI")) {
268 assert(xrd); assert(xrd_service); 269 assert(xrd); assert(xrd_service);
269 cdata = &(xrd_service->uris.add(element_priority(a),string())); 270 cdata = &(xrd_service->uris.add(element_priority(a),string()));
270 }else if(is_qelement(n,NSURI_XRD "\tLocalID") 271 }else if(is_qelement(n,NSURI_XRD "\tLocalID")
271 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { 272 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) {
272 assert(xrd); assert(xrd_service); 273 assert(xrd); assert(xrd_service);
273 cdata = &(xrd_service->local_ids.add(element_priority(a),string())); 274 cdata = &(xrd_service->local_ids.add(element_priority(a),string()));
274 }else{ 275 }else{
275 skipping = 1; 276 skipping = 1;
276 } 277 }
277 }else 278 }else
278 skipping = 1; 279 skipping = 1;
279 }else if(xmode&xmode_html) { 280 }else if(xmode&xmode_html) {
280 html_start_element(n,a); 281 html_start_element(n,a);
281 }else{ 282 }else{
282 skipping = 1; 283 skipping = 1;
283 } 284 }
284 } 285 }
285 } 286 }
286 void end_element(const XML_Char *n) { 287 void end_element(const XML_Char *n) {
287 if(skipping<0) return; 288 if(skipping<0) return;
288 if(skipping) { 289 if(skipping) {
289 --skipping; return; 290 --skipping; return;
290 } 291 }
291 if(is_qelement(n,NSURI_XRD "\tType")) { 292 if(is_qelement(n,NSURI_XRD "\tType")) {
292 assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); 293 assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf);
293 xrd_service->types.insert(cdata_buf); 294 xrd_service->types.insert(cdata_buf);
294 }else if(is_qelement(n,NSURI_XRD "\tService")) { 295 }else if(is_qelement(n,NSURI_XRD "\tService")) {
295 assert(xrd); assert(xrd_service); 296 assert(xrd); assert(xrd_service);
296 assert(!pt_stack.empty()); 297 assert(!pt_stack.empty());
297 assert(pt_stack.back()==(NSURI_XRD "\tService")); 298 assert(pt_stack.back()==(NSURI_XRD "\tService"));
298 pt_stack.pop_back(); 299 pt_stack.pop_back();
299 xrd_service = 0; 300 xrd_service = 0;
300 }else if(is_qelement(n,NSURI_XRD "\tStatus")) { 301 }else if(is_qelement(n,NSURI_XRD "\tStatus")) {
301 assert(xrd); 302 assert(xrd);
302 if(is_qelement(pt_stack.back().c_str(),n)) { 303 if(is_qelement(pt_stack.back().c_str(),n)) {
303 assert(cdata==&status_string); 304 assert(cdata==&status_string);
304 pt_stack.pop_back(); 305 pt_stack.pop_back();
305 if(status_code!=100) 306 if(status_code!=100)
306 skipping = -1; 307 skipping = -1;
307 } 308 }
309 }else if(is_qelement(n,NSURI_XRD "\tExpires")) {
310 assert(xrd);
311 xrd->expires = util::w3c_to_time(cdata_buf);
308 }else if((xmode&xmode_html) && is_element(n,"head")) { 312 }else if((xmode&xmode_html) && is_element(n,"head")) {
309 skipping = -1; 313 skipping = -1;
310 } 314 }
311 cdata = 0; 315 cdata = 0;
312 } 316 }
313 void character_data(const XML_Char *s,int l) { 317 void character_data(const XML_Char *s,int l) {
314 if(skipping) return; 318 if(skipping) return;
315 if(cdata) cdata->append(s,l); 319 if(cdata) cdata->append(s,l);
316 } 320 }
317 321
318 void html_start_element(const XML_Char *n,const XML_Char **a) { 322 void html_start_element(const XML_Char *n,const XML_Char **a) {
319 if(is_element(n,"meta")) { 323 if(is_element(n,"meta")) {
320 bool heq = false; 324 bool heq = false;
321 string l; 325 string l;
322 for(;*a;a+=2) { 326 for(;*a;a+=2) {
323 if(!( strcasecmp(a[0],"http-equiv") 327 if(!( strcasecmp(a[0],"http-equiv")
324 || strcasecmp(a[1],XRDS_HEADER) )) 328 || strcasecmp(a[1],XRDS_HEADER) ))
325 heq = true; 329 heq = true;
326 else if(!strcasecmp(a[0],"content")) 330 else if(!strcasecmp(a[0],"content"))
327 l.assign(a[1]); 331 l.assign(a[1]);
328 } 332 }
329 if(heq) 333 if(heq)
330 xrds_location = l; 334 xrds_location = l;
331 }else if(is_element(n,"link")) { 335 }else if(is_element(n,"link")) {
332 string rels; 336 string rels;
333 string href; 337 string href;
334 for(;*a;a+=2) { 338 for(;*a;a+=2) {
335 if( !strcasecmp(a[0],"rel") ) { 339 if( !strcasecmp(a[0],"rel") ) {
336 rels.assign(a[1]); 340 rels.assign(a[1]);
337 }else if( !strcasecmp(a[0],"href") ) { 341 }else if( !strcasecmp(a[0],"href") ) {
338 const char *ns = a[1]; 342 const char *ns = a[1];
339 for(;*ns && isspace(*ns);++ns); 343 for(;*ns && isspace(*ns);++ns);
340 href.assign(ns); 344 href.assign(ns);
341 string::size_type lns=href.find_last_not_of(whitespace); 345 string::size_type lns=href.find_last_not_of(whitespace);
342 href.erase(lns+1); 346 href.erase(lns+1);
343 } 347 }
344 } 348 }
345 for(string::size_type ns=rels.find_first_not_of(whitespace); 349 for(string::size_type ns=rels.find_first_not_of(whitespace);
346 ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) { 350 ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) {
347 string::size_type s = rels.find_first_of(whitespace,ns); 351 string::size_type s = rels.find_first_of(whitespace,ns);
348 string rel; 352 string rel;
349 if(s==string::npos) { 353 if(s==string::npos) {
350 rel.assign(rels,ns,string::npos); 354 rel.assign(rels,ns,string::npos);
351 ns = string::npos; 355 ns = string::npos;
352 }else{ 356 }else{
353 rel.assign(rels,ns,s-ns); 357 rel.assign(rels,ns,s-ns);
354 ns = s; 358 ns = s;
355 } 359 }