aboutsummaryrefslogtreecommitdiff
path: root/src/json/test_json_mhd.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-05-03 16:18:59 +0200
committerChristian Grothoff <christian@grothoff.org>2019-05-03 16:18:59 +0200
commit782565417337605572b66758bf13d7123e26f744 (patch)
treeaaf2094c7bdea5fa809f03573cb4292f1895160d /src/json/test_json_mhd.c
parent4444fb80284aa86fe24f3640a0b1e4c841a98f9a (diff)
downloadgnunet-782565417337605572b66758bf13d7123e26f744.tar.gz
gnunet-782565417337605572b66758bf13d7123e26f744.zip
support compressed JSON uploads
Diffstat (limited to 'src/json/test_json_mhd.c')
-rw-r--r--src/json/test_json_mhd.c47
1 files changed, 42 insertions, 5 deletions
diff --git a/src/json/test_json_mhd.c b/src/json/test_json_mhd.c
index 665fd140e..13338b32c 100644
--- a/src/json/test_json_mhd.c
+++ b/src/json/test_json_mhd.c
@@ -27,6 +27,7 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include "gnunet_json_lib.h" 28#include "gnunet_json_lib.h"
29#include "gnunet_curl_lib.h" 29#include "gnunet_curl_lib.h"
30#include <zlib.h>
30 31
31#define MAX_SIZE 1024 * 1024 32#define MAX_SIZE 1024 * 1024
32 33
@@ -69,7 +70,7 @@ access_handler_cb (void *cls,
69 global_ret = 6; 70 global_ret = 6;
70 } 71 }
71 json_decref (json); 72 json_decref (json);
72 resp = MHD_create_response_from_buffer (2, "OK", MHD_RESPMEM_PERSISTENT); 73 resp = MHD_create_response_from_buffer (3, "OK\n", MHD_RESPMEM_PERSISTENT);
73 ret = MHD_queue_response (connection, MHD_HTTP_OK, resp); 74 ret = MHD_queue_response (connection, MHD_HTTP_OK, resp);
74 MHD_destroy_response (resp); 75 MHD_destroy_response (resp);
75 return ret; 76 return ret;
@@ -100,8 +101,12 @@ main (int argc, const char *const argv[])
100 uint16_t port; 101 uint16_t port;
101 CURL *easy; 102 CURL *easy;
102 char *url; 103 char *url;
104 char *str;
105 size_t slen;
103 long post_data_size; 106 long post_data_size;
104 void *post_data; 107 void *post_data;
108 uLongf dlen;
109 struct curl_slist *json_header;
105 110
106 GNUNET_log_setup ("test-json-mhd", "WARNING", NULL); 111 GNUNET_log_setup ("test-json-mhd", "WARNING", NULL);
107 global_ret = 2; 112 global_ret = 2;
@@ -123,28 +128,60 @@ main (int argc, const char *const argv[])
123 GNUNET_snprintf (tmp, sizeof (tmp), "%u", i); 128 GNUNET_snprintf (tmp, sizeof (tmp), "%u", i);
124 json_object_set_new (bigj, tmp, json_string (tmp)); 129 json_object_set_new (bigj, tmp, json_string (tmp));
125 } 130 }
126 post_data = json_dumps (bigj, JSON_INDENT (2)); 131 str = json_dumps (bigj, JSON_INDENT (2));
127 post_data_size = strlen (post_data); 132 slen = strlen (str);
128 133
134#ifdef compressBound
135 dlen = compressBound (slen);
136#else
137 dlen = slen + slen / 100 + 20;
138 /* documentation says 100.1% oldSize + 12 bytes, but we
139 * should be able to overshoot by more to be safe */
140#endif
141 post_data = GNUNET_malloc (dlen);
142 if (Z_OK !=
143 compress2 ((Bytef *) post_data, &dlen, (const Bytef *) str, slen, 9))
144 {
145 GNUNET_break (0);
146 MHD_stop_daemon (daemon);
147 GNUNET_free (url);
148 json_decref (bigj);
149 GNUNET_free (post_data);
150 GNUNET_free (str);
151 return 1;
152 }
153 post_data_size = (long) dlen;
129 port = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT)->port; 154 port = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT)->port;
130 easy = curl_easy_init (); 155 easy = curl_easy_init ();
131 GNUNET_asprintf (&url, "http://localhost:%u/", (unsigned int) port); 156 GNUNET_asprintf (&url, "http://localhost:%u/", (unsigned int) port);
132 curl_easy_setopt (easy, CURLOPT_VERBOSE, 1); 157 curl_easy_setopt (easy, CURLOPT_VERBOSE, 0);
133 curl_easy_setopt (easy, CURLOPT_URL, url); 158 curl_easy_setopt (easy, CURLOPT_URL, url);
134 curl_easy_setopt (easy, CURLOPT_POST, 1); 159 curl_easy_setopt (easy, CURLOPT_POST, 1);
135 curl_easy_setopt (easy, CURLOPT_POSTFIELDS, post_data); 160 curl_easy_setopt (easy, CURLOPT_POSTFIELDS, post_data);
136 curl_easy_setopt (easy, CURLOPT_POSTFIELDSIZE, post_data_size); 161 curl_easy_setopt (easy, CURLOPT_POSTFIELDSIZE, post_data_size);
162
163 json_header = curl_slist_append (NULL, "Content-Type: application/json");
164 json_header = curl_slist_append (json_header, "Content-Encoding: deflate");
165 curl_easy_setopt (easy, CURLOPT_HTTPHEADER, json_header);
137 if (0 != curl_easy_perform (easy)) 166 if (0 != curl_easy_perform (easy))
138 { 167 {
139 GNUNET_break (0); 168 GNUNET_break (0);
140 MHD_stop_daemon (daemon); 169 MHD_stop_daemon (daemon);
141 GNUNET_free (url); 170 GNUNET_free (url);
142 json_decref (bigj); 171 json_decref (bigj);
172 GNUNET_free (post_data);
173 GNUNET_free (str);
174 curl_slist_free_all (json_header);
175 curl_easy_cleanup (easy);
143 return 1; 176 return 1;
144 } 177 }
145 MHD_stop_daemon (daemon); 178 MHD_stop_daemon (daemon);
146 GNUNET_free (url); 179 GNUNET_free (url);
147 json_decref (bigj); 180 json_decref (bigj);
181 GNUNET_free (post_data);
182 GNUNET_free (str);
183 curl_slist_free_all (json_header);
184 curl_easy_cleanup (easy);
148 return global_ret; 185 return global_ret;
149} 186}
150 187