diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-11-24 17:32:30 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-11-24 17:32:30 +0100 |
commit | d6108a63b016ebe8410049ca1292b0d7fef73291 (patch) | |
tree | bd5fd3b42330af33fe4b1749948bb6acbdcb432d /src/sync | |
parent | e462042a4d7ce91c0731871f014b78e6906b4171 (diff) | |
download | sync-d6108a63b016ebe8410049ca1292b0d7fef73291.tar.gz sync-d6108a63b016ebe8410049ca1292b0d7fef73291.tar.bz2 sync-d6108a63b016ebe8410049ca1292b0d7fef73291.zip |
use libtalermhd in sync
Diffstat (limited to 'src/sync')
-rw-r--r-- | src/sync/Makefile.am | 3 | ||||
-rw-r--r-- | src/sync/sync-httpd.c | 270 | ||||
-rw-r--r-- | src/sync/sync-httpd.h | 6 | ||||
-rw-r--r-- | src/sync/sync-httpd_backup.c | 72 | ||||
-rw-r--r-- | src/sync/sync-httpd_backup_post.c | 88 | ||||
-rw-r--r-- | src/sync/sync-httpd_mhd.c | 96 | ||||
-rw-r--r-- | src/sync/sync-httpd_mhd.h | 42 | ||||
-rw-r--r-- | src/sync/sync-httpd_parsing.c | 267 | ||||
-rw-r--r-- | src/sync/sync-httpd_parsing.h | 93 | ||||
-rw-r--r-- | src/sync/sync-httpd_responses.c | 412 | ||||
-rw-r--r-- | src/sync/sync-httpd_responses.h | 230 | ||||
-rw-r--r-- | src/sync/sync-httpd_terms.c | 19 |
12 files changed, 128 insertions, 1470 deletions
diff --git a/src/sync/Makefile.am b/src/sync/Makefile.am index 40c8aba..ae62e37 100644 --- a/src/sync/Makefile.am +++ b/src/sync/Makefile.am @@ -14,14 +14,13 @@ sync_httpd_SOURCES = \ sync-httpd_backup.c sync-httpd_backup.h \ sync-httpd_backup_post.c \ sync-httpd_mhd.c sync-httpd_mhd.h \ - sync-httpd_parsing.c sync-httpd_parsing.h \ - sync-httpd_responses.c sync-httpd_responses.h \ sync-httpd_terms.c sync-httpd_terms.h sync_httpd_LDADD = \ $(top_builddir)/src/syncdb/libsyncdb.la \ -lmicrohttpd \ -ljansson \ -ltalermerchant \ + -ltalermhd \ -ltalerjson \ -ltalerutil \ -lgnunetcurl \ diff --git a/src/sync/sync-httpd.c b/src/sync/sync-httpd.c index d8f8072..1918d42 100644 --- a/src/sync/sync-httpd.c +++ b/src/sync/sync-httpd.c @@ -14,16 +14,14 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ /** - * @file backup/sync-httpd.c + * @file sync/sync-httpd.c * @brief HTTP serving layer intended to provide basic backup operations * @author Christian Grothoff */ #include "platform.h" #include <microhttpd.h> #include <gnunet/gnunet_util_lib.h> -#include "sync-httpd_responses.h" #include "sync-httpd.h" -#include "sync-httpd_parsing.h" #include "sync-httpd_mhd.h" #include "sync_database_lib.h" #include "sync-httpd_backup.h" @@ -35,15 +33,11 @@ #define UNIX_BACKLOG 500 -/** - * The port we are running on - */ -static long long unsigned port; /** * Should a "Connection: close" header be added to each HTTP response? */ -int SH_sync_connection_close; +static int SH_sync_connection_close; /** * Upload limit to the service, in megabytes. @@ -91,17 +85,6 @@ static int result; static struct MHD_Daemon *mhd; /** - * Path for the unix domain-socket - * to run the daemon on. - */ -static char *serve_unixpath; - -/** - * File mode for unix-domain socket. - */ -static mode_t unixpath_mode; - -/** * Connection handle to the our database */ struct SYNC_DatabasePlugin *db; @@ -505,9 +488,15 @@ run (void *cls, const struct GNUNET_CONFIGURATION_Handle *config) { int fh; + enum TALER_MHD_GlobalOptions go; + uint16_t port; GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting sync-httpd\n"); + go = TALER_MHD_GO_NONE; + if (SH_sync_connection_close) + go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE; + TALER_MHD_setup (go); result = GNUNET_SYSERR; GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); @@ -577,243 +566,14 @@ run (void *cls, return; } + fh = TALER_MHD_bind (config, + "sync", + &port); + if ( (0 == port) && + (-1 == fh) ) { - const char *choices[] = {"tcp", - "unix", - NULL}; - - const char *serve_type; - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_choice (config, - "sync", - "SERVE", - choices, - &serve_type)) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - "sync", - "SERVE", - "serve type required"); - GNUNET_SCHEDULER_shutdown (); - return; - } - - if (0 == strcmp (serve_type, "unix")) - { - struct sockaddr_un *un; - char *mode; - struct GNUNET_NETWORK_Handle *nh; - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_filename (config, - "sync", - "unixpath", - &serve_unixpath)) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - "sync", - "unixpath", - "unixpath required"); - GNUNET_SCHEDULER_shutdown (); - return; - } - - if (strlen (serve_unixpath) >= sizeof (un->sun_path)) - { - fprintf (stderr, - "Invalid configuration: unix path too long\n"); - GNUNET_SCHEDULER_shutdown (); - return; - } - - if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (config, - "sync", - "UNIXPATH_MODE", - &mode)) - { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "sync", - "UNIXPATH_MODE"); - GNUNET_SCHEDULER_shutdown (); - return; - } - errno = 0; - unixpath_mode = (mode_t) strtoul (mode, NULL, 8); - if (0 != errno) - { - GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, - "sync", - "UNIXPATH_MODE", - "must be octal number"); - GNUNET_free (mode); - GNUNET_SCHEDULER_shutdown (); - return; - } - GNUNET_free (mode); - - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Creating listen socket '%s' with mode %o\n", - serve_unixpath, unixpath_mode); - - if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (serve_unixpath)) - { - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, - "mkdir", - serve_unixpath); - } - - un = GNUNET_new (struct sockaddr_un); - un->sun_family = AF_UNIX; - strncpy (un->sun_path, - serve_unixpath, - sizeof (un->sun_path) - 1); - - GNUNET_NETWORK_unix_precheck (un); - - if (NULL == (nh = GNUNET_NETWORK_socket_create (AF_UNIX, - SOCK_STREAM, - 0))) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "socket(AF_UNIX)"); - GNUNET_SCHEDULER_shutdown (); - return; - } - if (GNUNET_OK != - GNUNET_NETWORK_socket_bind (nh, - (void *) un, - sizeof (struct sockaddr_un))) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "bind(AF_UNIX)"); - GNUNET_SCHEDULER_shutdown (); - return; - } - if (GNUNET_OK != - GNUNET_NETWORK_socket_listen (nh, - UNIX_BACKLOG)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "listen(AF_UNIX)"); - GNUNET_SCHEDULER_shutdown (); - return; - } - - fh = GNUNET_NETWORK_get_fd (nh); - GNUNET_NETWORK_socket_free_memory_only_ (nh); - if (0 != chmod (serve_unixpath, - unixpath_mode)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "chmod"); - GNUNET_SCHEDULER_shutdown (); - return; - } - port = 0; - } - else if (0 == strcmp (serve_type, "tcp")) - { - char *bind_to; - - if (GNUNET_SYSERR == - GNUNET_CONFIGURATION_get_value_number (config, - "sync", - "PORT", - &port)) - { - GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, - "sync", - "PORT"); - GNUNET_SCHEDULER_shutdown (); - return; - } - if (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (config, - "sync", - "BIND_TO", - &bind_to)) - { - char port_str[6]; - struct addrinfo hints; - struct addrinfo *res; - int ec; - struct GNUNET_NETWORK_Handle *nh; - - GNUNET_snprintf (port_str, - sizeof (port_str), - "%u", - (uint16_t) port); - memset (&hints, 0, sizeof (hints)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - hints.ai_flags = AI_PASSIVE -#ifdef AI_IDN - | AI_IDN -#endif - ; - if (0 != - (ec = getaddrinfo (bind_to, - port_str, - &hints, - &res))) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Failed to resolve BIND_TO address `%s': %s\n", - bind_to, - gai_strerror (ec)); - GNUNET_free (bind_to); - GNUNET_SCHEDULER_shutdown (); - return; - } - GNUNET_free (bind_to); - - if (NULL == (nh = GNUNET_NETWORK_socket_create (res->ai_family, - res->ai_socktype, - res->ai_protocol))) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "socket"); - freeaddrinfo (res); - GNUNET_SCHEDULER_shutdown (); - return; - } - if (GNUNET_OK != - GNUNET_NETWORK_socket_bind (nh, - res->ai_addr, - res->ai_addrlen)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "bind"); - freeaddrinfo (res); - GNUNET_SCHEDULER_shutdown (); - return; - } - freeaddrinfo (res); - if (GNUNET_OK != - GNUNET_NETWORK_socket_listen (nh, - UNIX_BACKLOG)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "listen"); - GNUNET_SCHEDULER_shutdown (); - return; - } - fh = GNUNET_NETWORK_get_fd (nh); - GNUNET_NETWORK_socket_free_memory_only_ (nh); - } - else - { - fh = -1; - } - } - else - { - // not reached - GNUNET_assert (0); - } + GNUNET_SCHEDULER_shutdown (); + return; } mhd = MHD_start_daemon (MHD_USE_SUSPEND_RESUME | MHD_USE_DUAL_STACK, port, diff --git a/src/sync/sync-httpd.h b/src/sync/sync-httpd.h index 33cdb26..a7279d5 100644 --- a/src/sync/sync-httpd.h +++ b/src/sync/sync-httpd.h @@ -23,6 +23,7 @@ #include "platform.h" #include <microhttpd.h> +#include <taler/taler_mhd_lib.h> #include "sync_database_lib.h" /** @@ -122,11 +123,6 @@ struct TM_HandlerContext /** - * Should a "Connection: close" header be added to each HTTP response? - */ -extern int SH_sync_connection_close; - -/** * Handle to the database backend. */ extern struct SYNC_DatabasePlugin *db; diff --git a/src/sync/sync-httpd_backup.c b/src/sync/sync-httpd_backup.c index e55c7d9..d74f722 100644 --- a/src/sync/sync-httpd_backup.c +++ b/src/sync/sync-httpd_backup.c @@ -22,7 +22,7 @@ #include "sync-httpd.h" #include <gnunet/gnunet_util_lib.h> #include "sync-httpd_backup.h" -#include "sync-httpd_responses.h" + /** * Handle request on @a connection for retrieval of the latest @@ -47,23 +47,27 @@ sync_handler_backup_get (struct MHD_Connection *connection, { case SYNC_DB_OLD_BACKUP_MISSMATCH: GNUNET_break (0); - return SH_RESPONSE_reply_internal_error (connection, - TALER_EC_INTERNAL_INVARIANT_FAILURE, - "unexpected return status (backup missmatch)"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_INVARIANT_FAILURE, + "unexpected return status (backup missmatch)"); case SYNC_DB_PAYMENT_REQUIRED: - return SH_RESPONSE_reply_not_found (connection, - TALER_EC_SYNC_ACCOUNT_UNKNOWN, - "account"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_SYNC_ACCOUNT_UNKNOWN, + "account"); case SYNC_DB_HARD_ERROR: GNUNET_break (0); - return SH_RESPONSE_reply_internal_error (connection, - TALER_EC_SYNC_DB_FETCH_ERROR, - "hard database failure"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_SYNC_DB_FETCH_ERROR, + "hard database failure"); case SYNC_DB_SOFT_ERROR: GNUNET_break (0); - return SH_RESPONSE_reply_internal_error (connection, - TALER_EC_SYNC_DB_FETCH_ERROR, - "soft database failure"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_SYNC_DB_FETCH_ERROR, + "soft database failure"); case SYNC_DB_NO_RESULTS: { struct MHD_Response *resp; @@ -99,9 +103,10 @@ sync_handler_backup_get (struct MHD_Connection *connection, sizeof (inm_h))) { GNUNET_break_op (0); - return SH_RESPONSE_reply_bad_request (connection, - TALER_EC_SYNC_BAD_IF_NONE_MATCH, - "Etag does not include a base32-encoded SHA-512 hash"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_SYNC_BAD_IF_NONE_MATCH, + "Etag does not include a base32-encoded SHA-512 hash"); } if (0 == GNUNET_memcmp (&inm_h, &backup_hash)) @@ -167,33 +172,38 @@ SH_return_backup (struct MHD_Connection *connection, { case SYNC_DB_OLD_BACKUP_MISSMATCH: GNUNET_break (0); - return SH_RESPONSE_reply_internal_error (connection, - TALER_EC_INTERNAL_INVARIANT_FAILURE, - "unexpected return status (backup missmatch)"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_INVARIANT_FAILURE, + "unexpected return status (backup missmatch)"); case SYNC_DB_PAYMENT_REQUIRED: GNUNET_break (0); - return SH_RESPONSE_reply_internal_error (connection, - TALER_EC_INTERNAL_INVARIANT_FAILURE, - "unexpected return status (payment required)"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_INVARIANT_FAILURE, + "unexpected return status (payment required)"); case SYNC_DB_HARD_ERROR: GNUNET_break (0); - return SH_RESPONSE_reply_internal_error (connection, - TALER_EC_SYNC_DB_FETCH_ERROR, - "hard database failure"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_SYNC_DB_FETCH_ERROR, + "hard database failure"); case SYNC_DB_SOFT_ERROR: GNUNET_break (0); - return SH_RESPONSE_reply_internal_error (connection, - TALER_EC_SYNC_DB_FETCH_ERROR, - "soft database failure"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_SYNC_DB_FETCH_ERROR, + "soft database failure"); case SYNC_DB_NO_RESULTS: GNUNET_break (0); /* Note: can theoretically happen due to non-transactional nature if the backup expired / was gc'ed JUST between the two SQL calls. But too rare to handle properly, as doing a transaction would be expensive. Just admit to failure ;-) */ - return SH_RESPONSE_reply_internal_error (connection, - TALER_EC_SYNC_DB_FETCH_ERROR, - "unexpected empty result set (try again?)"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_SYNC_DB_FETCH_ERROR, + "unexpected empty result set (try again?)"); case SYNC_DB_ONE_RESULT: /* interesting case below */ break; diff --git a/src/sync/sync-httpd_backup_post.c b/src/sync/sync-httpd_backup_post.c index 41d6350..8b8ea35 100644 --- a/src/sync/sync-httpd_backup_post.c +++ b/src/sync/sync-httpd_backup_post.c @@ -22,7 +22,7 @@ #include "sync-httpd.h" #include <gnunet/gnunet_util_lib.h> #include "sync-httpd_backup.h" -#include "sync-httpd_responses.h" +#include <taler/taler_json_lib.h> #include <taler/taler_merchant_service.h> #include <taler/taler_signatures.h> @@ -273,8 +273,8 @@ proposal_cb (void *cls, if (0 >= qs) { GNUNET_break (0); - bc->resp = SH_RESPONSE_make_error (TALER_EC_SYNC_PAYMENT_CREATE_DB_ERROR, - "Failed to persist payment request in sync database"); + bc->resp = TALER_MHD_make_error (TALER_EC_SYNC_PAYMENT_CREATE_DB_ERROR, + "Failed to persist payment request in sync database"); bc->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; return; } @@ -358,8 +358,8 @@ check_payment_cb (void *cls, if (0 <= qs) return; /* continue as planned */ GNUNET_break (0); - bc->resp = SH_RESPONSE_make_error (TALER_EC_SYNC_PAYMENT_CONFIRM_DB_ERROR, - "Failed to persist payment confirmation in sync database"); + bc->resp = TALER_MHD_make_error (TALER_EC_SYNC_PAYMENT_CONFIRM_DB_ERROR, + "Failed to persist payment confirmation in sync database"); bc->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; return; /* continue as planned */ } @@ -370,8 +370,8 @@ check_payment_cb (void *cls, bc->response_code = MHD_HTTP_PAYMENT_REQUIRED; return; } - bc->resp = SH_RESPONSE_make_error (TALER_EC_SYNC_PAYMENT_TIMEOUT, - "Timeout awaiting promised payment"); + bc->resp = TALER_MHD_make_error (TALER_EC_SYNC_PAYMENT_TIMEOUT, + "Timeout awaiting promised payment"); bc->response_code = MHD_HTTP_REQUEST_TIMEOUT; } @@ -431,8 +431,8 @@ begin_payment (struct BackupContext *bc, struct MHD_Response *resp; int ret; - resp = SH_RESPONSE_make_error (TALER_EC_SYNC_PAYMENT_CHECK_ORDER_DB_ERROR, - "Failed to check for existing orders in sync database"); + resp = TALER_MHD_make_error (TALER_EC_SYNC_PAYMENT_CHECK_ORDER_DB_ERROR, + "Failed to check for existing orders in sync database"); ret = MHD_queue_response (bc->con, MHD_HTTP_INTERNAL_SERVER_ERROR, resp); @@ -503,9 +503,10 @@ handle_database_error (struct BackupContext *bc, case SYNC_DB_HARD_ERROR: case SYNC_DB_SOFT_ERROR: GNUNET_break (0); - return SH_RESPONSE_reply_internal_error (bc->con, - TALER_EC_SYNC_DATABASE_FETCH_ERROR, - "failed to fetch existing record from database"); + return TALER_MHD_reply_with_error (bc->con, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_SYNC_DATABASE_FETCH_ERROR, + "failed to fetch existing record from database"); case SYNC_DB_NO_RESULTS: GNUNET_assert (0); return MHD_NO; @@ -563,29 +564,30 @@ sync_handler_backup_post (struct MHD_Connection *connection, &len)) ) { GNUNET_break_op (0); - return SH_RESPONSE_reply_bad_request (connection, - TALER_EC_SYNC_BAD_CONTENT_LENGTH, - (NULL == lens) - ? "Content-length value missing" - : "Content-length value malformed"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_SYNC_BAD_CONTENT_LENGTH, + (NULL == lens) + ? "Content-length value missing" + : "Content-length value malformed"); } if (len / 1024 / 1024 >= SH_upload_limit_mb) { GNUNET_break_op (0); - return SH_RESPONSE_reply_rc (connection, - MHD_HTTP_PAYLOAD_TOO_LARGE, - TALER_EC_SYNC_BAD_CONTENT_LENGTH, - "Content-length value not acceptable"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_PAYLOAD_TOO_LARGE, + TALER_EC_SYNC_BAD_CONTENT_LENGTH, + "Content-length value not acceptable"); } bc->upload = GNUNET_malloc_large (len); if (NULL == bc->upload) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); - return SH_RESPONSE_reply_rc (connection, - MHD_HTTP_PAYLOAD_TOO_LARGE, - TALER_EC_SYNC_OUT_OF_MEMORY_ON_CONTENT_LENGTH, - "Server out of memory, try again later"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_PAYLOAD_TOO_LARGE, + TALER_EC_SYNC_OUT_OF_MEMORY_ON_CONTENT_LENGTH, + "Server out of memory, try again later"); } bc->upload_size = (size_t) len; } @@ -603,9 +605,10 @@ sync_handler_backup_post (struct MHD_Connection *connection, sizeof (&bc->old_backup_hash))) ) { GNUNET_break_op (0); - return SH_RESPONSE_reply_bad_request (connection, - TALER_EC_SYNC_BAD_IF_MATCH, - "If-Match does not include not a base32-encoded SHA-512 hash"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_SYNC_BAD_IF_MATCH, + "If-Match does not include not a base32-encoded SHA-512 hash"); } } { @@ -622,9 +625,10 @@ sync_handler_backup_post (struct MHD_Connection *connection, sizeof (&bc->account_sig))) ) { GNUNET_break_op (0); - return SH_RESPONSE_reply_bad_request (connection, - TALER_EC_SYNC_BAD_SYNC_SIGNATURE, - "Sync-Signature does not include a base32-encoded EdDSA signature"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_SYNC_BAD_SYNC_SIGNATURE, + "Sync-Signature does not include a base32-encoded EdDSA signature"); } } { @@ -641,9 +645,10 @@ sync_handler_backup_post (struct MHD_Connection *connection, sizeof (&bc->new_backup_hash))) ) { GNUNET_break_op (0); - return SH_RESPONSE_reply_bad_request (connection, - TALER_EC_SYNC_BAD_ETAG, - "Etag does not include not a base32-encoded SHA-512 hash"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_SYNC_BAD_ETAG, + "Etag does not include not a base32-encoded SHA-512 hash"); } } /* validate signature */ @@ -661,10 +666,10 @@ sync_handler_backup_post (struct MHD_Connection *connection, &account->eddsa_pub)) { GNUNET_break_op (0); - return SH_RESPONSE_reply_rc (connection, - MHD_HTTP_UNAUTHORIZED, - TALER_EC_SYNC_INVALID_SIGNATURE, - "Account signature does not match upload"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_UNAUTHORIZED, + TALER_EC_SYNC_INVALID_SIGNATURE, + "Account signature does not match upload"); } } /* get ready to hash (done here as we may go async for payments next) */ @@ -760,9 +765,10 @@ sync_handler_backup_post (struct MHD_Connection *connection, &bc->new_backup_hash)) { GNUNET_break_op (0); - return SH_RESPONSE_reply_bad_request (connection, - TALER_EC_SYNC_INVALID_UPLOAD, - "Data uploaded does not match Etag promise"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_BAD_REQUEST, + TALER_EC_SYNC_INVALID_UPLOAD, + "Data uploaded does not match Etag promise"); } } diff --git a/src/sync/sync-httpd_mhd.c b/src/sync/sync-httpd_mhd.c index 077105e..75f2a63 100644 --- a/src/sync/sync-httpd_mhd.c +++ b/src/sync/sync-httpd_mhd.c @@ -25,7 +25,6 @@ #include "platform.h" #include <jansson.h> #include "sync-httpd_mhd.h" -#include "sync-httpd_responses.h" /** @@ -46,29 +45,16 @@ SH_MHD_handler_static_response (struct SH_RequestHandler *rh, const char *upload_data, size_t *upload_data_size) { - struct MHD_Response *response; - int ret; - + (void) connection_cls; + (void) upload_data; + (void) upload_data_size; if (0 == rh->data_size) rh->data_size = strlen ((const char *) rh->data); - response = MHD_create_response_from_buffer (rh->data_size, - (void *) rh->data, - MHD_RESPMEM_PERSISTENT); - if (NULL == response) - { - GNUNET_break (0); - return MHD_NO; - } - SH_RESPONSE_add_global_headers (response); - if (NULL != rh->mime_type) - (void) MHD_add_response_header (response, - MHD_HTTP_HEADER_CONTENT_TYPE, - rh->mime_type); - ret = MHD_queue_response (connection, - rh->response_code, - response); - MHD_destroy_response (response); - return ret; + return TALER_MHD_reply_static (connection, + rh->response_code, + rh->mime_type, + (void *) rh->data, + rh->data_size); } @@ -90,66 +76,12 @@ SH_MHD_handler_agpl_redirect (struct SH_RequestHandler *rh, const char *upload_data, size_t *upload_data_size) { - const char *agpl = - "This server is licensed under the Affero GPL. You will now be redirected to the source code."; - struct MHD_Response *response; - int ret; - - response = MHD_create_response_from_buffer (strlen (agpl), - (void *) agpl, - MHD_RESPMEM_PERSISTENT); - if (NULL == response) - { - GNUNET_break (0); - return MHD_NO; - } - SH_RESPONSE_add_global_headers (response); - if (NULL != rh->mime_type) - (void) MHD_add_response_header (response, - MHD_HTTP_HEADER_CONTENT_TYPE, - rh->mime_type); - if (MHD_NO == - MHD_add_response_header (response, - MHD_HTTP_HEADER_LOCATION, - "http://www.git.taler.net/sync.git")) - { - GNUNET_break (0); - ret = MHD_NO; - } - else - { - ret = MHD_queue_response (connection, - rh->response_code, - response); - } - MHD_destroy_response (response); - return ret; -} - - -/** - * Function to call to handle the request by building a JSON - * reply with an error message from @a rh. - * - * @param rh context of the handler - * @param connection the MHD connection to handle - * @param[in,out] connection_cls the connection's closure (can be updated) - * @param upload_data upload data - * @param[in,out] upload_data_size number of bytes (left) in @a upload_data - * @return MHD result code - */ -int -SH_MHD_handler_send_json_pack_error (struct SH_RequestHandler *rh, - struct MHD_Connection *connection, - void **connection_cls, - const char *upload_data, - size_t *upload_data_size) -{ - return SH_RESPONSE_reply_json_pack (connection, - rh->response_code, - "{s:s}", - "error", - rh->data); + (void) rh; + (void) connection_cls; + (void) upload_data; + (void) upload_data_size; + return TALER_MHD_reply_agpl (connection, + "http://www.git.taler.net/sync.git"); } diff --git a/src/sync/sync-httpd_mhd.h b/src/sync/sync-httpd_mhd.h index 36612ff..40efd2e 100644 --- a/src/sync/sync-httpd_mhd.h +++ b/src/sync/sync-httpd_mhd.h @@ -68,46 +68,4 @@ SH_MHD_handler_agpl_redirect (struct SH_RequestHandler *rh, size_t *upload_data_size); -/** - * Function to call to handle the request by building a JSON - * reply from varargs. - * - * @param rh context of the handler - * @param connection the MHD connection to handle - * @param[in,out] connection_cls the connection's closure (can be updated) - * @param response_code HTTP response code to use - * @param do_cache can the response be cached? (0: no, 1: yes) - * @param fmt format string for pack - * @param ... varargs - * @return MHD result code - */ -int -SH_MHD_helper_send_json_pack (struct SH_RequestHandler *rh, - struct MHD_Connection *connection, - void *connection_cls, - int response_code, - int do_cache, - const char *fmt, - ...); - - -/** - * Function to call to handle the request by building a JSON - * reply with an error message from @a rh. - * - * @param rh context of the handler - * @param connection the MHD connection to handle - * @param[in,out] connection_cls the connection's closure (can be updated) - * @param upload_data upload data - * @param[in,out] upload_data_size number of bytes (left) in @a upload_data - * @return MHD result code - */ -int -SH_MHD_handler_send_json_pack_error (struct SH_RequestHandler *rh, - struct MHD_Connection *connection, - void **connection_cls, - const char *upload_data, - size_t *upload_data_size); - - #endif diff --git a/src/sync/sync-httpd_parsing.c b/src/sync/sync-httpd_parsing.c deleted file mode 100644 index 180f47c..0000000 --- a/src/sync/sync-httpd_parsing.c +++ /dev/null @@ -1,267 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014, 2015, 2016 GNUnet e.V. - - TALER is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public - License along with TALER; see the file COPYING. If not, - see <http://www.gnu.org/licenses/> -*/ -/** - * @file sync-httpd_parsing.c - * @brief functions to parse incoming requests - * (MHD arguments and JSON snippets) - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#include "platform.h" -#include <gnunet/gnunet_util_lib.h> -#include <taler/taler_json_lib.h> -#include "sync-httpd_parsing.h" -#include "sync-httpd_responses.h" - -/* FIXME: de-duplicate code with taler-exchange-httpd_parsing.c - and taler-exchange-httpd_response.c */ - -/** - * Initial size for POST request buffer. - */ -#define REQUEST_BUFFER_INITIAL (2 * 1024) - -/** - * Maximum POST request size. - */ -#define REQUEST_BUFFER_MAX (1024 * 1024) - - -/** - * Buffer for POST requests. - */ -struct Buffer -{ - /** - * Allocated memory - */ - char *data; - - /** - * Number of valid bytes in buffer. - */ - size_t fill; - - /** - * Number of allocated bytes in buffer. - */ - size_t alloc; -}; - - -/** - * Free the data in a buffer. Does *not* free - * the buffer object itself. - * - * @param buf buffer to de-initialize - */ -static void -buffer_deinit (struct Buffer *buf) -{ - GNUNET_free_non_null (buf->data); - buf->data = NULL; -} - - -/** - * Function called whenever we are done with a request - * to clean up our state. - * - * @param con_cls value as it was left by - * #SH_PARSE_post_json(), to be cleaned up - */ -void -SH_PARSE_post_cleanup_callback (void *con_cls) -{ - struct Buffer *r = con_cls; - - if (NULL != r) - { - buffer_deinit (r); - GNUNET_free (r); - } -} - - -/** - * Process a POST request containing a JSON object. This function - * realizes an MHD POST processor that will (incrementally) process - * JSON data uploaded to the HTTP server. It will store the - * required state in the @a con_cls, which must be cleaned up - * using #SH_PARSE_post_cleanup_callback(). - * - * @param connection the MHD connection - * @param con_cls the closure (points to a `struct Buffer *`) - * @param upload_data the POST data - * @param upload_data_size number of bytes in @a upload_data - * @param json the JSON object for a completed request - * @return - * #GNUNET_YES if json object was parsed or at least - * may be parsed in the future (call again); - * `*json` will be NULL if we need to be called again, - * and non-NULL if we are done. - * #GNUNET_NO if request is incomplete or invalid - * (error message was generated) - * #GNUNET_SYSERR on internal error - * (we could not even queue an error message, - * close HTTP session with MHD_NO) - */ -int -SH_PARSE_post_json (struct MHD_Connection *connection, - void **con_cls, - const char *upload_data, - size_t *upload_data_size, - json_t **json) -{ - enum GNUNET_JSON_PostResult pr; - - pr = GNUNET_JSON_post_parser (REQUEST_BUFFER_MAX, - connection, - con_cls, - upload_data, - upload_data_size, - json); - switch (pr) - { - - case GNUNET_JSON_PR_OUT_OF_MEMORY: - return (MHD_NO == SH_RESPONSE_reply_internal_error - (connection, - TALER_EC_PARSER_OUT_OF_MEMORY, - "out of memory")) ? GNUNET_SYSERR : GNUNET_NO; - - case GNUNET_JSON_PR_CONTINUE: - return GNUNET_YES; - - case GNUNET_JSON_PR_REQUEST_TOO_LARGE: - return (MHD_NO == SH_RESPONSE_reply_request_too_large - (connection)) ? GNUNET_SYSERR : GNUNET_NO; - - case GNUNET_JSON_PR_JSON_INVALID: - return (MHD_YES == - SH_RESPONSE_reply_invalid_json (connection)) - ? GNUNET_NO : GNUNET_SYSERR; - case GNUNET_JSON_PR_SUCCESS: - GNUNET_break (NULL != *json); - return GNUNET_YES; - } - /* this should never happen */ - GNUNET_break (0); - return GNUNET_SYSERR; -} - - -/** - * Parse JSON object into components based on the given field - * specification. - * - * @param connection the connection to send an error response to - * @param root the JSON node to start the navigation at. - * @param spec field specification for the parser - * @return - * #GNUNET_YES if navigation was successful (caller is responsible - * for freeing allocated variable-size data using - * #GNUNET_JSON_parse_free() when done) - * #GNUNET_NO if json is malformed, error response was generated - * #GNUNET_SYSERR on internal error - */ -int -SH_PARSE_json_data (struct MHD_Connection *connection, - const json_t *root, - struct GNUNET_JSON_Specification *spec) -{ - int ret; - const char *error_json_name; - unsigned int error_line; - - ret = GNUNET_JSON_parse (root, - spec, - &error_json_name, - &error_line); - if (GNUNET_SYSERR == ret) - { - if (NULL == error_json_name) - error_json_name = "<no field>"; - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Parsing failed due to field '%s'\n", - error_json_name); - ret = (MHD_YES == - SH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s, s:s, s:I}", - "error", "parse error", - "field", error_json_name, - "line", (json_int_t) error_line)) - ? GNUNET_NO : GNUNET_SYSERR; - return ret; - } - return GNUNET_YES; -} - - -/** - * Extract base32crockford encoded data from request. - * - * Queues an error response to the connection if the parameter is - * missing or invalid. - * - * @param connection the MHD connection - * @param param_name the name of the parameter with the key - * @param[out] out_data pointer to store the result - * @param out_size expected size of data - * @return - * #GNUNET_YES if the the argument is present - * #GNUNET_NO if the argument is absent or malformed - * #GNUNET_SYSERR on internal error (error response could not be sent) - */ -int -SH_PARSE_mhd_request_arg_data (struct MHD_Connection *connection, - const char *param_name, - void *out_data, - size_t out_size) -{ - const char *str; - - str = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - param_name); - if (NULL == str) - { - return (MHD_NO == - SH_RESPONSE_reply_arg_missing (connection, - TALER_EC_PARAMETER_MISSING, - param_name)) - ? GNUNET_SYSERR : GNUNET_NO; - } - if (GNUNET_OK != - GNUNET_STRINGS_string_to_data (str, - strlen (str), - out_data, - out_size)) - return (MHD_NO == - SH_RESPONSE_reply_arg_invalid (connection, - TALER_EC_PARAMETER_MALFORMED, - param_name)) - ? GNUNET_SYSERR : GNUNET_NO; - return GNUNET_OK; -} - - -/* end of taler-merchant-httpd_parsing.c */ diff --git a/src/sync/sync-httpd_parsing.h b/src/sync/sync-httpd_parsing.h deleted file mode 100644 index d7e5be3..0000000 --- a/src/sync/sync-httpd_parsing.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014, 2015, 2016 GNUnet e.V. - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file sync-httpd_parsing.h - * @brief functions to parse incoming requests - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#ifndef sync_HTTPD_PARSING_H -#define sync_HTTPD_PARSING_H - -#include <microhttpd.h> -#include <taler/taler_util.h> -#include <taler/taler_json_lib.h> - -/** - * Process a POST request containing a JSON object. This - * function realizes an MHD POST processor that will - * (incrementally) process JSON data uploaded to the HTTP - * server. It will store the required state in the - * "connection_cls", which must be cleaned up using - * #SH_PARSE_post_cleanup_callback(). - * - * @param connection the MHD connection - * @param con_cls the closure (points to a `struct Buffer *`) - * @param upload_data the POST data - * @param upload_data_size number of bytes in @a upload_data - * @param json the JSON object for a completed request - * @return - * #GNUNET_YES if json object was parsed or at least - * may be parsed in the future (call again); - * `*json` will be NULL if we need to be called again, - * and non-NULL if we are done. - * #GNUNET_NO is request incomplete or invalid - * (error message was generated) - * #GNUNET_SYSERR on internal error - * (we could not even queue an error message, - * close HTTP session with MHD_NO) - */ -int -SH_PARSE_post_json (struct MHD_Connection *connection, - void **con_cls, - const char *upload_data, - size_t *upload_data_size, - json_t **json); - - -/** - * Function called whenever we are done with a request - * to clean up our state. - * - * @param con_cls value as it was left by - * #SH_PARSE_post_json(), to be cleaned up - */ -void -SH_PARSE_post_cleanup_callback (void *con_cls); - - -/** - * Parse JSON object into components based on the given field - * specification. - * - * @param connection the connection to send an error response to - * @param root the JSON node to start the navigation at. - * @param spec field specification for the parser - * @return - * #GNUNET_YES if navigation was successful (caller is responsible - * for freeing allocated variable-size data using - * #GNUNET_JSON_parse_free() when done) - * #GNUNET_NO if json is malformed, error response was generated - * #GNUNET_SYSERR on internal error - */ -int -SH_PARSE_json_data (struct MHD_Connection *connection, - const json_t *root, - struct GNUNET_JSON_Specification *spec); - - -#endif /* TALER_MERCHANT_HTTPD_PARSING_H */ diff --git a/src/sync/sync-httpd_responses.c b/src/sync/sync-httpd_responses.c deleted file mode 100644 index 628a0ac..0000000 --- a/src/sync/sync-httpd_responses.c +++ /dev/null @@ -1,412 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014-2017 GNUnet e.V. - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file sync-httpd_responses.c - * @brief API for generating the various replies of the exchange; these - * functions are called SH_RESPONSE_reply_ and they generate - * and queue MHD response objects for a given connection. - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#include "platform.h" -#include "sync-httpd.h" -#include "sync-httpd_responses.h" -#include <taler/taler_util.h> -#include <taler/taler_json_lib.h> -#include <gnunet/gnunet_util_lib.h> - - -/** - * Make JSON response object. - * - * @param json the json object - * @return MHD response object - */ -struct MHD_Response * -SH_RESPONSE_make_json (const json_t *json) -{ - struct MHD_Response *resp; - char *json_str; - - json_str = json_dumps (json, - JSON_INDENT (2)); - if (NULL == json_str) - { - GNUNET_break (0); - return NULL; - } - resp = MHD_create_response_from_buffer (strlen (json_str), - json_str, - MHD_RESPMEM_MUST_FREE); - if (NULL == resp) - { - free (json_str); - GNUNET_break (0); - return NULL; - } - GNUNET_break (MHD_YES == - MHD_add_response_header (resp, - MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, - "*")); - GNUNET_break (MHD_YES == - MHD_add_response_header (resp, - MHD_HTTP_HEADER_CONTENT_TYPE, - "application/json")); - return resp; -} - - -/** - * Send JSON object as response. - * - * @param connection the MHD connection - * @param json the json object - * @param response_code the http response code - * @return MHD result code - */ -int -SH_RESPONSE_reply_json (struct MHD_Connection *connection, - const json_t *json, - unsigned int response_code) -{ - struct MHD_Response *resp; - int ret; - - resp = SH_RESPONSE_make_json (json); - if (NULL == resp) - return MHD_NO; - ret = MHD_queue_response (connection, - response_code, - resp); - MHD_destroy_response (resp); - return ret; -} - - -/** - * Make JSON response object. - * - * @param fmt format string for pack - * @param ... varargs - * @return MHD response object - */ -struct MHD_Response * -SH_RESPONSE_make_json_pack (const char *fmt, - ...) -{ - json_t *json; - va_list argp; - struct MHD_Response *ret; - json_error_t jerror; - - va_start (argp, fmt); - json = json_vpack_ex (&jerror, - 0, - fmt, - argp); - va_end (argp); - if (NULL == json) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Failed to pack JSON with format `%s': %s\n", - fmt, - jerror.text); - GNUNET_break (0); - return MHD_NO; - } - ret = SH_RESPONSE_make_json (json); - json_decref (json); - return ret; -} - - -/** - * Function to call to handle the request by building a JSON - * reply from a format string and varargs. - * - * @param connection the MHD connection to handle - * @param response_code HTTP response code to use - * @param fmt format string for pack - * @param ... varargs - * @return MHD result code - */ -int -SH_RESPONSE_reply_json_pack (struct MHD_Connection *connection, - unsigned int response_code, - const char *fmt, - ...) -{ - json_t *json; - va_list argp; - int ret; - json_error_t jerror; - - va_start (argp, fmt); - json = json_vpack_ex (&jerror, - 0, - fmt, - argp); - va_end (argp); - if (NULL == json) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Failed to pack JSON with format `%s': %s\n", - fmt, - jerror.text); - GNUNET_break (0); - return MHD_NO; - } - ret = SH_RESPONSE_reply_json (connection, - json, - response_code); - json_decref (json); - return ret; -} - - -/** - * Create a response indicating an internal error. - * - * @param ec error code to return - * @param hint hint about the internal error's nature - * @return a MHD response object - */ -struct MHD_Response * -SH_RESPONSE_make_error (enum TALER_ErrorCode ec, - const char *hint) -{ - return SH_RESPONSE_make_json_pack ("{s:I, s:s}", - "code", (json_int_t) ec, - "hint", hint); -} - - -/** - * Send a response indicating an internal error. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param hint hint about the internal error's nature - * @return a MHD result code - */ -int -SH_RESPONSE_reply_internal_error (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *hint) -{ - return SH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_INTERNAL_SERVER_ERROR, - "{s:I, s:s}", - "code", (json_int_t) ec, - "hint", hint); -} - - -/** - * Send a response indicating that the request was too big. - * - * @param connection the MHD connection to use - * @return a MHD result code - */ -int -SH_RESPONSE_reply_request_too_large (struct MHD_Connection *connection) -{ - struct MHD_Response *resp; - int ret; - - resp = MHD_create_response_from_buffer (0, - NULL, - MHD_RESPMEM_PERSISTENT); - if (NULL == resp) - return MHD_NO; - ret = MHD_queue_response (connection, - MHD_HTTP_REQUEST_ENTITY_TOO_LARGE, - resp); - MHD_destroy_response (resp); - return ret; -} - - -/** - * Send a response indicating that we did not find the @a object - * needed for the reply. - * - * @param connection the MHD connection to use - * @param response_code response code to use - * @param ec error code to return - * @param msg human-readable diagnostic - * @return a MHD result code - */ -int -SH_RESPONSE_reply_rc (struct MHD_Connection *connection, - unsigned int response_code, - enum TALER_ErrorCode ec, - const char *msg) -{ - return SH_RESPONSE_reply_json_pack (connection, - response_code, - "{s:I, s:s}", - "code", (json_int_t) ec, - "error", msg); -} - - -/** - * Send a response indicating that the JSON was malformed. - * - * @param connection the MHD connection to use - * @return a MHD result code - */ -int -SH_RESPONSE_reply_invalid_json (struct MHD_Connection *connection) -{ - return SH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:I, s:s}", - "code", - (json_int_t) TALER_EC_JSON_INVALID, - "error", "invalid json"); -} - - -/** - * Send a response indicating that we did not find the @a object - * needed for the reply. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param object name of the object we did not find - * @return a MHD result code - */ -int -SH_RESPONSE_reply_not_found (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *object) -{ - return SH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_NOT_FOUND, - "{s:I, s:s}", - "code", (json_int_t) ec, - "error", object); -} - - -/** - * Send a response indicating that the request was malformed. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param issue description of what was wrong with the request - * @return a MHD result code - */ -int -SH_RESPONSE_reply_bad_request (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *issue) -{ - return SH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:I, s:s}", - "code", (json_int_t) ec, - "error", issue); -} - - -/** - * Add headers we want to return in every response. - * Useful for testing, like if we want to always close - * connections. - * - * @param response response to modify - */ -void -SH_RESPONSE_add_global_headers (struct MHD_Response *response) -{ - if (SH_sync_connection_close) - GNUNET_break (MHD_YES == - MHD_add_response_header (response, - MHD_HTTP_HEADER_CONNECTION, - "close")); -} - - -/** - * Send a response indicating an external error. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param hint hint about the error's nature - * @return a MHD result code - */ -int -SH_RESPONSE_reply_external_error (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *hint) -{ - return SH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:I, s:s}", - "code", (json_int_t) ec, - "hint", hint); -} - - -/** - * Send a response indicating a missing argument. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param param_name the parameter that is missing - * @return a MHD result code - */ -int -SH_RESPONSE_reply_arg_missing (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *param_name) -{ - return SH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s, s:I, s:s}", - "error", "missing parameter", - "code", (json_int_t) ec, - "parameter", param_name); -} - - -/** - * Send a response indicating an invalid argument. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param param_name the parameter that is invalid - * @return a MHD result code - */ -int -SH_RESPONSE_reply_arg_invalid (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *param_name) -{ - return SH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s, s:I, s:s}", - "error", "invalid parameter", - "code", (json_int_t) ec, - "parameter", param_name); -} - - -/* end of taler-exchange-httpd_responses.c */ diff --git a/src/sync/sync-httpd_responses.h b/src/sync/sync-httpd_responses.h deleted file mode 100644 index 4ff4002..0000000 --- a/src/sync/sync-httpd_responses.h +++ /dev/null @@ -1,230 +0,0 @@ -/* - This file is part of TALER - Copyright (C) 2014-2017 GNUnet e.V. - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU Affero 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License along with - TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> -*/ -/** - * @file sync-httpd_responses.h - * @brief API for generating the various replies of the exchange; these - * functions are called SH_RESPONSE_reply_ and they generate - * and queue MHD response objects for a given connection. - * @author Florian Dold - * @author Benedikt Mueller - * @author Christian Grothoff - */ -#ifndef sync_HTTPD_RESPONSES_H -#define sync_HTTPD_RESPONSES_H -#include "sync-httpd.h" -#include <taler/taler_util.h> -#include <taler/taler_json_lib.h> -#include <gnunet/gnunet_util_lib.h> - - -/** - * Make JSON response object. - * - * @param json the json object - * @return MHD response object - */ -struct MHD_Response * -SH_RESPONSE_make_json (const json_t *json); - - -/** - * Send JSON object as response. - * - * @param connection the MHD connection - * @param json the json object - * @param response_code the http response code - * @return MHD result code - */ -int -SH_RESPONSE_reply_json (struct MHD_Connection *connection, - const json_t *json, - unsigned int response_code); - - -/** - * Make JSON response object. - * - * @param fmt format string for pack - * @param ... varargs - * @return MHD response object - */ -struct MHD_Response * -SH_RESPONSE_make_json_pack (const char *fmt, - ...); - - -/** - * Function to call to handle the request by building a JSON - * reply from a format string and varargs. - * - * @param connection the MHD connection to handle - * @param response_code HTTP response code to use - * @param fmt format string for pack - * @param ... varargs - * @return MHD result code - */ -int -SH_RESPONSE_reply_json_pack (struct MHD_Connection *connection, - unsigned int response_code, - const char *fmt, - ...); - - -/** - * Create a response indicating an internal error. - * - * @param ec error code to return - * @param hint hint about the internal error's nature - * @return a MHD response object - */ -struct MHD_Response * -SH_RESPONSE_make_error (enum TALER_ErrorCode ec, - const char *hint); - - -/** - * Send a response indicating an internal error. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param hint hint about the internal error's nature - * @return a MHD result code - */ -int -SH_RESPONSE_reply_internal_error (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *hint); - - -/** - * Send a response indicating that the request was too big. - * - * @param connection the MHD connection to use - * @return a MHD result code - */ -int -SH_RESPONSE_reply_request_too_large (struct MHD_Connection *connection); - - -/** - * Send a response indicating that we did not find the @a object - * needed for the reply. - * - * @param connection the MHD connection to use - * @param response_code response code to use - * @param ec error code to return - * @param msg human-readable diagnostic - * @return a MHD result code - */ -int -SH_RESPONSE_reply_rc (struct MHD_Connection *connection, - unsigned int response_code, - enum TALER_ErrorCode ec, - const char *msg); - - -/** - * Send a response indicating that the JSON was malformed. - * - * @param connection the MHD connection to use - * @return a MHD result code - */ -int -SH_RESPONSE_reply_invalid_json (struct MHD_Connection *connection); - - -/** - * Send a response indicating that we did not find the @a object - * needed for the reply. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param object name of the object we did not find - * @return a MHD result code - */ -int -SH_RESPONSE_reply_not_found (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *object); - - -/** - * Send a response indicating that the request was malformed. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param issue description of what was wrong with the request - * @return a MHD result code - */ -int -SH_RESPONSE_reply_bad_request (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *issue); - - -/** - * Add headers we want to return in every response. - * Useful for testing, like if we want to always close - * connections. - * - * @param response response to modify - */ -void -SH_RESPONSE_add_global_headers (struct MHD_Response *response); - - -/** - * Send a response indicating an external error. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param hint hint about the error's nature - * @return a MHD result code - */ -int -SH_RESPONSE_reply_external_error (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *hint); - - -/** - * Send a response indicating a missing argument. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param param_name the parameter that is missing - * @return a MHD result code - */ -int -SH_RESPONSE_reply_arg_missing (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *param_name); - - -/** - * Send a response indicating an invalid argument. - * - * @param connection the MHD connection to use - * @param ec error code to return - * @param param_name the parameter that is invalid - * @return a MHD result code - */ -int -SH_RESPONSE_reply_arg_invalid (struct MHD_Connection *connection, - enum TALER_ErrorCode ec, - const char *param_name); - -#endif diff --git a/src/sync/sync-httpd_terms.c b/src/sync/sync-httpd_terms.c index a96e4d5..2528ff0 100644 --- a/src/sync/sync-httpd_terms.c +++ b/src/sync/sync-httpd_terms.c @@ -19,7 +19,6 @@ * @author Christian Grothoff */ #include "platform.h" -#include "sync-httpd_responses.h" #include "sync-httpd_terms.h" #include <taler/taler_json_lib.h> @@ -41,13 +40,13 @@ SH_handler_terms (struct SH_RequestHandler *rh, const char *upload_data, size_t *upload_data_size) { - return SH_RESPONSE_reply_json_pack (connection, - MHD_HTTP_OK, - "{s:I, s:o, s:s}", - "storage_limit_in_megabytes", - (json_int_t) SH_upload_limit_mb, - "annual_fee", - TALER_JSON_from_amount (&SH_annual_fee), - "version", - "0.0"); + return TALER_MHD_reply_json_pack (connection, + MHD_HTTP_OK, + "{s:I, s:o, s:s}", + "storage_limit_in_megabytes", + (json_int_t) SH_upload_limit_mb, + "annual_fee", + TALER_JSON_from_amount (&SH_annual_fee), + "version", + "0.0"); } |