summaryrefslogtreecommitdiff
path: root/src/restclient
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-01-19 12:55:56 +0100
committerChristian Grothoff <christian@grothoff.org>2022-01-19 12:55:56 +0100
commit16bb30ce4b424fa5ada004c53721a6f3f202b538 (patch)
tree1e25885759a5b19201c972b023726dfc40c123ef /src/restclient
parentfbf87011b5129a44e2494758effc7bd6716d6d3b (diff)
downloadanastasis-16bb30ce4b424fa5ada004c53721a6f3f202b538.tar.gz
anastasis-16bb30ce4b424fa5ada004c53721a6f3f202b538.tar.bz2
anastasis-16bb30ce4b424fa5ada004c53721a6f3f202b538.zip
add meta handler and client implementation
Diffstat (limited to 'src/restclient')
-rw-r--r--src/restclient/Makefile.am2
-rw-r--r--src/restclient/anastasis_api_policy_meta_lookup.c267
2 files changed, 268 insertions, 1 deletions
diff --git a/src/restclient/Makefile.am b/src/restclient/Makefile.am
index 075d3a7..19bf64c 100644
--- a/src/restclient/Makefile.am
+++ b/src/restclient/Makefile.am
@@ -19,6 +19,7 @@ libanastasisrest_la_SOURCES = \
anastasis_api_policy_store.c \
anastasis_api_truth_store.c \
anastasis_api_policy_lookup.c \
+ anastasis_api_policy_meta_lookup.c \
anastasis_api_keyshare_lookup.c \
anastasis_api_curl_defaults.c anastasis_api_curl_defaults.h
libanastasisrest_la_LIBADD = \
@@ -39,4 +40,3 @@ if HAVE_LIBGNURL
libanastasisrest_la_LIBADD += -lgnurl
endif
endif
-
diff --git a/src/restclient/anastasis_api_policy_meta_lookup.c b/src/restclient/anastasis_api_policy_meta_lookup.c
new file mode 100644
index 0000000..9be49ca
--- /dev/null
+++ b/src/restclient/anastasis_api_policy_meta_lookup.c
@@ -0,0 +1,267 @@
+/*
+ This file is part of ANASTASIS
+ Copyright (C) 2022 Anastasis SARL
+
+ ANASTASIS is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2.1,
+ or (at your option) any later version.
+
+ ANASTASIS is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public
+ License along with ANASTASIS; see the file COPYING.LGPL. If not,
+ see <http://www.gnu.org/licenses/>
+*/
+
+/**
+ * @file restclient/anastasis_api_policy_meta_lookup.c
+ * @brief Implementation of the /policy/$POL/meta GET request
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include <curl/curl.h>
+#include <jansson.h>
+#include <microhttpd.h> /* just for HTTP status codes */
+#include "anastasis_service.h"
+#include "anastasis_api_curl_defaults.h"
+#include <taler/taler_signatures.h>
+
+
+/**
+ * @brief A Meta Operation Handle
+ */
+struct ANASTASIS_PolicyMetaLookupOperation
+{
+
+ /**
+ * The url for this request, including parameters.
+ */
+ char *url;
+
+ /**
+ * Handle for the request.
+ */
+ struct GNUNET_CURL_Job *job;
+
+ /**
+ * Function to call with the result.
+ */
+ ANASTASIS_PolicyMetaLookupCallback cb;
+
+ /**
+ * Closure for @a cb.
+ */
+ void *cb_cls;
+
+ /**
+ * Reference to the execution context.
+ */
+ struct GNUNET_CURL_Context *ctx;
+
+ /**
+ * Public key of the account we are downloading from.
+ */
+ struct ANASTASIS_CRYPTO_AccountPublicKeyP account_pub;
+
+ /**
+ * Maximum version to fetch.
+ */
+ uint32_t max_version;
+
+};
+
+
+/**
+ * Process GET /policy/$POL/meta response
+ *
+ * @param cls our `struct ANASTASIS_PolicyMetaLookupOperation *`
+ * @param response_code HTTP status
+ * @param data response body, a `json_t *`, NULL on error
+ */
+static void
+handle_policy_meta_lookup_finished (void *cls,
+ long response_code,
+ const void *response)
+{
+ struct ANASTASIS_PolicyMetaLookupOperation *plo = cls;
+ const json_t *json = response;
+
+ plo->job = NULL;
+ switch (response_code)
+ {
+ case 0:
+ /* Hard error */
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Backend didn't even return from GET /policy\n");
+ break;
+ case MHD_HTTP_OK:
+ {
+ size_t mlen = json_object_size (json);
+
+ /* put a cap, as we will stack-allocate below and the
+ current service LIMITs the result to 1000 anyway;
+ could theoretically be increased in the future, but
+ then we should not put this onto the stack anymore... */
+ if (mlen > 10000)
+ {
+ GNUNET_break (0);
+ response_code = 0;
+ break;
+ }
+ {
+ struct ANASTASIS_MetaDataEntry metas[GNUNET_NZL (mlen)];
+ void *md[GNUNET_NZL (mlen)];
+ struct ANASTASIS_MetaDownloadDetails mdd = {
+ .metas = metas,
+ .metas_length = mlen
+ };
+ size_t off = 0;
+ const char *label;
+ const json_t *val;
+
+ memset (md,
+ 0,
+ sizeof (md));
+ json_object_foreach ((json_t *) json,
+ label,
+ val)
+ {
+ unsigned int ver;
+ char dummy;
+ const char *vals;
+
+ if (1 != sscanf (label,
+ "%u%c",
+ &ver,
+ &dummy))
+ {
+ GNUNET_break (0);
+ break;
+ }
+ metas[off].version = (uint32_t) ver;
+ if (json_is_null (val))
+ {
+ metas[off].meta_data = NULL;
+ metas[off].meta_data_size = 0;
+ off++;
+ continue;
+ }
+ vals = json_string_value (val);
+ if ( (NULL == vals) ||
+ (GNUNET_OK !=
+ GNUNET_STRINGS_string_to_data_alloc (vals,
+ strlen (vals),
+ &md[off],
+ &metas[off].meta_data_size)) )
+ {
+ GNUNET_break (0);
+ break;
+ }
+ metas[off].version = (uint32_t) ver;
+ metas[off].meta_data = md[off];
+ off++;
+ }
+ if (off < mlen)
+ {
+ GNUNET_break (0);
+ response_code = 0;
+ for (size_t i = 0; i<off; i++)
+ GNUNET_free (md[i]);
+ break;
+ }
+ plo->cb (plo->cb_cls,
+ response_code,
+ &mdd);
+ for (size_t i = 0; i<off; i++)
+ GNUNET_free (md[i]);
+ plo->cb = NULL;
+ }
+ ANASTASIS_policy_meta_lookup_cancel (plo);
+ return;
+ }
+ case MHD_HTTP_BAD_REQUEST:
+ /* This should never happen, either us or the anastasis server is buggy
+ (or API version conflict); just pass JSON reply to the application */
+ break;
+ case MHD_HTTP_NOT_FOUND:
+ /* Nothing really to verify */
+ break;
+ case MHD_HTTP_INTERNAL_SERVER_ERROR:
+ /* Server had an internal issue; we should retry, but this API
+ leaves this to the application */
+ break;
+ default:
+ /* unexpected response code */
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Unexpected response code %u\n",
+ (unsigned int) response_code);
+ GNUNET_break (0);
+ response_code = 0;
+ break;
+ }
+ plo->cb (plo->cb_cls,
+ response_code,
+ NULL);
+ plo->cb = NULL;
+ ANASTASIS_policy_meta_lookup_cancel (plo);
+}
+
+
+struct ANASTASIS_PolicyMetaLookupOperation *
+ANASTASIS_policy_meta_lookup (
+ struct GNUNET_CURL_Context *ctx,
+ const char *backend_url,
+ const struct ANASTASIS_CRYPTO_AccountPublicKeyP *anastasis_pub,
+ uint32_t max_version,
+ ANASTASIS_PolicyMetaLookupCallback cb,
+ void *cb_cls)
+{
+ struct ANASTASIS_PolicyMetaLookupOperation *plo;
+ CURL *eh;
+ char *path;
+
+ GNUNET_assert (NULL != cb);
+ plo = GNUNET_new (struct ANASTASIS_PolicyMetaLookupOperation);
+ plo->account_pub = *anastasis_pub;
+ {
+ char *acc_pub_str;
+
+ acc_pub_str = GNUNET_STRINGS_data_to_string_alloc (anastasis_pub,
+ sizeof (*anastasis_pub));
+ GNUNET_asprintf (&path,
+ "policy/%s/meta",
+ acc_pub_str);
+ GNUNET_free (acc_pub_str);
+ }
+ plo->url = TALER_url_join (backend_url,
+ path,
+ NULL);
+ GNUNET_free (path);
+ eh = ANASTASIS_curl_easy_get_ (plo->url);
+ GNUNET_assert (NULL != eh);
+ plo->cb = cb;
+ plo->cb_cls = cb_cls;
+ plo->job = GNUNET_CURL_job_add (ctx,
+ eh,
+ &handle_policy_meta_lookup_finished,
+ plo);
+ return plo;
+}
+
+
+void
+ANASTASIS_policy_meta_lookup_cancel (struct
+ ANASTASIS_PolicyMetaLookupOperation *plo)
+{
+ if (NULL != plo->job)
+ {
+ GNUNET_CURL_job_cancel (plo->job);
+ plo->job = NULL;
+ }
+ GNUNET_free (plo->url);
+ GNUNET_free (plo);
+}