-rw-r--r-- | lib/data.cc | 3 | ||||
-rw-r--r-- | lib/discovery.cc | 17 | ||||
-rw-r--r-- | lib/util.cc | 41 |
3 files changed, 49 insertions, 12 deletions
diff --git a/lib/data.cc b/lib/data.cc index c040430..f71788f 100644 --- a/lib/data.cc +++ b/lib/data.cc | |||
@@ -1,11 +1,14 @@ | |||
1 | #include <opkele/data.h> | 1 | #include <opkele/data.h> |
2 | 2 | ||
3 | namespace opkele { | 3 | namespace opkele { |
4 | 4 | ||
5 | namespace data { | 5 | namespace data { |
6 | 6 | ||
7 | const char *_default_p = "155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443"; | 7 | const char *_default_p = "155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443"; |
8 | const char *_default_g = "2"; | 8 | const char *_default_g = "2"; |
9 | 9 | ||
10 | const char *_iname_leaders = "=@+$!("; | ||
11 | const char *_whitespace_chars = " \t\r\n"; | ||
12 | |||
10 | } | 13 | } |
11 | } | 14 | } |
diff --git a/lib/discovery.cc b/lib/discovery.cc index b7f2db6..5913ad4 100644 --- a/lib/discovery.cc +++ b/lib/discovery.cc | |||
@@ -1,577 +1,576 @@ | |||
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/data.h> | ||
9 | #include <opkele/debug.h> | 10 | #include <opkele/debug.h> |
10 | 11 | ||
11 | #include "config.h" | 12 | #include "config.h" |
12 | 13 | ||
13 | #define XRDS_HEADER "X-XRDS-Location" | 14 | #define XRDS_HEADER "X-XRDS-Location" |
14 | #define CT_HEADER "Content-Type" | 15 | #define CT_HEADER "Content-Type" |
15 | 16 | ||
16 | namespace opkele { | 17 | namespace opkele { |
17 | using std::list; | 18 | using std::list; |
18 | using xrd::XRD_t; | 19 | using xrd::XRD_t; |
19 | using xrd::service_t; | 20 | using xrd::service_t; |
20 | 21 | ||
21 | /* TODO: the whole discovery thing needs cleanup and optimization due to | 22 | /* TODO: the whole discovery thing needs cleanup and optimization due to |
22 | * many changes of concept. */ | 23 | * many changes of concept. */ |
23 | 24 | ||
24 | static const char *whitespace = " \t\r\n"; | ||
25 | static const char *i_leaders = "=@+$!("; | ||
26 | static const size_t max_html = 16384; | 25 | static const size_t max_html = 16384; |
27 | 26 | ||
28 | static const struct service_type_t { | 27 | static const struct service_type_t { |
29 | const char *uri; | 28 | const char *uri; |
30 | const char *forceid; | 29 | const char *forceid; |
31 | } op_service_types[] = { | 30 | } op_service_types[] = { |
32 | { STURI_OPENID20_OP, IDURI_SELECT20 }, | 31 | { STURI_OPENID20_OP, IDURI_SELECT20 }, |
33 | { STURI_OPENID20, 0 }, | 32 | { STURI_OPENID20, 0 }, |
34 | { STURI_OPENID11, 0 }, | 33 | { STURI_OPENID11, 0 }, |
35 | { STURI_OPENID10, 0 } | 34 | { STURI_OPENID10, 0 } |
36 | }; | 35 | }; |
37 | enum { | 36 | enum { |
38 | st_index_1 = 2, st_index_2 = 1 | 37 | st_index_1 = 2, st_index_2 = 1 |
39 | }; | 38 | }; |
40 | 39 | ||
41 | 40 | ||
42 | static inline bool is_qelement(const XML_Char *n,const char *qen) { | 41 | static inline bool is_qelement(const XML_Char *n,const char *qen) { |
43 | return !strcasecmp(n,qen); | 42 | return !strcasecmp(n,qen); |
44 | } | 43 | } |
45 | static inline bool is_element(const XML_Char *n,const char *en) { | 44 | static inline bool is_element(const XML_Char *n,const char *en) { |
46 | if(!strcasecmp(n,en)) return true; | 45 | if(!strcasecmp(n,en)) return true; |
47 | int nl = strlen(n), enl = strlen(en); | 46 | int nl = strlen(n), enl = strlen(en); |
48 | if( (nl>=(enl+1)) && n[nl-enl-1]=='\t' | 47 | if( (nl>=(enl+1)) && n[nl-enl-1]=='\t' |
49 | && !strcasecmp(&n[nl-enl],en) ) | 48 | && !strcasecmp(&n[nl-enl],en) ) |
50 | return true; | 49 | return true; |
51 | return false; | 50 | return false; |
52 | } | 51 | } |
53 | 52 | ||
54 | static long element_priority(const XML_Char **a) { | 53 | static long element_priority(const XML_Char **a) { |
55 | for(;*a;++a) | 54 | for(;*a;++a) |
56 | if(!strcasecmp(*(a++),"priority")) { | 55 | if(!strcasecmp(*(a++),"priority")) { |
57 | long rv; | 56 | long rv; |
58 | return (sscanf(*a,"%ld",&rv)==1)?rv:-1; | 57 | return (sscanf(*a,"%ld",&rv)==1)?rv:-1; |
59 | } | 58 | } |
60 | return -1; | 59 | return -1; |
61 | } | 60 | } |
62 | /* TODO: ideally all attributes should be | 61 | /* TODO: ideally all attributes should be |
63 | * retrieved in one run */ | 62 | * retrieved in one run */ |
64 | static const char *element_attr(const XML_Char **a, const char *at) { | 63 | static const char *element_attr(const XML_Char **a, const char *at) { |
65 | for(;*a;++a) | 64 | for(;*a;++a) |
66 | if(!strcasecmp(*(a++),at)) { | 65 | if(!strcasecmp(*(a++),at)) { |
67 | return *a; | 66 | return *a; |
68 | } | 67 | } |
69 | return 0; | 68 | return 0; |
70 | } | 69 | } |
71 | 70 | ||
72 | class idigger_t : public util::curl_t, public util::expat_t { | 71 | class idigger_t : public util::curl_t, public util::expat_t { |
73 | public: | 72 | public: |
74 | string xri_proxy; | 73 | string xri_proxy; |
75 | 74 | ||
76 | enum { | 75 | enum { |
77 | xmode_html = 1, xmode_xrd = 2, xmode_cid = 4, | 76 | xmode_html = 1, xmode_xrd = 2, xmode_cid = 4, |
78 | xmode_noredirs = 8 | 77 | xmode_noredirs = 8 |
79 | }; | 78 | }; |
80 | int xmode; | 79 | int xmode; |
81 | 80 | ||
82 | string xrds_location; | 81 | string xrds_location; |
83 | string http_content_type; | 82 | string http_content_type; |
84 | service_t html_openid1; | 83 | service_t html_openid1; |
85 | service_t html_openid2; | 84 | service_t html_openid2; |
86 | string cdata_buf; | 85 | string cdata_buf; |
87 | long status_code; | 86 | long status_code; |
88 | string status_string; | 87 | string status_string; |
89 | 88 | ||
90 | typedef list<string> pt_stack_t; | 89 | typedef list<string> pt_stack_t; |
91 | pt_stack_t pt_stack; | 90 | pt_stack_t pt_stack; |
92 | int skipping; | 91 | int skipping; |
93 | bool parser_choked; | 92 | bool parser_choked; |
94 | string save_html; | 93 | string save_html; |
95 | 94 | ||
96 | XRD_t *xrd; | 95 | XRD_t *xrd; |
97 | service_t *xrd_service; | 96 | service_t *xrd_service; |
98 | string* cdata; | 97 | string* cdata; |
99 | 98 | ||
100 | idigger_t() | 99 | idigger_t() |
101 | : util::curl_t(easy_init()), | 100 | : util::curl_t(easy_init()), |
102 | util::expat_t(0), | 101 | util::expat_t(0), |
103 | xri_proxy(XRI_PROXY_URL) { | 102 | xri_proxy(XRI_PROXY_URL) { |
104 | CURLcode r; | 103 | CURLcode r; |
105 | (r=misc_sets()) | 104 | (r=misc_sets()) |
106 | || (r=set_write()) | 105 | || (r=set_write()) |
107 | || (r=set_header()) | 106 | || (r=set_header()) |
108 | ; | 107 | ; |
109 | if(r) | 108 | if(r) |
110 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); | 109 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); |
111 | } | 110 | } |
112 | ~idigger_t() throw() { } | 111 | ~idigger_t() throw() { } |
113 | 112 | ||
114 | void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) { | 113 | void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) { |
115 | idiscovery_t idis; | 114 | idiscovery_t idis; |
116 | idis.xri_identity = false; | 115 | idis.xri_identity = false; |
117 | discover_at(idis,yurl,xmode_html|xmode_xrd|(redirs?0:xmode_noredirs)); | 116 | discover_at(idis,yurl,xmode_html|xmode_xrd|(redirs?0:xmode_noredirs)); |
118 | if(!xrds_location.empty()) { | 117 | if(!xrds_location.empty()) { |
119 | idis.clear(); | 118 | idis.clear(); |
120 | discover_at(idis,xrds_location,xmode_xrd); | 119 | discover_at(idis,xrds_location,xmode_xrd); |
121 | } | 120 | } |
122 | idis.normalized_id = idis.canonicalized_id = yurl; | 121 | idis.normalized_id = idis.canonicalized_id = yurl; |
123 | service_type_t st; | 122 | service_type_t st; |
124 | for(st.uri=*types;*types;st.uri=*(++types)) | 123 | for(st.uri=*types;*types;st.uri=*(++types)) |
125 | queue_endpoints(oi,idis,&st); | 124 | queue_endpoints(oi,idis,&st); |
126 | } | 125 | } |
127 | 126 | ||
128 | string discover(endpoint_discovery_iterator& oi,const string& identity) { | 127 | string discover(endpoint_discovery_iterator& oi,const string& identity) { |
129 | string rv; | 128 | string rv; |
130 | idiscovery_t idis; | 129 | idiscovery_t idis; |
131 | string::size_type fsc = identity.find_first_not_of(whitespace); | 130 | string::size_type fsc = identity.find_first_not_of(data::_whitespace_chars); |
132 | if(fsc==string::npos) | 131 | if(fsc==string::npos) |
133 | throw bad_input(OPKELE_CP_ "whitespace-only identity"); | 132 | throw bad_input(OPKELE_CP_ "whitespace-only identity"); |
134 | string::size_type lsc = identity.find_last_not_of(whitespace); | 133 | string::size_type lsc = identity.find_last_not_of(data::_whitespace_chars); |
135 | assert(lsc!=string::npos); | 134 | assert(lsc!=string::npos); |
136 | if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) | 135 | if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) |
137 | fsc += sizeof("xri://")-1; | 136 | fsc += sizeof("xri://")-1; |
138 | if((fsc+1)>=lsc) | 137 | if((fsc+1)>=lsc) |
139 | throw bad_input(OPKELE_CP_ "not a character of importance in identity"); | 138 | throw bad_input(OPKELE_CP_ "not a character of importance in identity"); |
140 | string id(identity,fsc,lsc-fsc+1); | 139 | string id(identity,fsc,lsc-fsc+1); |
141 | idis.clear(); | 140 | idis.clear(); |
142 | if(strchr(i_leaders,id[0])) { | 141 | if(strchr(data::_iname_leaders,id[0])) { |
143 | /* TODO: further normalize xri identity? Like folding case | 142 | /* TODO: further normalize xri identity? Like folding case |
144 | * or whatever... */ | 143 | * or whatever... */ |
145 | rv = id; | 144 | rv = id; |
146 | set<string> cids; | 145 | set<string> cids; |
147 | for(const struct service_type_t *st=op_service_types; | 146 | for(const struct service_type_t *st=op_service_types; |
148 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) { | 147 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) { |
149 | idis.clear(); | 148 | idis.clear(); |
150 | discover_at( idis, | 149 | discover_at( idis, |
151 | xri_proxy + util::url_encode(id)+ | 150 | xri_proxy + util::url_encode(id)+ |
152 | "?_xrd_t="+util::url_encode(st->uri)+ | 151 | "?_xrd_t="+util::url_encode(st->uri)+ |
153 | "&_xrd_r=application/xrd%2Bxml" | 152 | "&_xrd_r=application/xrd%2Bxml" |
154 | ";sep=true;refs=true", | 153 | ";sep=true;refs=true", |
155 | xmode_xrd ); | 154 | xmode_xrd ); |
156 | if(status_code==241) continue; | 155 | if(status_code==241) continue; |
157 | if(status_code!=100) | 156 | if(status_code!=100) |
158 | throw failed_xri_resolution(OPKELE_CP_ | 157 | throw failed_xri_resolution(OPKELE_CP_ |
159 | "XRI resolution failed with '"+status_string+"' message" | 158 | "XRI resolution failed with '"+status_string+"' message" |
160 | ", while looking for SEP with type '"+st->uri+"'", status_code); | 159 | ", while looking for SEP with type '"+st->uri+"'", status_code); |
161 | if(idis.xrd.canonical_ids.empty()) | 160 | if(idis.xrd.canonical_ids.empty()) |
162 | throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found"); | 161 | throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found"); |
163 | string cid = idis.xrd.canonical_ids.begin()->second; | 162 | string cid = idis.xrd.canonical_ids.begin()->second; |
164 | if(cids.find(cid)==cids.end()) { | 163 | if(cids.find(cid)==cids.end()) { |
165 | cids.insert(cid); | 164 | cids.insert(cid); |
166 | idis.clear(); | 165 | idis.clear(); |
167 | discover_at( idis, | 166 | discover_at( idis, |
168 | xri_proxy + util::url_encode(id)+ | 167 | xri_proxy + util::url_encode(id)+ |
169 | "?_xrd_t="+util::url_encode(st->uri)+ | 168 | "?_xrd_t="+util::url_encode(st->uri)+ |
170 | "&_xrd_r=application/xrd%2Bxml" | 169 | "&_xrd_r=application/xrd%2Bxml" |
171 | ";sep=true;refs=true", | 170 | ";sep=true;refs=true", |
172 | xmode_xrd ); | 171 | xmode_xrd ); |
173 | if(status_code==241) continue; | 172 | if(status_code==241) continue; |
174 | if(status_code!=100) | 173 | if(status_code!=100) |
175 | throw failed_xri_resolution(OPKELE_CP_ | 174 | throw failed_xri_resolution(OPKELE_CP_ |
176 | "XRI resolution failed with '"+status_string+"' message" | 175 | "XRI resolution failed with '"+status_string+"' message" |
177 | ", while looking for SEP with type '"+st->uri+"'" | 176 | ", while looking for SEP with type '"+st->uri+"'" |
178 | " on canonical id", status_code); | 177 | " on canonical id", status_code); |
179 | } | 178 | } |
180 | idis.canonicalized_id = cid; | 179 | idis.canonicalized_id = cid; |
181 | idis.normalized_id = rv; idis.xri_identity = true; | 180 | idis.normalized_id = rv; idis.xri_identity = true; |
182 | queue_endpoints(oi,idis,st); | 181 | queue_endpoints(oi,idis,st); |
183 | } | 182 | } |
184 | }else{ | 183 | }else{ |
185 | idis.xri_identity = false; | 184 | idis.xri_identity = false; |
186 | if(id.find("://")==string::npos) | 185 | if(id.find("://")==string::npos) |
187 | id.insert(0,"http://"); | 186 | id.insert(0,"http://"); |
188 | string::size_type fp = id.find('#'); | 187 | string::size_type fp = id.find('#'); |
189 | if(fp!=string::npos) { | 188 | if(fp!=string::npos) { |
190 | string::size_type qp = id.find('?'); | 189 | string::size_type qp = id.find('?'); |
191 | if(qp==string::npos || qp<fp) | 190 | if(qp==string::npos || qp<fp) |
192 | id.erase(fp); | 191 | id.erase(fp); |
193 | else if(qp>fp) | 192 | else if(qp>fp) |
194 | id.erase(fp,qp-fp); | 193 | id.erase(fp,qp-fp); |
195 | } | 194 | } |
196 | rv = idis.normalized_id = util::rfc_3986_normalize_uri(id); | 195 | rv = idis.normalized_id = util::rfc_3986_normalize_uri(id); |
197 | discover_at(idis,id,xmode_html|xmode_xrd); | 196 | discover_at(idis,id,xmode_html|xmode_xrd); |
198 | const char * eu = 0; | 197 | const char * eu = 0; |
199 | CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); | 198 | CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu); |
200 | if(r) | 199 | if(r) |
201 | throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r); | 200 | 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) ); | 201 | string cid = util::strip_uri_fragment_part( idis.canonicalized_id = util::rfc_3986_normalize_uri(eu) ); |
203 | if(xrds_location.empty()) { | 202 | if(xrds_location.empty()) { |
204 | html2xrd(oi,idis); | 203 | html2xrd(oi,idis); |
205 | }else{ | 204 | }else{ |
206 | idis.clear(); | 205 | idis.clear(); |
207 | idis.canonicalized_id = cid; | 206 | idis.canonicalized_id = cid; |
208 | discover_at(idis,xrds_location,xmode_xrd); | 207 | discover_at(idis,xrds_location,xmode_xrd); |
209 | if(idis.xrd.empty()) | 208 | if(idis.xrd.empty()) |
210 | html2xrd(oi,idis); | 209 | html2xrd(oi,idis); |
211 | else{ | 210 | else{ |
212 | for(const service_type_t *st=op_service_types; | 211 | for(const service_type_t *st=op_service_types; |
213 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) | 212 | st<&op_service_types[sizeof(op_service_types)/sizeof(*op_service_types)];++st) |
214 | queue_endpoints(oi,idis,st); | 213 | queue_endpoints(oi,idis,st); |
215 | } | 214 | } |
216 | } | 215 | } |
217 | } | 216 | } |
218 | return rv; | 217 | return rv; |
219 | } | 218 | } |
220 | 219 | ||
221 | void discover_at(idiscovery_t& idis,const string& url,int xm) { | 220 | void discover_at(idiscovery_t& idis,const string& url,int xm) { |
222 | CURLcode r = easy_setopt(CURLOPT_MAXREDIRS, (xm&xmode_noredirs)?0:5); | 221 | CURLcode r = easy_setopt(CURLOPT_MAXREDIRS, (xm&xmode_noredirs)?0:5); |
223 | if(r) | 222 | if(r) |
224 | throw exception_curl(OPKELE_CP_ "failed to set curly maxredirs option"); | 223 | throw exception_curl(OPKELE_CP_ "failed to set curly maxredirs option"); |
225 | if( (r=easy_setopt(CURLOPT_URL,url.c_str())) ) | 224 | if( (r=easy_setopt(CURLOPT_URL,url.c_str())) ) |
226 | throw exception_curl(OPKELE_CP_ "failed to set curly urlie",r); | 225 | throw exception_curl(OPKELE_CP_ "failed to set curly urlie",r); |
227 | 226 | ||
228 | http_content_type.clear(); | 227 | http_content_type.clear(); |
229 | xmode = xm; | 228 | xmode = xm; |
230 | prepare_to_parse(); | 229 | prepare_to_parse(); |
231 | if(xmode&xmode_html) { | 230 | if(xmode&xmode_html) { |
232 | xrds_location.clear(); | 231 | xrds_location.clear(); |
233 | save_html.clear(); | 232 | save_html.clear(); |
234 | save_html.reserve(max_html); | 233 | save_html.reserve(max_html); |
235 | } | 234 | } |
236 | xrd = &idis.xrd; | 235 | xrd = &idis.xrd; |
237 | 236 | ||
238 | r = easy_perform(); | 237 | r = easy_perform(); |
239 | if(r && r!=CURLE_WRITE_ERROR) | 238 | if(r && r!=CURLE_WRITE_ERROR) |
240 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); | 239 | throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); |
241 | 240 | ||
242 | if(!parser_choked) { | 241 | if(!parser_choked) { |
243 | parse(0,0,true); | 242 | parse(0,0,true); |
244 | }else if(xmode&xmode_html){ | 243 | }else if(xmode&xmode_html){ |
245 | /* TODO: do not bother if we've seen xml */ | 244 | /* TODO: do not bother if we've seen xml */ |
246 | try { | 245 | try { |
247 | util::tidy_doc_t td = util::tidy_doc_t::create(); | 246 | util::tidy_doc_t td = util::tidy_doc_t::create(); |
248 | if(!td) | 247 | if(!td) |
249 | throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document"); | 248 | throw exception_tidy(OPKELE_CP_ "failed to create htmltidy document"); |
250 | #ifndef NDEBUG | 249 | #ifndef NDEBUG |
251 | td.opt_set(TidyQuiet,false); | 250 | td.opt_set(TidyQuiet,false); |
252 | td.opt_set(TidyShowWarnings,false); | 251 | td.opt_set(TidyShowWarnings,false); |
253 | #endif /* NDEBUG */ | 252 | #endif /* NDEBUG */ |
254 | td.opt_set(TidyForceOutput,true); | 253 | td.opt_set(TidyForceOutput,true); |
255 | td.opt_set(TidyXhtmlOut,true); | 254 | td.opt_set(TidyXhtmlOut,true); |
256 | td.opt_set(TidyDoctypeMode,TidyDoctypeOmit); | 255 | td.opt_set(TidyDoctypeMode,TidyDoctypeOmit); |
257 | td.opt_set(TidyMark,false); | 256 | td.opt_set(TidyMark,false); |
258 | if(td.parse_string(save_html)<=0) | 257 | if(td.parse_string(save_html)<=0) |
259 | throw exception_tidy(OPKELE_CP_ "tidy failed to parse document"); | 258 | throw exception_tidy(OPKELE_CP_ "tidy failed to parse document"); |
260 | if(td.clean_and_repair()<=0) | 259 | if(td.clean_and_repair()<=0) |
261 | throw exception_tidy(OPKELE_CP_ "tidy failed to clean and repair"); | 260 | throw exception_tidy(OPKELE_CP_ "tidy failed to clean and repair"); |
262 | util::tidy_buf_t tide; | 261 | util::tidy_buf_t tide; |
263 | if(td.save_buffer(tide)<=0) | 262 | if(td.save_buffer(tide)<=0) |
264 | throw exception_tidy(OPKELE_CP_ "tidy failed to save buffer"); | 263 | throw exception_tidy(OPKELE_CP_ "tidy failed to save buffer"); |
265 | prepare_to_parse(); | 264 | prepare_to_parse(); |
266 | parse(tide.c_str(),tide.size(),true); | 265 | parse(tide.c_str(),tide.size(),true); |
267 | }catch(exception_tidy& et) { } | 266 | }catch(exception_tidy& et) { } |
268 | } | 267 | } |
269 | save_html.clear(); | 268 | save_html.clear(); |
270 | } | 269 | } |
271 | 270 | ||
272 | void prepare_to_parse() { | 271 | void prepare_to_parse() { |
273 | (*(expat_t*)this) = parser_create_ns(); | 272 | (*(expat_t*)this) = parser_create_ns(); |
274 | set_user_data(); set_element_handler(); | 273 | set_user_data(); set_element_handler(); |
275 | set_character_data_handler(); | 274 | set_character_data_handler(); |
276 | 275 | ||
277 | if(xmode&xmode_html) { | 276 | if(xmode&xmode_html) { |
278 | html_openid1.clear(); html_openid2.clear(); | 277 | html_openid1.clear(); html_openid2.clear(); |
279 | parser_choked = false; | 278 | parser_choked = false; |
280 | } | 279 | } |
281 | 280 | ||
282 | cdata = 0; xrd_service = 0; skipping = 0; | 281 | cdata = 0; xrd_service = 0; skipping = 0; |
283 | pt_stack.clear(); | 282 | pt_stack.clear(); |
284 | status_code = 100; status_string.clear(); | 283 | status_code = 100; status_string.clear(); |
285 | } | 284 | } |
286 | 285 | ||
287 | void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) { | 286 | void html2xrd(endpoint_discovery_iterator& oi,idiscovery_t& id) { |
288 | XRD_t& x = id.xrd; | 287 | XRD_t& x = id.xrd; |
289 | if(!html_openid2.uris.empty()) { | 288 | if(!html_openid2.uris.empty()) { |
290 | html_openid2.types.insert(STURI_OPENID20); | 289 | html_openid2.types.insert(STURI_OPENID20); |
291 | x.services.add(-1,html_openid2); | 290 | x.services.add(-1,html_openid2); |
292 | queue_endpoints(oi,id,&op_service_types[st_index_2]); | 291 | queue_endpoints(oi,id,&op_service_types[st_index_2]); |
293 | } | 292 | } |
294 | if(!html_openid1.uris.empty()) { | 293 | if(!html_openid1.uris.empty()) { |
295 | html_openid1.types.insert(STURI_OPENID11); | 294 | html_openid1.types.insert(STURI_OPENID11); |
296 | x.services.add(-1,html_openid1); | 295 | x.services.add(-1,html_openid1); |
297 | queue_endpoints(oi,id,&op_service_types[st_index_1]); | 296 | queue_endpoints(oi,id,&op_service_types[st_index_1]); |
298 | } | 297 | } |
299 | } | 298 | } |
300 | 299 | ||
301 | size_t write(void *p,size_t s,size_t nm) { | 300 | size_t write(void *p,size_t s,size_t nm) { |
302 | /* TODO: limit total size */ | 301 | /* TODO: limit total size */ |
303 | size_t bytes = s*nm; | 302 | size_t bytes = s*nm; |
304 | const char *inbuf = (const char*)p; | 303 | const char *inbuf = (const char*)p; |
305 | if(xmode&xmode_html) { | 304 | if(xmode&xmode_html) { |
306 | size_t mbts = save_html.capacity()-save_html.size(); | 305 | size_t mbts = save_html.capacity()-save_html.size(); |
307 | size_t bts = 0; | 306 | size_t bts = 0; |
308 | if(mbts>0) { | 307 | if(mbts>0) { |
309 | bts = (bytes>mbts)?mbts:bytes; | 308 | bts = (bytes>mbts)?mbts:bytes; |
310 | save_html.append(inbuf,bts); | 309 | save_html.append(inbuf,bts); |
311 | } | 310 | } |
312 | if(skipping<0) return bts; | 311 | if(skipping<0) return bts; |
313 | } | 312 | } |
314 | if(skipping<0) return 0; | 313 | if(skipping<0) return 0; |
315 | bool rp = parse(inbuf,bytes,false); | 314 | bool rp = parse(inbuf,bytes,false); |
316 | if(!rp) { | 315 | if(!rp) { |
317 | parser_choked = true; | 316 | parser_choked = true; |
318 | skipping = -1; | 317 | skipping = -1; |
319 | if(!(xmode&xmode_html)) | 318 | if(!(xmode&xmode_html)) |
320 | bytes = 0; | 319 | bytes = 0; |
321 | } | 320 | } |
322 | return bytes; | 321 | return bytes; |
323 | } | 322 | } |
324 | size_t header(void *p,size_t s,size_t nm) { | 323 | size_t header(void *p,size_t s,size_t nm) { |
325 | size_t bytes = s*nm; | 324 | size_t bytes = s*nm; |
326 | const char *h = (const char*)p; | 325 | const char *h = (const char*)p; |
327 | const char *colon = (const char*)memchr(p,':',bytes); | 326 | const char *colon = (const char*)memchr(p,':',bytes); |
328 | const char *space = (const char*)memchr(p,' ',bytes); | 327 | const char *space = (const char*)memchr(p,' ',bytes); |
329 | if(space && ( (!colon) || space<colon ) ) { | 328 | if(space && ( (!colon) || space<colon ) ) { |
330 | xrds_location.clear(); http_content_type.clear(); | 329 | xrds_location.clear(); http_content_type.clear(); |
331 | }else if(colon) { | 330 | }else if(colon) { |
332 | const char *hv = ++colon; | 331 | const char *hv = ++colon; |
333 | size_t hnl = colon-h; | 332 | size_t hnl = colon-h; |
334 | int rb; | 333 | int rb; |
335 | for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb); | 334 | for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb); |
336 | while(rb>0 && isspace(hv[rb-1])) --rb; | 335 | while(rb>0 && isspace(hv[rb-1])) --rb; |
337 | if(rb) { | 336 | if(rb) { |
338 | if( (hnl>=sizeof(XRDS_HEADER)) | 337 | if( (hnl>=sizeof(XRDS_HEADER)) |
339 | && !strncasecmp(h,XRDS_HEADER":", | 338 | && !strncasecmp(h,XRDS_HEADER":", |
340 | sizeof(XRDS_HEADER)) ) { | 339 | sizeof(XRDS_HEADER)) ) { |
341 | xrds_location.assign(hv,rb); | 340 | xrds_location.assign(hv,rb); |
342 | }else if( (hnl>=sizeof(CT_HEADER)) | 341 | }else if( (hnl>=sizeof(CT_HEADER)) |
343 | && !strncasecmp(h,CT_HEADER":", | 342 | && !strncasecmp(h,CT_HEADER":", |
344 | sizeof(CT_HEADER)) ) { | 343 | sizeof(CT_HEADER)) ) { |
345 | const char *sc = (const char*)memchr( | 344 | const char *sc = (const char*)memchr( |
346 | hv,';',rb); | 345 | hv,';',rb); |
347 | http_content_type.assign(hv,sc?(sc-hv):rb); | 346 | http_content_type.assign(hv,sc?(sc-hv):rb); |
348 | } | 347 | } |
349 | } | 348 | } |
350 | } | 349 | } |
351 | return curl_t::header(p,s,nm); | 350 | return curl_t::header(p,s,nm); |
352 | } | 351 | } |
353 | 352 | ||
354 | void start_element(const XML_Char *n,const XML_Char **a) { | 353 | void start_element(const XML_Char *n,const XML_Char **a) { |
355 | if(skipping<0) return; | 354 | if(skipping<0) return; |
356 | if(skipping) { | 355 | if(skipping) { |
357 | if(xmode&xmode_html) | 356 | if(xmode&xmode_html) |
358 | html_start_element(n,a); | 357 | html_start_element(n,a); |
359 | ++skipping; return; | 358 | ++skipping; return; |
360 | } | 359 | } |
361 | if(pt_stack.empty()) { | 360 | if(pt_stack.empty()) { |
362 | if(is_qelement(n,NSURI_XRDS "\tXRDS")) | 361 | if(is_qelement(n,NSURI_XRDS "\tXRDS")) |
363 | return; | 362 | return; |
364 | if(is_qelement(n,NSURI_XRD "\tXRD")) { | 363 | if(is_qelement(n,NSURI_XRD "\tXRD")) { |
365 | assert(xrd); | 364 | assert(xrd); |
366 | xrd->clear(); | 365 | xrd->clear(); |
367 | pt_stack.push_back(n); | 366 | pt_stack.push_back(n); |
368 | }else if(xmode&xmode_html) { | 367 | }else if(xmode&xmode_html) { |
369 | html_start_element(n,a); | 368 | html_start_element(n,a); |
370 | }else{ | 369 | }else{ |
371 | skipping = -1; | 370 | skipping = -1; |
372 | } | 371 | } |
373 | }else{ | 372 | }else{ |
374 | int pt_s = pt_stack.size(); | 373 | int pt_s = pt_stack.size(); |
375 | if(pt_s==1) { | 374 | if(pt_s==1) { |
376 | if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { | 375 | if(is_qelement(n,NSURI_XRD "\tCanonicalID")) { |
377 | assert(xrd); | 376 | assert(xrd); |
378 | cdata = &(xrd->canonical_ids.add(element_priority(a),string())); | 377 | cdata = &(xrd->canonical_ids.add(element_priority(a),string())); |
379 | }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { | 378 | }else if(is_qelement(n,NSURI_XRD "\tLocalID")) { |
380 | assert(xrd); | 379 | assert(xrd); |
381 | cdata = &(xrd->local_ids.add(element_priority(a),string())); | 380 | cdata = &(xrd->local_ids.add(element_priority(a),string())); |
382 | }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { | 381 | }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { |
383 | assert(xrd); | 382 | assert(xrd); |
384 | cdata = &(xrd->provider_id); | 383 | cdata = &(xrd->provider_id); |
385 | }else if(is_qelement(n,NSURI_XRD "\tService")) { | 384 | }else if(is_qelement(n,NSURI_XRD "\tService")) { |
386 | assert(xrd); | 385 | assert(xrd); |
387 | xrd_service = &(xrd->services.add(element_priority(a), | 386 | xrd_service = &(xrd->services.add(element_priority(a), |
388 | service_t())); | 387 | service_t())); |
389 | pt_stack.push_back(n); | 388 | pt_stack.push_back(n); |
390 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { | 389 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { |
391 | for(;*a;) { | 390 | for(;*a;) { |
392 | if(!strcasecmp(*(a++),"code")) { | 391 | if(!strcasecmp(*(a++),"code")) { |
393 | if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { | 392 | if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) { |
394 | cdata = &status_string; | 393 | cdata = &status_string; |
395 | pt_stack.push_back(n); | 394 | pt_stack.push_back(n); |
396 | break; | 395 | break; |
397 | } | 396 | } |
398 | }else | 397 | }else |
399 | ++a; | 398 | ++a; |
400 | } | 399 | } |
401 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { | 400 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { |
402 | assert(xrd); | 401 | assert(xrd); |
403 | cdata_buf.clear(); | 402 | cdata_buf.clear(); |
404 | cdata = &cdata_buf; | 403 | cdata = &cdata_buf; |
405 | }else if(xmode&xmode_html) { | 404 | }else if(xmode&xmode_html) { |
406 | html_start_element(n,a); | 405 | html_start_element(n,a); |
407 | }else{ | 406 | }else{ |
408 | skipping = 1; | 407 | skipping = 1; |
409 | } | 408 | } |
410 | }else if(pt_s==2) { | 409 | }else if(pt_s==2) { |
411 | if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { | 410 | if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { |
412 | if(is_qelement(n,NSURI_XRD "\tType")) { | 411 | if(is_qelement(n,NSURI_XRD "\tType")) { |
413 | assert(xrd); assert(xrd_service); | 412 | assert(xrd); assert(xrd_service); |
414 | cdata_buf.clear(); | 413 | cdata_buf.clear(); |
415 | cdata = &cdata_buf; | 414 | cdata = &cdata_buf; |
416 | }else if(is_qelement(n,NSURI_XRD "\tURI")) { | 415 | }else if(is_qelement(n,NSURI_XRD "\tURI")) { |
417 | assert(xrd); assert(xrd_service); | 416 | assert(xrd); assert(xrd_service); |
418 | const char *append = element_attr(a,"append"); | 417 | const char *append = element_attr(a,"append"); |
419 | xrd::uri_t& uri = xrd_service->uris.add(element_priority(a),xrd::uri_t("",append?append:"")); | 418 | xrd::uri_t& uri = xrd_service->uris.add(element_priority(a),xrd::uri_t("",append?append:"")); |
420 | cdata = &uri.uri; | 419 | cdata = &uri.uri; |
421 | }else if(is_qelement(n,NSURI_XRD "\tLocalID") | 420 | }else if(is_qelement(n,NSURI_XRD "\tLocalID") |
422 | || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { | 421 | || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { |
423 | assert(xrd); assert(xrd_service); | 422 | assert(xrd); assert(xrd_service); |
424 | cdata = &(xrd_service->local_ids.add(element_priority(a),string())); | 423 | cdata = &(xrd_service->local_ids.add(element_priority(a),string())); |
425 | }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { | 424 | }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { |
426 | assert(xrd); assert(xrd_service); | 425 | assert(xrd); assert(xrd_service); |
427 | cdata = &(xrd_service->provider_id); | 426 | cdata = &(xrd_service->provider_id); |
428 | }else{ | 427 | }else{ |
429 | skipping = 1; | 428 | skipping = 1; |
430 | } | 429 | } |
431 | }else | 430 | }else |
432 | skipping = 1; | 431 | skipping = 1; |
433 | }else if(xmode&xmode_html) { | 432 | }else if(xmode&xmode_html) { |
434 | html_start_element(n,a); | 433 | html_start_element(n,a); |
435 | }else{ | 434 | }else{ |
436 | skipping = 1; | 435 | skipping = 1; |
437 | } | 436 | } |
438 | } | 437 | } |
439 | } | 438 | } |
440 | void end_element(const XML_Char *n) { | 439 | void end_element(const XML_Char *n) { |
441 | if(skipping<0) return; | 440 | if(skipping<0) return; |
442 | if(skipping) { | 441 | if(skipping) { |
443 | --skipping; return; | 442 | --skipping; return; |
444 | } | 443 | } |
445 | if(is_qelement(n,NSURI_XRD "\tType")) { | 444 | if(is_qelement(n,NSURI_XRD "\tType")) { |
446 | assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); | 445 | assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf); |
447 | xrd_service->types.insert(cdata_buf); | 446 | xrd_service->types.insert(cdata_buf); |
448 | }else if(is_qelement(n,NSURI_XRD "\tService")) { | 447 | }else if(is_qelement(n,NSURI_XRD "\tService")) { |
449 | assert(xrd); assert(xrd_service); | 448 | assert(xrd); assert(xrd_service); |
450 | assert(!pt_stack.empty()); | 449 | assert(!pt_stack.empty()); |
451 | assert(pt_stack.back()==(NSURI_XRD "\tService")); | 450 | assert(pt_stack.back()==(NSURI_XRD "\tService")); |
452 | pt_stack.pop_back(); | 451 | pt_stack.pop_back(); |
453 | xrd_service = 0; | 452 | xrd_service = 0; |
454 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { | 453 | }else if(is_qelement(n,NSURI_XRD "\tStatus")) { |
455 | assert(xrd); | 454 | assert(xrd); |
456 | if(is_qelement(pt_stack.back().c_str(),n)) { | 455 | if(is_qelement(pt_stack.back().c_str(),n)) { |
457 | assert(cdata==&status_string); | 456 | assert(cdata==&status_string); |
458 | pt_stack.pop_back(); | 457 | pt_stack.pop_back(); |
459 | if(status_code!=100) | 458 | if(status_code!=100) |
460 | skipping = -1; | 459 | skipping = -1; |
461 | } | 460 | } |
462 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { | 461 | }else if(is_qelement(n,NSURI_XRD "\tExpires")) { |
463 | assert(xrd); | 462 | assert(xrd); |
464 | xrd->expires = util::w3c_to_time(cdata_buf); | 463 | xrd->expires = util::w3c_to_time(cdata_buf); |
465 | }else if((xmode&xmode_html) && is_element(n,"head")) { | 464 | }else if((xmode&xmode_html) && is_element(n,"head")) { |
466 | skipping = -1; | 465 | skipping = -1; |
467 | } | 466 | } |
468 | cdata = 0; | 467 | cdata = 0; |
469 | } | 468 | } |
470 | void character_data(const XML_Char *s,int l) { | 469 | void character_data(const XML_Char *s,int l) { |
471 | if(skipping) return; | 470 | if(skipping) return; |
472 | if(cdata) cdata->append(s,l); | 471 | if(cdata) cdata->append(s,l); |
473 | } | 472 | } |
474 | 473 | ||
475 | void html_start_element(const XML_Char *n,const XML_Char **a) { | 474 | void html_start_element(const XML_Char *n,const XML_Char **a) { |
476 | if(is_element(n,"meta")) { | 475 | if(is_element(n,"meta")) { |
477 | bool heq = false; | 476 | bool heq = false; |
478 | string l; | 477 | string l; |
479 | for(;*a;a+=2) { | 478 | for(;*a;a+=2) { |
480 | if(!( strcasecmp(a[0],"http-equiv") | 479 | if(!( strcasecmp(a[0],"http-equiv") |
481 | || strcasecmp(a[1],XRDS_HEADER) )) | 480 | || strcasecmp(a[1],XRDS_HEADER) )) |
482 | heq = true; | 481 | heq = true; |
483 | else if(!strcasecmp(a[0],"content")) | 482 | else if(!strcasecmp(a[0],"content")) |
484 | l.assign(a[1]); | 483 | l.assign(a[1]); |
485 | } | 484 | } |
486 | if(heq) | 485 | if(heq) |
487 | xrds_location = l; | 486 | xrds_location = l; |
488 | }else if(is_element(n,"link")) { | 487 | }else if(is_element(n,"link")) { |
489 | string rels; | 488 | string rels; |
490 | string href; | 489 | string href; |
491 | for(;*a;a+=2) { | 490 | for(;*a;a+=2) { |
492 | if( !strcasecmp(a[0],"rel") ) { | 491 | if( !strcasecmp(a[0],"rel") ) { |
493 | rels.assign(a[1]); | 492 | rels.assign(a[1]); |
494 | }else if( !strcasecmp(a[0],"href") ) { | 493 | }else if( !strcasecmp(a[0],"href") ) { |
495 | const char *ns = a[1]; | 494 | const char *ns = a[1]; |
496 | for(;*ns && isspace(*ns);++ns); | 495 | for(;*ns && isspace(*ns);++ns); |
497 | href.assign(ns); | 496 | href.assign(ns); |
498 | string::size_type lns=href.find_last_not_of(whitespace); | 497 | string::size_type lns=href.find_last_not_of(data::_whitespace_chars); |
499 | href.erase(lns+1); | 498 | href.erase(lns+1); |
500 | } | 499 | } |
501 | } | 500 | } |
502 | for(string::size_type ns=rels.find_first_not_of(whitespace); | 501 | for(string::size_type ns=rels.find_first_not_of(data::_whitespace_chars); |
503 | ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) { | 502 | ns!=string::npos; ns=rels.find_first_not_of(data::_whitespace_chars,ns)) { |
504 | string::size_type s = rels.find_first_of(whitespace,ns); | 503 | string::size_type s = rels.find_first_of(data::_whitespace_chars,ns); |
505 | string rel; | 504 | string rel; |
506 | if(s==string::npos) { | 505 | if(s==string::npos) { |
507 | rel.assign(rels,ns,string::npos); | 506 | rel.assign(rels,ns,string::npos); |
508 | ns = string::npos; | 507 | ns = string::npos; |
509 | }else{ | 508 | }else{ |
510 | rel.assign(rels,ns,s-ns); | 509 | rel.assign(rels,ns,s-ns); |
511 | ns = s; | 510 | ns = s; |
512 | } | 511 | } |
513 | if(rel=="openid.server") | 512 | if(rel=="openid.server") |
514 | html_openid1.uris.add(-1,xrd::uri_t(href)); | 513 | html_openid1.uris.add(-1,xrd::uri_t(href)); |
515 | else if(rel=="openid.delegate") | 514 | else if(rel=="openid.delegate") |
516 | html_openid1.local_ids.add(-1,href); | 515 | html_openid1.local_ids.add(-1,href); |
517 | else if(rel=="openid2.provider") | 516 | else if(rel=="openid2.provider") |
518 | html_openid2.uris.add(-1,xrd::uri_t(href)); | 517 | html_openid2.uris.add(-1,xrd::uri_t(href)); |
519 | else if(rel=="openid2.local_id") | 518 | else if(rel=="openid2.local_id") |
520 | html_openid2.local_ids.add(-1,href); | 519 | html_openid2.local_ids.add(-1,href); |
521 | } | 520 | } |
522 | }else if(is_element(n,"body")) { | 521 | }else if(is_element(n,"body")) { |
523 | skipping = -1; | 522 | skipping = -1; |
524 | } | 523 | } |
525 | } | 524 | } |
526 | 525 | ||
527 | void queue_endpoints(endpoint_discovery_iterator& oi, | 526 | void queue_endpoints(endpoint_discovery_iterator& oi, |
528 | const idiscovery_t &id, | 527 | const idiscovery_t &id, |
529 | const service_type_t *st) { | 528 | const service_type_t *st) { |
530 | openid_endpoint_t ep; | 529 | openid_endpoint_t ep; |
531 | ep.claimed_id = id.canonicalized_id; | 530 | ep.claimed_id = id.canonicalized_id; |
532 | for(xrd::services_t::const_iterator isvc=id.xrd.services.begin(); | 531 | for(xrd::services_t::const_iterator isvc=id.xrd.services.begin(); |
533 | isvc!=id.xrd.services.end(); ++isvc) { | 532 | isvc!=id.xrd.services.end(); ++isvc) { |
534 | const xrd::service_t svc = isvc->second; | 533 | const xrd::service_t svc = isvc->second; |
535 | if(svc.types.find(st->uri)==svc.types.end()) continue; | 534 | if(svc.types.find(st->uri)==svc.types.end()) continue; |
536 | for(xrd::uris_t::const_iterator iu=svc.uris.begin();iu!=svc.uris.end();++iu) { | 535 | for(xrd::uris_t::const_iterator iu=svc.uris.begin();iu!=svc.uris.end();++iu) { |
537 | ep.uri = iu->second.uri; | 536 | ep.uri = iu->second.uri; |
538 | if(id.xri_identity) { | 537 | if(id.xri_identity) { |
539 | if(iu->second.append=="qxri") { | 538 | if(iu->second.append=="qxri") { |
540 | ep.uri += id.normalized_id; | 539 | ep.uri += id.normalized_id; |
541 | } /* TODO: else handle other append attribute values */ | 540 | } /* TODO: else handle other append attribute values */ |
542 | } | 541 | } |
543 | if(st->forceid) { | 542 | if(st->forceid) { |
544 | ep.local_id = ep.claimed_id = st->forceid; | 543 | ep.local_id = ep.claimed_id = st->forceid; |
545 | *(oi++) = ep; | 544 | *(oi++) = ep; |
546 | }else{ | 545 | }else{ |
547 | if(svc.local_ids.empty()) { | 546 | if(svc.local_ids.empty()) { |
548 | ep.local_id = ep.claimed_id; | 547 | ep.local_id = ep.claimed_id; |
549 | *(oi++) = ep; | 548 | *(oi++) = ep; |
550 | }else{ | 549 | }else{ |
551 | for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin(); | 550 | for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin(); |
552 | ilid!=svc.local_ids.end(); ++ilid) { | 551 | ilid!=svc.local_ids.end(); ++ilid) { |
553 | ep.local_id = ilid->second; | 552 | ep.local_id = ilid->second; |
554 | *(oi++) = ep; | 553 | *(oi++) = ep; |
555 | } | 554 | } |
556 | } | 555 | } |
557 | } | 556 | } |
558 | } | 557 | } |
559 | } | 558 | } |
560 | } | 559 | } |
561 | 560 | ||
562 | }; | 561 | }; |
563 | 562 | ||
564 | string idiscover(endpoint_discovery_iterator oi,const string& identity) { | 563 | string idiscover(endpoint_discovery_iterator oi,const string& identity) { |
565 | idigger_t idigger; | 564 | idigger_t idigger; |
566 | return idigger.discover(oi,identity); | 565 | return idigger.discover(oi,identity); |
567 | } | 566 | } |
568 | 567 | ||
569 | void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) try { | 568 | void yadiscover(endpoint_discovery_iterator oi,const string& yurl,const char **types,bool redirs) try { |
570 | idigger_t idigger; | 569 | idigger_t idigger; |
571 | idigger.yadiscover(oi,yurl,types,redirs); | 570 | idigger.yadiscover(oi,yurl,types,redirs); |
572 | }catch(exception_curl& ec) { | 571 | }catch(exception_curl& ec) { |
573 | if(redirs || ec._error!=CURLE_TOO_MANY_REDIRECTS) | 572 | if(redirs || ec._error!=CURLE_TOO_MANY_REDIRECTS) |
574 | throw; | 573 | throw; |
575 | } | 574 | } |
576 | 575 | ||
577 | } | 576 | } |
diff --git a/lib/util.cc b/lib/util.cc index bb8a2e8..29e6738 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,435 +1,470 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <cctype> | 3 | #include <cctype> |
4 | #include <cstring> | 4 | #include <cstring> |
5 | #include <vector> | 5 | #include <vector> |
6 | #include <string> | 6 | #include <string> |
7 | #include <stack> | 7 | #include <stack> |
8 | #include <algorithm> | 8 | #include <algorithm> |
9 | #include <openssl/bio.h> | 9 | #include <openssl/bio.h> |
10 | #include <openssl/evp.h> | 10 | #include <openssl/evp.h> |
11 | #include <openssl/sha.h> | 11 | #include <openssl/sha.h> |
12 | #include <openssl/hmac.h> | 12 | #include <openssl/hmac.h> |
13 | #include <curl/curl.h> | 13 | #include <curl/curl.h> |
14 | #include <opkele/util.h> | 14 | #include <opkele/util.h> |
15 | #include <opkele/exception.h> | 15 | #include <opkele/exception.h> |
16 | #include <opkele/data.h> | ||
16 | #include <opkele/debug.h> | 17 | #include <opkele/debug.h> |
17 | 18 | ||
18 | #include <config.h> | 19 | #include <config.h> |
19 | #ifdef HAVE_DEMANGLE | 20 | #ifdef HAVE_DEMANGLE |
20 | # include <cxxabi.h> | 21 | # include <cxxabi.h> |
21 | #endif | 22 | #endif |
22 | 23 | ||
23 | namespace opkele { | 24 | namespace opkele { |
24 | using namespace std; | 25 | using namespace std; |
25 | 26 | ||
26 | namespace util { | 27 | namespace util { |
27 | 28 | ||
28 | /* | 29 | /* |
29 | * base64 | 30 | * base64 |
30 | */ | 31 | */ |
31 | string encode_base64(const void *data,size_t length) { | 32 | string encode_base64(const void *data,size_t length) { |
32 | BIO *b64 = 0, *bmem = 0; | 33 | BIO *b64 = 0, *bmem = 0; |
33 | try { | 34 | try { |
34 | b64 = BIO_new(BIO_f_base64()); | 35 | b64 = BIO_new(BIO_f_base64()); |
35 | if(!b64) | 36 | if(!b64) |
36 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 37 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
37 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 38 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
38 | bmem = BIO_new(BIO_s_mem()); | 39 | bmem = BIO_new(BIO_s_mem()); |
39 | BIO_set_flags(b64,BIO_CLOSE); | 40 | BIO_set_flags(b64,BIO_CLOSE); |
40 | if(!bmem) | 41 | if(!bmem) |
41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 42 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
42 | BIO_push(b64,bmem); | 43 | BIO_push(b64,bmem); |
43 | if(((size_t)BIO_write(b64,data,length))!=length) | 44 | if(((size_t)BIO_write(b64,data,length))!=length) |
44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 45 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
45 | if(BIO_flush(b64)!=1) | 46 | if(BIO_flush(b64)!=1) |
46 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | 47 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); |
47 | char *rvd; | 48 | char *rvd; |
48 | long rvl = BIO_get_mem_data(bmem,&rvd); | 49 | long rvl = BIO_get_mem_data(bmem,&rvd); |
49 | string rv(rvd,rvl); | 50 | string rv(rvd,rvl); |
50 | BIO_free_all(b64); | 51 | BIO_free_all(b64); |
51 | return rv; | 52 | return rv; |
52 | }catch(...) { | 53 | }catch(...) { |
53 | if(b64) BIO_free_all(b64); | 54 | if(b64) BIO_free_all(b64); |
54 | throw; | 55 | throw; |
55 | } | 56 | } |
56 | } | 57 | } |
57 | 58 | ||
58 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 59 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
59 | BIO *b64 = 0, *bmem = 0; | 60 | BIO *b64 = 0, *bmem = 0; |
60 | rv.clear(); | 61 | rv.clear(); |
61 | try { | 62 | try { |
62 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | 63 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); |
63 | if(!bmem) | 64 | if(!bmem) |
64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 65 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
65 | b64 = BIO_new(BIO_f_base64()); | 66 | b64 = BIO_new(BIO_f_base64()); |
66 | if(!b64) | 67 | if(!b64) |
67 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 68 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
68 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 69 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
69 | BIO_push(b64,bmem); | 70 | BIO_push(b64,bmem); |
70 | unsigned char tmp[512]; | 71 | unsigned char tmp[512]; |
71 | size_t rb = 0; | 72 | size_t rb = 0; |
72 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 73 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
73 | rv.insert(rv.end(),tmp,&tmp[rb]); | 74 | rv.insert(rv.end(),tmp,&tmp[rb]); |
74 | BIO_free_all(b64); | 75 | BIO_free_all(b64); |
75 | }catch(...) { | 76 | }catch(...) { |
76 | if(b64) BIO_free_all(b64); | 77 | if(b64) BIO_free_all(b64); |
77 | throw; | 78 | throw; |
78 | } | 79 | } |
79 | } | 80 | } |
80 | 81 | ||
81 | /* | 82 | /* |
82 | * big numerics | 83 | * big numerics |
83 | */ | 84 | */ |
84 | 85 | ||
85 | BIGNUM *base64_to_bignum(const string& b64) { | 86 | BIGNUM *base64_to_bignum(const string& b64) { |
86 | vector<unsigned char> bin; | 87 | vector<unsigned char> bin; |
87 | decode_base64(b64,bin); | 88 | decode_base64(b64,bin); |
88 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 89 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
89 | if(!rv) | 90 | if(!rv) |
90 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 91 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
91 | return rv; | 92 | return rv; |
92 | } | 93 | } |
93 | 94 | ||
94 | BIGNUM *dec_to_bignum(const string& dec) { | 95 | BIGNUM *dec_to_bignum(const string& dec) { |
95 | BIGNUM *rv = 0; | 96 | BIGNUM *rv = 0; |
96 | if(!BN_dec2bn(&rv,dec.c_str())) | 97 | if(!BN_dec2bn(&rv,dec.c_str())) |
97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 98 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
98 | return rv; | 99 | return rv; |
99 | } | 100 | } |
100 | 101 | ||
101 | string bignum_to_base64(const BIGNUM *bn) { | 102 | string bignum_to_base64(const BIGNUM *bn) { |
102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 103 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
103 | unsigned char *binptr = &(bin.front())+1; | 104 | unsigned char *binptr = &(bin.front())+1; |
104 | int l = BN_bn2bin(bn,binptr); | 105 | int l = BN_bn2bin(bn,binptr); |
105 | if(l && (*binptr)&0x80){ | 106 | if(l && (*binptr)&0x80){ |
106 | (*(--binptr)) = 0; ++l; | 107 | (*(--binptr)) = 0; ++l; |
107 | } | 108 | } |
108 | return encode_base64(binptr,l); | 109 | return encode_base64(binptr,l); |
109 | } | 110 | } |
110 | 111 | ||
111 | /* | 112 | /* |
112 | * w3c times | 113 | * w3c times |
113 | */ | 114 | */ |
114 | 115 | ||
115 | string time_to_w3c(time_t t) { | 116 | string time_to_w3c(time_t t) { |
116 | struct tm tm_t; | 117 | struct tm tm_t; |
117 | if(!gmtime_r(&t,&tm_t)) | 118 | if(!gmtime_r(&t,&tm_t)) |
118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 119 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
119 | char rv[25]; | 120 | char rv[25]; |
120 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 121 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 122 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
122 | return rv; | 123 | return rv; |
123 | } | 124 | } |
124 | 125 | ||
125 | time_t w3c_to_time(const string& w) { | 126 | time_t w3c_to_time(const string& w) { |
126 | int fraction; | 127 | int fraction; |
127 | struct tm tm_t; | 128 | struct tm tm_t; |
128 | memset(&tm_t,0,sizeof(tm_t)); | 129 | memset(&tm_t,0,sizeof(tm_t)); |
129 | if( ( | 130 | if( ( |
130 | sscanf( | 131 | sscanf( |
131 | w.c_str(), | 132 | w.c_str(), |
132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 133 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
133 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 134 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
134 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 135 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
135 | ) != 6 | 136 | ) != 6 |
136 | ) && ( | 137 | ) && ( |
137 | sscanf( | 138 | sscanf( |
138 | w.c_str(), | 139 | w.c_str(), |
139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 140 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
140 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 141 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
141 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, | 142 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
142 | &fraction | 143 | &fraction |
143 | ) != 7 | 144 | ) != 7 |
144 | ) ) | 145 | ) ) |
145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 146 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
146 | tm_t.tm_mon--; | 147 | tm_t.tm_mon--; |
147 | tm_t.tm_year-=1900; | 148 | tm_t.tm_year-=1900; |
148 | time_t rv = mktime(&tm_t); | 149 | time_t rv = mktime(&tm_t); |
149 | if(rv==(time_t)-1) | 150 | if(rv==(time_t)-1) |
150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 151 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
151 | return rv-timezone; | 152 | return rv-timezone; |
152 | } | 153 | } |
153 | 154 | ||
154 | /* | 155 | /* |
155 | * | 156 | * |
156 | */ | 157 | */ |
157 | 158 | ||
158 | string url_encode(const string& str) { | 159 | string url_encode(const string& str) { |
159 | char * t = curl_escape(str.c_str(),str.length()); | 160 | char * t = curl_escape(str.c_str(),str.length()); |
160 | if(!t) | 161 | if(!t) |
161 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 162 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
162 | string rv(t); | 163 | string rv(t); |
163 | curl_free(t); | 164 | curl_free(t); |
164 | return rv; | 165 | return rv; |
165 | } | 166 | } |
166 | 167 | ||
167 | string attr_escape(const string& str) { | 168 | string attr_escape(const string& str) { |
168 | static const char *unsafechars = "<>&\n\"'"; | 169 | static const char *unsafechars = "<>&\n\"'"; |
169 | string rv; | 170 | string rv; |
170 | string::size_type p=0; | 171 | string::size_type p=0; |
171 | while(true) { | 172 | while(true) { |
172 | string::size_type us = str.find_first_of(unsafechars,p); | 173 | string::size_type us = str.find_first_of(unsafechars,p); |
173 | if(us==string::npos) { | 174 | if(us==string::npos) { |
174 | if(p!=str.length()) | 175 | if(p!=str.length()) |
175 | rv.append(str,p,str.length()-p); | 176 | rv.append(str,p,str.length()-p); |
176 | return rv; | 177 | return rv; |
177 | } | 178 | } |
178 | rv.append(str,p,us-p); | 179 | rv.append(str,p,us-p); |
179 | rv += "&#"; | 180 | rv += "&#"; |
180 | rv += long_to_string((long)str[us]); | 181 | rv += long_to_string((long)str[us]); |
181 | rv += ';'; | 182 | rv += ';'; |
182 | p = us+1; | 183 | p = us+1; |
183 | } | 184 | } |
184 | } | 185 | } |
185 | 186 | ||
186 | string long_to_string(long l) { | 187 | string long_to_string(long l) { |
187 | char rv[32]; | 188 | char rv[32]; |
188 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 189 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
189 | if(r<0 || r>=(int)sizeof(rv)) | 190 | if(r<0 || r>=(int)sizeof(rv)) |
190 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 191 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
191 | return rv; | 192 | return rv; |
192 | } | 193 | } |
193 | 194 | ||
194 | long string_to_long(const string& s) { | 195 | long string_to_long(const string& s) { |
195 | char *endptr = 0; | 196 | char *endptr = 0; |
196 | long rv = strtol(s.c_str(),&endptr,10); | 197 | long rv = strtol(s.c_str(),&endptr,10); |
197 | if((!endptr) || endptr==s.c_str()) | 198 | if((!endptr) || endptr==s.c_str()) |
198 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 199 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
199 | return rv; | 200 | return rv; |
200 | } | 201 | } |
201 | 202 | ||
202 | /* | 203 | /* |
203 | * Normalize URL according to the rules, described in rfc 3986, section 6 | 204 | * Normalize URL according to the rules, described in rfc 3986, section 6 |
204 | * | 205 | * |
205 | * - uppercase hex triplets (e.g. %ab -> %AB) | 206 | * - uppercase hex triplets (e.g. %ab -> %AB) |
206 | * - lowercase scheme and host | 207 | * - lowercase scheme and host |
207 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, | 208 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, |
208 | * that is - [:alpha:][:digit:]._~- | 209 | * that is - [:alpha:][:digit:]._~- |
209 | * - remove dot segments | 210 | * - remove dot segments |
210 | * - remove empty and default ports | 211 | * - remove empty and default ports |
211 | * - if there's no path component, add '/' | 212 | * - if there's no path component, add '/' |
212 | */ | 213 | */ |
213 | string rfc_3986_normalize_uri(const string& uri) { | 214 | string rfc_3986_normalize_uri(const string& uri) { |
214 | static const char *whitespace = " \t\r\n"; | ||
215 | string rv; | 215 | string rv; |
216 | string::size_type ns = uri.find_first_not_of(whitespace); | 216 | string::size_type ns = uri.find_first_not_of(data::_whitespace_chars); |
217 | if(ns==string::npos) | 217 | if(ns==string::npos) |
218 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); | 218 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); |
219 | string::size_type colon = uri.find(':',ns); | 219 | string::size_type colon = uri.find(':',ns); |
220 | if(colon==string::npos) | 220 | if(colon==string::npos) |
221 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); | 221 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); |
222 | transform( | 222 | transform( |
223 | uri.begin()+ns, uri.begin()+colon+1, | 223 | uri.begin()+ns, uri.begin()+colon+1, |
224 | back_inserter(rv), ::tolower ); | 224 | back_inserter(rv), ::tolower ); |
225 | bool s; | 225 | bool s; |
226 | string::size_type ul = uri.find_last_not_of(whitespace)+1; | 226 | string::size_type ul = uri.find_last_not_of(data::_whitespace_chars)+1; |
227 | if(ul <= (colon+3)) | 227 | if(ul <= (colon+3)) |
228 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); | 228 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); |
229 | if(uri[colon+1]!='/' || uri[colon+2]!='/') | 229 | if(uri[colon+1]!='/' || uri[colon+2]!='/') |
230 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); | 230 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); |
231 | if(rv=="http:") | 231 | if(rv=="http:") |
232 | s = false; | 232 | s = false; |
233 | else if(rv=="https:") | 233 | else if(rv=="https:") |
234 | s = true; | 234 | s = true; |
235 | else{ | 235 | else{ |
236 | /* TODO: support more schemes. e.g. xri. How do we normalize | 236 | /* TODO: support more schemes. e.g. xri. How do we normalize |
237 | * xri? | 237 | * xri? |
238 | */ | 238 | */ |
239 | rv.append(uri,colon+1,ul-colon-1); | 239 | rv.append(uri,colon+1,ul-colon-1); |
240 | return rv; | 240 | return rv; |
241 | } | 241 | } |
242 | rv += "//"; | 242 | rv += "//"; |
243 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); | 243 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); |
244 | if(interesting==string::npos) { | 244 | if(interesting==string::npos) { |
245 | transform( | 245 | transform( |
246 | uri.begin()+colon+3,uri.begin()+ul, | 246 | uri.begin()+colon+3,uri.begin()+ul, |
247 | back_inserter(rv), ::tolower ); | 247 | back_inserter(rv), ::tolower ); |
248 | rv += '/'; return rv; | 248 | rv += '/'; return rv; |
249 | } | 249 | } |
250 | transform( | 250 | transform( |
251 | uri.begin()+colon+3,uri.begin()+interesting, | 251 | uri.begin()+colon+3,uri.begin()+interesting, |
252 | back_inserter(rv), ::tolower ); | 252 | back_inserter(rv), ::tolower ); |
253 | bool qf = false; | 253 | bool qf = false; |
254 | char ic = uri[interesting]; | 254 | char ic = uri[interesting]; |
255 | if(ic==':') { | 255 | if(ic==':') { |
256 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 256 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
257 | const char *nptr = uri.data()+interesting+1; | 257 | const char *nptr = uri.data()+interesting+1; |
258 | char *eptr = 0; | 258 | char *eptr = 0; |
259 | long port = strtol(nptr,&eptr,10); | 259 | long port = strtol(nptr,&eptr,10); |
260 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 260 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
261 | char tmp[8]; | 261 | char tmp[8]; |
262 | snprintf(tmp,sizeof(tmp),":%ld",port); | 262 | snprintf(tmp,sizeof(tmp),":%ld",port); |
263 | rv += tmp; | 263 | rv += tmp; |
264 | } | 264 | } |
265 | if(ni==string::npos) { | 265 | if(ni==string::npos) { |
266 | rv += '/'; return rv; | 266 | rv += '/'; return rv; |
267 | } | 267 | } |
268 | interesting = ni; | 268 | interesting = ni; |
269 | }else if(ic!='/') { | 269 | }else if(ic!='/') { |
270 | rv += '/'; rv += ic; | 270 | rv += '/'; rv += ic; |
271 | qf = true; | 271 | qf = true; |
272 | ++interesting; | 272 | ++interesting; |
273 | } | 273 | } |
274 | string::size_type n = interesting; | 274 | string::size_type n = interesting; |
275 | char tmp[3] = { 0,0,0 }; | 275 | char tmp[3] = { 0,0,0 }; |
276 | stack<string::size_type> psegs; psegs.push(rv.length()); | 276 | stack<string::size_type> psegs; psegs.push(rv.length()); |
277 | string pseg; | 277 | string pseg; |
278 | for(;n<ul;) { | 278 | for(;n<ul;) { |
279 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 279 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
280 | if(unsafe==string::npos) { | 280 | if(unsafe==string::npos) { |
281 | pseg.append(uri,n,ul-n-1); n = ul-1; | 281 | pseg.append(uri,n,ul-n-1); n = ul-1; |
282 | }else{ | 282 | }else{ |
283 | pseg.append(uri,n,unsafe-n); | 283 | pseg.append(uri,n,unsafe-n); |
284 | n = unsafe; | 284 | n = unsafe; |
285 | } | 285 | } |
286 | char c = uri[n++]; | 286 | char c = uri[n++]; |
287 | if(c=='%') { | 287 | if(c=='%') { |
288 | if((n+1)>=ul) | 288 | if((n+1)>=ul) |
289 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); | 289 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); |
290 | tmp[0] = uri[n++]; | 290 | tmp[0] = uri[n++]; |
291 | tmp[1] = uri[n++]; | 291 | tmp[1] = uri[n++]; |
292 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) | 292 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) |
293 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 293 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
294 | int cc = strtol(tmp,0,16); | 294 | int cc = strtol(tmp,0,16); |
295 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 295 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
296 | pseg += cc; | 296 | pseg += cc; |
297 | else{ | 297 | else{ |
298 | pseg += '%'; | 298 | pseg += '%'; |
299 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); | 299 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); |
300 | } | 300 | } |
301 | }else if(qf) { | 301 | }else if(qf) { |
302 | rv += pseg; rv += c; | 302 | rv += pseg; rv += c; |
303 | pseg.clear(); | 303 | pseg.clear(); |
304 | }else if(n>=ul || strchr("?/#",c)) { | 304 | }else if(n>=ul || strchr("?/#",c)) { |
305 | if(pseg.empty() || pseg==".") { | 305 | if(pseg.empty() || pseg==".") { |
306 | }else if(pseg=="..") { | 306 | }else if(pseg=="..") { |
307 | if(psegs.size()>1) { | 307 | if(psegs.size()>1) { |
308 | rv.resize(psegs.top()); psegs.pop(); | 308 | rv.resize(psegs.top()); psegs.pop(); |
309 | } | 309 | } |
310 | }else{ | 310 | }else{ |
311 | psegs.push(rv.length()); | 311 | psegs.push(rv.length()); |
312 | if(c!='/') { | 312 | if(c!='/') { |
313 | pseg += c; | 313 | pseg += c; |
314 | qf = true; | 314 | qf = true; |
315 | } | 315 | } |
316 | rv += '/'; rv += pseg; | 316 | rv += '/'; rv += pseg; |
317 | } | 317 | } |
318 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 318 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
319 | rv += '/'; | 319 | rv += '/'; |
320 | if(n<ul) | 320 | if(n<ul) |
321 | qf = true; | 321 | qf = true; |
322 | }else if(strchr("?#",c)) { | 322 | }else if(strchr("?#",c)) { |
323 | if(psegs.size()==1 && psegs.top()==rv.length()) | 323 | if(psegs.size()==1 && psegs.top()==rv.length()) |
324 | rv += '/'; | 324 | rv += '/'; |
325 | if(pseg.empty()) | 325 | if(pseg.empty()) |
326 | rv += c; | 326 | rv += c; |
327 | qf = true; | 327 | qf = true; |
328 | } | 328 | } |
329 | pseg.clear(); | 329 | pseg.clear(); |
330 | }else{ | 330 | }else{ |
331 | pseg += c; | 331 | pseg += c; |
332 | } | 332 | } |
333 | } | 333 | } |
334 | if(!pseg.empty()) { | 334 | if(!pseg.empty()) { |
335 | if(!qf) rv += '/'; | 335 | if(!qf) rv += '/'; |
336 | rv += pseg; | 336 | rv += pseg; |
337 | } | 337 | } |
338 | return rv; | 338 | return rv; |
339 | } | 339 | } |
340 | 340 | ||
341 | string& strip_uri_fragment_part(string& u) { | 341 | string& strip_uri_fragment_part(string& u) { |
342 | string::size_type q = u.find('?'), f = u.find('#'); | 342 | string::size_type q = u.find('?'), f = u.find('#'); |
343 | if(q==string::npos) { | 343 | if(q==string::npos) { |
344 | if(f!=string::npos) | 344 | if(f!=string::npos) |
345 | u.erase(f); | 345 | u.erase(f); |
346 | }else{ | 346 | }else{ |
347 | if(f!=string::npos) { | 347 | if(f!=string::npos) { |
348 | if(f<q) | 348 | if(f<q) |
349 | u.erase(f,q-f); | 349 | u.erase(f,q-f); |
350 | else | 350 | else |
351 | u.erase(f); | 351 | u.erase(f); |
352 | } | 352 | } |
353 | } | 353 | } |
354 | return u; | 354 | return u; |
355 | } | 355 | } |
356 | 356 | ||
357 | bool uri_matches_realm(const string& uri,const string& realm) { | 357 | bool uri_matches_realm(const string& uri,const string& realm) { |
358 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | 358 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); |
359 | string nu = opkele::util::rfc_3986_normalize_uri(uri); | 359 | string nu = opkele::util::rfc_3986_normalize_uri(uri); |
360 | string::size_type pr = nrealm.find("://"); | 360 | string::size_type pr = nrealm.find("://"); |
361 | string::size_type pu = nu.find("://"); | 361 | string::size_type pu = nu.find("://"); |
362 | assert(!(pr==string::npos || pu==string::npos)); | 362 | assert(!(pr==string::npos || pu==string::npos)); |
363 | pr += sizeof("://")-1; | 363 | pr += sizeof("://")-1; |
364 | pu += sizeof("://")-1; | 364 | pu += sizeof("://")-1; |
365 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | 365 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { |
366 | pr = nrealm.find('.',pr); | 366 | pr = nrealm.find('.',pr); |
367 | pu = nu.find('.',pu); | 367 | pu = nu.find('.',pu); |
368 | assert(pr!=string::npos); | 368 | assert(pr!=string::npos); |
369 | if(pu==string::npos) | 369 | if(pu==string::npos) |
370 | return false; | 370 | return false; |
371 | // TODO: check for overgeneralized realm | 371 | // TODO: check for overgeneralized realm |
372 | } | 372 | } |
373 | string::size_type lr = nrealm.length(); | 373 | string::size_type lr = nrealm.length(); |
374 | string::size_type lu = nu.length(); | 374 | string::size_type lu = nu.length(); |
375 | if( (lu-pu) < (lr-pr) ) | 375 | if( (lu-pu) < (lr-pr) ) |
376 | return false; | 376 | return false; |
377 | pair<const char*,const char*> mp = mismatch( | 377 | pair<const char*,const char*> mp = mismatch( |
378 | nrealm.c_str()+pr,nrealm.c_str()+lr, | 378 | nrealm.c_str()+pr,nrealm.c_str()+lr, |
379 | nu.c_str()+pu); | 379 | nu.c_str()+pu); |
380 | if( (*(mp.first-1))!='/' | 380 | if( (*(mp.first-1))!='/' |
381 | && !strchr("/?#",*mp.second) ) | 381 | && !strchr("/?#",*mp.second) ) |
382 | return false; | 382 | return false; |
383 | return true; | 383 | return true; |
384 | } | 384 | } |
385 | 385 | ||
386 | string abi_demangle(const char *mn) { | 386 | string abi_demangle(const char *mn) { |
387 | #ifndef HAVE_DEMANGLE | 387 | #ifndef HAVE_DEMANGLE |
388 | return mn; | 388 | return mn; |
389 | #else /* !HAVE_DEMANGLE */ | 389 | #else /* !HAVE_DEMANGLE */ |
390 | int dstat; | 390 | int dstat; |
391 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); | 391 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); |
392 | if(dstat) | 392 | if(dstat) |
393 | return mn; | 393 | return mn; |
394 | string rv = demangled; | 394 | string rv = demangled; |
395 | free(demangled); | 395 | free(demangled); |
396 | return rv; | 396 | return rv; |
397 | #endif /* !HAVE_DEMANGLE */ | 397 | #endif /* !HAVE_DEMANGLE */ |
398 | } | 398 | } |
399 | 399 | ||
400 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { | 400 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { |
401 | const string& slist = om.get_field("signed"); | 401 | const string& slist = om.get_field("signed"); |
402 | string kv; | 402 | string kv; |
403 | string::size_type p=0; | 403 | string::size_type p=0; |
404 | while(true) { | 404 | while(true) { |
405 | string::size_type co = slist.find(',',p); | 405 | string::size_type co = slist.find(',',p); |
406 | string f = (co==string::npos) | 406 | string f = (co==string::npos) |
407 | ?slist.substr(p):slist.substr(p,co-p); | 407 | ?slist.substr(p):slist.substr(p,co-p); |
408 | kv += f; | 408 | kv += f; |
409 | kv += ':'; | 409 | kv += ':'; |
410 | kv += om.get_field(f); | 410 | kv += om.get_field(f); |
411 | kv += '\n'; | 411 | kv += '\n'; |
412 | if(co==string::npos) break; | 412 | if(co==string::npos) break; |
413 | p = co+1; | 413 | p = co+1; |
414 | } | 414 | } |
415 | const secret_t& secret = assoc->secret(); | 415 | const secret_t& secret = assoc->secret(); |
416 | const EVP_MD *evpmd; | 416 | const EVP_MD *evpmd; |
417 | const string& at = assoc->assoc_type(); | 417 | const string& at = assoc->assoc_type(); |
418 | if(at=="HMAC-SHA256") | 418 | if(at=="HMAC-SHA256") |
419 | evpmd = EVP_sha256(); | 419 | evpmd = EVP_sha256(); |
420 | else if(at=="HMAC-SHA1") | 420 | else if(at=="HMAC-SHA1") |
421 | evpmd = EVP_sha1(); | 421 | evpmd = EVP_sha1(); |
422 | else | 422 | else |
423 | throw unsupported(OPKELE_CP_ "unknown association type"); | 423 | throw unsupported(OPKELE_CP_ "unknown association type"); |
424 | unsigned int md_len = 0; | 424 | unsigned int md_len = 0; |
425 | unsigned char md[SHA256_DIGEST_LENGTH]; | 425 | unsigned char md[SHA256_DIGEST_LENGTH]; |
426 | HMAC(evpmd, | 426 | HMAC(evpmd, |
427 | &(secret.front()),secret.size(), | 427 | &(secret.front()),secret.size(), |
428 | (const unsigned char*)kv.data(),kv.length(), | 428 | (const unsigned char*)kv.data(),kv.length(), |
429 | md,&md_len); | 429 | md,&md_len); |
430 | return encode_base64(md,md_len); | 430 | return encode_base64(md,md_len); |
431 | } | 431 | } |
432 | 432 | ||
433 | string normalize_identifier(const string& usi,bool strip_fragment) { | ||
434 | if(usi.empty()) | ||
435 | return usi; | ||
436 | string rv; | ||
437 | string::size_type fsc = usi.find_first_not_of(data::_whitespace_chars); | ||
438 | if(fsc==string::npos) | ||
439 | return rv; | ||
440 | string::size_type lsc = usi.find_last_not_of(data::_whitespace_chars); | ||
441 | assert(lsc!=string::npos); | ||
442 | if(!strncasecmp(usi.c_str()+fsc,"xri://",sizeof("xri://")-1)) | ||
443 | fsc += sizeof("xri://")-1; | ||
444 | if( (fsc+1) >= lsc ) | ||
445 | return rv; | ||
446 | rv.assign(usi,fsc,lsc-fsc+1); | ||
447 | if(strchr(data::_iname_leaders,rv[0])) { | ||
448 | /* TODO: further normalize xri identity, fold case or | ||
449 | * whatever... */ | ||
450 | }else{ | ||
451 | if(rv.find("://")==string::npos) | ||
452 | rv.insert(0,"http://"); | ||
453 | if(strip_fragment) { | ||
454 | string::size_type fp = rv.find('#'); | ||
455 | if(fp!=string::npos) { | ||
456 | string::size_type qp = rv.find('?'); | ||
457 | if(qp==string::npos || qp<fp) | ||
458 | rv.erase(fp); | ||
459 | else if(qp>fp) | ||
460 | rv.erase(fp,qp-fp); | ||
461 | } | ||
462 | } | ||
463 | rv = rfc_3986_normalize_uri(rv); | ||
464 | } | ||
465 | return rv; | ||
466 | } | ||
467 | |||
433 | } | 468 | } |
434 | 469 | ||
435 | } | 470 | } |