taler-auditor-httpd_mhd.c (4106B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014-2020 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 /** 18 * @file taler-auditor-httpd_mhd.c 19 * @brief helpers for MHD interaction; these are TALER_MHD_handler_ functions 20 * that generate simple MHD replies that do not require any real operations 21 * to be performed (error handling, static pages, etc.) 22 * @author Florian Dold 23 * @author Benedikt Mueller 24 * @author Christian Grothoff 25 */ 26 #include <gnunet/gnunet_util_lib.h> 27 #include <jansson.h> 28 #include <microhttpd.h> 29 #include <pthread.h> 30 #include "taler/taler_mhd_lib.h" 31 #include "taler-auditor-httpd.h" 32 #include "taler-auditor-httpd_mhd.h" 33 34 35 /** 36 * Check recursively that integers in @a value are valid SafeUint64 values. 37 * 38 * @param value JSON value to inspect 39 * @return true if all integers are safe 40 */ 41 static bool 42 json_integers_are_safe (const json_t *value) 43 { 44 if (json_is_integer (value)) 45 { 46 json_int_t number = json_integer_value (value); 47 48 return (number >= 0) && 49 ((uint64_t) number <= TAH_SAFE_UINT64_MAX); 50 } 51 if (json_is_array (value)) 52 { 53 size_t index; 54 json_t *child; 55 56 json_array_foreach ((json_t *) value, index, child) 57 if (! json_integers_are_safe (child)) 58 return false; 59 } 60 if (json_is_object (value)) 61 { 62 const char *key; 63 json_t *child; 64 65 json_object_foreach ((json_t *) value, key, child) 66 if (! json_integers_are_safe (child)) 67 return false; 68 } 69 return true; 70 } 71 72 73 enum MHD_Result 74 TAH_reply_json_records (struct MHD_Connection *connection, 75 json_t *records) 76 { 77 if (! json_integers_are_safe (records)) 78 { 79 GNUNET_break (0); 80 json_decref (records); 81 return TALER_MHD_reply_with_error ( 82 connection, 83 MHD_HTTP_INTERNAL_SERVER_ERROR, 84 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 85 "monitoring response contains an integer outside SafeUint64"); 86 } 87 return TALER_MHD_REPLY_JSON_PACK ( 88 connection, 89 MHD_HTTP_OK, 90 GNUNET_JSON_pack_array_steal ("records", 91 records)); 92 } 93 94 95 enum MHD_Result 96 TAH_MHD_handler_static_response (struct TAH_RequestHandler *rh, 97 struct MHD_Connection *connection, 98 void **connection_cls, 99 const char *upload_data, 100 size_t *upload_data_size, 101 const char *const args[]) 102 { 103 size_t dlen; 104 105 (void) connection_cls; 106 (void) upload_data; 107 (void) upload_data_size; 108 dlen = (0 == rh->data_size) 109 ? strlen ((const char *) rh->data) 110 : rh->data_size; 111 return TALER_MHD_reply_static (connection, 112 rh->response_code, 113 rh->mime_type, 114 rh->data, 115 dlen); 116 } 117 118 119 enum MHD_Result 120 TAH_MHD_handler_agpl_redirect (struct TAH_RequestHandler *rh, 121 struct MHD_Connection *connection, 122 void **connection_cls, 123 const char *upload_data, 124 size_t *upload_data_size, 125 const char *const args[]) 126 { 127 (void) rh; 128 (void) connection_cls; 129 (void) upload_data; 130 (void) upload_data_size; 131 return TALER_MHD_reply_agpl (connection, 132 "http://www.git.taler.net/?p=exchange.git"); 133 } 134 135 136 /* end of taler-auditor-httpd_mhd.c */