author | Michael Krelin <hacker@klever.net> | 2007-12-29 21:15:05 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-01-04 18:21:47 (UTC) |
commit | d81fa443c048d6919e98e82fff1fe6013c9deff4 (patch) (unidiff) | |
tree | 53e458f164b3e192a9ef8adc67e4141fab9b5a40 | |
parent | ff04188567b117c28d54d6f81a9dca40ff0b1730 (diff) | |
download | libopkele-d81fa443c048d6919e98e82fff1fe6013c9deff4.zip libopkele-d81fa443c048d6919e98e82fff1fe6013c9deff4.tar.gz libopkele-d81fa443c048d6919e98e82fff1fe6013c9deff4.tar.bz2 |
parse xrd:Expires
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/discovery.cc | 10 |
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,377 +1,381 @@ | |||
1 | #include <iostream> | ||
2 | using 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 | ||
16 | namespace opkele { | 14 | namespace 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 |
51 | }; | 49 | }; |
52 | int xmode; | 50 | int xmode; |
53 | 51 | ||
54 | string xrds_location; | 52 | string xrds_location; |
55 | string http_content_type; | 53 | string http_content_type; |
56 | service_t html_openid1; | 54 | service_t html_openid1; |
57 | service_t html_openid2; | 55 | service_t html_openid2; |
58 | string cdata_buf; | 56 | string cdata_buf; |
59 | long status_code; | 57 | long status_code; |
60 | string status_string; | 58 | string status_string; |
61 | 59 | ||
62 | typedef list<string> pt_stack_t; | 60 | typedef list<string> pt_stack_t; |
63 | pt_stack_t pt_stack; | 61 | pt_stack_t pt_stack; |
64 | int skipping; | 62 | int skipping; |
65 | 63 | ||
66 | XRD_t *xrd; | 64 | XRD_t *xrd; |
67 | service_t *xrd_service; | 65 | service_t *xrd_service; |
68 | string* cdata; | 66 | string* cdata; |
69 | 67 | ||
70 | idigger_t() | 68 | idigger_t() |
71 | : util::curl_t(easy_init()), | 69 | : util::curl_t(easy_init()), |
72 | util::expat_t(0), | 70 | util::expat_t(0), |
73 | xri_proxy(XRI_PROXY_URL) { | 71 | xri_proxy(XRI_PROXY_URL) { |
74 | CURLcode r; | 72 | CURLcode r; |
75 | (r=misc_sets()) | 73 | (r=misc_sets()) |
76 | || (r=set_write()) | 74 | || (r=set_write()) |
77 | || (r=set_header()) | 75 | || (r=set_header()) |
78 | ; | 76 | ; |
79 | if(r) | 77 | if(r) |
80 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); | 78 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); |
81 | } | 79 | } |
82 | ~idigger_t() throw() { } | 80 | ~idigger_t() throw() { } |
83 | 81 | ||
84 | void discover(idiscovery_t& result,const string& identity) { | 82 | void discover(idiscovery_t& result,const string& identity) { |
85 | result.clear(); | 83 | result.clear(); |
86 | string::size_type fsc = identity.find_first_not_of(whitespace); | 84 | string::size_type fsc = identity.find_first_not_of(whitespace); |
87 | if(fsc==string::npos) | 85 | if(fsc==string::npos) |
88 | throw bad_input(OPKELE_CP_ "whtiespace-only identity"); | 86 | throw bad_input(OPKELE_CP_ "whtiespace-only identity"); |
89 | string::size_type lsc = identity.find_last_not_of(whitespace); | 87 | string::size_type lsc = identity.find_last_not_of(whitespace); |
90 | assert(lsc!=string::npos); | 88 | assert(lsc!=string::npos); |
91 | if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) | 89 | if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) |
92 | fsc += sizeof("xri://")-1; | 90 | fsc += sizeof("xri://")-1; |
93 | if((fsc+1)>=lsc) | 91 | if((fsc+1)>=lsc) |
94 | 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"); |
95 | string id(identity,fsc,lsc-fsc+1); | 93 | string id(identity,fsc,lsc-fsc+1); |
96 | if(strchr(i_leaders,id[0])) { | 94 | if(strchr(i_leaders,id[0])) { |
97 | result.normalized_id = id; | 95 | result.normalized_id = id; |
98 | result.xri_identity = true; | 96 | result.xri_identity = true; |
99 | /* TODO: further canonicalize xri identity? Like folding case or whatever... */ | 97 | /* TODO: further canonicalize xri identity? Like folding case or whatever... */ |
100 | discover_at( | 98 | discover_at( |
101 | result, | 99 | result, |
102 | xri_proxy + util::url_encode(id)+ | 100 | xri_proxy + util::url_encode(id)+ |
103 | "?_xrd_r=application/xrd+xml;sep=false", xmode_xrd); | 101 | "?_xrd_r=application/xrd+xml;sep=false", xmode_xrd); |
104 | if(status_code!=100) | 102 | if(status_code!=100) |
105 | throw failed_xri_resolution(OPKELE_CP_ | 103 | throw failed_xri_resolution(OPKELE_CP_ |
106 | "XRI resolution failed with '"+status_string+"' message",status_code); | 104 | "XRI resolution failed with '"+status_string+"' message",status_code); |
107 | if(result.xrd.canonical_ids.empty()) | 105 | if(result.xrd.canonical_ids.empty()) |
108 | 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"); |
109 | }else{ | 107 | }else{ |
110 | result.xri_identity = false; | 108 | result.xri_identity = false; |
111 | if(id.find("://")==string::npos) | 109 | if(id.find("://")==string::npos) |
112 | id.insert(0,"http://"); | 110 | id.insert(0,"http://"); |
113 | string::size_type fp = id.find('#'); | 111 | string::size_type fp = id.find('#'); |
114 | if(fp!=string::npos) { | 112 | if(fp!=string::npos) { |
115 | string::size_type qp = id.find('?'); | 113 | string::size_type qp = id.find('?'); |
116 | if(qp==string::npos || qp<fp) | 114 | if(qp==string::npos || qp<fp) |
117 | id.erase(fp); | 115 | id.erase(fp); |
118 | else if(qp>fp) | 116 | else if(qp>fp) |
119 | id.erase(fp,qp-fp); | 117 | id.erase(fp,qp-fp); |
120 | } | 118 | } |
121 | result.normalized_id = util::rfc_3986_normalize_uri(id); | 119 | result.normalized_id = util::rfc_3986_normalize_uri(id); |
122 | discover_at(result,id,xmode_html|xmode_xrd); | 120 | discover_at(result,id,xmode_html|xmode_xrd); |
123 | const char * eu = 0; | 121 | const char * eu = 0; |
124 | CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); | 122 | CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); |
125 | if(r) | 123 | if(r) |
126 | throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); | 124 | throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); |
127 | result.canonicalized_id = util::rfc_3986_normalize_uri(eu); /* XXX: strip fragment part? */ | 125 | result.canonicalized_id = util::rfc_3986_normalize_uri(eu); /* XXX: strip fragment part? */ |
128 | if(xrds_location.empty()) { | 126 | if(xrds_location.empty()) { |
129 | html2xrd(result.xrd); | 127 | html2xrd(result.xrd); |
130 | }else{ | 128 | }else{ |
131 | discover_at(result,xrds_location,xmode_xrd); | 129 | discover_at(result,xrds_location,xmode_xrd); |
132 | if(result.xrd.empty()) | 130 | if(result.xrd.empty()) |
133 | html2xrd(result.xrd); | 131 | html2xrd(result.xrd); |
134 | } | 132 | } |
135 | } | 133 | } |
136 | } | 134 | } |
137 | 135 | ||
138 | void discover_at(idiscovery_t& result,const string& url,int xm) { | 136 | void discover_at(idiscovery_t& result,const string& url,int xm) { |
139 | CURLcode r = easy_setopt(CURLOPT_URL,url.c_str()); | 137 | CURLcode r = easy_setopt(CURLOPT_URL,url.c_str()); |
140 | if(r) | 138 | if(r) |
141 | throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r); | 139 | throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r); |
142 | 140 | ||
143 | (*(expat_t*)this) = parser_create_ns(); | 141 | (*(expat_t*)this) = parser_create_ns(); |
144 | set_user_data(); set_element_handler(); | 142 | set_user_data(); set_element_handler(); |
145 | set_character_data_handler(); | 143 | set_character_data_handler(); |
146 | 144 | ||
147 | http_content_type.clear(); | 145 | http_content_type.clear(); |
148 | xmode = xm; | 146 | xmode = xm; |
149 | if(xmode&xmode_html) { | 147 | if(xmode&xmode_html) { |
150 | xrds_location.clear(); | 148 | xrds_location.clear(); |
151 | html_openid1.clear(); html_openid2.clear(); | 149 | html_openid1.clear(); html_openid2.clear(); |
152 | } | 150 | } |
153 | xrd = &result.xrd; | 151 | xrd = &result.xrd; |
154 | cdata = 0; xrd_service = 0; skipping = 0; | 152 | cdata = 0; xrd_service = 0; skipping = 0; |
155 | status_code = 100; status_string.clear(); | 153 | status_code = 100; status_string.clear(); |
156 | 154 | ||
157 | r = easy_perform(); | 155 | r = easy_perform(); |
158 | if(r && r!=CURLE_WRITE_ERROR) | 156 | if(r && r!=CURLE_WRITE_ERROR) |
159 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); | 157 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); |
160 | 158 | ||
161 | parse(0,0,true); | 159 | parse(0,0,true); |
162 | } | 160 | } |
163 | 161 | ||
164 | void html2xrd(XRD_t& x) { | 162 | void html2xrd(XRD_t& x) { |
165 | if(!html_openid1.uris.empty()) { | 163 | if(!html_openid1.uris.empty()) { |
166 | html_openid1.types.insert(STURI_OPENID11); | 164 | html_openid1.types.insert(STURI_OPENID11); |
167 | x.services.add(-1,html_openid1); | 165 | x.services.add(-1,html_openid1); |
168 | } | 166 | } |
169 | if(!html_openid2.uris.empty()) { | 167 | if(!html_openid2.uris.empty()) { |
170 | html_openid2.types.insert(STURI_OPENID20); | 168 | html_openid2.types.insert(STURI_OPENID20); |
171 | x.services.add(-1,html_openid2); | 169 | x.services.add(-1,html_openid2); |
172 | } | 170 | } |
173 | } | 171 | } |
174 | 172 | ||
175 | size_t write(void *p,size_t s,size_t nm) { | 173 | size_t write(void *p,size_t s,size_t nm) { |
176 | if(skipping<0) return 0; | 174 | if(skipping<0) return 0; |
177 | /* TODO: limit total size */ | 175 | /* TODO: limit total size */ |
178 | size_t bytes = s*nm; | 176 | size_t bytes = s*nm; |
179 | parse((const char *)p,bytes,false); | 177 | parse((const char *)p,bytes,false); |
180 | return bytes; | 178 | return bytes; |
181 | } | 179 | } |
182 | size_t header(void *p,size_t s,size_t nm) { | 180 | size_t header(void *p,size_t s,size_t nm) { |
183 | size_t bytes = s*nm; | 181 | size_t bytes = s*nm; |
184 | const char *h = (const char*)p; | 182 | const char *h = (const char*)p; |
185 | const char *colon = (const char*)memchr(p,':',bytes); | 183 | const char *colon = (const char*)memchr(p,':',bytes); |
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 | } |
356 | if(rel=="openid.server") | 360 | if(rel=="openid.server") |
357 | html_openid1.uris.add(-1,href); | 361 | html_openid1.uris.add(-1,href); |
358 | else if(rel=="openid.delegate") | 362 | else if(rel=="openid.delegate") |
359 | html_openid1.local_ids.add(-1,href); | 363 | html_openid1.local_ids.add(-1,href); |
360 | else if(rel=="openid2.provider") | 364 | else if(rel=="openid2.provider") |
361 | html_openid2.uris.add(-1,href); | 365 | html_openid2.uris.add(-1,href); |
362 | else if(rel=="openid2.local_id") | 366 | else if(rel=="openid2.local_id") |
363 | html_openid2.local_ids.add(-1,href); | 367 | html_openid2.local_ids.add(-1,href); |
364 | } | 368 | } |
365 | }else if(is_element(n,"body")) { | 369 | }else if(is_element(n,"body")) { |
366 | skipping = -1; | 370 | skipping = -1; |
367 | } | 371 | } |
368 | } | 372 | } |
369 | 373 | ||
370 | }; | 374 | }; |
371 | 375 | ||
372 | void idiscover(idiscovery_t& result,const string& identity) { | 376 | void idiscover(idiscovery_t& result,const string& identity) { |
373 | idigger_t idigger; | 377 | idigger_t idigger; |
374 | idigger.discover(result,identity); | 378 | idigger.discover(result,identity); |
375 | } | 379 | } |
376 | 380 | ||
377 | } | 381 | } |