aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/daemontest_get_chunked.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/daemontest_get_chunked.c')
-rw-r--r--src/daemon/daemontest_get_chunked.c364
1 files changed, 364 insertions, 0 deletions
diff --git a/src/daemon/daemontest_get_chunked.c b/src/daemon/daemontest_get_chunked.c
new file mode 100644
index 00000000..4b2739cf
--- /dev/null
+++ b/src/daemon/daemontest_get_chunked.c
@@ -0,0 +1,364 @@
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_get_chunked.c
23 * @brief Testcase for libmicrohttpd GET operations with chunked content encoding
24 * TODO:
25 * - how to test that chunking was actually used?
26 * - use CURLOPT_HEADERFUNCTION to validate
27 * footer was sent
28 * @author Christian Grothoff
29 */
30
31#include "config.h"
32#include <curl/curl.h>
33#include <microhttpd.h>
34#include <stdlib.h>
35#include <string.h>
36#include <time.h>
37
38#ifndef WINDOWS
39#include <unistd.h>
40#endif
41
42struct CBC
43{
44 char *buf;
45 size_t pos;
46 size_t size;
47};
48
49static size_t
50copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
51{
52 struct CBC *cbc = ctx;
53
54 if (cbc->pos + size * nmemb > cbc->size)
55 return 0; /* overflow */
56 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
57 cbc->pos += size * nmemb;
58 return size * nmemb;
59}
60
61/**
62 * MHD content reader callback that returns
63 * data in chunks.
64 */
65static int
66crc (void *cls, size_t pos, char *buf, int max)
67{
68 struct MHD_Response **responseptr = cls;
69
70 if (pos == 128 * 10)
71 {
72 MHD_add_response_header (*responseptr, "Footer", "working");
73 return -1; /* end of stream */
74 }
75 if (max < 128)
76 abort (); /* should not happen in this testcase... */
77 memset (buf, 'A' + (pos / 128), 128);
78 return 128;
79}
80
81/**
82 * Dummy function that does nothing.
83 */
84static void
85crcf (void *ptr)
86{
87 free (ptr);
88}
89
90static int
91ahc_echo (void *cls,
92 struct MHD_Connection *connection,
93 const char *url,
94 const char *method,
95 const char *version,
96 const char *upload_data, unsigned int *upload_data_size, void **ptr)
97{
98 static int aptr;
99 const char *me = cls;
100 struct MHD_Response *response;
101 struct MHD_Response **responseptr;
102 int ret;
103
104 if (0 != strcmp (me, method))
105 return MHD_NO; /* unexpected method */
106 if (&aptr != *ptr)
107 {
108 /* do never respond on first call */
109 *ptr = &aptr;
110 return MHD_YES;
111 }
112 responseptr = malloc (sizeof (struct MHD_Response *));
113 response = MHD_create_response_from_callback (-1,
114 1024,
115 &crc, responseptr, &crcf);
116 *responseptr = response;
117 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
118 MHD_destroy_response (response);
119 return ret;
120}
121
122static int
123validate (struct CBC cbc, int ebase)
124{
125 int i;
126 char buf[128];
127
128 if (cbc.pos != 128 * 10)
129 return ebase;
130
131 for (i = 0; i < 10; i++)
132 {
133 memset (buf, 'A' + i, 128);
134 if (0 != memcmp (buf, &cbc.buf[i * 128], 128))
135 {
136 fprintf (stderr,
137 "Got `%.*s'\nWant `%.*s'\n",
138 128, buf, 128, &cbc.buf[i * 128]);
139 return ebase * 2;
140 }
141 }
142 return 0;
143}
144
145static int
146testInternalGet ()
147{
148 struct MHD_Daemon *d;
149 CURL *c;
150 char buf[2048];
151 struct CBC cbc;
152 CURLcode errornum;
153
154 cbc.buf = buf;
155 cbc.size = 2048;
156 cbc.pos = 0;
157 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
158 1080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
159 if (d == NULL)
160 return 1;
161 c = curl_easy_init ();
162 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
163 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
164 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
165 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
166 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
167 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
168 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
169 // NOTE: use of CONNECTTIMEOUT without also
170 // setting NOSIGNAL results in really weird
171 // crashes on my system!
172 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
173 if (CURLE_OK != (errornum = curl_easy_perform (c)))
174 {
175 fprintf (stderr,
176 "curl_easy_perform failed: `%s'\n",
177 curl_easy_strerror (errornum));
178 curl_easy_cleanup (c);
179 MHD_stop_daemon (d);
180 return 2;
181 }
182 curl_easy_cleanup (c);
183 MHD_stop_daemon (d);
184 return validate (cbc, 4);
185}
186
187static int
188testMultithreadedGet ()
189{
190 struct MHD_Daemon *d;
191 CURL *c;
192 char buf[2048];
193 struct CBC cbc;
194 CURLcode errornum;
195
196 cbc.buf = buf;
197 cbc.size = 2048;
198 cbc.pos = 0;
199 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
200 1081, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
201 if (d == NULL)
202 return 16;
203 c = curl_easy_init ();
204 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
205 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
206 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
207 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
208 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
209 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
210 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
211 // NOTE: use of CONNECTTIMEOUT without also
212 // setting NOSIGNAL results in really weird
213 // crashes on my system!
214 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
215 if (CURLE_OK != (errornum = curl_easy_perform (c)))
216 {
217 fprintf (stderr,
218 "curl_easy_perform failed: `%s'\n",
219 curl_easy_strerror (errornum));
220 curl_easy_cleanup (c);
221 MHD_stop_daemon (d);
222 return 32;
223 }
224 curl_easy_cleanup (c);
225 MHD_stop_daemon (d);
226 return validate (cbc, 64);
227}
228
229
230static int
231testExternalGet ()
232{
233 struct MHD_Daemon *d;
234 CURL *c;
235 char buf[2048];
236 struct CBC cbc;
237 CURLM *multi;
238 CURLMcode mret;
239 fd_set rs;
240 fd_set ws;
241 fd_set es;
242 int max;
243 int running;
244 struct CURLMsg *msg;
245 time_t start;
246 struct timeval tv;
247
248 multi = NULL;
249 cbc.buf = buf;
250 cbc.size = 2048;
251 cbc.pos = 0;
252 d = MHD_start_daemon (MHD_USE_DEBUG,
253 1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
254 if (d == NULL)
255 return 256;
256 c = curl_easy_init ();
257 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
258 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
259 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
260 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
261 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
262 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
263 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L);
264 // NOTE: use of CONNECTTIMEOUT without also
265 // setting NOSIGNAL results in really weird
266 // crashes on my system!
267 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
268
269
270 multi = curl_multi_init ();
271 if (multi == NULL)
272 {
273 curl_easy_cleanup (c);
274 MHD_stop_daemon (d);
275 return 512;
276 }
277 mret = curl_multi_add_handle (multi, c);
278 if (mret != CURLM_OK)
279 {
280 curl_multi_cleanup (multi);
281 curl_easy_cleanup (c);
282 MHD_stop_daemon (d);
283 return 1024;
284 }
285 start = time (NULL);
286 while ((time (NULL) - start < 5) && (multi != NULL))
287 {
288 max = 0;
289 FD_ZERO (&rs);
290 FD_ZERO (&ws);
291 FD_ZERO (&es);
292 curl_multi_perform (multi, &running);
293 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
294 if (mret != CURLM_OK)
295 {
296 curl_multi_remove_handle (multi, c);
297 curl_multi_cleanup (multi);
298 curl_easy_cleanup (c);
299 MHD_stop_daemon (d);
300 return 2048;
301 }
302 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
303 {
304 curl_multi_remove_handle (multi, c);
305 curl_multi_cleanup (multi);
306 curl_easy_cleanup (c);
307 MHD_stop_daemon (d);
308 return 4096;
309 }
310 tv.tv_sec = 0;
311 tv.tv_usec = 1000;
312 select (max + 1, &rs, &ws, &es, &tv);
313 curl_multi_perform (multi, &running);
314 if (running == 0)
315 {
316 msg = curl_multi_info_read (multi, &running);
317 if (msg == NULL)
318 break;
319 if (msg->msg == CURLMSG_DONE)
320 {
321 if (msg->data.result != CURLE_OK)
322 printf ("%s failed at %s:%d: `%s'\n",
323 "curl_multi_perform",
324 __FILE__,
325 __LINE__, curl_easy_strerror (msg->data.result));
326 curl_multi_remove_handle (multi, c);
327 curl_multi_cleanup (multi);
328 curl_easy_cleanup (c);
329 c = NULL;
330 multi = NULL;
331 }
332 }
333 MHD_run (d);
334 }
335 if (multi != NULL)
336 {
337 curl_multi_remove_handle (multi, c);
338 curl_easy_cleanup (c);
339 curl_multi_cleanup (multi);
340 }
341 MHD_stop_daemon (d);
342 return validate (cbc, 8192);
343}
344
345
346
347int
348main (int argc, char *const *argv)
349{
350 unsigned int errorCount = 0;
351
352 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
353 return 2;
354 errorCount += testInternalGet ();
355 if (0)
356 {
357 errorCount += testMultithreadedGet ();
358 errorCount += testExternalGet ();
359 }
360 if (errorCount != 0)
361 fprintf (stderr, "Error (code: %u)\n", errorCount);
362 curl_global_cleanup ();
363 return errorCount != 0; /* 0 == pass */
364}