aboutsummaryrefslogtreecommitdiff
path: root/src/testzzuf/daemontest_put_chunked.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testzzuf/daemontest_put_chunked.c')
-rw-r--r--src/testzzuf/daemontest_put_chunked.c383
1 files changed, 383 insertions, 0 deletions
diff --git a/src/testzzuf/daemontest_put_chunked.c b/src/testzzuf/daemontest_put_chunked.c
new file mode 100644
index 00000000..0cc3eb8f
--- /dev/null
+++ b/src/testzzuf/daemontest_put_chunked.c
@@ -0,0 +1,383 @@
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_chunked.c
23 * @brief Testcase for libmicrohttpd PUT operations with chunked encoding
24 * for the upload data
25 * @author Christian Grothoff
26 */
27
28#include "config.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
39struct CBC
40{
41 char *buf;
42 size_t pos;
43 size_t size;
44};
45
46static size_t
47putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
48{
49 unsigned int *pos = ptr;
50 unsigned int wrt;
51
52 wrt = size * nmemb;
53 if (wrt > 8 - (*pos))
54 wrt = 8 - (*pos);
55 if (wrt > 4)
56 wrt = 4; /* only send half at first => force multiple chunks! */
57 memcpy (stream, &("Hello123"[*pos]), wrt);
58 (*pos) += wrt;
59 return wrt;
60}
61
62static size_t
63copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
64{
65 struct CBC *cbc = ctx;
66
67 if (cbc->pos + size * nmemb > cbc->size)
68 return 0; /* overflow */
69 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
70 cbc->pos += size * nmemb;
71 return size * nmemb;
72}
73
74static int
75ahc_echo (void *cls,
76 struct MHD_Connection *connection,
77 const char *url,
78 const char *method,
79 const char *version,
80 const char *upload_data, unsigned int *upload_data_size,
81 void **unused)
82{
83 int *done = cls;
84 struct MHD_Response *response;
85 int ret;
86 int have;
87
88 if (0 != strcmp ("PUT", method))
89 return MHD_NO; /* unexpected method */
90 if ((*done) < 8)
91 {
92 have = *upload_data_size;
93 if (have + *done > 8)
94 {
95 printf ("Invalid upload data `%8s'!\n", upload_data);
96 return MHD_NO;
97 }
98 if (0 == memcmp (upload_data, &"Hello123"[*done], have))
99 {
100 *done += have;
101 *upload_data_size = 0;
102 }
103 else
104 {
105 printf ("Invalid upload data `%8s'!\n", upload_data);
106 return MHD_NO;
107 }
108#if 0
109 fprintf (stderr, "Not ready for response: %u/%u\n", *done, 8);
110#endif
111 return MHD_YES;
112 }
113 response = MHD_create_response_from_data (strlen (url),
114 (void *) url, MHD_NO, MHD_YES);
115 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
116 MHD_destroy_response (response);
117 return ret;
118}
119
120
121static int
122testInternalPut ()
123{
124 struct MHD_Daemon *d;
125 CURL *c;
126 char buf[2048];
127 struct CBC cbc;
128 unsigned int pos = 0;
129 int done_flag = 0;
130 CURLcode errornum;
131
132 cbc.buf = buf;
133 cbc.size = 2048;
134 cbc.pos = 0;
135 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
136 11080,
137 NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
138 if (d == NULL)
139 return 1;
140 c = curl_easy_init ();
141 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11080/hello_world");
142 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
143 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
144 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
145 curl_easy_setopt (c, CURLOPT_READDATA, &pos);
146 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
147 /*
148 // by not giving the file size, we force chunking!
149 curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
150 */
151 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
152 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
153 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
154 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
155 // NOTE: use of CONNECTTIMEOUT without also
156 // setting NOSIGNAL results in really weird
157 // crashes on my system!
158 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
159 if (CURLE_OK != (errornum = curl_easy_perform (c)))
160 {
161 fprintf (stderr,
162 "curl_easy_perform failed: `%s'\n",
163 curl_easy_strerror (errornum));
164 curl_easy_cleanup (c);
165 MHD_stop_daemon (d);
166 return 2;
167 }
168 curl_easy_cleanup (c);
169 MHD_stop_daemon (d);
170 if (cbc.pos != strlen ("/hello_world"))
171 return 4;
172 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
173 return 8;
174 return 0;
175}
176
177static int
178testMultithreadedPut ()
179{
180 struct MHD_Daemon *d;
181 CURL *c;
182 char buf[2048];
183 struct CBC cbc;
184 unsigned int pos = 0;
185 int done_flag = 0;
186 CURLcode errornum;
187
188 cbc.buf = buf;
189 cbc.size = 2048;
190 cbc.pos = 0;
191 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
192 11081,
193 NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
194 if (d == NULL)
195 return 16;
196 c = curl_easy_init ();
197 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11081/hello_world");
198 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
199 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
200 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
201 curl_easy_setopt (c, CURLOPT_READDATA, &pos);
202 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
203 /*
204 // by not giving the file size, we force chunking!
205 curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
206 */
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 if (cbc.pos != strlen ("/hello_world"))
227 return 64;
228 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
229 return 128;
230
231 return 0;
232}
233
234
235static int
236testExternalPut ()
237{
238 struct MHD_Daemon *d;
239 CURL *c;
240 char buf[2048];
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
255 multi = NULL;
256 cbc.buf = buf;
257 cbc.size = 2048;
258 cbc.pos = 0;
259 d = MHD_start_daemon (MHD_USE_DEBUG,
260 11082,
261 NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
262 if (d == NULL)
263 return 256;
264 c = curl_easy_init ();
265 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11082/hello_world");
266 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
267 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
268 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
269 curl_easy_setopt (c, CURLOPT_READDATA, &pos);
270 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
271 /*
272 // by not giving the file size, we force chunking!
273 curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
274 */
275 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
276 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
277 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
278 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
279 // NOTE: use of CONNECTTIMEOUT without also
280 // setting NOSIGNAL results in really weird
281 // crashes on my system!
282 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
283
284
285 multi = curl_multi_init ();
286 if (multi == NULL)
287 {
288 curl_easy_cleanup (c);
289 MHD_stop_daemon (d);
290 return 512;
291 }
292 mret = curl_multi_add_handle (multi, c);
293 if (mret != CURLM_OK)
294 {
295 curl_multi_cleanup (multi);
296 curl_easy_cleanup (c);
297 MHD_stop_daemon (d);
298 return 1024;
299 }
300 start = time (NULL);
301 while ((time (NULL) - start < 5) && (multi != NULL))
302 {
303 max = 0;
304 FD_ZERO (&rs);
305 FD_ZERO (&ws);
306 FD_ZERO (&es);
307 curl_multi_perform (multi, &running);
308 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
309 if (mret != CURLM_OK)
310 {
311 curl_multi_remove_handle (multi, c);
312 curl_multi_cleanup (multi);
313 curl_easy_cleanup (c);
314 MHD_stop_daemon (d);
315 return 2048;
316 }
317 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
318 {
319 curl_multi_remove_handle (multi, c);
320 curl_multi_cleanup (multi);
321 curl_easy_cleanup (c);
322 MHD_stop_daemon (d);
323 return 4096;
324 }
325 tv.tv_sec = 0;
326 tv.tv_usec = 1000;
327 select (max + 1, &rs, &ws, &es, &tv);
328 curl_multi_perform (multi, &running);
329 if (running == 0)
330 {
331 msg = curl_multi_info_read (multi, &running);
332 if (msg == NULL)
333 break;
334 if (msg->msg == CURLMSG_DONE)
335 {
336 if (msg->data.result != CURLE_OK)
337 printf ("%s failed at %s:%d: `%s'\n",
338 "curl_multi_perform",
339 __FILE__,
340 __LINE__, curl_easy_strerror (msg->data.result));
341 curl_multi_remove_handle (multi, c);
342 curl_multi_cleanup (multi);
343 curl_easy_cleanup (c);
344 c = NULL;
345 multi = NULL;
346 }
347 }
348 MHD_run (d);
349 }
350 if (multi != NULL)
351 {
352 curl_multi_remove_handle (multi, c);
353 curl_easy_cleanup (c);
354 curl_multi_cleanup (multi);
355 }
356 MHD_stop_daemon (d);
357 if (cbc.pos != strlen ("/hello_world"))
358 return 8192;
359 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
360 return 16384;
361 return 0;
362}
363
364
365
366int
367main (int argc, char *const *argv)
368{
369 unsigned int errorCount = 0;
370
371 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
372 return 2;
373 errorCount += testInternalPut ();
374 if (0)
375 {
376 errorCount += testMultithreadedPut ();
377 errorCount += testExternalPut ();
378 }
379 if (errorCount != 0)
380 fprintf (stderr, "Error (code: %u)\n", errorCount);
381 curl_global_cleanup ();
382 return errorCount != 0; /* 0 == pass */
383}