diff options
Diffstat (limited to 'src/sync/sync-httpd_backup_post.c')
-rw-r--r-- | src/sync/sync-httpd_backup_post.c | 91 |
1 files changed, 81 insertions, 10 deletions
diff --git a/src/sync/sync-httpd_backup_post.c b/src/sync/sync-httpd_backup_post.c index 86327cb..3c7fe57 100644 --- a/src/sync/sync-httpd_backup_post.c +++ b/src/sync/sync-httpd_backup_post.c @@ -68,6 +68,11 @@ struct BackupContext struct GNUNET_HashCode new_backup_hash; /** + * Claim token, all zeros if not known. Only set if @e existing_order_id is non-NULL. + */ + struct TALER_ClaimTokenP token; + + /** * Hash context for the upload. */ struct GNUNET_HashContext *hash_ctx; @@ -207,10 +212,13 @@ cleanup_ctx (struct TM_HandlerContext *hc) * * @param connection MHD connection * @param order_id our backend's order ID + * @param token the claim token generated by the merchant (NULL if + * it wasn't generated). * @return MHD response to use */ static struct MHD_Response * -make_payment_request (const char *order_id) +make_payment_request (const char *order_id, + const struct TALER_ClaimTokenP *token) { struct MHD_Response *resp; @@ -224,12 +232,63 @@ make_payment_request (const char *order_id) TALER_MHD_add_global_headers (resp); { char *hdr; + char *pfx; + char *hn; - /* TODO: support instances? */ - GNUNET_asprintf (&hdr, - "taler://pay/%s/-/-/%s", - SH_backend_url, - order_id); + if (0 == strncasecmp ("https://", + SH_backend_url, + strlen ("https://"))) + { + pfx = "taler://"; + hn = &SH_backend_url[strlen ("https://")]; + } + else if (0 == strncasecmp ("http://", + SH_backend_url, + strlen ("http://"))) + { + pfx = "taler+http://"; + hn = &SH_backend_url[strlen ("http://")]; + } + else + { + GNUNET_break (0); + MHD_destroy_response (resp); + return NULL; + } + if (0 == strlen (hn)) + { + GNUNET_break (0); + MHD_destroy_response (resp); + return NULL; + } + // FIXME: support instances? + if (NULL != token) + { + char tok[256]; + + GNUNET_assert (NULL != + GNUNET_STRINGS_data_to_string (token, + sizeof (*token), + tok, + sizeof (tok))); + GNUNET_asprintf (&hdr, + "%spay/%s%s%s/?c=%s", + pfx, + hn, + ('/' == hn[strlen (hn) - 1]) + ? "" + : "/", + order_id, + tok); + } + else + { + GNUNET_asprintf (&hdr, + "%spay/%s/%s/", + pfx, + hn, + order_id); + } GNUNET_break (MHD_YES == MHD_add_response_header (resp, "Taler", @@ -247,11 +306,14 @@ make_payment_request (const char *order_id) * @param cls our `struct BackupContext` * @param hr HTTP response details * @param order_id order id of the newly created order + * @param token the claim token generated by the merchant (NULL if + * it wasn't generated). */ static void proposal_cb (void *cls, const struct TALER_MERCHANT_HttpResponse *hr, - const char *order_id) + const char *order_id, + const struct TALER_ClaimTokenP *token) { struct BackupContext *bc = cls; enum SYNC_DB_QueryStatus qs; @@ -294,6 +356,7 @@ proposal_cb (void *cls, qs = db->store_payment_TR (db->cls, &bc->account, order_id, + token, &SH_annual_fee); if (0 >= qs) { @@ -307,7 +370,8 @@ proposal_cb (void *cls, GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Obtained fresh order `%s'\n", order_id); - bc->resp = make_payment_request (order_id); + bc->resp = make_payment_request (order_id, + token); GNUNET_assert (NULL != bc->resp); bc->response_code = MHD_HTTP_PAYMENT_REQUIRED; } @@ -320,12 +384,14 @@ proposal_cb (void *cls, * @param cls closure, our `struct BackupContext` * @param timestamp for how long have we been waiting * @param order_id order id in the backend + * @param token claim token to use (or NULL for none) * @param amount how much is the order for */ static void ongoing_payment_cb (void *cls, struct GNUNET_TIME_Absolute timestamp, const char *order_id, + const struct TALER_ClaimTokenP *token, const struct TALER_Amount *amount) { struct BackupContext *bc = cls; @@ -340,6 +406,8 @@ ongoing_payment_cb (void *cls, GNUNET_free (bc->existing_order_id); bc->existing_order_id = GNUNET_strdup (order_id); bc->existing_order_timestamp = timestamp; + if (NULL != token) + bc->token = *token; } } @@ -390,7 +458,10 @@ check_payment_cb (void *cls, /* repeat payment request */ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Repeating payment request\n"); - bc->resp = make_payment_request (bc->existing_order_id); + bc->resp = make_payment_request (bc->existing_order_id, + (GNUNET_YES == GNUNET_is_zero (&bc->token)) + ? NULL + : &bc->token); GNUNET_assert (NULL != bc->resp); bc->response_code = MHD_HTTP_PAYMENT_REQUIRED; return; @@ -853,7 +924,7 @@ SH_backup_post (struct MHD_Connection *connection, { enum SYNC_DB_QueryStatus qs; - if (0 == GNUNET_is_zero (&bc->old_backup_hash)) + if (GNUNET_YES == GNUNET_is_zero (&bc->old_backup_hash)) { GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Uploading first backup to account\n"); |