commit d0eef446f63cc40ab304ca8eb358d0a285cdf4b1
parent 9996307da1512ed98f09ea49a4531cfe89ff9dd5
Author: Christian Grothoff <christian@grothoff.org>
Date: Sun, 2 Mar 2025 01:53:12 +0100
port mhd.c
Diffstat:
4 files changed, 136 insertions(+), 18 deletions(-)
diff --git a/src/include/taler_mhd2_lib.h b/src/include/taler_mhd2_lib.h
@@ -62,6 +62,35 @@ enum TALER_MHD2_GlobalOptions
/**
+ * Find out if an MHD connection is using HTTPS (either
+ * directly or via proxy).
+ *
+ * @param request MHD request
+ * @returns #GNUNET_YES if the MHD connection is using https,
+ * #GNUNET_NO if the MHD connection is using http,
+ * #GNUNET_SYSERR if the connection type couldn't be determined
+ */
+enum GNUNET_GenericReturnValue
+TALER_MHD2_is_https (struct MHD_Request *request);
+
+
+/**
+ * Convert query argument to @a yna value.
+ *
+ * @param request request to take query argument from
+ * @param arg argument to try for
+ * @param default_val value to assign if the argument is not present
+ * @param[out] yna value to set
+ * @return true on success, false if the parameter was malformed
+ */
+bool
+TALER_MHD2_arg_to_yna (struct MHD_Request *request,
+ const char *arg,
+ enum TALER_EXCHANGE_YesNoAll default_val,
+ enum TALER_EXCHANGE_YesNoAll *yna);
+
+
+/**
* Set global options for response generation within libtalermhd.
*
* @param go global options to use
diff --git a/src/mhd/Makefile.am b/src/mhd/Makefile.am
@@ -37,6 +37,7 @@ lib_LTLIBRARIES += \
libtalermhd2_la_SOURCES = \
mhd_config.c \
+ mhd2.c \
mhd2_legal.c \
mhd2_responses.c \
mhd2_run.c \
diff --git a/src/mhd/mhd.c b/src/mhd/mhd.c
@@ -22,15 +22,7 @@
#include "taler_util.h"
#include "taler_mhd_lib.h"
-/**
- * Find out if an MHD connection is using HTTPS (either
- * directly or via proxy).
- *
- * @param connection MHD connection
- * @returns #GNUNET_YES if the MHD connection is using https,
- * #GNUNET_NO if the MHD connection is using http,
- * #GNUNET_SYSERR if the connection type couldn't be determined
- */
+
enum GNUNET_GenericReturnValue
TALER_mhd_is_https (struct MHD_Connection *connection)
{
@@ -73,15 +65,6 @@ TALER_mhd_is_https (struct MHD_Connection *connection)
}
-/**
- * Convert query argument to @a yna value.
- *
- * @param connection connection to take query argument from
- * @param arg argument to try for
- * @param default_val value to assign if the argument is not present
- * @param[out] yna value to set
- * @return true on success, false if the parameter was malformed
- */
bool
TALER_MHD_arg_to_yna (struct MHD_Connection *connection,
const char *arg,
diff --git a/src/mhd/mhd2.c b/src/mhd/mhd2.c
@@ -0,0 +1,105 @@
+/*
+ This file is part of TALER
+ Copyright (C) 2014-2025 Taler Systems SA
+
+ TALER is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+*/
+/**
+ * @file mhd2.c
+ * @brief MHD utility functions (used by the merchant backend)
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "taler_util.h"
+#include "taler_mhd2_lib.h"
+
+
+enum GNUNET_GenericReturnValue
+TALER_MHD2_is_https (struct MHD_Request *request)
+{
+ const struct MHD_StringNullable *forwarded_proto;
+ union MHD_RequestInfoFixedData ci;
+ union MHD_DaemonInfoFixedData di;
+
+ forwarded_proto = MHD_request_get_value (request,
+ MHD_VK_HEADER,
+ "X-Forwarded-Proto");
+ if ( (NULL != forwarded_proto) &&
+ (NULL != forwarded_proto->cstr) )
+ {
+ if (0 == strcasecmp (forwarded_proto->cstr,
+ "https"))
+ return GNUNET_YES;
+ if (0 == strcasecmp (forwarded_proto->cstr,
+ "http"))
+ return GNUNET_NO;
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ /* likely not reverse proxy, figure out if we are
+ http by asking MHD */
+ if (MHD_SC_OK !=
+ MHD_request_get_info_fixed (request,
+ MHD_REQUEST_INFO_FIXED_DAEMON,
+ &ci))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ if (MHD_SC_OK !=
+ MHD_daemon_get_info_fixed (ci.v_daemon,
+ MHD_DAEMON_INFO_FIXED_TLS_TYPE,
+ &di))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ if (MHD_TLS_BACKEND_NONE != di.v_tls_backend)
+ return GNUNET_YES;
+ return GNUNET_NO;
+}
+
+
+bool
+TALER_MHD2_arg_to_yna (struct MHD_Request *request,
+ const char *arg,
+ enum TALER_EXCHANGE_YesNoAll default_val,
+ enum TALER_EXCHANGE_YesNoAll *yna)
+{
+ const struct MHD_StringNullable *nstr;
+
+ nstr = MHD_request_get_value (request,
+ MHD_VK_GET_ARGUMENT,
+ arg);
+ if ( (NULL == nstr) ||
+ (NULL == nstr->cstr) )
+ {
+ *yna = default_val;
+ return true;
+ }
+ if (0 == strcasecmp (nstr->cstr, "yes"))
+ {
+ *yna = TALER_EXCHANGE_YNA_YES;
+ return true;
+ }
+ if (0 == strcasecmp (nstr->cstr, "no"))
+ {
+ *yna = TALER_EXCHANGE_YNA_NO;
+ return true;
+ }
+ if (0 == strcasecmp (nstr->cstr, "all"))
+ {
+ *yna = TALER_EXCHANGE_YNA_ALL;
+ return true;
+ }
+ return false;
+}