diff options
Diffstat (limited to 'src/testcurl/daemontest_large_put.c')
-rw-r--r-- | src/testcurl/daemontest_large_put.c | 404 |
1 files changed, 404 insertions, 0 deletions
diff --git a/src/testcurl/daemontest_large_put.c b/src/testcurl/daemontest_large_put.c new file mode 100644 index 00000000..b0c7bf37 --- /dev/null +++ b/src/testcurl/daemontest_large_put.c | |||
@@ -0,0 +1,404 @@ | |||
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 | |||
38 | static 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 | |||
47 | static char *put_buffer; | ||
48 | |||
49 | struct CBC | ||
50 | { | ||
51 | char *buf; | ||
52 | size_t pos; | ||
53 | size_t size; | ||
54 | }; | ||
55 | |||
56 | static size_t | ||
57 | putBuffer (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 | |||
70 | static size_t | ||
71 | copyBuffer (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 | |||
82 | static int | ||
83 | ahc_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 | void **unused) | ||
90 | { | ||
91 | int *done = cls; | ||
92 | struct MHD_Response *response; | ||
93 | int ret; | ||
94 | |||
95 | if (0 != strcmp ("PUT", method)) | ||
96 | return MHD_NO; /* unexpected method */ | ||
97 | if ((*done) == 0) | ||
98 | { | ||
99 | if (*upload_data_size != PUT_SIZE) | ||
100 | { | ||
101 | #if 0 | ||
102 | fprintf (stderr, | ||
103 | "Waiting for more data (%u/%u)...\n", | ||
104 | *upload_data_size, PUT_SIZE); | ||
105 | #endif | ||
106 | return MHD_YES; /* not yet ready */ | ||
107 | } | ||
108 | if (0 == memcmp (upload_data, put_buffer, PUT_SIZE)) | ||
109 | { | ||
110 | *upload_data_size = 0; | ||
111 | } | ||
112 | else | ||
113 | { | ||
114 | printf ("Invalid upload data!\n"); | ||
115 | return MHD_NO; | ||
116 | } | ||
117 | *done = 1; | ||
118 | return MHD_YES; | ||
119 | } | ||
120 | response = MHD_create_response_from_data (strlen (url), | ||
121 | (void *) url, MHD_NO, MHD_YES); | ||
122 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
123 | MHD_destroy_response (response); | ||
124 | return ret; | ||
125 | } | ||
126 | |||
127 | |||
128 | static int | ||
129 | testInternalPut () | ||
130 | { | ||
131 | struct MHD_Daemon *d; | ||
132 | CURL *c; | ||
133 | struct CBC cbc; | ||
134 | unsigned int pos = 0; | ||
135 | int done_flag = 0; | ||
136 | CURLcode errornum; | ||
137 | char buf[2048]; | ||
138 | |||
139 | cbc.buf = buf; | ||
140 | cbc.size = 2048; | ||
141 | cbc.pos = 0; | ||
142 | d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, | ||
143 | 1080, | ||
144 | NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); | ||
145 | if (d == NULL) | ||
146 | return 1; | ||
147 | c = curl_easy_init (); | ||
148 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world"); | ||
149 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
150 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
151 | curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); | ||
152 | curl_easy_setopt (c, CURLOPT_READDATA, &pos); | ||
153 | curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); | ||
154 | curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE); | ||
155 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
156 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); | ||
157 | if (oneone) | ||
158 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
159 | else | ||
160 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
161 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); | ||
162 | // NOTE: use of CONNECTTIMEOUT without also | ||
163 | // setting NOSIGNAL results in really weird | ||
164 | // crashes on my system! | ||
165 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
166 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
167 | { | ||
168 | fprintf (stderr, | ||
169 | "curl_easy_perform failed: `%s'\n", | ||
170 | curl_easy_strerror (errornum)); | ||
171 | curl_easy_cleanup (c); | ||
172 | MHD_stop_daemon (d); | ||
173 | return 2; | ||
174 | } | ||
175 | curl_easy_cleanup (c); | ||
176 | MHD_stop_daemon (d); | ||
177 | if (cbc.pos != strlen ("/hello_world")) | ||
178 | return 4; | ||
179 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
180 | return 8; | ||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | static int | ||
185 | testMultithreadedPut () | ||
186 | { | ||
187 | struct MHD_Daemon *d; | ||
188 | CURL *c; | ||
189 | struct CBC cbc; | ||
190 | unsigned int pos = 0; | ||
191 | int done_flag = 0; | ||
192 | CURLcode errornum; | ||
193 | char buf[2048]; | ||
194 | |||
195 | cbc.buf = buf; | ||
196 | cbc.size = 2048; | ||
197 | cbc.pos = 0; | ||
198 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | ||
199 | 1081, | ||
200 | NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); | ||
201 | if (d == NULL) | ||
202 | { | ||
203 | free (cbc.buf); | ||
204 | return 16; | ||
205 | } | ||
206 | c = curl_easy_init (); | ||
207 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world"); | ||
208 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
209 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
210 | curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); | ||
211 | curl_easy_setopt (c, CURLOPT_READDATA, &pos); | ||
212 | curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); | ||
213 | curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE); | ||
214 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
215 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); | ||
216 | if (oneone) | ||
217 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
218 | else | ||
219 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
220 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); | ||
221 | // NOTE: use of CONNECTTIMEOUT without also | ||
222 | // setting NOSIGNAL results in really weird | ||
223 | // crashes on my system! | ||
224 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
225 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
226 | { | ||
227 | fprintf (stderr, | ||
228 | "curl_easy_perform failed: `%s'\n", | ||
229 | curl_easy_strerror (errornum)); | ||
230 | curl_easy_cleanup (c); | ||
231 | MHD_stop_daemon (d); | ||
232 | return 32; | ||
233 | } | ||
234 | curl_easy_cleanup (c); | ||
235 | MHD_stop_daemon (d); | ||
236 | if (cbc.pos != strlen ("/hello_world")) | ||
237 | { | ||
238 | fprintf (stderr, "Got invalid response `%.*s'\n", cbc.pos, cbc.buf); | ||
239 | return 64; | ||
240 | } | ||
241 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
242 | return 128; | ||
243 | return 0; | ||
244 | } | ||
245 | |||
246 | |||
247 | static int | ||
248 | testExternalPut () | ||
249 | { | ||
250 | struct MHD_Daemon *d; | ||
251 | CURL *c; | ||
252 | struct CBC cbc; | ||
253 | CURLM *multi; | ||
254 | CURLMcode mret; | ||
255 | fd_set rs; | ||
256 | fd_set ws; | ||
257 | fd_set es; | ||
258 | int max; | ||
259 | int running; | ||
260 | struct CURLMsg *msg; | ||
261 | time_t start; | ||
262 | struct timeval tv; | ||
263 | unsigned int pos = 0; | ||
264 | int done_flag = 0; | ||
265 | char buf[2048]; | ||
266 | |||
267 | cbc.buf = buf; | ||
268 | cbc.size = 2048; | ||
269 | cbc.pos = 0; | ||
270 | multi = NULL; | ||
271 | d = MHD_start_daemon (MHD_USE_DEBUG, | ||
272 | 1082, | ||
273 | NULL, NULL, &ahc_echo, &done_flag, | ||
274 | MHD_OPTION_CONNECTION_MEMORY_LIMIT, | ||
275 | PUT_SIZE * 4, MHD_OPTION_END); | ||
276 | if (d == NULL) | ||
277 | return 256; | ||
278 | c = curl_easy_init (); | ||
279 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world"); | ||
280 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
281 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
282 | curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); | ||
283 | curl_easy_setopt (c, CURLOPT_READDATA, &pos); | ||
284 | curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); | ||
285 | curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE); | ||
286 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
287 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); | ||
288 | if (oneone) | ||
289 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
290 | else | ||
291 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
292 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); | ||
293 | // NOTE: use of CONNECTTIMEOUT without also | ||
294 | // setting NOSIGNAL results in really weird | ||
295 | // crashes on my system! | ||
296 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
297 | |||
298 | |||
299 | multi = curl_multi_init (); | ||
300 | if (multi == NULL) | ||
301 | { | ||
302 | curl_easy_cleanup (c); | ||
303 | MHD_stop_daemon (d); | ||
304 | return 512; | ||
305 | } | ||
306 | mret = curl_multi_add_handle (multi, c); | ||
307 | if (mret != CURLM_OK) | ||
308 | { | ||
309 | curl_multi_cleanup (multi); | ||
310 | curl_easy_cleanup (c); | ||
311 | MHD_stop_daemon (d); | ||
312 | return 1024; | ||
313 | } | ||
314 | start = time (NULL); | ||
315 | while ((time (NULL) - start < 5) && (multi != NULL)) | ||
316 | { | ||
317 | max = 0; | ||
318 | FD_ZERO (&rs); | ||
319 | FD_ZERO (&ws); | ||
320 | FD_ZERO (&es); | ||
321 | curl_multi_perform (multi, &running); | ||
322 | mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); | ||
323 | if (mret != CURLM_OK) | ||
324 | { | ||
325 | curl_multi_remove_handle (multi, c); | ||
326 | curl_multi_cleanup (multi); | ||
327 | curl_easy_cleanup (c); | ||
328 | MHD_stop_daemon (d); | ||
329 | return 2048; | ||
330 | } | ||
331 | if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) | ||
332 | { | ||
333 | curl_multi_remove_handle (multi, c); | ||
334 | curl_multi_cleanup (multi); | ||
335 | curl_easy_cleanup (c); | ||
336 | MHD_stop_daemon (d); | ||
337 | return 4096; | ||
338 | } | ||
339 | tv.tv_sec = 0; | ||
340 | tv.tv_usec = 1000; | ||
341 | select (max + 1, &rs, &ws, &es, &tv); | ||
342 | curl_multi_perform (multi, &running); | ||
343 | if (running == 0) | ||
344 | { | ||
345 | msg = curl_multi_info_read (multi, &running); | ||
346 | if (msg == NULL) | ||
347 | break; | ||
348 | if (msg->msg == CURLMSG_DONE) | ||
349 | { | ||
350 | if (msg->data.result != CURLE_OK) | ||
351 | printf ("%s failed at %s:%d: `%s'\n", | ||
352 | "curl_multi_perform", | ||
353 | __FILE__, | ||
354 | __LINE__, curl_easy_strerror (msg->data.result)); | ||
355 | curl_multi_remove_handle (multi, c); | ||
356 | curl_multi_cleanup (multi); | ||
357 | curl_easy_cleanup (c); | ||
358 | c = NULL; | ||
359 | multi = NULL; | ||
360 | } | ||
361 | } | ||
362 | MHD_run (d); | ||
363 | } | ||
364 | if (multi != NULL) | ||
365 | { | ||
366 | curl_multi_remove_handle (multi, c); | ||
367 | curl_easy_cleanup (c); | ||
368 | curl_multi_cleanup (multi); | ||
369 | } | ||
370 | MHD_stop_daemon (d); | ||
371 | if (cbc.pos != strlen ("/hello_world")) | ||
372 | { | ||
373 | fprintf (stderr, "Got invalid response `%.*s'\n", cbc.pos, cbc.buf); | ||
374 | return 8192; | ||
375 | } | ||
376 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
377 | return 16384; | ||
378 | return 0; | ||
379 | } | ||
380 | |||
381 | |||
382 | |||
383 | int | ||
384 | main (int argc, char *const *argv) | ||
385 | { | ||
386 | unsigned int errorCount = 0; | ||
387 | |||
388 | oneone = NULL != strstr (argv[0], "11"); | ||
389 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | ||
390 | return 2; | ||
391 | put_buffer = malloc (PUT_SIZE); | ||
392 | memset (put_buffer, 1, PUT_SIZE); | ||
393 | if (0) | ||
394 | { | ||
395 | errorCount += testInternalPut (); | ||
396 | errorCount += testMultithreadedPut (); | ||
397 | } | ||
398 | errorCount += testExternalPut (); | ||
399 | free (put_buffer); | ||
400 | if (errorCount != 0) | ||
401 | fprintf (stderr, "Error (code: %u)\n", errorCount); | ||
402 | curl_global_cleanup (); | ||
403 | return errorCount != 0; /* 0 == pass */ | ||
404 | } | ||