From 8bca461ea9f604e4bce279255663f207f0d104ac Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 23 Nov 2019 17:55:47 +0100 Subject: first uses of libtalermhd --- src/auditor/taler-auditor-httpd.c | 268 ++------------------------------------ 1 file changed, 10 insertions(+), 258 deletions(-) (limited to 'src/auditor/taler-auditor-httpd.c') diff --git a/src/auditor/taler-auditor-httpd.c b/src/auditor/taler-auditor-httpd.c index e37b4a0e8..f0e735c01 100644 --- a/src/auditor/taler-auditor-httpd.c +++ b/src/auditor/taler-auditor-httpd.c @@ -27,6 +27,7 @@ #include #include #include +#include "taler_mhd_lib.h" #include "taler_auditordb_lib.h" #include "taler-auditor-httpd_deposit-confirmation.h" #include "taler-auditor-httpd_exchanges.h" @@ -427,122 +428,6 @@ handle_mhd_request (void *cls, } -/** - * Parse the configuration to determine on which port - * or UNIX domain path we should run an HTTP service. - * - * @param section section of the configuration to parse ("auditor" or "auditor-admin") - * @param[out] rport set to the port number, or 0 for none - * @param[out] unix_path set to the UNIX path, or NULL for none - * @param[out] unix_mode set to the mode to be used for @a unix_path - * @return #GNUNET_OK on success - */ -static int -parse_port_config (const char *section, - uint16_t *rport, - char **unix_path, - mode_t *unix_mode) -{ - const char *choices[] = {"tcp", "unix"}; - const char *serve_type; - unsigned long long port; - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_choice (cfg, - section, - "serve", - choices, - &serve_type)) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - section, - "serve", - "serve type required"); - return GNUNET_SYSERR; - } - - if (0 == strcmp (serve_type, "tcp")) - { - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_number (cfg, - section, - "port", - &port)) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - section, - "port", - "port number required"); - return GNUNET_SYSERR; - } - - if ( (0 == port) || - (port > UINT16_MAX) ) - { - fprintf (stderr, - "Invalid configuration (value out of range): %llu is not a valid port\n", - port); - return GNUNET_SYSERR; - } - *rport = (uint16_t) port; - *unix_path = NULL; - return GNUNET_OK; - } - if (0 == strcmp (serve_type, "unix")) - { - struct sockaddr_un s_un; - char *modestring; - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (cfg, - section, - "unixpath", - unix_path)) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - section, - "unixpath", - "unixpath required"); - return GNUNET_SYSERR; - } - if (strlen (*unix_path) >= sizeof (s_un.sun_path)) - { - fprintf (stderr, - "Invalid configuration: unix path too long\n"); - return GNUNET_SYSERR; - } - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, - section, - "UNIXPATH_MODE", - &modestring)) - { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - section, - "UNIXPATH_MODE"); - return GNUNET_SYSERR; - } - errno = 0; - *unix_mode = (mode_t) strtoul (modestring, NULL, 8); - if (0 != errno) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - section, - "UNIXPATH_MODE", - "must be octal number"); - GNUNET_free (modestring); - return GNUNET_SYSERR; - } - GNUNET_free (modestring); - return GNUNET_OK; - } - /* not reached */ - GNUNET_assert (0); - return GNUNET_SYSERR; -} - - /** * Load configuration parameters for the auditor * server into the corresponding global variables. @@ -562,10 +447,11 @@ auditor_serve_process_config () return GNUNET_SYSERR; } if (GNUNET_OK != - parse_port_config ("auditor", - &serve_port, - &serve_unixpath, - &unixpath_mode)) + TALER_MHD_parse_config (cfg, + "auditor", + &serve_port, + &serve_unixpath, + &unixpath_mode)) { return GNUNET_SYSERR; } @@ -640,141 +526,6 @@ auditor_serve_process_config () } -/** - * Function called for logging by MHD. - * - * @param cls closure, NULL - * @param fm format string (`printf()`-style) - * @param ap arguments to @a fm - */ -static void -handle_mhd_logs (void *cls, - const char *fm, - va_list ap) -{ - static int cache; - char buf[2048]; - - if (-1 == cache) - return; - if (0 == cache) - { - if (0 == - GNUNET_get_log_call_status (GNUNET_ERROR_TYPE_INFO, - "auditor-httpd", - __FILE__, - __FUNCTION__, - __LINE__)) - { - cache = -1; - return; - } - } - cache = 1; - vsnprintf (buf, - sizeof (buf), - fm, - ap); - GNUNET_log_from_nocheck (GNUNET_ERROR_TYPE_INFO, - "auditor-httpd", - "%s", - buf); -} - - -/** - * Open UNIX domain socket for listining at @a unix_path with - * permissions @a unix_mode. - * - * @param unix_path where to listen - * @param unix_mode access permissions to set - * @return -1 on error, otherwise the listen socket - */ -static int -open_unix_path (const char *unix_path, - mode_t unix_mode) -{ - struct GNUNET_NETWORK_Handle *nh; - struct sockaddr_un *un; - int fd; - - if (sizeof (un->sun_path) <= strlen (unix_path)) - { - fprintf (stderr, - "unixpath `%s' too long\n", - unix_path); - return -1; - } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Creating listen socket '%s' with mode %o\n", - unix_path, - unix_mode); - - if (GNUNET_OK != - GNUNET_DISK_directory_create_for_file (unix_path)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, - "mkdir", - unix_path); - } - - un = GNUNET_new (struct sockaddr_un); - un->sun_family = AF_UNIX; - strncpy (un->sun_path, - unix_path, - sizeof (un->sun_path) - 1); - GNUNET_NETWORK_unix_precheck (un); - - if (NULL == (nh = GNUNET_NETWORK_socket_create (AF_UNIX, - SOCK_STREAM, - 0))) - { - fprintf (stderr, - "create failed for AF_UNIX\n"); - GNUNET_free (un); - return -1; - } - if (GNUNET_OK != - GNUNET_NETWORK_socket_bind (nh, - (void *) un, - sizeof (struct sockaddr_un))) - { - fprintf (stderr, - "bind failed for AF_UNIX\n"); - GNUNET_free (un); - GNUNET_NETWORK_socket_close (nh); - return -1; - } - GNUNET_free (un); - if (GNUNET_OK != - GNUNET_NETWORK_socket_listen (nh, - UNIX_BACKLOG)) - { - fprintf (stderr, - "listen failed for AF_UNIX\n"); - GNUNET_NETWORK_socket_close (nh); - return -1; - } - - if (0 != chmod (unix_path, - unix_mode)) - { - fprintf (stderr, - "chmod failed: %s\n", - strerror (errno)); - GNUNET_NETWORK_socket_close (nh); - return -1; - } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "set socket '%s' to mode %o\n", - unix_path, - unix_mode); - fd = GNUNET_NETWORK_get_fd (nh); - GNUNET_NETWORK_socket_free_memory_only_ (nh); - return fd; -} - - /** * The main function of the taler-auditor-httpd server ("the auditor"). * @@ -877,8 +628,8 @@ main (int argc, if ( (-1 == fh) && (NULL != serve_unixpath) ) { - fh = open_unix_path (serve_unixpath, - unixpath_mode); + fh = TALER_MHD_open_unix_path (serve_unixpath, + unixpath_mode); if (-1 == fh) return 1; } @@ -894,7 +645,8 @@ main (int argc, MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) 32, MHD_OPTION_LISTEN_BACKLOG_SIZE, (unsigned int) 1024, MHD_OPTION_LISTEN_SOCKET, fh, - MHD_OPTION_EXTERNAL_LOGGER, &handle_mhd_logs, NULL, + MHD_OPTION_EXTERNAL_LOGGER, &TALER_MHD_handle_logs, + NULL, MHD_OPTION_NOTIFY_COMPLETED, &handle_mhd_completion_callback, NULL, MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout, -- cgit v1.2.3 From e8a88392da98c3325ba39b20901a5c220158d1f5 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 23 Nov 2019 18:47:07 +0100 Subject: more refactoring towards using libtalermhd --- src/auditor/taler-auditor-httpd.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/auditor/taler-auditor-httpd.c') diff --git a/src/auditor/taler-auditor-httpd.c b/src/auditor/taler-auditor-httpd.c index f0e735c01..fa4f572ec 100644 --- a/src/auditor/taler-auditor-httpd.c +++ b/src/auditor/taler-auditor-httpd.c @@ -385,7 +385,7 @@ handle_mhd_request (void *cls, &TAH_MHD_handler_static_response, MHD_HTTP_OK }, /* AGPL licensing page, redirect to source. As per the AGPL-license, every deployment is required to offer the user a download of the - source. We make this easy by including a redirect to the source + source. We make this easy by including a redirect t the source here. */ { "/agpl", MHD_HTTP_METHOD_GET, "text/plain", NULL, 0, @@ -393,11 +393,6 @@ handle_mhd_request (void *cls, { NULL, NULL, NULL, NULL, 0, 0 } }; - static struct TAH_RequestHandler h404 = { - "", NULL, "text/html", - "404: not found", 0, - &TAH_MHD_handler_static_response, MHD_HTTP_NOT_FOUND - }; struct TAH_RequestHandler *rh; GNUNET_log (GNUNET_ERROR_TYPE_INFO, @@ -420,11 +415,13 @@ handle_mhd_request (void *cls, upload_data, upload_data_size); } - return TAH_MHD_handler_static_response (&h404, - connection, - con_cls, - upload_data, - upload_data_size); +#define NOT_FOUND "404: not found" + return TALER_MHD_reply_static (connection, + MHD_HTTP_NOT_FOUND, + "text/html", + NOT_FOUND, + strlen (NOT_FOUND)); +#undef NOT_FOUND } -- cgit v1.2.3 From 7aae6c90452c1e9bcae78a5e948f381c1165010a Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 23 Nov 2019 22:21:47 +0100 Subject: use CONFLICT for double spending to distinguish properly from FORBIDDEN for bad signatures --- src/auditor/taler-auditor-httpd.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/auditor/taler-auditor-httpd.c') diff --git a/src/auditor/taler-auditor-httpd.c b/src/auditor/taler-auditor-httpd.c index fa4f572ec..bf1e7ac71 100644 --- a/src/auditor/taler-auditor-httpd.c +++ b/src/auditor/taler-auditor-httpd.c @@ -31,7 +31,6 @@ #include "taler_auditordb_lib.h" #include "taler-auditor-httpd_deposit-confirmation.h" #include "taler-auditor-httpd_exchanges.h" -#include "taler-auditor-httpd_parsing.h" #include "taler-auditor-httpd_responses.h" #include "taler-auditor-httpd_mhd.h" #include "taler-auditor-httpd.h" @@ -292,7 +291,7 @@ handle_mhd_completion_callback (void *cls, { if (NULL == *con_cls) return; - TAH_PARSE_post_cleanup_callback (*con_cls); + TALER_MHD_parse_post_cleanup_callback (*con_cls); *con_cls = NULL; } @@ -559,12 +558,17 @@ main (int argc, const char *listen_pid; const char *listen_fds; int fh = -1; + enum TALER_MHD_GlobalOptions go; if (0 >= GNUNET_GETOPT_run ("taler-auditor-httpd", options, argc, argv)) return 1; + go = TALER_MHD_GO_NONE; + if (TAH_auditor_connection_close) + go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE; + TALER_MHD_setup (go); GNUNET_assert (GNUNET_OK == GNUNET_log_setup ("taler-auditor-httpd", (NULL == loglev) ? "INFO" : loglev, -- cgit v1.2.3 From 7510b6310b59967a21e3683a13b8232179f7b26a Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 23 Nov 2019 22:26:11 +0100 Subject: more libtalermhd migration --- src/auditor/taler-auditor-httpd.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/auditor/taler-auditor-httpd.c') diff --git a/src/auditor/taler-auditor-httpd.c b/src/auditor/taler-auditor-httpd.c index bf1e7ac71..55a343776 100644 --- a/src/auditor/taler-auditor-httpd.c +++ b/src/auditor/taler-auditor-httpd.c @@ -31,7 +31,6 @@ #include "taler_auditordb_lib.h" #include "taler-auditor-httpd_deposit-confirmation.h" #include "taler-auditor-httpd_exchanges.h" -#include "taler-auditor-httpd_responses.h" #include "taler-auditor-httpd_mhd.h" #include "taler-auditor-httpd.h" @@ -332,9 +331,9 @@ handle_version (struct TAH_RequestHandler *rh, GNUNET_break (0); return MHD_NO; } - return TAH_RESPONSE_reply_json (connection, - ver, - MHD_HTTP_OK); + return TALER_MHD_reply_json (connection, + ver, + MHD_HTTP_OK); } -- cgit v1.2.3