diff options
Diffstat (limited to 'src/backend/taler-merchant-httpd_get-tips-ID.c')
-rw-r--r-- | src/backend/taler-merchant-httpd_get-tips-ID.c | 145 |
1 files changed, 134 insertions, 11 deletions
diff --git a/src/backend/taler-merchant-httpd_get-tips-ID.c b/src/backend/taler-merchant-httpd_get-tips-ID.c index 52a0a561..487e88f4 100644 --- a/src/backend/taler-merchant-httpd_get-tips-ID.c +++ b/src/backend/taler-merchant-httpd_get-tips-ID.c @@ -24,6 +24,79 @@ #include <taler/taler_signatures.h> #include <taler/taler_json_lib.h> #include "taler-merchant-httpd_get-tips-ID.h" +#include "taler-merchant-httpd_mhd.h" +#include "taler-merchant-httpd_qr.h" +#include "taler-merchant-httpd_templating.h" + + +/** + * Create a taler://tip/ URI for the given @a con and @a tip_id + * and @a instance_id. + * + * @param con HTTP connection + * @param tip_id the tip id + * @param instance_id instance, may be "default" + * @return corresponding taler://tip/ URI, or NULL on missing "host" + */ +static char * +make_taler_tip_uri (struct MHD_Connection *con, + const struct GNUNET_HashCode *tip_id, + const char *instance_id) +{ + const char *host; + const char *forwarded_host; + const char *uri_path; + struct GNUNET_Buffer buf = { 0 }; + + host = MHD_lookup_connection_value (con, + MHD_HEADER_KIND, + "Host"); + forwarded_host = MHD_lookup_connection_value (con, + MHD_HEADER_KIND, + "X-Forwarded-Host"); + + uri_path = MHD_lookup_connection_value (con, + MHD_HEADER_KIND, + "X-Forwarded-Prefix"); + if (NULL != forwarded_host) + host = forwarded_host; + + if (NULL == host) + { + GNUNET_break (0); + return NULL; + } + + GNUNET_assert (NULL != instance_id); + GNUNET_assert (NULL != tip_id); + + GNUNET_buffer_write_str (&buf, + "taler"); + if (GNUNET_NO == TALER_mhd_is_https (con)) + GNUNET_buffer_write_str (&buf, + "+http"); + GNUNET_buffer_write_str (&buf, + "://tip/"); + GNUNET_buffer_write_str (&buf, + host); + if (NULL != uri_path) + GNUNET_buffer_write_path (&buf, + uri_path); + if (0 != strcmp ("default", + instance_id)) + { + GNUNET_buffer_write_path (&buf, + "instances"); + GNUNET_buffer_write_path (&buf, + instance_id); + } + GNUNET_buffer_write_data_encoded (&buf, + tip_id, + sizeof (*tip_id)); + GNUNET_buffer_write_str (&buf, + "/"); + return GNUNET_buffer_reap_str (&buf); +} /** @@ -93,9 +166,9 @@ TMH_get_tips_ID (const struct TMH_RequestHandler *rh, /* Build response */ { - MHD_RESULT ret; struct TALER_Amount remaining; struct GNUNET_TIME_Absolute expiration_round = expiration; + MHD_RESULT ret; GNUNET_break (0 <= TALER_amount_subtract (&remaining, @@ -103,17 +176,67 @@ TMH_get_tips_ID (const struct TMH_RequestHandler *rh, &total_picked_up)); GNUNET_TIME_round_abs (&expiration_round); + if (TMH_MHD_test_html_desired (connection)) + { + char *qr; + char *uri; + + uri = make_taler_tip_uri (connection, + &tip_id, + hc->instance->settings.id); + qr = TMH_create_qrcode (uri); + if (NULL == qr) + { + GNUNET_break (0); + GNUNET_free (uri); + ret = TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_ALLOCATION_FAILURE, + "during QR code generation"); + } + else + { + struct KVC kvc[] = { + { "remaining_tip", + TALER_amount2s (&remaining) }, + { "taler_tip_uri", + uri }, + { "taler_tip_qrcode_svg", + qr }, + { NULL, NULL } + }; - ret = TALER_MHD_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:s, s:o, s:o}", - "exchange_url", - exchange_url, - "tip_amount", - TALER_JSON_from_amount (&remaining), - "expiration", - GNUNET_JSON_from_time_abs ( - expiration_round)); + ret = TMH_return_from_template (connection, + ( (0 == remaining.value) && + (0 == remaining.fraction) ) + ? MHD_HTTP_GONE + : MHD_HTTP_OK, + ( (0 == remaining.value) && + (0 == remaining.fraction) ) + ? "depleted_tip" + : "offer_tip", + uri, + kvc); + } + GNUNET_free (uri); + GNUNET_free (qr); + } + else + { + ret = TALER_MHD_reply_json_pack (connection, + ( (0 == remaining.value) && + (0 == remaining.fraction) ) + ? MHD_HTTP_GONE + : MHD_HTTP_OK, + "{s:s, s:o, s:o}", + "exchange_url", + exchange_url, + "tip_amount", + TALER_JSON_from_amount (&remaining), + "expiration", + GNUNET_JSON_from_time_abs ( + expiration_round)); + } GNUNET_free (exchange_url); return ret; } |