taler-auditor-httpd_mhd.c (4322B)
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 can be represented exactly by 37 * JavaScript clients, that is, that their magnitude does not exceed the 38 * largest SafeUint64. Negative values occur (such as "profitable" being -1 39 * for a loss). 40 * 41 * @param value JSON value to inspect 42 * @return true if all integers are safe 43 */ 44 static bool 45 json_integers_are_safe (const json_t *value) 46 { 47 if (json_is_integer (value)) 48 { 49 json_int_t number = json_integer_value (value); 50 51 if (number < 0) 52 return ((uint64_t) (-(number + 1))) < TAH_SAFE_UINT64_MAX; 53 return ((uint64_t) number <= TAH_SAFE_UINT64_MAX); 54 } 55 if (json_is_array (value)) 56 { 57 size_t index; 58 json_t *child; 59 60 json_array_foreach ((json_t *) value, index, child) 61 if (! json_integers_are_safe (child)) 62 return false; 63 } 64 if (json_is_object (value)) 65 { 66 const char *key; 67 json_t *child; 68 69 json_object_foreach ((json_t *) value, key, child) 70 if (! json_integers_are_safe (child)) 71 return false; 72 } 73 return true; 74 } 75 76 77 enum MHD_Result 78 TAH_reply_json_records (struct MHD_Connection *connection, 79 json_t *records) 80 { 81 if (! json_integers_are_safe (records)) 82 { 83 GNUNET_break (0); 84 json_decref (records); 85 return TALER_MHD_reply_with_error ( 86 connection, 87 MHD_HTTP_INTERNAL_SERVER_ERROR, 88 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 89 "monitoring response contains an integer outside SafeUint64"); 90 } 91 return TALER_MHD_REPLY_JSON_PACK ( 92 connection, 93 MHD_HTTP_OK, 94 GNUNET_JSON_pack_array_steal ("records", 95 records)); 96 } 97 98 99 enum MHD_Result 100 TAH_MHD_handler_static_response (struct TAH_RequestHandler *rh, 101 struct MHD_Connection *connection, 102 void **connection_cls, 103 const char *upload_data, 104 size_t *upload_data_size, 105 const char *const args[]) 106 { 107 size_t dlen; 108 109 (void) connection_cls; 110 (void) upload_data; 111 (void) upload_data_size; 112 dlen = (0 == rh->data_size) 113 ? strlen ((const char *) rh->data) 114 : rh->data_size; 115 return TALER_MHD_reply_static (connection, 116 rh->response_code, 117 rh->mime_type, 118 rh->data, 119 dlen); 120 } 121 122 123 enum MHD_Result 124 TAH_MHD_handler_agpl_redirect (struct TAH_RequestHandler *rh, 125 struct MHD_Connection *connection, 126 void **connection_cls, 127 const char *upload_data, 128 size_t *upload_data_size, 129 const char *const args[]) 130 { 131 (void) rh; 132 (void) connection_cls; 133 (void) upload_data; 134 (void) upload_data_size; 135 return TALER_MHD_reply_agpl (connection, 136 "http://www.git.taler.net/?p=exchange.git"); 137 } 138 139 140 /* end of taler-auditor-httpd_mhd.c */