aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/daemontest_get_sendfile.c.in
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/daemontest_get_sendfile.c.in')
-rw-r--r--src/testcurl/daemontest_get_sendfile.c.in470
1 files changed, 470 insertions, 0 deletions
diff --git a/src/testcurl/daemontest_get_sendfile.c.in b/src/testcurl/daemontest_get_sendfile.c.in
new file mode 100644
index 00000000..2c282d13
--- /dev/null
+++ b/src/testcurl/daemontest_get_sendfile.c.in
@@ -0,0 +1,470 @@
1/* DO NOT CHANGE THIS LINE */
2/*
3 This file is part of libmicrohttpd
4 (C) 2007, 2009 Christian Grothoff
5
6 libmicrohttpd is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2, or (at your
9 option) any later version.
10
11 libmicrohttpd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libmicrohttpd; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20*/
21
22/**
23 * @file daemontest_get_sendfile.c
24 * @brief Testcase for libmicrohttpd response from FD
25 * @author Christian Grothoff
26 */
27
28#include "MHD_config.h"
29#include "platform.h"
30#include <curl/curl.h>
31#include <microhttpd.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35#include <sys/types.h>
36#include <fcntl.h>
37
38#ifndef WINDOWS
39#include <sys/socket.h>
40#include <unistd.h>
41#endif
42
43#define TESTSTR "/* DO NOT CHANGE THIS LINE */"
44
45char *sourcefile = "@SENDFILE_SOURCE_DIR@/src/testcurl/daemontest_get_sendfile.c";
46
47static int oneone;
48
49struct CBC
50{
51 char *buf;
52 size_t pos;
53 size_t size;
54};
55
56static size_t
57copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
58{
59 struct CBC *cbc = ctx;
60
61 if (cbc->pos + size * nmemb > cbc->size)
62 return 0; /* overflow */
63 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
64 cbc->pos += size * nmemb;
65 return size * nmemb;
66}
67
68
69static int
70ahc_echo (void *cls,
71 struct MHD_Connection *connection,
72 const char *url,
73 const char *method,
74 const char *version,
75 const char *upload_data, size_t *upload_data_size,
76 void **unused)
77{
78 static int ptr;
79 const char *me = cls;
80 struct MHD_Response *response;
81 int ret;
82 int fd;
83
84 if (0 != strcmp (me, method))
85 return MHD_NO; /* unexpected method */
86 if (&ptr != *unused)
87 {
88 *unused = &ptr;
89 return MHD_YES;
90 }
91 *unused = NULL;
92 fd = open (sourcefile, O_RDONLY);
93 if (fd == -1)
94 {
95 fprintf (stderr, "Failed to open `%s': %s\n",
96 sourcefile,
97 STRERROR (errno));
98 exit (1);
99 }
100 response = MHD_create_response_from_fd (strlen (TESTSTR), fd);
101 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
102 MHD_destroy_response (response);
103 if (ret == MHD_NO)
104 abort ();
105 return ret;
106}
107
108
109static int
110testInternalGet ()
111{
112 struct MHD_Daemon *d;
113 CURL *c;
114 char buf[2048];
115 struct CBC cbc;
116 CURLcode errornum;
117
118 cbc.buf = buf;
119 cbc.size = 2048;
120 cbc.pos = 0;
121 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
122 11080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
123 if (d == NULL)
124 return 1;
125 c = curl_easy_init ();
126 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:11080/");
127 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
128 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
129 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
130 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
131 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
132 if (oneone)
133 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
134 else
135 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
136 /* NOTE: use of CONNECTTIMEOUT without also
137 setting NOSIGNAL results in really weird
138 crashes on my system!*/
139 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
140 if (CURLE_OK != (errornum = curl_easy_perform (c)))
141 {
142 fprintf (stderr,
143 "curl_easy_perform failed: `%s'\n",
144 curl_easy_strerror (errornum));
145 curl_easy_cleanup (c);
146 MHD_stop_daemon (d);
147 return 2;
148 }
149 curl_easy_cleanup (c);
150 MHD_stop_daemon (d);
151 if (cbc.pos != strlen (TESTSTR))
152 return 4;
153 if (0 != strncmp (TESTSTR, cbc.buf, strlen (TESTSTR)))
154 return 8;
155 return 0;
156}
157
158static int
159testMultithreadedGet ()
160{
161 struct MHD_Daemon *d;
162 CURL *c;
163 char buf[2048];
164 struct CBC cbc;
165 CURLcode errornum;
166
167 cbc.buf = buf;
168 cbc.size = 2048;
169 cbc.pos = 0;
170 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
171 1081, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
172 if (d == NULL)
173 return 16;
174 c = curl_easy_init ();
175 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:1081/");
176 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
177 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
178 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
179 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
180 if (oneone)
181 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
182 else
183 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
184 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
185 /* NOTE: use of CONNECTTIMEOUT without also
186 setting NOSIGNAL results in really weird
187 crashes on my system! */
188 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
189 if (CURLE_OK != (errornum = curl_easy_perform (c)))
190 {
191 fprintf (stderr,
192 "curl_easy_perform failed: `%s'\n",
193 curl_easy_strerror (errornum));
194 curl_easy_cleanup (c);
195 MHD_stop_daemon (d);
196 return 32;
197 }
198 curl_easy_cleanup (c);
199 MHD_stop_daemon (d);
200 if (cbc.pos != strlen (TESTSTR))
201 return 64;
202 if (0 != strncmp (TESTSTR, cbc.buf, strlen (TESTSTR)))
203 return 128;
204 return 0;
205}
206
207static int
208testMultithreadedPoolGet ()
209{
210 struct MHD_Daemon *d;
211 CURL *c;
212 char buf[2048];
213 struct CBC cbc;
214 CURLcode errornum;
215
216 cbc.buf = buf;
217 cbc.size = 2048;
218 cbc.pos = 0;
219 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
220 1081, NULL, NULL, &ahc_echo, "GET",
221 MHD_OPTION_THREAD_POOL_SIZE, 4, MHD_OPTION_END);
222 if (d == NULL)
223 return 16;
224 c = curl_easy_init ();
225 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:1081/");
226 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
227 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
228 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
229 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
230 if (oneone)
231 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
232 else
233 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
234 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
235 /* NOTE: use of CONNECTTIMEOUT without also
236 setting NOSIGNAL results in really weird
237 crashes on my system!*/
238 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
239 if (CURLE_OK != (errornum = curl_easy_perform (c)))
240 {
241 fprintf (stderr,
242 "curl_easy_perform failed: `%s'\n",
243 curl_easy_strerror (errornum));
244 curl_easy_cleanup (c);
245 MHD_stop_daemon (d);
246 return 32;
247 }
248 curl_easy_cleanup (c);
249 MHD_stop_daemon (d);
250 if (cbc.pos != strlen (TESTSTR))
251 return 64;
252 if (0 != strncmp (TESTSTR, cbc.buf, strlen (TESTSTR)))
253 return 128;
254 return 0;
255}
256
257static int
258testExternalGet ()
259{
260 struct MHD_Daemon *d;
261 CURL *c;
262 char buf[2048];
263 struct CBC cbc;
264 CURLM *multi;
265 CURLMcode mret;
266 fd_set rs;
267 fd_set ws;
268 fd_set es;
269 int max;
270 int running;
271 struct CURLMsg *msg;
272 time_t start;
273 struct timeval tv;
274
275 multi = NULL;
276 cbc.buf = buf;
277 cbc.size = 2048;
278 cbc.pos = 0;
279 d = MHD_start_daemon (MHD_USE_DEBUG,
280 1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
281 if (d == NULL)
282 return 256;
283 c = curl_easy_init ();
284 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:1082/");
285 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
286 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
287 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
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_TIMEOUT, 150L);
293 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
294 /* NOTE: use of CONNECTTIMEOUT without also
295 setting NOSIGNAL results in really weird
296 crashes on my system! */
297 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
298
299
300 multi = curl_multi_init ();
301 if (multi == NULL)
302 {
303 curl_easy_cleanup (c);
304 MHD_stop_daemon (d);
305 return 512;
306 }
307 mret = curl_multi_add_handle (multi, c);
308 if (mret != CURLM_OK)
309 {
310 curl_multi_cleanup (multi);
311 curl_easy_cleanup (c);
312 MHD_stop_daemon (d);
313 return 1024;
314 }
315 start = time (NULL);
316 while ((time (NULL) - start < 5) && (multi != NULL))
317 {
318 max = 0;
319 FD_ZERO (&rs);
320 FD_ZERO (&ws);
321 FD_ZERO (&es);
322 curl_multi_perform (multi, &running);
323 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
324 if (mret != CURLM_OK)
325 {
326 curl_multi_remove_handle (multi, c);
327 curl_multi_cleanup (multi);
328 curl_easy_cleanup (c);
329 MHD_stop_daemon (d);
330 return 2048;
331 }
332 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
333 {
334 curl_multi_remove_handle (multi, c);
335 curl_multi_cleanup (multi);
336 curl_easy_cleanup (c);
337 MHD_stop_daemon (d);
338 return 4096;
339 }
340 tv.tv_sec = 0;
341 tv.tv_usec = 1000;
342 select (max + 1, &rs, &ws, &es, &tv);
343 curl_multi_perform (multi, &running);
344 if (running == 0)
345 {
346 msg = curl_multi_info_read (multi, &running);
347 if (msg == NULL)
348 break;
349 if (msg->msg == CURLMSG_DONE)
350 {
351 if (msg->data.result != CURLE_OK)
352 printf ("%s failed at %s:%d: `%s'\n",
353 "curl_multi_perform",
354 __FILE__,
355 __LINE__, curl_easy_strerror (msg->data.result));
356 curl_multi_remove_handle (multi, c);
357 curl_multi_cleanup (multi);
358 curl_easy_cleanup (c);
359 c = NULL;
360 multi = NULL;
361 }
362 }
363 MHD_run (d);
364 }
365 if (multi != NULL)
366 {
367 curl_multi_remove_handle (multi, c);
368 curl_easy_cleanup (c);
369 curl_multi_cleanup (multi);
370 }
371 MHD_stop_daemon (d);
372 if (cbc.pos != strlen (TESTSTR))
373 return 8192;
374 if (0 != strncmp (TESTSTR, cbc.buf, strlen (TESTSTR)))
375 return 16384;
376 return 0;
377}
378
379static int
380testUnknownPortGet ()
381{
382 struct MHD_Daemon *d;
383 const union MHD_DaemonInfo *di;
384 CURL *c;
385 char buf[2048];
386 struct CBC cbc;
387 CURLcode errornum;
388
389 struct sockaddr_in addr;
390 socklen_t addr_len = sizeof(addr);
391 memset(&addr, 0, sizeof(addr));
392 addr.sin_family = AF_INET;
393 addr.sin_port = 0;
394 addr.sin_addr.s_addr = INADDR_ANY;
395
396 cbc.buf = buf;
397 cbc.size = 2048;
398 cbc.pos = 0;
399 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
400 1, NULL, NULL, &ahc_echo, "GET",
401 MHD_OPTION_SOCK_ADDR, &addr,
402 MHD_OPTION_END);
403 if (d == NULL)
404 return 32768;
405
406 di = MHD_get_daemon_info (d, MHD_DAEMON_INFO_LISTEN_FD);
407 if (di == NULL)
408 return 65536;
409
410 if (0 != getsockname(di->listen_fd, &addr, &addr_len))
411 return 131072;
412
413 if (addr.sin_family != AF_INET)
414 return 26214;
415
416 snprintf(buf, sizeof(buf), "http://127.0.0.1:%hu/",
417 ntohs(addr.sin_port));
418
419 c = curl_easy_init ();
420 curl_easy_setopt (c, CURLOPT_URL, buf);
421 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
422 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
423 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
424 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
425 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
426 if (oneone)
427 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
428 else
429 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
430 /* NOTE: use of CONNECTTIMEOUT without also
431 setting NOSIGNAL results in really weird
432 crashes on my system! */
433 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
434 if (CURLE_OK != (errornum = curl_easy_perform (c)))
435 {
436 fprintf (stderr,
437 "curl_easy_perform failed: `%s'\n",
438 curl_easy_strerror (errornum));
439 curl_easy_cleanup (c);
440 MHD_stop_daemon (d);
441 return 524288;
442 }
443 curl_easy_cleanup (c);
444 MHD_stop_daemon (d);
445 if (cbc.pos != strlen (TESTSTR))
446 return 1048576;
447 if (0 != strncmp (TESTSTR, cbc.buf, strlen (TESTSTR)))
448 return 2097152;
449 return 0;
450}
451
452
453int
454main (int argc, char *const *argv)
455{
456 unsigned int errorCount = 0;
457
458 oneone = NULL != strstr (argv[0], "11");
459 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
460 return 2;
461 errorCount += testInternalGet ();
462 errorCount += testMultithreadedGet ();
463 errorCount += testMultithreadedPoolGet ();
464 errorCount += testExternalGet ();
465 errorCount += testUnknownPortGet ();
466 if (errorCount != 0)
467 fprintf (stderr, "Error (code: %u)\n", errorCount);
468 curl_global_cleanup ();
469 return errorCount != 0; /* 0 == pass */
470}