comparison src/c/openid.c @ 27:f129ddee75f3

Some XRDS fixes; ignore query strings in naming endpoints for association purposes
author Adam Chlipala <adam@chlipala.net>
date Sun, 23 Jan 2011 17:40:42 -0500
parents ee97bc0e08fa
children 8d23d76b5d48
comparison
equal deleted inserted replaced
26:ee97bc0e08fa 27:f129ddee75f3
75 75
76 typedef struct { 76 typedef struct {
77 uw_context ctx; 77 uw_context ctx;
78 uw_OpenidFfi_discovery *d; 78 uw_OpenidFfi_discovery *d;
79 xrds_mode mode; 79 xrds_mode mode;
80 int cur_priority, max_priority;
80 } endpoint; 81 } endpoint;
81 82
82 static void XMLCALL startElement(void *userData, const XML_Char *name, const XML_Char **atts) { 83 static void XMLCALL startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
83 endpoint *ep = userData; 84 endpoint *ep = userData;
84 85
103 return; 104 return;
104 } 105 }
105 } 106 }
106 } 107 }
107 } 108 }
108 else if (!strcmp(name, "Service")) 109 else if (!strcmp(name, "Service")) {
110 const XML_Char **attp;
111
112 ep->cur_priority = 0;
113 for (attp = atts; *attp; attp += 2)
114 if (!strcmp(attp[0], "priority")) {
115 ep->cur_priority = atoi(attp[1]);
116 break;
117 }
118
109 ep->mode = SERVICE; 119 ep->mode = SERVICE;
110 else if (!strcmp(name, "Type")) { 120 } else if (!strcmp(name, "Type")) {
111 if (ep->mode == SERVICE) 121 if (ep->mode == SERVICE)
112 ep->mode = TYPE; 122 ep->mode = TYPE;
113 } 123 }
114 else if (!strcmp(name, "URI")) { 124 else if (!strcmp(name, "URI")) {
115 if (ep->mode == MATCHED) 125 if (ep->mode == MATCHED)
128 if ((len == sizeof(server)-1 && !memcmp(server, s, sizeof(server)-1)) 138 if ((len == sizeof(server)-1 && !memcmp(server, s, sizeof(server)-1))
129 || (len == sizeof(signon)-1 && !memcmp(signon, s, sizeof(signon)-1))) 139 || (len == sizeof(signon)-1 && !memcmp(signon, s, sizeof(signon)-1)))
130 ep->mode = MATCHED; 140 ep->mode = MATCHED;
131 break; 141 break;
132 case URI: 142 case URI:
133 ep->d->endpoint = uw_malloc(ep->ctx, len+1); 143 if (ep->cur_priority < ep->max_priority) {
134 memcpy(ep->d->endpoint, s, len); 144 ep->d->endpoint = uw_malloc(ep->ctx, len+1);
135 ep->d->endpoint[len] = 0; 145 memcpy(ep->d->endpoint, s, len);
146 ep->d->endpoint[len] = 0;
147 ep->max_priority = ep->cur_priority;
148 }
136 break; 149 break;
137 default: 150 default:
138 break; 151 break;
139 } 152 }
140 } 153 }
172 uw_OpenidFfi_discovery *uw_OpenidFfi_discover(uw_context ctx, uw_Basis_string id) { 185 uw_OpenidFfi_discovery *uw_OpenidFfi_discover(uw_context ctx, uw_Basis_string id) {
173 char *s; 186 char *s;
174 CURL *c = curl(ctx); 187 CURL *c = curl(ctx);
175 curl_discovery_data cd = {}; 188 curl_discovery_data cd = {};
176 uw_OpenidFfi_discovery *dy = uw_malloc(ctx, sizeof(uw_OpenidFfi_discovery)); 189 uw_OpenidFfi_discovery *dy = uw_malloc(ctx, sizeof(uw_OpenidFfi_discovery));
177 endpoint ep = {ctx, dy, NONE}; 190 endpoint ep = {ctx, dy, NONE, 0, INT_MAX};
178 CURLcode code; 191 CURLcode code;
192 struct curl_slist *headers = NULL;
179 193
180 dy->endpoint = dy->localId = NULL; 194 dy->endpoint = dy->localId = NULL;
181 195
182 if (!strchr(id, ':')) { 196 if (!strchr(id, ':')) {
183 id = uw_Basis_strcat(ctx, "http://", id); 197 id = uw_Basis_strcat(ctx, "http://", id);
198 212
199 curl_easy_reset(c); 213 curl_easy_reset(c);
200 curl_easy_setopt(c, CURLOPT_URL, id); 214 curl_easy_setopt(c, CURLOPT_URL, id);
201 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_discovery_data); 215 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_discovery_data);
202 curl_easy_setopt(c, CURLOPT_WRITEDATA, &cd); 216 curl_easy_setopt(c, CURLOPT_WRITEDATA, &cd);
217 curl_slist_append(headers, "Accept: application/xrds+xml");
218 uw_push_cleanup(ctx, (void (*)(void *))curl_slist_free_all, headers);
219 curl_easy_setopt(c, CURLOPT_HTTPHEADER, headers);
203 220
204 code = curl_easy_perform(c); 221 code = curl_easy_perform(c);
222 uw_pop_cleanup(ctx);
205 uw_pop_cleanup(ctx); 223 uw_pop_cleanup(ctx);
206 224
207 if (code || !dy->endpoint) 225 if (code || !dy->endpoint)
208 return NULL; 226 return NULL;
209 else 227 else
246 for (; *s; s = strchr(strchr(s, 0)+1, 0)+1) 264 for (; *s; s = strchr(strchr(s, 0)+1, 0)+1)
247 if (!strcmp(key, s)) 265 if (!strcmp(key, s))
248 return strchr(s, 0)+1; 266 return strchr(s, 0)+1;
249 267
250 return NULL; 268 return NULL;
269 }
270
271 uw_unit uw_OpenidFfi_printOutputs(uw_context ctx, uw_OpenidFfi_outputs buf) {
272 char *s = buf->start;
273
274 for (; *s; s = strchr(strchr(s, 0)+1, 0)+1)
275 fprintf(stderr, "%s => %s\n", s, strchr(s, 0)+1);
276
277 return uw_unit_v;
251 } 278 }
252 279
253 static size_t write_buffer_data(void *buffer, size_t size, size_t nmemb, void *userp) { 280 static size_t write_buffer_data(void *buffer, size_t size, size_t nmemb, void *userp) {
254 uw_buffer *buf = userp; 281 uw_buffer *buf = userp;
255 282