summaryrefslogtreecommitdiffabout
path: root/lib
Unidiff
Diffstat (limited to 'lib') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/discovery.cc1
1 files changed, 1 insertions, 0 deletions
diff --git a/lib/discovery.cc b/lib/discovery.cc
index 3b184ee..bc7d6fb 100644
--- a/lib/discovery.cc
+++ b/lib/discovery.cc
@@ -1,298 +1,299 @@
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 8
9#include "config.h" 9#include "config.h"
10 10
11#define XRDS_HEADER "X-XRDS-Location" 11#define XRDS_HEADER "X-XRDS-Location"
12#define CT_HEADER "Content-Type" 12#define CT_HEADER "Content-Type"
13 13
14namespace opkele { 14namespace opkele {
15 using std::list; 15 using std::list;
16 using xrd::XRD_t; 16 using xrd::XRD_t;
17 using xrd::service_t; 17 using xrd::service_t;
18 18
19 static const char *whitespace = " \t\r\n"; 19 static const char *whitespace = " \t\r\n";
20 static const char *i_leaders = "=@+$!("; 20 static const char *i_leaders = "=@+$!(";
21 21
22 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) {
23 return !strcasecmp(n,qen); 23 return !strcasecmp(n,qen);
24 } 24 }
25 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) {
26 if(!strcasecmp(n,en)) return true; 26 if(!strcasecmp(n,en)) return true;
27 int nl = strlen(n), enl = strlen(en); 27 int nl = strlen(n), enl = strlen(en);
28 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t' 28 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t'
29 && !strcasecmp(&n[nl-enl],en) ) 29 && !strcasecmp(&n[nl-enl],en) )
30 return true; 30 return true;
31 return false; 31 return false;
32 } 32 }
33 33
34 static long element_priority(const XML_Char **a) { 34 static long element_priority(const XML_Char **a) {
35 for(;*a;++a) 35 for(;*a;++a)
36 if(!strcasecmp(*(a++),"priority")) { 36 if(!strcasecmp(*(a++),"priority")) {
37 long rv; 37 long rv;
38 return (sscanf(*a,"%ld",&rv)==1)?rv:-1; 38 return (sscanf(*a,"%ld",&rv)==1)?rv:-1;
39 } 39 }
40 return -1; 40 return -1;
41 } 41 }
42 42
43 class idigger_t : public util::curl_t, public util::expat_t { 43 class idigger_t : public util::curl_t, public util::expat_t {
44 public: 44 public:
45 string xri_proxy; 45 string xri_proxy;
46 46
47 enum { 47 enum {
48 xmode_html = 1, xmode_xrd = 2 48 xmode_html = 1, xmode_xrd = 2
49 }; 49 };
50 int xmode; 50 int xmode;
51 51
52 string xrds_location; 52 string xrds_location;
53 string http_content_type; 53 string http_content_type;
54 service_t html_openid1; 54 service_t html_openid1;
55 service_t html_openid2; 55 service_t html_openid2;
56 string cdata_buf; 56 string cdata_buf;
57 long status_code; 57 long status_code;
58 string status_string; 58 string status_string;
59 59
60 typedef list<string> pt_stack_t; 60 typedef list<string> pt_stack_t;
61 pt_stack_t pt_stack; 61 pt_stack_t pt_stack;
62 int skipping; 62 int skipping;
63 63
64 XRD_t *xrd; 64 XRD_t *xrd;
65 service_t *xrd_service; 65 service_t *xrd_service;
66 string* cdata; 66 string* cdata;
67 67
68 idigger_t() 68 idigger_t()
69 : util::curl_t(easy_init()), 69 : util::curl_t(easy_init()),
70 util::expat_t(0), 70 util::expat_t(0),
71 xri_proxy(XRI_PROXY_URL) { 71 xri_proxy(XRI_PROXY_URL) {
72 CURLcode r; 72 CURLcode r;
73 (r=misc_sets()) 73 (r=misc_sets())
74 || (r=set_write()) 74 || (r=set_write())
75 || (r=set_header()) 75 || (r=set_header())
76 ; 76 ;
77 if(r) 77 if(r)
78 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 78 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
79 } 79 }
80 ~idigger_t() throw() { } 80 ~idigger_t() throw() { }
81 81
82 void discover(idiscovery_t& result,const string& identity) { 82 void discover(idiscovery_t& result,const string& identity) {
83 result.clear(); 83 result.clear();
84 string::size_type fsc = identity.find_first_not_of(whitespace); 84 string::size_type fsc = identity.find_first_not_of(whitespace);
85 if(fsc==string::npos) 85 if(fsc==string::npos)
86 throw bad_input(OPKELE_CP_ "whtiespace-only identity"); 86 throw bad_input(OPKELE_CP_ "whtiespace-only identity");
87 string::size_type lsc = identity.find_last_not_of(whitespace); 87 string::size_type lsc = identity.find_last_not_of(whitespace);
88 assert(lsc!=string::npos); 88 assert(lsc!=string::npos);
89 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) 89 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1))
90 fsc += sizeof("xri://")-1; 90 fsc += sizeof("xri://")-1;
91 if((fsc+1)>=lsc) 91 if((fsc+1)>=lsc)
92 throw bad_input(OPKELE_CP_ "not a character of importance in identity"); 92 throw bad_input(OPKELE_CP_ "not a character of importance in identity");
93 string id(identity,fsc,lsc-fsc+1); 93 string id(identity,fsc,lsc-fsc+1);
94 if(strchr(i_leaders,id[0])) { 94 if(strchr(i_leaders,id[0])) {
95 result.normalized_id = id; 95 result.normalized_id = id;
96 result.xri_identity = true; 96 result.xri_identity = true;
97 /* TODO: further canonicalize xri identity? Like folding case or whatever... */ 97 /* TODO: further canonicalize xri identity? Like folding case or whatever... */
98 discover_at( 98 discover_at(
99 result, 99 result,
100 xri_proxy + util::url_encode(id)+ 100 xri_proxy + util::url_encode(id)+
101 "?_xrd_r=application/xrd+xml;sep=false", xmode_xrd); 101 "?_xrd_r=application/xrd+xml;sep=false", xmode_xrd);
102 if(status_code!=100) 102 if(status_code!=100)
103 throw failed_xri_resolution(OPKELE_CP_ 103 throw failed_xri_resolution(OPKELE_CP_
104 "XRI resolution failed with '"+status_string+"' message",status_code); 104 "XRI resolution failed with '"+status_string+"' message",status_code);
105 if(result.xrd.canonical_ids.empty()) 105 if(result.xrd.canonical_ids.empty())
106 throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found"); 106 throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found");
107 result.canonicalized_id = result.xrd.canonical_ids.begin()->second;
107 }else{ 108 }else{
108 result.xri_identity = false; 109 result.xri_identity = false;
109 if(id.find("://")==string::npos) 110 if(id.find("://")==string::npos)
110 id.insert(0,"http://"); 111 id.insert(0,"http://");
111 string::size_type fp = id.find('#'); 112 string::size_type fp = id.find('#');
112 if(fp!=string::npos) { 113 if(fp!=string::npos) {
113 string::size_type qp = id.find('?'); 114 string::size_type qp = id.find('?');
114 if(qp==string::npos || qp<fp) 115 if(qp==string::npos || qp<fp)
115 id.erase(fp); 116 id.erase(fp);
116 else if(qp>fp) 117 else if(qp>fp)
117 id.erase(fp,qp-fp); 118 id.erase(fp,qp-fp);
118 } 119 }
119 result.normalized_id = util::rfc_3986_normalize_uri(id); 120 result.normalized_id = util::rfc_3986_normalize_uri(id);
120 discover_at(result,id,xmode_html|xmode_xrd); 121 discover_at(result,id,xmode_html|xmode_xrd);
121 const char * eu = 0; 122 const char * eu = 0;
122 CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); 123 CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu);
123 if(r) 124 if(r)
124 throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); 125 throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r);
125 result.canonicalized_id = util::rfc_3986_normalize_uri(eu); /* XXX: strip fragment part? */ 126 result.canonicalized_id = util::rfc_3986_normalize_uri(eu); /* XXX: strip fragment part? */
126 if(xrds_location.empty()) { 127 if(xrds_location.empty()) {
127 html2xrd(result.xrd); 128 html2xrd(result.xrd);
128 }else{ 129 }else{
129 discover_at(result,xrds_location,xmode_xrd); 130 discover_at(result,xrds_location,xmode_xrd);
130 if(result.xrd.empty()) 131 if(result.xrd.empty())
131 html2xrd(result.xrd); 132 html2xrd(result.xrd);
132 } 133 }
133 } 134 }
134 } 135 }
135 136
136 void discover_at(idiscovery_t& result,const string& url,int xm) { 137 void discover_at(idiscovery_t& result,const string& url,int xm) {
137 CURLcode r = easy_setopt(CURLOPT_URL,url.c_str()); 138 CURLcode r = easy_setopt(CURLOPT_URL,url.c_str());
138 if(r) 139 if(r)
139 throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r); 140 throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r);
140 141
141 (*(expat_t*)this) = parser_create_ns(); 142 (*(expat_t*)this) = parser_create_ns();
142 set_user_data(); set_element_handler(); 143 set_user_data(); set_element_handler();
143 set_character_data_handler(); 144 set_character_data_handler();
144 145
145 http_content_type.clear(); 146 http_content_type.clear();
146 xmode = xm; 147 xmode = xm;
147 if(xmode&xmode_html) { 148 if(xmode&xmode_html) {
148 xrds_location.clear(); 149 xrds_location.clear();
149 html_openid1.clear(); html_openid2.clear(); 150 html_openid1.clear(); html_openid2.clear();
150 } 151 }
151 xrd = &result.xrd; 152 xrd = &result.xrd;
152 cdata = 0; xrd_service = 0; skipping = 0; 153 cdata = 0; xrd_service = 0; skipping = 0;
153 status_code = 100; status_string.clear(); 154 status_code = 100; status_string.clear();
154 155
155 r = easy_perform(); 156 r = easy_perform();
156 if(r && r!=CURLE_WRITE_ERROR) 157 if(r && r!=CURLE_WRITE_ERROR)
157 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 158 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
158 159
159 parse(0,0,true); 160 parse(0,0,true);
160 } 161 }
161 162
162 void html2xrd(XRD_t& x) { 163 void html2xrd(XRD_t& x) {
163 if(!html_openid1.uris.empty()) { 164 if(!html_openid1.uris.empty()) {
164 html_openid1.types.insert(STURI_OPENID11); 165 html_openid1.types.insert(STURI_OPENID11);
165 x.services.add(-1,html_openid1); 166 x.services.add(-1,html_openid1);
166 } 167 }
167 if(!html_openid2.uris.empty()) { 168 if(!html_openid2.uris.empty()) {
168 html_openid2.types.insert(STURI_OPENID20); 169 html_openid2.types.insert(STURI_OPENID20);
169 x.services.add(-1,html_openid2); 170 x.services.add(-1,html_openid2);
170 } 171 }
171 } 172 }
172 173
173 size_t write(void *p,size_t s,size_t nm) { 174 size_t write(void *p,size_t s,size_t nm) {
174 if(skipping<0) return 0; 175 if(skipping<0) return 0;
175 /* TODO: limit total size */ 176 /* TODO: limit total size */
176 size_t bytes = s*nm; 177 size_t bytes = s*nm;
177 parse((const char *)p,bytes,false); 178 parse((const char *)p,bytes,false);
178 return bytes; 179 return bytes;
179 } 180 }
180 size_t header(void *p,size_t s,size_t nm) { 181 size_t header(void *p,size_t s,size_t nm) {
181 size_t bytes = s*nm; 182 size_t bytes = s*nm;
182 const char *h = (const char*)p; 183 const char *h = (const char*)p;
183 const char *colon = (const char*)memchr(p,':',bytes); 184 const char *colon = (const char*)memchr(p,':',bytes);
184 const char *space = (const char*)memchr(p,' ',bytes); 185 const char *space = (const char*)memchr(p,' ',bytes);
185 if(space && ( (!colon) || space<colon ) ) { 186 if(space && ( (!colon) || space<colon ) ) {
186 xrds_location.clear(); http_content_type.clear(); 187 xrds_location.clear(); http_content_type.clear();
187 }else if(colon) { 188 }else if(colon) {
188 const char *hv = ++colon; 189 const char *hv = ++colon;
189 int hnl = colon-h; 190 int hnl = colon-h;
190 int rb; 191 int rb;
191 for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb); 192 for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb);
192 while(rb>0 && isspace(hv[rb-1])) --rb; 193 while(rb>0 && isspace(hv[rb-1])) --rb;
193 if(rb) { 194 if(rb) {
194 if( (hnl>=sizeof(XRDS_HEADER)) 195 if( (hnl>=sizeof(XRDS_HEADER))
195 && !strncasecmp(h,XRDS_HEADER":", 196 && !strncasecmp(h,XRDS_HEADER":",
196 sizeof(XRDS_HEADER)) ) { 197 sizeof(XRDS_HEADER)) ) {
197 xrds_location.assign(hv,rb); 198 xrds_location.assign(hv,rb);
198 }else if( (hnl>=sizeof(CT_HEADER)) 199 }else if( (hnl>=sizeof(CT_HEADER))
199 && !strncasecmp(h,CT_HEADER":", 200 && !strncasecmp(h,CT_HEADER":",
200 sizeof(CT_HEADER)) ) { 201 sizeof(CT_HEADER)) ) {
201 const char *sc = (const char*)memchr( 202 const char *sc = (const char*)memchr(
202 hv,';',rb); 203 hv,';',rb);
203 http_content_type.assign(hv,sc?(sc-hv):rb); 204 http_content_type.assign(hv,sc?(sc-hv):rb);
204 } 205 }
205 } 206 }
206 } 207 }
207 return curl_t::header(p,s,nm); 208 return curl_t::header(p,s,nm);
208 } 209 }
209 210
210 void start_element(const XML_Char *n,const XML_Char **a) { 211 void start_element(const XML_Char *n,const XML_Char **a) {
211 if(skipping<0) return; 212 if(skipping<0) return;
212 if(skipping) { 213 if(skipping) {
213 if(xmode&xmode_html) 214 if(xmode&xmode_html)
214 html_start_element(n,a); 215 html_start_element(n,a);
215 ++skipping; return; 216 ++skipping; return;
216 } 217 }
217 if(pt_stack.empty()) { 218 if(pt_stack.empty()) {
218 if(is_qelement(n,NSURI_XRDS "\tXRDS")) 219 if(is_qelement(n,NSURI_XRDS "\tXRDS"))
219 return; 220 return;
220 if(is_qelement(n,NSURI_XRD "\tXRD")) { 221 if(is_qelement(n,NSURI_XRD "\tXRD")) {
221 assert(xrd); 222 assert(xrd);
222 xrd->clear(); 223 xrd->clear();
223 pt_stack.push_back(n); 224 pt_stack.push_back(n);
224 }else if(xmode&xmode_html) { 225 }else if(xmode&xmode_html) {
225 html_start_element(n,a); 226 html_start_element(n,a);
226 }else{ 227 }else{
227 skipping = -1; 228 skipping = -1;
228 } 229 }
229 }else{ 230 }else{
230 int pt_s = pt_stack.size(); 231 int pt_s = pt_stack.size();
231 if(pt_s==1) { 232 if(pt_s==1) {
232 if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { 233 if(is_qelement(n,NSURI_XRD "\tCanonicalID")) {
233 assert(xrd); 234 assert(xrd);
234 cdata = &(xrd->canonical_ids.add(element_priority(a),string())); 235 cdata = &(xrd->canonical_ids.add(element_priority(a),string()));
235 }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { 236 }else if(is_qelement(n,NSURI_XRD "\tLocalID")) {
236 assert(xrd); 237 assert(xrd);
237 cdata = &(xrd->local_ids.add(element_priority(a),string())); 238 cdata = &(xrd->local_ids.add(element_priority(a),string()));
238 }else if(is_qelement(n,NSURI_XRD "\tService")) { 239 }else if(is_qelement(n,NSURI_XRD "\tService")) {
239 assert(xrd); 240 assert(xrd);
240 xrd_service = &(xrd->services.add(element_priority(a), 241 xrd_service = &(xrd->services.add(element_priority(a),
241 service_t())); 242 service_t()));
242 pt_stack.push_back(n); 243 pt_stack.push_back(n);
243 }else if(is_qelement(n,NSURI_XRD "\tStatus")) { 244 }else if(is_qelement(n,NSURI_XRD "\tStatus")) {
244 for(;*a;) { 245 for(;*a;) {
245 if(!strcasecmp(*(a++),"code")) { 246 if(!strcasecmp(*(a++),"code")) {
246 if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { 247 if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) {
247 cdata = &status_string; 248 cdata = &status_string;
248 pt_stack.push_back(n); 249 pt_stack.push_back(n);
249 break; 250 break;
250 } 251 }
251 } 252 }
252 } 253 }
253 }else if(is_qelement(n,NSURI_XRD "\tExpires")) { 254 }else if(is_qelement(n,NSURI_XRD "\tExpires")) {
254 assert(xrd); 255 assert(xrd);
255 cdata_buf.clear(); 256 cdata_buf.clear();
256 cdata = &cdata_buf; 257 cdata = &cdata_buf;
257 }else if(xmode&xmode_html) { 258 }else if(xmode&xmode_html) {
258 html_start_element(n,a); 259 html_start_element(n,a);
259 }else{ 260 }else{
260 skipping = 1; 261 skipping = 1;
261 } 262 }
262 }else if(pt_s==2) { 263 }else if(pt_s==2) {
263 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { 264 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) {
264 if(is_qelement(n,NSURI_XRD "\tType")) { 265 if(is_qelement(n,NSURI_XRD "\tType")) {
265 assert(xrd); assert(xrd_service); 266 assert(xrd); assert(xrd_service);
266 cdata_buf.clear(); 267 cdata_buf.clear();
267 cdata = &cdata_buf; 268 cdata = &cdata_buf;
268 }else if(is_qelement(n,NSURI_XRD "\tURI")) { 269 }else if(is_qelement(n,NSURI_XRD "\tURI")) {
269 assert(xrd); assert(xrd_service); 270 assert(xrd); assert(xrd_service);
270 cdata = &(xrd_service->uris.add(element_priority(a),string())); 271 cdata = &(xrd_service->uris.add(element_priority(a),string()));
271 }else if(is_qelement(n,NSURI_XRD "\tLocalID") 272 }else if(is_qelement(n,NSURI_XRD "\tLocalID")
272 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { 273 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) {
273 assert(xrd); assert(xrd_service); 274 assert(xrd); assert(xrd_service);
274 cdata = &(xrd_service->local_ids.add(element_priority(a),string())); 275 cdata = &(xrd_service->local_ids.add(element_priority(a),string()));
275 }else{ 276 }else{
276 skipping = 1; 277 skipping = 1;
277 } 278 }
278 }else 279 }else
279 skipping = 1; 280 skipping = 1;
280 }else if(xmode&xmode_html) { 281 }else if(xmode&xmode_html) {
281 html_start_element(n,a); 282 html_start_element(n,a);
282 }else{ 283 }else{
283 skipping = 1; 284 skipping = 1;
284 } 285 }
285 } 286 }
286 } 287 }
287 void end_element(const XML_Char *n) { 288 void end_element(const XML_Char *n) {
288 if(skipping<0) return; 289 if(skipping<0) return;
289 if(skipping) { 290 if(skipping) {
290 --skipping; return; 291 --skipping; return;
291 } 292 }
292 if(is_qelement(n,NSURI_XRD "\tType")) { 293 if(is_qelement(n,NSURI_XRD "\tType")) {
293 assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); 294 assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf);
294 xrd_service->types.insert(cdata_buf); 295 xrd_service->types.insert(cdata_buf);
295 }else if(is_qelement(n,NSURI_XRD "\tService")) { 296 }else if(is_qelement(n,NSURI_XRD "\tService")) {
296 assert(xrd); assert(xrd_service); 297 assert(xrd); assert(xrd_service);
297 assert(!pt_stack.empty()); 298 assert(!pt_stack.empty());
298 assert(pt_stack.back()==(NSURI_XRD "\tService")); 299 assert(pt_stack.back()==(NSURI_XRD "\tService"));