From cf4e7ee80eacf42dd4d0fcab716c3580b01dabb8 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 22 Jan 2018 17:48:44 +0100 Subject: implement check payment API client --- src/lib/merchant_api_common.c | 86 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'src/lib/merchant_api_common.c') diff --git a/src/lib/merchant_api_common.c b/src/lib/merchant_api_common.c index 05e2637a..12bc81d4 100644 --- a/src/lib/merchant_api_common.c +++ b/src/lib/merchant_api_common.c @@ -20,6 +20,7 @@ * @author Christian Grothoff */ #include "platform.h" +#include #include @@ -47,4 +48,89 @@ MAH_path_to_url_ (const char *base_url, return url; } + +/** + * Concatenate two strings and grow the first buffer (of size n) + * if necessary. + */ +#define STR_CAT_GROW(s, p, n) do { \ + for (; strlen (s) + strlen (p) >= n; (n) = (n) * 2); \ + (s) = GNUNET_realloc ((s), (n)); \ + GNUNET_assert (NULL != (s)); \ + strncat (s, p, n); \ + } while (0) + + +/** + * Make an absolute URL with query parameters. + * + * @param base_url absolute base URL to use + * @param path path of the url + * @param ... NULL-terminated key-value pairs (char *) for query parameters + * @returns the URL, must be freed with #GNUNET_free + */ +char * +MAH_make_url (const char *base_url, + const char *path, + ...) +{ + static CURL *curl = NULL; + if (NULL == curl) + { + curl = curl_easy_init(); + GNUNET_assert (NULL != curl); + } + + size_t n = 256; + char *res = GNUNET_malloc (n); + + GNUNET_assert (NULL != res); + + STR_CAT_GROW (res, base_url, n); + + if ( ('/' == path[0]) && + (0 < strlen (base_url)) && + ('/' == base_url[strlen (base_url) - 1]) ) + { + /* avoid generating URL with "//" from concat */ + path++; + } + else if ( ('/' != path[0]) && + ('/' != base_url[strlen (base_url) - 1])) + { + /* put '/' between path and base URL if necessary */ + STR_CAT_GROW (res, "/", n); + } + + STR_CAT_GROW (res, path, n); + + va_list args; + va_start (args, path); + + unsigned int iparam = 0; + + while (1) { + char *key = va_arg (args, char *); + if (NULL == key) + break; + char *value = va_arg (args, char *); + if (NULL == value) + continue; + if (0 == iparam) + STR_CAT_GROW (res, "?", n); + else + STR_CAT_GROW (res, "&", n); + iparam++; + char *urlencoded_value = curl_easy_escape (curl, value, strlen (value)); + STR_CAT_GROW (res, key, n); + STR_CAT_GROW (res, "=", n); + STR_CAT_GROW (res, urlencoded_value, n); + curl_free (urlencoded_value); + } + + va_end (args); + + return res; +} + /* end of merchant_api_common.c */ -- cgit v1.2.3