2 This file is part of GNUnet
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file json/test_json_mhd.c
23 * @brief Tests for JSON MHD integration functions
24 * @author Christian Grothoff <christian@grothoff.org>
27 #include "gnunet_util_lib.h"
28 #include "gnunet_json_lib.h"
29 #include "gnunet_curl_lib.h"
32 #define MAX_SIZE 1024 * 1024
36 static int global_ret;
40 access_handler_cb (void *cls,
41 struct MHD_Connection *connection,
45 const char *upload_data,
46 size_t *upload_data_size,
51 struct MHD_Response *resp;
54 ret = GNUNET_JSON_post_parser (MAX_SIZE,
62 case GNUNET_JSON_PR_SUCCESS:
63 if (json_equal (bigj, json))
73 resp = MHD_create_response_from_buffer (3, "OK\n", MHD_RESPMEM_PERSISTENT);
74 ret = MHD_queue_response (connection, MHD_HTTP_OK, resp);
75 MHD_destroy_response (resp);
78 case GNUNET_JSON_PR_CONTINUE:
81 case GNUNET_JSON_PR_OUT_OF_MEMORY:
86 case GNUNET_JSON_PR_REQUEST_TOO_LARGE:
91 case GNUNET_JSON_PR_JSON_INVALID:
102 main (int argc, const char *const argv[])
104 struct MHD_Daemon *daemon;
113 struct curl_slist *json_header;
115 GNUNET_log_setup ("test-json-mhd", "WARNING", NULL);
117 daemon = MHD_start_daemon (MHD_USE_DUAL_STACK | MHD_USE_AUTO_INTERNAL_THREAD,
126 bigj = json_object ();
127 json_object_set_new (bigj, "test", json_string ("value"));
128 for (unsigned int i = 0; i < 1000; i++)
132 GNUNET_snprintf (tmp, sizeof(tmp), "%u", i);
133 json_object_set_new (bigj, tmp, json_string (tmp));
135 str = json_dumps (bigj, JSON_INDENT (2));
139 dlen = compressBound (slen);
141 dlen = slen + slen / 100 + 20;
142 /* documentation says 100.1% oldSize + 12 bytes, but we
143 * should be able to overshoot by more to be safe */
145 post_data = GNUNET_malloc (dlen);
147 compress2 ((Bytef *) post_data, &dlen, (const Bytef *) str, slen, 9))
150 MHD_stop_daemon (daemon);
152 GNUNET_free (post_data);
156 post_data_size = (long) dlen;
157 port = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT)->port;
158 easy = curl_easy_init ();
159 GNUNET_asprintf (&url, "http://localhost:%u/", (unsigned int) port);
160 curl_easy_setopt (easy, CURLOPT_VERBOSE, 0);
161 curl_easy_setopt (easy, CURLOPT_URL, url);
162 curl_easy_setopt (easy, CURLOPT_POST, 1);
163 curl_easy_setopt (easy, CURLOPT_POSTFIELDS, post_data);
164 curl_easy_setopt (easy, CURLOPT_POSTFIELDSIZE, post_data_size);
166 json_header = curl_slist_append (NULL, "Content-Type: application/json");
167 json_header = curl_slist_append (json_header, "Content-Encoding: deflate");
168 curl_easy_setopt (easy, CURLOPT_HTTPHEADER, json_header);
169 if (0 != curl_easy_perform (easy))
172 MHD_stop_daemon (daemon);
175 GNUNET_free (post_data);
177 curl_slist_free_all (json_header);
178 curl_easy_cleanup (easy);
181 MHD_stop_daemon (daemon);
184 GNUNET_free (post_data);
186 curl_slist_free_all (json_header);
187 curl_easy_cleanup (easy);
192 /* end of test_json_mhd.c */