Mercurial > openid
changeset 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 |
files | include/openid.h src/c/openid.c src/ur/lib.urp src/ur/openid.ur src/ur/openid.urs src/ur/openidFfi.urs tests/test.ur |
diffstat | 7 files changed, 49 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/include/openid.h Sun Dec 26 13:10:36 2010 -0500 +++ b/include/openid.h Sun Dec 26 13:28:06 2010 -0500 @@ -1,4 +1,9 @@ #include <urweb/urweb.h> +typedef struct uw_OpenidFfi_discovery *uw_OpenidFfi_discovery; + +uw_Basis_string uw_OpenidFfi_endpoint(uw_context, uw_OpenidFfi_discovery); +uw_Basis_string uw_OpenidFfi_localId(uw_context, uw_OpenidFfi_discovery); + uw_unit uw_OpenidFfi_init(uw_context); -uw_Basis_string uw_OpenidFfi_discover(uw_context, uw_Basis_string id); +uw_OpenidFfi_discovery *uw_OpenidFfi_discover(uw_context, uw_Basis_string id);
--- a/src/c/openid.c Sun Dec 26 13:10:36 2010 -0500 +++ b/src/c/openid.c Sun Dec 26 13:28:06 2010 -0500 @@ -6,6 +6,18 @@ #include <openid.h> +struct uw_OpenidFfi_discovery { + uw_Basis_string endpoint, localId; +}; + +uw_Basis_string uw_OpenidFfi_endpoint(uw_context ctx, uw_OpenidFfi_discovery d) { + return d->endpoint; +} + +uw_Basis_string uw_OpenidFfi_localId(uw_context ctx, uw_OpenidFfi_discovery d) { + return d->localId; +} + uw_unit uw_OpenidFfi_init(uw_context ctx) { curl_global_init(CURL_GLOBAL_ALL); @@ -26,7 +38,7 @@ typedef struct { uw_context ctx; - char *result; + uw_OpenidFfi_discovery d; } endpoint; static void XMLCALL startElement(void *userData, const XML_Char *name, const XML_Char **atts) { @@ -46,7 +58,7 @@ if (found) { for (attp = atts; *attp; attp += 2) { if (!strcmp(attp[0], "href")) { - ep->result = uw_strdup(ep->ctx, attp[1]); + ep->d->endpoint = uw_strdup(ep->ctx, attp[1]); return; } } @@ -71,13 +83,16 @@ return size * nmemb; } -uw_Basis_string uw_OpenidFfi_discover(uw_context ctx, uw_Basis_string id) { +uw_OpenidFfi_discovery *uw_OpenidFfi_discover(uw_context ctx, uw_Basis_string id) { char *s; CURL *c = curl(ctx); curl_data cd = {}; - endpoint ep = {ctx}; + uw_OpenidFfi_discovery dy = uw_malloc(ctx, sizeof(struct uw_OpenidFfi_discovery)); + endpoint ep = {ctx, dy}; CURLcode code; + dy->endpoint = dy->localId = NULL; + if (!strchr(id, ':')) { id = uw_Basis_strcat(ctx, "http://", id); if ((s = strchr(id, '#')) != NULL) @@ -101,8 +116,11 @@ code = curl_easy_perform(c); uw_pop_cleanup(ctx); - if (code) + if (code || !ep.d->endpoint) return NULL; - else - return ep.result; + else { + uw_OpenidFfi_discovery *dyp = malloc(sizeof(uw_OpenidFfi_discovery)); + *dyp = ep.d; + return dyp; + } }
--- a/src/ur/lib.urp Sun Dec 26 13:10:36 2010 -0500 +++ b/src/ur/lib.urp Sun Dec 26 13:28:06 2010 -0500 @@ -4,4 +4,5 @@ effectful OpenidFfi.init effectful OpenidFfi.discover +$/option openid
--- a/src/ur/openid.ur Sun Dec 26 13:10:36 2010 -0500 +++ b/src/ur/openid.ur Sun Dec 26 13:28:06 2010 -0500 @@ -1,3 +1,6 @@ task initialize = fn () => OpenidFfi.init -val discover = OpenidFfi.discover +fun discover s = + r <- OpenidFfi.discover s; + return (Option.mp (fn r => {Endpoint = OpenidFfi.endpoint r, + LocalId = OpenidFfi.localId r}) r)
--- a/src/ur/openid.urs Sun Dec 26 13:10:36 2010 -0500 +++ b/src/ur/openid.urs Sun Dec 26 13:28:06 2010 -0500 @@ -1,1 +1,1 @@ -val discover : string -> transaction (option string) +val discover : string -> transaction (option {Endpoint : string, LocalId : option string})
--- a/src/ur/openidFfi.urs Sun Dec 26 13:10:36 2010 -0500 +++ b/src/ur/openidFfi.urs Sun Dec 26 13:28:06 2010 -0500 @@ -1,2 +1,6 @@ val init : transaction {} -val discover : string -> transaction (option string) + +type discovery +val discover : string -> transaction (option discovery) +val endpoint : discovery -> string +val localId : discovery -> option string
--- a/tests/test.ur Sun Dec 26 13:10:36 2010 -0500 +++ b/tests/test.ur Sun Dec 26 13:28:06 2010 -0500 @@ -1,8 +1,11 @@ fun discover r = - code <- Openid.discover r.Id; - return <xml><body> - Code: {[code]} - </body></xml> + dy <- Openid.discover r.Id; + case dy of + None => return <xml>No dice</xml> + | Some dy => return <xml><body> + Endpoint: {[dy.Endpoint]}<br/> + Local ID: {[dy.LocalId]}<br/> + </body></xml> fun main () = return <xml><body> <form>