gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-merchant] branch master updated: additional headers


From: gnunet
Subject: [GNUnet-SVN] [taler-merchant] branch master updated: additional headers for taler-pay
Date: Tue, 09 Jan 2018 23:45:51 +0100

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository merchant.

The following commit(s) were added to refs/heads/master by this push:
     new abc3970  additional headers for taler-pay
abc3970 is described below

commit abc3970d38ddad7faf51e6b0e22969904df04c9b
Author: Florian Dold <address@hidden>
AuthorDate: Tue Jan 9 14:32:32 2018 +0100

    additional headers for taler-pay
---
 src/backend/taler-merchant-httpd.c               | 95 +++++++++++++++++++++++
 src/backend/taler-merchant-httpd.h               | 12 +++
 src/backend/taler-merchant-httpd_check-payment.c | 96 +-----------------------
 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 3365138..f5705c8 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 6418801..91dc7a2 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 145be9d..3079c5c 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 280ca65..33097a6 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);
 

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]