comparison src/c/openid.c @ 2:b757dc2bd2f6

Discovery returns a record
author Adam Chlipala <adam@chlipala.net>
date Sun, 26 Dec 2010 13:28:06 -0500
parents c230e6da3ff6
children f59083771ee2
comparison
equal deleted inserted replaced
1:c230e6da3ff6 2:b757dc2bd2f6
3 #include <openssl/sha.h> 3 #include <openssl/sha.h>
4 #include <curl/curl.h> 4 #include <curl/curl.h>
5 #include <expat.h> 5 #include <expat.h>
6 6
7 #include <openid.h> 7 #include <openid.h>
8
9 struct uw_OpenidFfi_discovery {
10 uw_Basis_string endpoint, localId;
11 };
12
13 uw_Basis_string uw_OpenidFfi_endpoint(uw_context ctx, uw_OpenidFfi_discovery d) {
14 return d->endpoint;
15 }
16
17 uw_Basis_string uw_OpenidFfi_localId(uw_context ctx, uw_OpenidFfi_discovery d) {
18 return d->localId;
19 }
8 20
9 uw_unit uw_OpenidFfi_init(uw_context ctx) { 21 uw_unit uw_OpenidFfi_init(uw_context ctx) {
10 curl_global_init(CURL_GLOBAL_ALL); 22 curl_global_init(CURL_GLOBAL_ALL);
11 23
12 return uw_unit_v; 24 return uw_unit_v;
24 return r; 36 return r;
25 } 37 }
26 38
27 typedef struct { 39 typedef struct {
28 uw_context ctx; 40 uw_context ctx;
29 char *result; 41 uw_OpenidFfi_discovery d;
30 } endpoint; 42 } endpoint;
31 43
32 static void XMLCALL startElement(void *userData, const XML_Char *name, const XML_Char **atts) { 44 static void XMLCALL startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
33 endpoint *ep = userData; 45 endpoint *ep = userData;
34 46
44 } 56 }
45 57
46 if (found) { 58 if (found) {
47 for (attp = atts; *attp; attp += 2) { 59 for (attp = atts; *attp; attp += 2) {
48 if (!strcmp(attp[0], "href")) { 60 if (!strcmp(attp[0], "href")) {
49 ep->result = uw_strdup(ep->ctx, attp[1]); 61 ep->d->endpoint = uw_strdup(ep->ctx, attp[1]);
50 return; 62 return;
51 } 63 }
52 } 64 }
53 } 65 }
54 } 66 }
69 d->any_errors = 1; 81 d->any_errors = 1;
70 82
71 return size * nmemb; 83 return size * nmemb;
72 } 84 }
73 85
74 uw_Basis_string uw_OpenidFfi_discover(uw_context ctx, uw_Basis_string id) { 86 uw_OpenidFfi_discovery *uw_OpenidFfi_discover(uw_context ctx, uw_Basis_string id) {
75 char *s; 87 char *s;
76 CURL *c = curl(ctx); 88 CURL *c = curl(ctx);
77 curl_data cd = {}; 89 curl_data cd = {};
78 endpoint ep = {ctx}; 90 uw_OpenidFfi_discovery dy = uw_malloc(ctx, sizeof(struct uw_OpenidFfi_discovery));
91 endpoint ep = {ctx, dy};
79 CURLcode code; 92 CURLcode code;
93
94 dy->endpoint = dy->localId = NULL;
80 95
81 if (!strchr(id, ':')) { 96 if (!strchr(id, ':')) {
82 id = uw_Basis_strcat(ctx, "http://", id); 97 id = uw_Basis_strcat(ctx, "http://", id);
83 if ((s = strchr(id, '#')) != NULL) 98 if ((s = strchr(id, '#')) != NULL)
84 *s = 0; 99 *s = 0;
99 curl_easy_setopt(c, CURLOPT_WRITEDATA, &cd); 114 curl_easy_setopt(c, CURLOPT_WRITEDATA, &cd);
100 115
101 code = curl_easy_perform(c); 116 code = curl_easy_perform(c);
102 uw_pop_cleanup(ctx); 117 uw_pop_cleanup(ctx);
103 118
104 if (code) 119 if (code || !ep.d->endpoint)
105 return NULL; 120 return NULL;
106 else 121 else {
107 return ep.result; 122 uw_OpenidFfi_discovery *dyp = malloc(sizeof(uw_OpenidFfi_discovery));
123 *dyp = ep.d;
124 return dyp;
125 }
108 } 126 }