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