diff options
author | Florian Dold <florian.dold@gmail.com> | 2018-01-09 14:32:32 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2018-01-09 23:45:42 +0100 |
commit | abc3970d38ddad7faf51e6b0e22969904df04c9b (patch) | |
tree | 5b7dc1bd67b6b1bf478471d720e6ab62080bdc54 /src | |
parent | c327c3465474e6c1ca2f4c526f66694b0d55ef2d (diff) | |
download | merchant-abc3970d38ddad7faf51e6b0e22969904df04c9b.tar.gz merchant-abc3970d38ddad7faf51e6b0e22969904df04c9b.tar.bz2 merchant-abc3970d38ddad7faf51e6b0e22969904df04c9b.zip |
additional headers for taler-pay
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/taler-merchant-httpd.c | 95 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd.h | 12 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_check-payment.c | 96 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_trigger-pay.c | 54 |
4 files changed, 140 insertions, 117 deletions
diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c index 33651380..f5705c88 100644 --- a/src/backend/taler-merchant-httpd.c +++ b/src/backend/taler-merchant-httpd.c @@ -1264,3 +1264,98 @@ main (int argc, return 3; return (GNUNET_OK == result) ? 0 : 1; } + + +/** + * 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 to the backend. + * + * @param connection MHD connection to take header values from + * @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 * +TMH_make_absolute_backend_url (struct MHD_Connection *connection, 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); + + // By default we assume we're running under HTTP + const char *proto = "http"; + const char *forwarded_proto = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "X-Forwarded-Proto"); + + if (NULL != forwarded_proto) + proto = forwarded_proto; + + const char *host = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "Host"); + const char *forwarded_host = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "X-Forwarded-Host"); + + const char *forwarded_prefix = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "X-Forwarded-Prefix"); + + if (NULL != forwarded_host) + host = forwarded_host; + + if (NULL == host) + { + // Should never happen, at last the host header should be defined + GNUNET_break (0); + return NULL; + } + + STR_CAT_GROW (res, proto, n); + STR_CAT_GROW (res, "://", n); + STR_CAT_GROW (res, host, n); + STR_CAT_GROW (res, "/", n); + if (NULL != forwarded_prefix) + STR_CAT_GROW (res, forwarded_prefix, 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; +} diff --git a/src/backend/taler-merchant-httpd.h b/src/backend/taler-merchant-httpd.h index 6418801d..91dc7a22 100644 --- a/src/backend/taler-merchant-httpd.h +++ b/src/backend/taler-merchant-httpd.h @@ -329,4 +329,16 @@ struct MerchantInstance * TMH_lookup_instance_json (struct json_t *json); +/** + * Make an absolute URL to the backend. + * + * @param connection MHD connection to take header values from + * @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 * +TMH_make_absolute_backend_url (struct MHD_Connection *connection, char *path, ...); + + #endif diff --git a/src/backend/taler-merchant-httpd_check-payment.c b/src/backend/taler-merchant-httpd_check-payment.c index 145be9de..3079c5c2 100644 --- a/src/backend/taler-merchant-httpd_check-payment.c +++ b/src/backend/taler-merchant-httpd_check-payment.c @@ -33,100 +33,6 @@ /** - * 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 to the backend. - * - * @param connection MHD connection to take header values from - * @param path path of the url - * @param ... NULL-terminated key-value pairs (char *) for query parameters - */ -static char * -make_absolute_backend_url (struct MHD_Connection *connection, 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); - - // By default we assume we're running under HTTP - const char *proto = "http"; - const char *forwarded_proto = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "X-Forwarded-Proto"); - - if (NULL != forwarded_proto) - proto = forwarded_proto; - - const char *host = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "Host"); - const char *forwarded_host = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "X-Forwarded-Host"); - - const char *forwarded_prefix = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "X-Forwarded-Prefix"); - - if (NULL != forwarded_host) - host = forwarded_host; - - if (NULL == host) - { - // Should never happen, at last the host header should be defined - GNUNET_break (0); - return NULL; - } - - STR_CAT_GROW (res, proto, n); - STR_CAT_GROW (res, "://", n); - STR_CAT_GROW (res, host, n); - STR_CAT_GROW (res, "/", n); - if (NULL != forwarded_prefix) - STR_CAT_GROW (res, forwarded_prefix, 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; -} - - -/** * Manages a /check-payment call, checking the status * of a payment and, if necessary, constructing the URL * for a payment redirect URL. @@ -311,7 +217,7 @@ MH_handler_check_payment (struct TMH_RequestHandler *rh, do_pay: { - char *url = make_absolute_backend_url (connection, "trigger-pay", + char *url = TMH_make_absolute_backend_url (connection, "trigger-pay", "contract_url", contract_url, "session_id", session_id, "h_contract_terms", h_contract_terms_str, diff --git a/src/backend/taler-merchant-httpd_trigger-pay.c b/src/backend/taler-merchant-httpd_trigger-pay.c index 280ca658..33097a61 100644 --- a/src/backend/taler-merchant-httpd_trigger-pay.c +++ b/src/backend/taler-merchant-httpd_trigger-pay.c @@ -14,8 +14,8 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ /** - * @file backend/taler-merchant-httpd_check-payment.c - * @brief implementation of /check-payment handler + * @file backend/taler-merchant-httpd_trigger-pay.c + * @brief implementation of /trigger-pay handler * @author Florian Dold */ #include "platform.h" @@ -33,6 +33,29 @@ /** + * Add a header to the response from a query parameter. + * + * + * @param connection connection to take query parameters from + * @param arg_name name of query parameter + * @param response response that receives the header + * @param header_name name of the header to set + */ +void +add_header_from_arg (struct MHD_Connection *connection, const char *arg_name, + struct MHD_Response *response, const char *header_name) +{ + const char *arg = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + arg_name); + if (NULL == arg) + return; + + MHD_add_response_header (response, header_name, arg); +} + + +/** * Serves a request to browsers to trigger a payment. * Contains all the logic to handle different platforms, so that the frontend * does not have to handle that. @@ -53,31 +76,18 @@ MH_handler_trigger_pay (struct TMH_RequestHandler *rh, { struct MHD_Response *response; - const char *contract_url; - const char *h_contract_terms_str; - const char *session_id; - - session_id = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "session_id"); - contract_url = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "contract_url"); - h_contract_terms_str = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "h_contract_terms"); - // FIXME: Taler wallet detection! char *data = "<html><body><p>Processing payment ...</p></body></html>"; response = MHD_create_response_from_buffer (strlen (data), data, MHD_RESPMEM_PERSISTENT); - if (NULL != session_id) - MHD_add_response_header (response, "X-Taler-Session-Id", session_id); - if (NULL != contract_url) - MHD_add_response_header (response, "X-Taler-Contract-Url", contract_url); - if (NULL != h_contract_terms_str) - MHD_add_response_header (response, "X-Taler-Contract-Hash", h_contract_terms_str); + + add_header_from_arg (connection, "session_id", response, "X-Taler-Session-Id"); + add_header_from_arg (connection, "contract_url", response, "X-Taler-Contract-Url"); + add_header_from_arg (connection, "h_contract_terms", response, "X-Taler-Contract-Hash"); + add_header_from_arg (connection, "tip_token", response, "X-Taler-Tip"); + add_header_from_arg (connection, "refund_url", response, "X-Taler-Refund-Url"); + MHD_queue_response (connection, 402, response); MHD_destroy_response (response); |