aboutsummaryrefslogtreecommitdiff
path: root/src/testzzuf/test_put_large.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testzzuf/test_put_large.c')
-rw-r--r--src/testzzuf/test_put_large.c403
1 files changed, 0 insertions, 403 deletions
diff --git a/src/testzzuf/test_put_large.c b/src/testzzuf/test_put_large.c
deleted file mode 100644
index 900284ef..00000000
--- a/src/testzzuf/test_put_large.c
+++ /dev/null
@@ -1,403 +0,0 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2007, 2008 Christian Grothoff
4
5 libmicrohttpd is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 libmicrohttpd 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libmicrohttpd; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/**
22 * @file test_put_large.c
23 * @brief Testcase for libmicrohttpd PUT operations
24 * @author Christian Grothoff
25 */
26
27#include "MHD_config.h"
28#include "platform.h"
29#include <curl/curl.h>
30#include <microhttpd.h>
31#include <stdlib.h>
32#include <string.h>
33#include <time.h>
34
35#ifndef WINDOWS
36#include <unistd.h>
37#endif
38
39#include "socat.c"
40
41static int oneone;
42
43/**
44 * Do not make this much larger since we will hit the
45 * MHD default buffer limit and the test code is not
46 * written for incremental upload processing...
47 */
48#define PUT_SIZE (256 * 1024)
49
50static char *put_buffer;
51
52struct CBC
53{
54 char *buf;
55 size_t pos;
56 size_t size;
57};
58
59static size_t
60putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
61{
62 unsigned int *pos = ptr;
63 unsigned int wrt;
64
65 wrt = size * nmemb;
66 if (wrt > PUT_SIZE - (*pos))
67 wrt = PUT_SIZE - (*pos);
68 memcpy (stream, &put_buffer[*pos], wrt);
69 (*pos) += wrt;
70 return wrt;
71}
72
73
74static size_t
75copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
76{
77 struct CBC *cbc = ctx;
78
79 if (cbc->pos + size * nmemb > cbc->size)
80 return 0; /* overflow */
81 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
82 cbc->pos += size * nmemb;
83 return size * nmemb;
84}
85
86
87static enum MHD_Result
88ahc_echo (void *cls,
89 struct MHD_Connection *connection,
90 const char *url,
91 const char *method,
92 const char *version,
93 const char *upload_data, size_t *upload_data_size,
94 void **req_cls)
95{
96 int *done = cls;
97 struct MHD_Response *response;
98 enum MHD_Result ret;
99 (void) version; (void) req_cls; /* Unused. Silent compiler warning. */
100
101 if (NULL == url)
102 fprintf (stderr, "The \"url\" parameter is NULL.\n");
103 if (NULL == method)
104 fprintf (stderr, "The \"method\" parameter is NULL.\n");
105 if (NULL == version)
106 fprintf (stderr, "The \"version\" parameter is NULL.\n");
107 if (NULL == upload_data_size)
108 fprintf (stderr, "The \"upload_data_size\" parameter is NULL.\n");
109 if ((0 != *upload_data_size) && (NULL == upload_data))
110 fprintf (stderr, "Upload data is NULL with non-zero size.\n");
111 if (0 != strcmp ("PUT", method))
112 return MHD_NO; /* unexpected method */
113 if ((*done) == 0)
114 {
115 if (*upload_data_size != PUT_SIZE)
116 {
117#if 0
118 fprintf (stderr,
119 "Waiting for more data (%u/%u)...\n",
120 *upload_data_size, PUT_SIZE);
121#endif
122 return MHD_YES; /* not yet ready */
123 }
124 if (0 == memcmp (upload_data, put_buffer, PUT_SIZE))
125 {
126 *upload_data_size = 0;
127 }
128 else
129 {
130 return MHD_NO;
131 }
132 *done = 1;
133 return MHD_YES;
134 }
135 response = MHD_create_response_from_buffer (strlen (url),
136 (void *) url,
137 MHD_RESPMEM_MUST_COPY);
138 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
139 MHD_destroy_response (response);
140 return ret;
141}
142
143
144static unsigned int
145testInternalPut ()
146{
147 struct MHD_Daemon *d;
148 CURL *c;
149 struct CBC cbc;
150 unsigned int pos = 0;
151 int done_flag = 0;
152 char buf[2048];
153 int i;
154
155 cbc.buf = buf;
156 cbc.size = 2048;
157 cbc.pos = 0;
158 d =
159 MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD /* | MHD_USE_ERROR_LOG */,
160 11080,
161 NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
162 if (d == NULL)
163 return 1;
164 zzuf_socat_start ();
165 for (i = 0; i < LOOP_COUNT; i++)
166 {
167 fprintf (stderr, ".");
168
169 c = curl_easy_init ();
170 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:11081/hello_world");
171 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
172 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
173 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
174 curl_easy_setopt (c, CURLOPT_READDATA, &pos);
175 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
176 curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE);
177 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
178 curl_easy_setopt (c, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT);
179 if (oneone)
180 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
181 else
182 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
183 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT_MS, CURL_TIMEOUT);
184 /* NOTE: use of CONNECTTIMEOUT without also
185 * setting NOSIGNAL results in really weird
186 * crashes on my system! */
187 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
188 curl_easy_perform (c);
189 curl_easy_cleanup (c);
190 }
191 fprintf (stderr, "\n");
192 zzuf_socat_stop ();
193 MHD_stop_daemon (d);
194 return 0;
195}
196
197
198static unsigned int
199testMultithreadedPut ()
200{
201 struct MHD_Daemon *d;
202 CURL *c;
203 struct CBC cbc;
204 unsigned int pos = 0;
205 int done_flag = 0;
206 char buf[2048];
207 int i;
208
209 cbc.buf = buf;
210 cbc.size = 2048;
211 cbc.pos = 0;
212 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
213 | MHD_USE_INTERNAL_POLLING_THREAD /* | MHD_USE_ERROR_LOG */,
214 11080,
215 NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
216 if (d == NULL)
217 return 16;
218 zzuf_socat_start ();
219 for (i = 0; i < LOOP_COUNT; i++)
220 {
221 fprintf (stderr, ".");
222
223 c = curl_easy_init ();
224 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:11081/hello_world");
225 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
226 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
227 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
228 curl_easy_setopt (c, CURLOPT_READDATA, &pos);
229 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
230 curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE);
231 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
232 curl_easy_setopt (c, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT);
233 if (oneone)
234 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
235 else
236 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
237 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT_MS, CURL_TIMEOUT);
238 /* NOTE: use of CONNECTTIMEOUT without also
239 * setting NOSIGNAL results in really weird
240 * crashes on my system! */
241 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
242 curl_easy_perform (c);
243 curl_easy_cleanup (c);
244 }
245 fprintf (stderr, "\n");
246 zzuf_socat_stop ();
247 MHD_stop_daemon (d);
248 return 0;
249}
250
251
252static unsigned int
253testExternalPut ()
254{
255 struct MHD_Daemon *d;
256 CURL *c;
257 struct CBC cbc;
258 CURLM *multi;
259 CURLMcode mret;
260 fd_set rs;
261 fd_set ws;
262 fd_set es;
263 int max;
264 int running;
265 time_t start;
266 struct timeval tv;
267 unsigned int pos = 0;
268 int done_flag = 0;
269 char buf[2048];
270 int i;
271
272 cbc.buf = buf;
273 cbc.size = 2048;
274 cbc.pos = 0;
275 multi = NULL;
276 d = MHD_start_daemon (MHD_NO_FLAG /* | MHD_USE_ERROR_LOG */,
277 11080,
278 NULL, NULL, &ahc_echo, &done_flag,
279 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
280 (size_t) (PUT_SIZE * 4), MHD_OPTION_END);
281 if (d == NULL)
282 return 256;
283 multi = curl_multi_init ();
284 if (multi == NULL)
285 {
286 MHD_stop_daemon (d);
287 return 512;
288 }
289 zzuf_socat_start ();
290 for (i = 0; i < LOOP_COUNT; i++)
291 {
292 fprintf (stderr, ".");
293
294 c = curl_easy_init ();
295 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:11081/hello_world");
296 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
297 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
298 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
299 curl_easy_setopt (c, CURLOPT_READDATA, &pos);
300 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
301 curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE);
302 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
303 curl_easy_setopt (c, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT);
304 if (oneone)
305 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
306 else
307 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
308 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT_MS, CURL_TIMEOUT);
309 /* NOTE: use of CONNECTTIMEOUT without also
310 * setting NOSIGNAL results in really weird
311 * crashes on my system! */
312 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
313
314
315 mret = curl_multi_add_handle (multi, c);
316 if (mret != CURLM_OK)
317 {
318 curl_multi_cleanup (multi);
319 curl_easy_cleanup (c);
320 zzuf_socat_stop ();
321 MHD_stop_daemon (d);
322 return 1024;
323 }
324 start = time (NULL);
325 while ((time (NULL) - start < 5) && (c != NULL))
326 {
327 max = 0;
328 FD_ZERO (&rs);
329 FD_ZERO (&ws);
330 FD_ZERO (&es);
331 curl_multi_perform (multi, &running);
332 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
333 if (mret != CURLM_OK)
334 {
335 curl_multi_remove_handle (multi, c);
336 curl_multi_cleanup (multi);
337 curl_easy_cleanup (c);
338 zzuf_socat_stop ();
339 MHD_stop_daemon (d);
340 return 2048;
341 }
342 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
343 {
344 curl_multi_remove_handle (multi, c);
345 curl_multi_cleanup (multi);
346 curl_easy_cleanup (c);
347 zzuf_socat_stop ();
348 MHD_stop_daemon (d);
349 return 4096;
350 }
351 tv.tv_sec = 0;
352 tv.tv_usec = 1000;
353 select (max + 1, &rs, &ws, &es, &tv);
354 curl_multi_perform (multi, &running);
355 if (running == 0)
356 {
357 curl_multi_info_read (multi, &running);
358 curl_multi_remove_handle (multi, c);
359 curl_easy_cleanup (c);
360 c = NULL;
361 }
362 MHD_run (d);
363 }
364 if (c != NULL)
365 {
366 curl_multi_remove_handle (multi, c);
367 curl_easy_cleanup (c);
368 }
369 }
370 fprintf (stderr, "\n");
371 zzuf_socat_stop ();
372 curl_multi_cleanup (multi);
373 MHD_stop_daemon (d);
374 return 0;
375}
376
377
378int
379main (int argc, char *const *argv)
380{
381 unsigned int errorCount = 0;
382 (void) argc; /* Unused. Silent compiler warning. */
383
384 oneone = (NULL != strrchr (argv[0], (int) '/')) ?
385 (NULL != strstr (strrchr (argv[0], (int) '/'), "11")) : 0;
386 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
387 return 2;
388 put_buffer = malloc (PUT_SIZE);
389 if (0 == put_buffer)
390 return 77;
391 memset (put_buffer, 1, PUT_SIZE);
392 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS))
393 {
394 errorCount += testInternalPut ();
395 errorCount += testMultithreadedPut ();
396 }
397 errorCount += testExternalPut ();
398 free (put_buffer);
399 if (errorCount != 0)
400 fprintf (stderr, "Error (code: %u)\n", errorCount);
401 curl_global_cleanup ();
402 return (0 == errorCount) ? 0 : 1; /* 0 == pass */
403}