diff options
Diffstat (limited to 'src/daemon/daemontest_put_chunked.c')
-rw-r--r-- | src/daemon/daemontest_put_chunked.c | 382 |
1 files changed, 382 insertions, 0 deletions
diff --git a/src/daemon/daemontest_put_chunked.c b/src/daemon/daemontest_put_chunked.c new file mode 100644 index 00000000..2abe4bd2 --- /dev/null +++ b/src/daemon/daemontest_put_chunked.c | |||
@@ -0,0 +1,382 @@ | |||
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 | |||
39 | struct CBC | ||
40 | { | ||
41 | char *buf; | ||
42 | size_t pos; | ||
43 | size_t size; | ||
44 | }; | ||
45 | |||
46 | static size_t | ||
47 | putBuffer (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 | |||
62 | static size_t | ||
63 | copyBuffer (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 | |||
74 | static int | ||
75 | ahc_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, | ||
99 | &"Hello123"[*done], | ||
100 | have)) | ||
101 | { | ||
102 | *done += have; | ||
103 | *upload_data_size = 0; | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | printf ("Invalid upload data `%8s'!\n", upload_data); | ||
108 | return MHD_NO; | ||
109 | } | ||
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 | |||
120 | static int | ||
121 | testInternalPut () | ||
122 | { | ||
123 | struct MHD_Daemon *d; | ||
124 | CURL *c; | ||
125 | char buf[2048]; | ||
126 | struct CBC cbc; | ||
127 | unsigned int pos = 0; | ||
128 | int done_flag = 0; | ||
129 | CURLcode errornum; | ||
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 | 11080, | ||
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:11080/hello_world"); | ||
141 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
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 | /* | ||
147 | // by not giving the file size, we force chunking! | ||
148 | curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); | ||
149 | */ | ||
150 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
151 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 15L); | ||
152 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
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 | |||
176 | static int | ||
177 | testMultithreadedPut () | ||
178 | { | ||
179 | struct MHD_Daemon *d; | ||
180 | CURL *c; | ||
181 | char buf[2048]; | ||
182 | struct CBC cbc; | ||
183 | unsigned int pos = 0; | ||
184 | int done_flag = 0; | ||
185 | CURLcode errornum; | ||
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 | 11081, | ||
192 | NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); | ||
193 | if (d == NULL) | ||
194 | return 16; | ||
195 | c = curl_easy_init (); | ||
196 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11081/hello_world"); | ||
197 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
198 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
199 | curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); | ||
200 | curl_easy_setopt (c, CURLOPT_READDATA, &pos); | ||
201 | curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); | ||
202 | /* | ||
203 | // by not giving the file size, we force chunking! | ||
204 | curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); | ||
205 | */ | ||
206 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
207 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 15L); | ||
208 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
209 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); | ||
210 | // NOTE: use of CONNECTTIMEOUT without also | ||
211 | // setting NOSIGNAL results in really weird | ||
212 | // crashes on my system! | ||
213 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
214 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
215 | { | ||
216 | fprintf (stderr, | ||
217 | "curl_easy_perform failed: `%s'\n", | ||
218 | curl_easy_strerror (errornum)); | ||
219 | curl_easy_cleanup (c); | ||
220 | MHD_stop_daemon (d); | ||
221 | return 32; | ||
222 | } | ||
223 | curl_easy_cleanup (c); | ||
224 | MHD_stop_daemon (d); | ||
225 | if (cbc.pos != strlen ("/hello_world")) | ||
226 | return 64; | ||
227 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
228 | return 128; | ||
229 | |||
230 | return 0; | ||
231 | } | ||
232 | |||
233 | |||
234 | static int | ||
235 | testExternalPut () | ||
236 | { | ||
237 | struct MHD_Daemon *d; | ||
238 | CURL *c; | ||
239 | char buf[2048]; | ||
240 | struct CBC cbc; | ||
241 | CURLM *multi; | ||
242 | CURLMcode mret; | ||
243 | fd_set rs; | ||
244 | fd_set ws; | ||
245 | fd_set es; | ||
246 | int max; | ||
247 | int running; | ||
248 | struct CURLMsg *msg; | ||
249 | time_t start; | ||
250 | struct timeval tv; | ||
251 | unsigned int pos = 0; | ||
252 | int done_flag = 0; | ||
253 | |||
254 | multi = NULL; | ||
255 | cbc.buf = buf; | ||
256 | cbc.size = 2048; | ||
257 | cbc.pos = 0; | ||
258 | d = MHD_start_daemon (MHD_USE_DEBUG, | ||
259 | 11082, | ||
260 | NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END); | ||
261 | if (d == NULL) | ||
262 | return 256; | ||
263 | c = curl_easy_init (); | ||
264 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11082/hello_world"); | ||
265 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
266 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
267 | curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); | ||
268 | curl_easy_setopt (c, CURLOPT_READDATA, &pos); | ||
269 | curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); | ||
270 | /* | ||
271 | // by not giving the file size, we force chunking! | ||
272 | curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L); | ||
273 | */ | ||
274 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
275 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 15L); | ||
276 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
277 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L); | ||
278 | // NOTE: use of CONNECTTIMEOUT without also | ||
279 | // setting NOSIGNAL results in really weird | ||
280 | // crashes on my system! | ||
281 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
282 | |||
283 | |||
284 | multi = curl_multi_init (); | ||
285 | if (multi == NULL) | ||
286 | { | ||
287 | curl_easy_cleanup (c); | ||
288 | MHD_stop_daemon (d); | ||
289 | return 512; | ||
290 | } | ||
291 | mret = curl_multi_add_handle (multi, c); | ||
292 | if (mret != CURLM_OK) | ||
293 | { | ||
294 | curl_multi_cleanup (multi); | ||
295 | curl_easy_cleanup (c); | ||
296 | MHD_stop_daemon (d); | ||
297 | return 1024; | ||
298 | } | ||
299 | start = time (NULL); | ||
300 | while ((time (NULL) - start < 5) && (multi != NULL)) | ||
301 | { | ||
302 | max = 0; | ||
303 | FD_ZERO (&rs); | ||
304 | FD_ZERO (&ws); | ||
305 | FD_ZERO (&es); | ||
306 | curl_multi_perform (multi, &running); | ||
307 | mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); | ||
308 | if (mret != CURLM_OK) | ||
309 | { | ||
310 | curl_multi_remove_handle (multi, c); | ||
311 | curl_multi_cleanup (multi); | ||
312 | curl_easy_cleanup (c); | ||
313 | MHD_stop_daemon (d); | ||
314 | return 2048; | ||
315 | } | ||
316 | if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) | ||
317 | { | ||
318 | curl_multi_remove_handle (multi, c); | ||
319 | curl_multi_cleanup (multi); | ||
320 | curl_easy_cleanup (c); | ||
321 | MHD_stop_daemon (d); | ||
322 | return 4096; | ||
323 | } | ||
324 | tv.tv_sec = 0; | ||
325 | tv.tv_usec = 1000; | ||
326 | select (max + 1, &rs, &ws, &es, &tv); | ||
327 | curl_multi_perform (multi, &running); | ||
328 | if (running == 0) | ||
329 | { | ||
330 | msg = curl_multi_info_read (multi, &running); | ||
331 | if (msg == NULL) | ||
332 | break; | ||
333 | if (msg->msg == CURLMSG_DONE) | ||
334 | { | ||
335 | if (msg->data.result != CURLE_OK) | ||
336 | printf ("%s failed at %s:%d: `%s'\n", | ||
337 | "curl_multi_perform", | ||
338 | __FILE__, | ||
339 | __LINE__, curl_easy_strerror (msg->data.result)); | ||
340 | curl_multi_remove_handle (multi, c); | ||
341 | curl_multi_cleanup (multi); | ||
342 | curl_easy_cleanup (c); | ||
343 | c = NULL; | ||
344 | multi = NULL; | ||
345 | } | ||
346 | } | ||
347 | MHD_run (d); | ||
348 | } | ||
349 | if (multi != NULL) | ||
350 | { | ||
351 | curl_multi_remove_handle (multi, c); | ||
352 | curl_easy_cleanup (c); | ||
353 | curl_multi_cleanup (multi); | ||
354 | } | ||
355 | MHD_stop_daemon (d); | ||
356 | if (cbc.pos != strlen ("/hello_world")) | ||
357 | return 8192; | ||
358 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
359 | return 16384; | ||
360 | return 0; | ||
361 | } | ||
362 | |||
363 | |||
364 | |||
365 | int | ||
366 | main (int argc, char *const *argv) | ||
367 | { | ||
368 | unsigned int errorCount = 0; | ||
369 | |||
370 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | ||
371 | return 2; | ||
372 | errorCount += testInternalPut (); | ||
373 | if (0) | ||
374 | { | ||
375 | errorCount += testMultithreadedPut (); | ||
376 | errorCount += testExternalPut (); | ||
377 | } | ||
378 | if (errorCount != 0) | ||
379 | fprintf (stderr, "Error (code: %u)\n", errorCount); | ||
380 | curl_global_cleanup (); | ||
381 | return errorCount != 0; /* 0 == pass */ | ||
382 | } | ||