diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Makefile.am | 3 | ||||
-rw-r--r-- | src/lib/merchant_api_common.c | 86 | ||||
-rw-r--r-- | src/lib/merchant_api_common.h | 13 |
3 files changed, 101 insertions, 1 deletions
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 4c6d5f8f..d407ee01 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -22,7 +22,8 @@ libtalermerchant_la_SOURCES = \ merchant_api_track_transaction.c \ merchant_api_track_transfer.c \ merchant_api_history.c \ - merchant_api_refund.c + merchant_api_refund.c \ + merchant_api_check_payment.c libtalermerchant_la_LIBADD = \ -ltalerexchange \ 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 */ diff --git a/src/lib/merchant_api_common.h b/src/lib/merchant_api_common.h index 683f1dbd..09696e3b 100644 --- a/src/lib/merchant_api_common.h +++ b/src/lib/merchant_api_common.h @@ -33,4 +33,17 @@ char * MAH_path_to_url_ (const char *base_url, const char *path); +/** + * 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, + ...); + #endif |