summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/expat.h6
-rw-r--r--lib/discovery.cc11
2 files changed, 7 insertions, 10 deletions
diff --git a/include/opkele/expat.h b/include/opkele/expat.h
index 97ed61a..60c41ac 100644
--- a/include/opkele/expat.h
+++ b/include/opkele/expat.h
@@ -1,93 +1,91 @@
1#ifndef __OPKELE_EXPAT_H 1#ifndef __OPKELE_EXPAT_H
2#define __OPKELE_EXPAT_H 2#define __OPKELE_EXPAT_H
3 3
4#include <cassert> 4#include <cassert>
5#include <expat.h> 5#include <expat.h>
6 6
7namespace opkele { 7namespace opkele {
8 8
9 namespace util { 9 namespace util {
10 10
11 class expat_t { 11 class expat_t {
12 public: 12 public:
13 XML_Parser _x; 13 XML_Parser _x;
14 14
15 expat_t() : _x(0) { } 15 expat_t() : _x(0) { }
16 expat_t(XML_Parser x) : _x(x) { } 16 expat_t(XML_Parser x) : _x(x) { }
17 virtual ~expat_t() throw(); 17 virtual ~expat_t() throw();
18 18
19 expat_t& operator=(XML_Parser x); 19 expat_t& operator=(XML_Parser x);
20 20
21 operator const XML_Parser(void) const { return _x; } 21 operator const XML_Parser(void) const { return _x; }
22 operator XML_Parser(void) { return _x; } 22 operator XML_Parser(void) { return _x; }
23 23
24 inline bool parse(const char *s,int len,bool final=false) { 24 inline bool parse(const char *s,int len,bool final=false) {
25 assert(_x); 25 assert(_x);
26 return XML_Parse(_x,s,len,final); } 26 return XML_Parse(_x,s,len,final);
27 enum XML_Status stop_parser(bool resumable=false) { 27 }
28 assert(_x);
29 return XML_StopParser(_x,resumable); }
30 28
31 virtual void start_element(const XML_Char *n,const XML_Char **a) { } 29 virtual void start_element(const XML_Char *n,const XML_Char **a) { }
32 virtual void end_element(const XML_Char *n) { } 30 virtual void end_element(const XML_Char *n) { }
33 void set_element_handler(); 31 void set_element_handler();
34 32
35 virtual void character_data(const XML_Char *s,int l) { } 33 virtual void character_data(const XML_Char *s,int l) { }
36 void set_character_data_handler(); 34 void set_character_data_handler();
37 35
38 virtual void processing_instruction(const XML_Char *t,const XML_Char *d) { } 36 virtual void processing_instruction(const XML_Char *t,const XML_Char *d) { }
39 void set_processing_instruction_handler(); 37 void set_processing_instruction_handler();
40 38
41 virtual void comment(const XML_Char *d) { } 39 virtual void comment(const XML_Char *d) { }
42 void set_comment_handler(); 40 void set_comment_handler();
43 41
44 virtual void start_cdata_section() { } 42 virtual void start_cdata_section() { }
45 virtual void end_cdata_section() { } 43 virtual void end_cdata_section() { }
46 void set_cdata_section_handler(); 44 void set_cdata_section_handler();
47 45
48 virtual void default_handler(const XML_Char *s,int l) { } 46 virtual void default_handler(const XML_Char *s,int l) { }
49 void set_default_handler(); 47 void set_default_handler();
50 void set_default_handler_expand(); 48 void set_default_handler_expand();
51 49
52 virtual void start_namespace_decl(const XML_Char *p,const XML_Char *u) { } 50 virtual void start_namespace_decl(const XML_Char *p,const XML_Char *u) { }
53 virtual void end_namespace_decl(const XML_Char *p) { } 51 virtual void end_namespace_decl(const XML_Char *p) { }
54 void set_namespace_decl_handler(); 52 void set_namespace_decl_handler();
55 53
56 inline enum XML_Error get_error_code() { 54 inline enum XML_Error get_error_code() {
57 assert(_x); return XML_GetErrorCode(_x); } 55 assert(_x); return XML_GetErrorCode(_x); }
58 static inline const XML_LChar *error_string(XML_Error c) { 56 static inline const XML_LChar *error_string(XML_Error c) {
59 return XML_ErrorString(c); } 57 return XML_ErrorString(c); }
60 58
61 inline long get_current_byte_index() { 59 inline long get_current_byte_index() {
62 assert(_x); return XML_GetCurrentByteIndex(_x); } 60 assert(_x); return XML_GetCurrentByteIndex(_x); }
63 inline int get_current_line_number() { 61 inline int get_current_line_number() {
64 assert(_x); return XML_GetCurrentLineNumber(_x); } 62 assert(_x); return XML_GetCurrentLineNumber(_x); }
65 inline int get_current_column_number() { 63 inline int get_current_column_number() {
66 assert(_x); return XML_GetCurrentColumnNumber(_x); } 64 assert(_x); return XML_GetCurrentColumnNumber(_x); }
67 65
68 inline void set_user_data() { 66 inline void set_user_data() {
69 assert(_x); XML_SetUserData(_x,this); } 67 assert(_x); XML_SetUserData(_x,this); }
70 68
71 inline bool set_base(const XML_Char *b) { 69 inline bool set_base(const XML_Char *b) {
72 assert(_x); return XML_SetBase(_x,b); } 70 assert(_x); return XML_SetBase(_x,b); }
73 inline const XML_Char *get_base() { 71 inline const XML_Char *get_base() {
74 assert(_x); return XML_GetBase(_x); } 72 assert(_x); return XML_GetBase(_x); }
75 73
76 inline int get_specified_attribute_count() { 74 inline int get_specified_attribute_count() {
77 assert(_x); return XML_GetSpecifiedAttributeCount(_x); } 75 assert(_x); return XML_GetSpecifiedAttributeCount(_x); }
78 76
79 inline bool set_param_entity_parsing(enum XML_ParamEntityParsing c) { 77 inline bool set_param_entity_parsing(enum XML_ParamEntityParsing c) {
80 assert(_x); return XML_SetParamEntityParsing(_x,c); } 78 assert(_x); return XML_SetParamEntityParsing(_x,c); }
81 79
82 inline static XML_Parser parser_create(const XML_Char *e=0) { 80 inline static XML_Parser parser_create(const XML_Char *e=0) {
83 return XML_ParserCreate(e); } 81 return XML_ParserCreate(e); }
84 inline static XML_Parser parser_create_ns(const XML_Char *e=0,XML_Char s='\t') { 82 inline static XML_Parser parser_create_ns(const XML_Char *e=0,XML_Char s='\t') {
85 return XML_ParserCreateNS(e,s); } 83 return XML_ParserCreateNS(e,s); }
86 84
87 }; 85 };
88 86
89 } 87 }
90 88
91} 89}
92 90
93#endif /* __OPKELE_EXPAT_H */ 91#endif /* __OPKELE_EXPAT_H */
diff --git a/lib/discovery.cc b/lib/discovery.cc
index af9686a..d868308 100644
--- a/lib/discovery.cc
+++ b/lib/discovery.cc
@@ -1,447 +1,446 @@
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 static const char *whitespace = " \t\r\n"; 21 static const char *whitespace = " \t\r\n";
22 static const char *i_leaders = "=@+$!("; 22 static const char *i_leaders = "=@+$!(";
23 static const size_t max_html = 16384; 23 static const size_t max_html = 16384;
24 24
25 static inline bool is_qelement(const XML_Char *n,const char *qen) { 25 static inline bool is_qelement(const XML_Char *n,const char *qen) {
26 return !strcasecmp(n,qen); 26 return !strcasecmp(n,qen);
27 } 27 }
28 static inline bool is_element(const XML_Char *n,const char *en) { 28 static inline bool is_element(const XML_Char *n,const char *en) {
29 if(!strcasecmp(n,en)) return true; 29 if(!strcasecmp(n,en)) return true;
30 int nl = strlen(n), enl = strlen(en); 30 int nl = strlen(n), enl = strlen(en);
31 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t' 31 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t'
32 && !strcasecmp(&n[nl-enl],en) ) 32 && !strcasecmp(&n[nl-enl],en) )
33 return true; 33 return true;
34 return false; 34 return false;
35 } 35 }
36 36
37 static long element_priority(const XML_Char **a) { 37 static long element_priority(const XML_Char **a) {
38 for(;*a;++a) 38 for(;*a;++a)
39 if(!strcasecmp(*(a++),"priority")) { 39 if(!strcasecmp(*(a++),"priority")) {
40 long rv; 40 long rv;
41 return (sscanf(*a,"%ld",&rv)==1)?rv:-1; 41 return (sscanf(*a,"%ld",&rv)==1)?rv:-1;
42 } 42 }
43 return -1; 43 return -1;
44 } 44 }
45 45
46 class idigger_t : public util::curl_t, public util::expat_t { 46 class idigger_t : public util::curl_t, public util::expat_t {
47 public: 47 public:
48 string xri_proxy; 48 string xri_proxy;
49 49
50 enum { 50 enum {
51 xmode_html = 1, xmode_xrd = 2 51 xmode_html = 1, xmode_xrd = 2
52 }; 52 };
53 int xmode; 53 int xmode;
54 54
55 string xrds_location; 55 string xrds_location;
56 string http_content_type; 56 string http_content_type;
57 service_t html_openid1; 57 service_t html_openid1;
58 service_t html_openid2; 58 service_t html_openid2;
59 string cdata_buf; 59 string cdata_buf;
60 long status_code; 60 long status_code;
61 string status_string; 61 string status_string;
62 62
63 typedef list<string> pt_stack_t; 63 typedef list<string> pt_stack_t;
64 pt_stack_t pt_stack; 64 pt_stack_t pt_stack;
65 int skipping; 65 int skipping;
66 bool parser_choked; 66 bool parser_choked;
67 string save_html; 67 string save_html;
68 68
69 XRD_t *xrd; 69 XRD_t *xrd;
70 service_t *xrd_service; 70 service_t *xrd_service;
71 string* cdata; 71 string* cdata;
72 72
73 idigger_t() 73 idigger_t()
74 : util::curl_t(easy_init()), 74 : util::curl_t(easy_init()),
75 util::expat_t(0), 75 util::expat_t(0),
76 xri_proxy(XRI_PROXY_URL) { 76 xri_proxy(XRI_PROXY_URL) {
77 CURLcode r; 77 CURLcode r;
78 (r=misc_sets()) 78 (r=misc_sets())
79 || (r=set_write()) 79 || (r=set_write())
80 || (r=set_header()) 80 || (r=set_header())
81 ; 81 ;
82 if(r) 82 if(r)
83 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 83 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
84 } 84 }
85 ~idigger_t() throw() { } 85 ~idigger_t() throw() { }
86 86
87 void discover(idiscovery_t& result,const string& identity) { 87 void discover(idiscovery_t& result,const string& identity) {
88 result.clear(); 88 result.clear();
89 string::size_type fsc = identity.find_first_not_of(whitespace); 89 string::size_type fsc = identity.find_first_not_of(whitespace);
90 if(fsc==string::npos) 90 if(fsc==string::npos)
91 throw bad_input(OPKELE_CP_ "whtiespace-only identity"); 91 throw bad_input(OPKELE_CP_ "whtiespace-only identity");
92 string::size_type lsc = identity.find_last_not_of(whitespace); 92 string::size_type lsc = identity.find_last_not_of(whitespace);
93 assert(lsc!=string::npos); 93 assert(lsc!=string::npos);
94 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) 94 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1))
95 fsc += sizeof("xri://")-1; 95 fsc += sizeof("xri://")-1;
96 if((fsc+1)>=lsc) 96 if((fsc+1)>=lsc)
97 throw bad_input(OPKELE_CP_ "not a character of importance in identity"); 97 throw bad_input(OPKELE_CP_ "not a character of importance in identity");
98 string id(identity,fsc,lsc-fsc+1); 98 string id(identity,fsc,lsc-fsc+1);
99 if(strchr(i_leaders,id[0])) { 99 if(strchr(i_leaders,id[0])) {
100 result.normalized_id = id; 100 result.normalized_id = id;
101 result.xri_identity = true; 101 result.xri_identity = true;
102 /* TODO: further canonicalize xri identity? Like folding case or whatever... */ 102 /* TODO: further canonicalize xri identity? Like folding case or whatever... */
103 discover_at( 103 discover_at(
104 result, 104 result,
105 xri_proxy + util::url_encode(id)+ 105 xri_proxy + util::url_encode(id)+
106 "?_xrd_r=application/xrd+xml;sep=false", xmode_xrd); 106 "?_xrd_r=application/xrd+xml;sep=false", xmode_xrd);
107 if(status_code!=100) 107 if(status_code!=100)
108 throw failed_xri_resolution(OPKELE_CP_ 108 throw failed_xri_resolution(OPKELE_CP_
109 "XRI resolution failed with '"+status_string+"' message",status_code); 109 "XRI resolution failed with '"+status_string+"' message",status_code);
110 if(result.xrd.canonical_ids.empty()) 110 if(result.xrd.canonical_ids.empty())
111 throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found"); 111 throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found");
112 result.canonicalized_id = result.xrd.canonical_ids.begin()->second; 112 result.canonicalized_id = result.xrd.canonical_ids.begin()->second;
113 }else{ 113 }else{
114 result.xri_identity = false; 114 result.xri_identity = false;
115 if(id.find("://")==string::npos) 115 if(id.find("://")==string::npos)
116 id.insert(0,"http://"); 116 id.insert(0,"http://");
117 string::size_type fp = id.find('#'); 117 string::size_type fp = id.find('#');
118 if(fp!=string::npos) { 118 if(fp!=string::npos) {
119 string::size_type qp = id.find('?'); 119 string::size_type qp = id.find('?');
120 if(qp==string::npos || qp<fp) 120 if(qp==string::npos || qp<fp)
121 id.erase(fp); 121 id.erase(fp);
122 else if(qp>fp) 122 else if(qp>fp)
123 id.erase(fp,qp-fp); 123 id.erase(fp,qp-fp);
124 } 124 }
125 result.normalized_id = util::rfc_3986_normalize_uri(id); 125 result.normalized_id = util::rfc_3986_normalize_uri(id);
126 discover_at(result,id,xmode_html|xmode_xrd); 126 discover_at(result,id,xmode_html|xmode_xrd);
127 const char * eu = 0; 127 const char * eu = 0;
128 CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); 128 CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu);
129 if(r) 129 if(r)
130 throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); 130 throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r);
131 result.canonicalized_id = util::rfc_3986_normalize_uri(eu); /* XXX: strip fragment part? */ 131 result.canonicalized_id = util::rfc_3986_normalize_uri(eu); /* XXX: strip fragment part? */
132 if(xrds_location.empty()) { 132 if(xrds_location.empty()) {
133 html2xrd(result.xrd); 133 html2xrd(result.xrd);
134 }else{ 134 }else{
135 discover_at(result,xrds_location,xmode_xrd); 135 discover_at(result,xrds_location,xmode_xrd);
136 if(result.xrd.empty()) 136 if(result.xrd.empty())
137 html2xrd(result.xrd); 137 html2xrd(result.xrd);
138 } 138 }
139 } 139 }
140 } 140 }
141 141
142 void discover_at(idiscovery_t& result,const string& url,int xm) { 142 void discover_at(idiscovery_t& result,const string& url,int xm) {
143 CURLcode r = easy_setopt(CURLOPT_URL,url.c_str()); 143 CURLcode r = easy_setopt(CURLOPT_URL,url.c_str());
144 if(r) 144 if(r)
145 throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r); 145 throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r);
146 146
147 http_content_type.clear(); 147 http_content_type.clear();
148 xmode = xm; 148 xmode = xm;
149 prepare_to_parse(); 149 prepare_to_parse();
150 if(xmode&xmode_html) { 150 if(xmode&xmode_html) {
151 xrds_location.clear(); 151 xrds_location.clear();
152 save_html.clear(); 152 save_html.clear();
153 save_html.reserve(max_html); 153 save_html.reserve(max_html);
154 } 154 }
155 xrd = &result.xrd; 155 xrd = &result.xrd;
156 156
157 r = easy_perform(); 157 r = easy_perform();
158 if(r && r!=CURLE_WRITE_ERROR) 158 if(r && r!=CURLE_WRITE_ERROR)
159 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 159 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
160 160
161 if(!parser_choked) { 161 if(!parser_choked) {
162 parse(0,0,true); 162 parse(0,0,true);
163 }else{ 163 }else{
164 /* TODO: do not bother if we've seen xml */ 164 /* TODO: do not bother if we've seen xml */
165 try { 165 try {
166 util::tidy_doc_t td = util::tidy_doc_t::create(); 166 util::tidy_doc_t td = util::tidy_doc_t::create();
167 if(!td) 167 if(!td)
168 throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document"); 168 throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document");
169#ifndef NDEBUG 169#ifndef NDEBUG
170 td.opt_set(TidyQuiet,false); 170 td.opt_set(TidyQuiet,false);
171 td.opt_set(TidyShowWarnings,false); 171 td.opt_set(TidyShowWarnings,false);
172#endif /* NDEBUG */ 172#endif /* NDEBUG */
173 td.opt_set(TidyForceOutput,true); 173 td.opt_set(TidyForceOutput,true);
174 td.opt_set(TidyXhtmlOut,true); 174 td.opt_set(TidyXhtmlOut,true);
175 td.opt_set(TidyDoctypeMode,TidyDoctypeOmit); 175 td.opt_set(TidyDoctypeMode,TidyDoctypeOmit);
176 td.opt_set(TidyMark,false); 176 td.opt_set(TidyMark,false);
177 if(td.parse_string(save_html)<=0) 177 if(td.parse_string(save_html)<=0)
178 throw exception_tidy(OPKELE_CP_ "tidy failed to parse document"); 178 throw exception_tidy(OPKELE_CP_ "tidy failed to parse document");
179 if(td.clean_and_repair()<=0) 179 if(td.clean_and_repair()<=0)
180 throw exception_tidy(OPKELE_CP_ "tidy failed to clean and repair"); 180 throw exception_tidy(OPKELE_CP_ "tidy failed to clean and repair");
181 util::tidy_buf_t tide; 181 util::tidy_buf_t tide;
182 if(td.save_buffer(tide)<=0) 182 if(td.save_buffer(tide)<=0)
183 throw exception_tidy(OPKELE_CP_ "tidy failed to save buffer"); 183 throw exception_tidy(OPKELE_CP_ "tidy failed to save buffer");
184 prepare_to_parse(); 184 prepare_to_parse();
185 parse(tide.c_str(),tide.size(),true); 185 parse(tide.c_str(),tide.size(),true);
186 }catch(exception_tidy& et) { } 186 }catch(exception_tidy& et) { }
187 } 187 }
188 save_html.clear(); 188 save_html.clear();
189 } 189 }
190 190
191 void prepare_to_parse() { 191 void prepare_to_parse() {
192 (*(expat_t*)this) = parser_create_ns(); 192 (*(expat_t*)this) = parser_create_ns();
193 set_user_data(); set_element_handler(); 193 set_user_data(); set_element_handler();
194 set_character_data_handler(); 194 set_character_data_handler();
195 195
196 if(xmode&xmode_html) { 196 if(xmode&xmode_html) {
197 html_openid1.clear(); html_openid2.clear(); 197 html_openid1.clear(); html_openid2.clear();
198 parser_choked = false; 198 parser_choked = false;
199 } 199 }
200 200
201 cdata = 0; xrd_service = 0; skipping = 0; 201 cdata = 0; xrd_service = 0; skipping = 0;
202 status_code = 100; status_string.clear(); 202 status_code = 100; status_string.clear();
203 } 203 }
204 204
205 void html2xrd(XRD_t& x) { 205 void html2xrd(XRD_t& x) {
206 if(!html_openid1.uris.empty()) { 206 if(!html_openid1.uris.empty()) {
207 html_openid1.types.insert(STURI_OPENID11); 207 html_openid1.types.insert(STURI_OPENID11);
208 x.services.add(-1,html_openid1); 208 x.services.add(-1,html_openid1);
209 } 209 }
210 if(!html_openid2.uris.empty()) { 210 if(!html_openid2.uris.empty()) {
211 html_openid2.types.insert(STURI_OPENID20); 211 html_openid2.types.insert(STURI_OPENID20);
212 x.services.add(-1,html_openid2); 212 x.services.add(-1,html_openid2);
213 } 213 }
214 } 214 }
215 215
216 size_t write(void *p,size_t s,size_t nm) { 216 size_t write(void *p,size_t s,size_t nm) {
217 /* TODO: limit total size */ 217 /* TODO: limit total size */
218 size_t bytes = s*nm; 218 size_t bytes = s*nm;
219 const char *inbuf = (const char*)p; 219 const char *inbuf = (const char*)p;
220 if(xmode&xmode_html) { 220 if(xmode&xmode_html) {
221 size_t mbts = save_html.capacity()-save_html.size(); 221 size_t mbts = save_html.capacity()-save_html.size();
222 size_t bts = 0; 222 size_t bts = 0;
223 if(mbts>0) { 223 if(mbts>0) {
224 bts = (bytes>mbts)?mbts:bytes; 224 bts = (bytes>mbts)?mbts:bytes;
225 save_html.append(inbuf,bts); 225 save_html.append(inbuf,bts);
226 } 226 }
227 if(skipping<0) return bts; 227 if(skipping<0) return bts;
228 } 228 }
229 if(skipping<0) return 0; 229 if(skipping<0) return 0;
230 bool rp = parse(inbuf,bytes,false); 230 bool rp = parse(inbuf,bytes,false);
231 if(!rp) { 231 if(!rp) {
232 parser_choked = true; 232 parser_choked = true;
233 skipping = -1; 233 skipping = -1;
234 if(!(xmode&xmode_html)) 234 if(!(xmode&xmode_html))
235 bytes = 0; 235 bytes = 0;
236 } 236 }
237 return bytes; 237 return bytes;
238 } 238 }
239 size_t header(void *p,size_t s,size_t nm) { 239 size_t header(void *p,size_t s,size_t nm) {
240 size_t bytes = s*nm; 240 size_t bytes = s*nm;
241 const char *h = (const char*)p; 241 const char *h = (const char*)p;
242 const char *colon = (const char*)memchr(p,':',bytes); 242 const char *colon = (const char*)memchr(p,':',bytes);
243 const char *space = (const char*)memchr(p,' ',bytes); 243 const char *space = (const char*)memchr(p,' ',bytes);
244 if(space && ( (!colon) || space<colon ) ) { 244 if(space && ( (!colon) || space<colon ) ) {
245 xrds_location.clear(); http_content_type.clear(); 245 xrds_location.clear(); http_content_type.clear();
246 }else if(colon) { 246 }else if(colon) {
247 const char *hv = ++colon; 247 const char *hv = ++colon;
248 int hnl = colon-h; 248 int hnl = colon-h;
249 int rb; 249 int rb;
250 for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb); 250 for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb);
251 while(rb>0 && isspace(hv[rb-1])) --rb; 251 while(rb>0 && isspace(hv[rb-1])) --rb;
252 if(rb) { 252 if(rb) {
253 if( (hnl>=sizeof(XRDS_HEADER)) 253 if( (hnl>=sizeof(XRDS_HEADER))
254 && !strncasecmp(h,XRDS_HEADER":", 254 && !strncasecmp(h,XRDS_HEADER":",
255 sizeof(XRDS_HEADER)) ) { 255 sizeof(XRDS_HEADER)) ) {
256 xrds_location.assign(hv,rb); 256 xrds_location.assign(hv,rb);
257 }else if( (hnl>=sizeof(CT_HEADER)) 257 }else if( (hnl>=sizeof(CT_HEADER))
258 && !strncasecmp(h,CT_HEADER":", 258 && !strncasecmp(h,CT_HEADER":",
259 sizeof(CT_HEADER)) ) { 259 sizeof(CT_HEADER)) ) {
260 const char *sc = (const char*)memchr( 260 const char *sc = (const char*)memchr(
261 hv,';',rb); 261 hv,';',rb);
262 http_content_type.assign(hv,sc?(sc-hv):rb); 262 http_content_type.assign(hv,sc?(sc-hv):rb);
263 } 263 }
264 } 264 }
265 } 265 }
266 return curl_t::header(p,s,nm); 266 return curl_t::header(p,s,nm);
267 } 267 }
268 268
269 void start_element(const XML_Char *n,const XML_Char **a) { 269 void start_element(const XML_Char *n,const XML_Char **a) {
270 if(skipping<0) return; 270 if(skipping<0) return;
271 if(skipping) { 271 if(skipping) {
272 if(xmode&xmode_html) 272 if(xmode&xmode_html)
273 html_start_element(n,a); 273 html_start_element(n,a);
274 ++skipping; return; 274 ++skipping; return;
275 } 275 }
276 if(pt_stack.empty()) { 276 if(pt_stack.empty()) {
277 if(is_qelement(n,NSURI_XRDS "\tXRDS")) 277 if(is_qelement(n,NSURI_XRDS "\tXRDS"))
278 return; 278 return;
279 if(is_qelement(n,NSURI_XRD "\tXRD")) { 279 if(is_qelement(n,NSURI_XRD "\tXRD")) {
280 assert(xrd); 280 assert(xrd);
281 xrd->clear(); 281 xrd->clear();
282 pt_stack.push_back(n); 282 pt_stack.push_back(n);
283 }else if(xmode&xmode_html) { 283 }else if(xmode&xmode_html) {
284 html_start_element(n,a); 284 html_start_element(n,a);
285 }else{ 285 }else{
286 skipping = -1; stop_parser(); 286 skipping = -1;
287 } 287 }
288 }else{ 288 }else{
289 int pt_s = pt_stack.size(); 289 int pt_s = pt_stack.size();
290 if(pt_s==1) { 290 if(pt_s==1) {
291 if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { 291 if(is_qelement(n,NSURI_XRD "\tCanonicalID")) {
292 assert(xrd); 292 assert(xrd);
293 cdata = &(xrd->canonical_ids.add(element_priority(a),string())); 293 cdata = &(xrd->canonical_ids.add(element_priority(a),string()));
294 }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { 294 }else if(is_qelement(n,NSURI_XRD "\tLocalID")) {
295 assert(xrd); 295 assert(xrd);
296 cdata = &(xrd->local_ids.add(element_priority(a),string())); 296 cdata = &(xrd->local_ids.add(element_priority(a),string()));
297 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { 297 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) {
298 assert(xrd); 298 assert(xrd);
299 cdata = &(xrd->provider_id); 299 cdata = &(xrd->provider_id);
300 }else if(is_qelement(n,NSURI_XRD "\tService")) { 300 }else if(is_qelement(n,NSURI_XRD "\tService")) {
301 assert(xrd); 301 assert(xrd);
302 xrd_service = &(xrd->services.add(element_priority(a), 302 xrd_service = &(xrd->services.add(element_priority(a),
303 service_t())); 303 service_t()));
304 pt_stack.push_back(n); 304 pt_stack.push_back(n);
305 }else if(is_qelement(n,NSURI_XRD "\tStatus")) { 305 }else if(is_qelement(n,NSURI_XRD "\tStatus")) {
306 for(;*a;) { 306 for(;*a;) {
307 if(!strcasecmp(*(a++),"code")) { 307 if(!strcasecmp(*(a++),"code")) {
308 if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { 308 if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) {
309 cdata = &status_string; 309 cdata = &status_string;
310 pt_stack.push_back(n); 310 pt_stack.push_back(n);
311 break; 311 break;
312 } 312 }
313 } 313 }
314 } 314 }
315 }else if(is_qelement(n,NSURI_XRD "\tExpires")) { 315 }else if(is_qelement(n,NSURI_XRD "\tExpires")) {
316 assert(xrd); 316 assert(xrd);
317 cdata_buf.clear(); 317 cdata_buf.clear();
318 cdata = &cdata_buf; 318 cdata = &cdata_buf;
319 }else if(xmode&xmode_html) { 319 }else if(xmode&xmode_html) {
320 html_start_element(n,a); 320 html_start_element(n,a);
321 }else{ 321 }else{
322 skipping = 1; 322 skipping = 1;
323 } 323 }
324 }else if(pt_s==2) { 324 }else if(pt_s==2) {
325 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { 325 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) {
326 if(is_qelement(n,NSURI_XRD "\tType")) { 326 if(is_qelement(n,NSURI_XRD "\tType")) {
327 assert(xrd); assert(xrd_service); 327 assert(xrd); assert(xrd_service);
328 cdata_buf.clear(); 328 cdata_buf.clear();
329 cdata = &cdata_buf; 329 cdata = &cdata_buf;
330 }else if(is_qelement(n,NSURI_XRD "\tURI")) { 330 }else if(is_qelement(n,NSURI_XRD "\tURI")) {
331 assert(xrd); assert(xrd_service); 331 assert(xrd); assert(xrd_service);
332 cdata = &(xrd_service->uris.add(element_priority(a),string())); 332 cdata = &(xrd_service->uris.add(element_priority(a),string()));
333 }else if(is_qelement(n,NSURI_XRD "\tLocalID") 333 }else if(is_qelement(n,NSURI_XRD "\tLocalID")
334 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { 334 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) {
335 assert(xrd); assert(xrd_service); 335 assert(xrd); assert(xrd_service);
336 cdata = &(xrd_service->local_ids.add(element_priority(a),string())); 336 cdata = &(xrd_service->local_ids.add(element_priority(a),string()));
337 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { 337 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) {
338 assert(xrd); assert(xrd_service); 338 assert(xrd); assert(xrd_service);
339 cdata = &(xrd_service->provider_id); 339 cdata = &(xrd_service->provider_id);
340 }else{ 340 }else{
341 skipping = 1; 341 skipping = 1;
342 } 342 }
343 }else 343 }else
344 skipping = 1; 344 skipping = 1;
345 }else if(xmode&xmode_html) { 345 }else if(xmode&xmode_html) {
346 html_start_element(n,a); 346 html_start_element(n,a);
347 }else{ 347 }else{
348 skipping = 1; 348 skipping = 1;
349 } 349 }
350 } 350 }
351 } 351 }
352 void end_element(const XML_Char *n) { 352 void end_element(const XML_Char *n) {
353 if(skipping<0) return; 353 if(skipping<0) return;
354 if(skipping) { 354 if(skipping) {
355 --skipping; return; 355 --skipping; return;
356 } 356 }
357 if(is_qelement(n,NSURI_XRD "\tType")) { 357 if(is_qelement(n,NSURI_XRD "\tType")) {
358 assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); 358 assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf);
359 xrd_service->types.insert(cdata_buf); 359 xrd_service->types.insert(cdata_buf);
360 }else if(is_qelement(n,NSURI_XRD "\tService")) { 360 }else if(is_qelement(n,NSURI_XRD "\tService")) {
361 assert(xrd); assert(xrd_service); 361 assert(xrd); assert(xrd_service);
362 assert(!pt_stack.empty()); 362 assert(!pt_stack.empty());
363 assert(pt_stack.back()==(NSURI_XRD "\tService")); 363 assert(pt_stack.back()==(NSURI_XRD "\tService"));
364 pt_stack.pop_back(); 364 pt_stack.pop_back();
365 xrd_service = 0; 365 xrd_service = 0;
366 }else if(is_qelement(n,NSURI_XRD "\tStatus")) { 366 }else if(is_qelement(n,NSURI_XRD "\tStatus")) {
367 assert(xrd); 367 assert(xrd);
368 if(is_qelement(pt_stack.back().c_str(),n)) { 368 if(is_qelement(pt_stack.back().c_str(),n)) {
369 assert(cdata==&status_string); 369 assert(cdata==&status_string);
370 pt_stack.pop_back(); 370 pt_stack.pop_back();
371 if(status_code!=100) { 371 if(status_code!=100)
372 skipping = -1; stop_parser(); 372 skipping = -1;
373 }
374 } 373 }
375 }else if(is_qelement(n,NSURI_XRD "\tExpires")) { 374 }else if(is_qelement(n,NSURI_XRD "\tExpires")) {
376 assert(xrd); 375 assert(xrd);
377 xrd->expires = util::w3c_to_time(cdata_buf); 376 xrd->expires = util::w3c_to_time(cdata_buf);
378 }else if((xmode&xmode_html) && is_element(n,"head")) { 377 }else if((xmode&xmode_html) && is_element(n,"head")) {
379 skipping = -1; stop_parser(); 378 skipping = -1;
380 } 379 }
381 cdata = 0; 380 cdata = 0;
382 } 381 }
383 void character_data(const XML_Char *s,int l) { 382 void character_data(const XML_Char *s,int l) {
384 if(skipping) return; 383 if(skipping) return;
385 if(cdata) cdata->append(s,l); 384 if(cdata) cdata->append(s,l);
386 } 385 }
387 386
388 void html_start_element(const XML_Char *n,const XML_Char **a) { 387 void html_start_element(const XML_Char *n,const XML_Char **a) {
389 if(is_element(n,"meta")) { 388 if(is_element(n,"meta")) {
390 bool heq = false; 389 bool heq = false;
391 string l; 390 string l;
392 for(;*a;a+=2) { 391 for(;*a;a+=2) {
393 if(!( strcasecmp(a[0],"http-equiv") 392 if(!( strcasecmp(a[0],"http-equiv")
394 || strcasecmp(a[1],XRDS_HEADER) )) 393 || strcasecmp(a[1],XRDS_HEADER) ))
395 heq = true; 394 heq = true;
396 else if(!strcasecmp(a[0],"content")) 395 else if(!strcasecmp(a[0],"content"))
397 l.assign(a[1]); 396 l.assign(a[1]);
398 } 397 }
399 if(heq) 398 if(heq)
400 xrds_location = l; 399 xrds_location = l;
401 }else if(is_element(n,"link")) { 400 }else if(is_element(n,"link")) {
402 string rels; 401 string rels;
403 string href; 402 string href;
404 for(;*a;a+=2) { 403 for(;*a;a+=2) {
405 if( !strcasecmp(a[0],"rel") ) { 404 if( !strcasecmp(a[0],"rel") ) {
406 rels.assign(a[1]); 405 rels.assign(a[1]);
407 }else if( !strcasecmp(a[0],"href") ) { 406 }else if( !strcasecmp(a[0],"href") ) {
408 const char *ns = a[1]; 407 const char *ns = a[1];
409 for(;*ns && isspace(*ns);++ns); 408 for(;*ns && isspace(*ns);++ns);
410 href.assign(ns); 409 href.assign(ns);
411 string::size_type lns=href.find_last_not_of(whitespace); 410 string::size_type lns=href.find_last_not_of(whitespace);
412 href.erase(lns+1); 411 href.erase(lns+1);
413 } 412 }
414 } 413 }
415 for(string::size_type ns=rels.find_first_not_of(whitespace); 414 for(string::size_type ns=rels.find_first_not_of(whitespace);
416 ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) { 415 ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) {
417 string::size_type s = rels.find_first_of(whitespace,ns); 416 string::size_type s = rels.find_first_of(whitespace,ns);
418 string rel; 417 string rel;
419 if(s==string::npos) { 418 if(s==string::npos) {
420 rel.assign(rels,ns,string::npos); 419 rel.assign(rels,ns,string::npos);
421 ns = string::npos; 420 ns = string::npos;
422 }else{ 421 }else{
423 rel.assign(rels,ns,s-ns); 422 rel.assign(rels,ns,s-ns);
424 ns = s; 423 ns = s;
425 } 424 }
426 if(rel=="openid.server") 425 if(rel=="openid.server")
427 html_openid1.uris.add(-1,href); 426 html_openid1.uris.add(-1,href);
428 else if(rel=="openid.delegate") 427 else if(rel=="openid.delegate")
429 html_openid1.local_ids.add(-1,href); 428 html_openid1.local_ids.add(-1,href);
430 else if(rel=="openid2.provider") 429 else if(rel=="openid2.provider")
431 html_openid2.uris.add(-1,href); 430 html_openid2.uris.add(-1,href);
432 else if(rel=="openid2.local_id") 431 else if(rel=="openid2.local_id")
433 html_openid2.local_ids.add(-1,href); 432 html_openid2.local_ids.add(-1,href);
434 } 433 }
435 }else if(is_element(n,"body")) { 434 }else if(is_element(n,"body")) {
436 skipping = -1; stop_parser(); 435 skipping = -1;
437 } 436 }
438 } 437 }
439 438
440 }; 439 };
441 440
442 void idiscover(idiscovery_t& result,const string& identity) { 441 void idiscover(idiscovery_t& result,const string& identity) {
443 idigger_t idigger; 442 idigger_t idigger;
444 idigger.discover(result,identity); 443 idigger.discover(result,identity);
445 } 444 }
446 445
447} 446}