diff options
author | Florian Dold <florian.dold@gmail.com> | 2018-01-22 17:48:44 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2018-01-22 17:51:02 +0100 |
commit | cf4e7ee80eacf42dd4d0fcab716c3580b01dabb8 (patch) | |
tree | 6915a7fc5105b35e273781b748a13a20ceefc0f5 /src/lib/merchant_api_common.c | |
parent | bee509869eb563509a83cbf88c13b6679cae1d9c (diff) | |
download | merchant-cf4e7ee80eacf42dd4d0fcab716c3580b01dabb8.tar.gz merchant-cf4e7ee80eacf42dd4d0fcab716c3580b01dabb8.tar.bz2 merchant-cf4e7ee80eacf42dd4d0fcab716c3580b01dabb8.zip |
implement check payment API client
Diffstat (limited to 'src/lib/merchant_api_common.c')
-rw-r--r-- | src/lib/merchant_api_common.c | 86 |
1 files changed, 86 insertions, 0 deletions
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 <curl/curl.h> #include <gnunet/gnunet_util_lib.h> @@ -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 */ |