diff options
Diffstat (limited to 'src/daemon')
-rw-r--r-- | src/daemon/Makefile.am | 14 | ||||
-rw-r--r-- | src/daemon/daemontest_postform.c | 400 |
2 files changed, 414 insertions, 0 deletions
diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index 3fa68c75..7a4696fc 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am | |||
@@ -38,10 +38,12 @@ check_PROGRAMS = \ | |||
38 | daemontest \ | 38 | daemontest \ |
39 | daemontest_get \ | 39 | daemontest_get \ |
40 | daemontest_post \ | 40 | daemontest_post \ |
41 | daemontest_postform \ | ||
41 | daemontest_put \ | 42 | daemontest_put \ |
42 | daemontest_large_put \ | 43 | daemontest_large_put \ |
43 | daemontest_get11 \ | 44 | daemontest_get11 \ |
44 | daemontest_post11 \ | 45 | daemontest_post11 \ |
46 | daemontest_postform11 \ | ||
45 | daemontest_put11 \ | 47 | daemontest_put11 \ |
46 | daemontest_large_put11 \ | 48 | daemontest_large_put11 \ |
47 | daemontest_long_header | 49 | daemontest_long_header |
@@ -65,6 +67,12 @@ daemontest_post_LDADD = \ | |||
65 | $(top_builddir)/src/daemon/libmicrohttpd.la \ | 67 | $(top_builddir)/src/daemon/libmicrohttpd.la \ |
66 | @LIBCURL@ | 68 | @LIBCURL@ |
67 | 69 | ||
70 | daemontest_postform_SOURCES = \ | ||
71 | daemontest_postform.c | ||
72 | daemontest_postform_LDADD = \ | ||
73 | $(top_builddir)/src/daemon/libmicrohttpd.la \ | ||
74 | @LIBCURL@ | ||
75 | |||
68 | daemontest_put_SOURCES = \ | 76 | daemontest_put_SOURCES = \ |
69 | daemontest_put.c | 77 | daemontest_put.c |
70 | daemontest_put_LDADD = \ | 78 | daemontest_put_LDADD = \ |
@@ -83,6 +91,12 @@ daemontest_post11_LDADD = \ | |||
83 | $(top_builddir)/src/daemon/libmicrohttpd.la \ | 91 | $(top_builddir)/src/daemon/libmicrohttpd.la \ |
84 | @LIBCURL@ | 92 | @LIBCURL@ |
85 | 93 | ||
94 | daemontest_postform11_SOURCES = \ | ||
95 | daemontest_postform.c | ||
96 | daemontest_postform11_LDADD = \ | ||
97 | $(top_builddir)/src/daemon/libmicrohttpd.la \ | ||
98 | @LIBCURL@ | ||
99 | |||
86 | daemontest_put11_SOURCES = \ | 100 | daemontest_put11_SOURCES = \ |
87 | daemontest_put.c | 101 | daemontest_put.c |
88 | daemontest_put11_LDADD = \ | 102 | daemontest_put11_LDADD = \ |
diff --git a/src/daemon/daemontest_postform.c b/src/daemon/daemontest_postform.c new file mode 100644 index 00000000..a82d4705 --- /dev/null +++ b/src/daemon/daemontest_postform.c | |||
@@ -0,0 +1,400 @@ | |||
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_post.c | ||
23 | * @brief Testcase for libmicrohttpd POST 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 | #define POST_DATA "name=daniel&project=curl" | ||
39 | |||
40 | static int oneone; | ||
41 | |||
42 | struct CBC | ||
43 | { | ||
44 | char *buf; | ||
45 | size_t pos; | ||
46 | size_t size; | ||
47 | }; | ||
48 | |||
49 | static size_t | ||
50 | copyBuffer (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 | * Note that this post_iterator is not perfect | ||
63 | * in that it fails to support incremental processing. | ||
64 | * (to be fixed in the future) | ||
65 | */ | ||
66 | static int | ||
67 | post_iterator (void *cls, | ||
68 | enum MHD_ValueKind kind, | ||
69 | const char *key, | ||
70 | const char *filename, | ||
71 | const char *content_type, | ||
72 | const char *transfer_encoding, | ||
73 | const char *value, size_t off, size_t size) | ||
74 | { | ||
75 | int *eok = cls; | ||
76 | |||
77 | if ((0 == strcmp (key, "name")) && | ||
78 | (size == strlen ("daniel")) && (0 == strncmp (value, "daniel", size))) | ||
79 | (*eok) |= 1; | ||
80 | if ((0 == strcmp (key, "project")) && | ||
81 | (size == strlen ("curl")) && (0 == strncmp (value, "curl", size))) | ||
82 | (*eok) |= 2; | ||
83 | return MHD_YES; | ||
84 | } | ||
85 | |||
86 | static int | ||
87 | ahc_echo (void *cls, | ||
88 | struct MHD_Connection *connection, | ||
89 | const char *url, | ||
90 | const char *method, | ||
91 | const char *version, | ||
92 | const char *upload_data, unsigned int *upload_data_size, | ||
93 | void **unused) | ||
94 | { | ||
95 | static int eok; | ||
96 | struct MHD_Response *response; | ||
97 | struct MHD_PostProcessor *pp; | ||
98 | int ret; | ||
99 | |||
100 | if (0 != strcmp ("POST", method)) | ||
101 | { | ||
102 | printf ("METHOD: %s\n", method); | ||
103 | return MHD_NO; /* unexpected method */ | ||
104 | } | ||
105 | pp = *unused; | ||
106 | if (pp == NULL) | ||
107 | { | ||
108 | eok = 0; | ||
109 | pp = MHD_create_post_processor (connection, 1024, &post_iterator, &eok); | ||
110 | *unused = pp; | ||
111 | } | ||
112 | MHD_post_process (pp, upload_data, *upload_data_size); | ||
113 | if ((eok == 3) && (0 == *upload_data_size)) | ||
114 | { | ||
115 | response = MHD_create_response_from_data (strlen (url), | ||
116 | (void *) url, | ||
117 | MHD_NO, MHD_YES); | ||
118 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
119 | MHD_destroy_response (response); | ||
120 | MHD_destroy_post_processor (pp); | ||
121 | *unused = NULL; | ||
122 | return ret; | ||
123 | } | ||
124 | *upload_data_size = 0; | ||
125 | return MHD_YES; | ||
126 | } | ||
127 | |||
128 | static struct curl_httppost * | ||
129 | make_form() { | ||
130 | struct curl_httppost* post = NULL; | ||
131 | struct curl_httppost* last = NULL; | ||
132 | |||
133 | curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", | ||
134 | CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END); | ||
135 | curl_formadd(&post, &last, CURLFORM_COPYNAME, "project", | ||
136 | CURLFORM_COPYCONTENTS, "curl", CURLFORM_END); | ||
137 | return post; | ||
138 | } | ||
139 | |||
140 | |||
141 | static int | ||
142 | testInternalPost () | ||
143 | { | ||
144 | struct MHD_Daemon *d; | ||
145 | CURL *c; | ||
146 | char buf[2048]; | ||
147 | struct CBC cbc; | ||
148 | CURLcode errornum; | ||
149 | struct curl_httppost * pd; | ||
150 | |||
151 | cbc.buf = buf; | ||
152 | cbc.size = 2048; | ||
153 | cbc.pos = 0; | ||
154 | d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, | ||
155 | 1080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); | ||
156 | if (d == NULL) | ||
157 | return 1; | ||
158 | c = curl_easy_init (); | ||
159 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world"); | ||
160 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
161 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
162 | pd = make_form(); | ||
163 | curl_easy_setopt(c, CURLOPT_HTTPPOST, pd); | ||
164 | curl_easy_setopt (c, CURLOPT_VERBOSE, 1L); | ||
165 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
166 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L); | ||
167 | if (oneone) | ||
168 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
169 | else | ||
170 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
171 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L); | ||
172 | // NOTE: use of CONNECTTIMEOUT without also | ||
173 | // setting NOSIGNAL results in really weird | ||
174 | // crashes on my system! | ||
175 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
176 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
177 | { | ||
178 | fprintf (stderr, | ||
179 | "curl_easy_perform failed: `%s'\n", | ||
180 | curl_easy_strerror (errornum)); | ||
181 | curl_easy_cleanup (c); | ||
182 | curl_formfree(pd); | ||
183 | MHD_stop_daemon (d); | ||
184 | return 2; | ||
185 | } | ||
186 | curl_easy_cleanup (c); | ||
187 | curl_formfree(pd); | ||
188 | MHD_stop_daemon (d); | ||
189 | if (cbc.pos != strlen ("/hello_world")) | ||
190 | return 4; | ||
191 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
192 | return 8; | ||
193 | return 0; | ||
194 | } | ||
195 | |||
196 | static int | ||
197 | testMultithreadedPost () | ||
198 | { | ||
199 | struct MHD_Daemon *d; | ||
200 | CURL *c; | ||
201 | char buf[2048]; | ||
202 | struct CBC cbc; | ||
203 | CURLcode errornum; | ||
204 | struct curl_httppost * pd; | ||
205 | |||
206 | cbc.buf = buf; | ||
207 | cbc.size = 2048; | ||
208 | cbc.pos = 0; | ||
209 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | ||
210 | 1081, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); | ||
211 | if (d == NULL) | ||
212 | return 16; | ||
213 | c = curl_easy_init (); | ||
214 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world"); | ||
215 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
216 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
217 | pd = make_form(); | ||
218 | curl_easy_setopt(c, CURLOPT_HTTPPOST, pd); | ||
219 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
220 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L); | ||
221 | if (oneone) | ||
222 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
223 | else | ||
224 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
225 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 2L); | ||
226 | // NOTE: use of CONNECTTIMEOUT without also | ||
227 | // setting NOSIGNAL results in really weird | ||
228 | // crashes on my system! | ||
229 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
230 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
231 | { | ||
232 | fprintf (stderr, | ||
233 | "curl_easy_perform failed: `%s'\n", | ||
234 | curl_easy_strerror (errornum)); | ||
235 | curl_easy_cleanup (c); | ||
236 | curl_formfree(pd); | ||
237 | MHD_stop_daemon (d); | ||
238 | return 32; | ||
239 | } | ||
240 | curl_easy_cleanup (c); | ||
241 | curl_formfree(pd); | ||
242 | MHD_stop_daemon (d); | ||
243 | if (cbc.pos != strlen ("/hello_world")) | ||
244 | return 64; | ||
245 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
246 | return 128; | ||
247 | return 0; | ||
248 | } | ||
249 | |||
250 | |||
251 | static int | ||
252 | testExternalPost () | ||
253 | { | ||
254 | struct MHD_Daemon *d; | ||
255 | CURL *c; | ||
256 | char buf[2048]; | ||
257 | struct CBC cbc; | ||
258 | CURLM *multi; | ||
259 | CURLMcode mret; | ||
260 | fd_set rs; | ||
261 | fd_set ws; | ||
262 | fd_set es; | ||
263 | int max; | ||
264 | int running; | ||
265 | struct CURLMsg *msg; | ||
266 | time_t start; | ||
267 | struct timeval tv; | ||
268 | struct curl_httppost * pd; | ||
269 | |||
270 | multi = NULL; | ||
271 | cbc.buf = buf; | ||
272 | cbc.size = 2048; | ||
273 | cbc.pos = 0; | ||
274 | d = MHD_start_daemon (MHD_USE_DEBUG, | ||
275 | 1082, NULL, NULL, &ahc_echo, NULL, 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 | pd = make_form(); | ||
283 | curl_easy_setopt(c, CURLOPT_HTTPPOST, pd); | ||
284 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
285 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 5L); | ||
286 | if (oneone) | ||
287 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
288 | else | ||
289 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
290 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L); | ||
291 | // NOTE: use of CONNECTTIMEOUT without also | ||
292 | // setting NOSIGNAL results in really weird | ||
293 | // crashes on my system! | ||
294 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
295 | |||
296 | |||
297 | multi = curl_multi_init (); | ||
298 | if (multi == NULL) | ||
299 | { | ||
300 | curl_easy_cleanup (c); | ||
301 | curl_formfree(pd); | ||
302 | MHD_stop_daemon (d); | ||
303 | return 512; | ||
304 | } | ||
305 | mret = curl_multi_add_handle (multi, c); | ||
306 | if (mret != CURLM_OK) | ||
307 | { | ||
308 | curl_multi_cleanup (multi); | ||
309 | curl_formfree(pd); | ||
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 | curl_formfree(pd); | ||
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 | curl_formfree(pd); | ||
338 | MHD_stop_daemon (d); | ||
339 | return 4096; | ||
340 | } | ||
341 | tv.tv_sec = 0; | ||
342 | tv.tv_usec = 1000; | ||
343 | select (max + 1, &rs, &ws, &es, &tv); | ||
344 | curl_multi_perform (multi, &running); | ||
345 | if (running == 0) | ||
346 | { | ||
347 | msg = curl_multi_info_read (multi, &running); | ||
348 | if (msg == NULL) | ||
349 | break; | ||
350 | if (msg->msg == CURLMSG_DONE) | ||
351 | { | ||
352 | if (msg->data.result != CURLE_OK) | ||
353 | printf ("%s failed at %s:%d: `%s'\n", | ||
354 | "curl_multi_perform", | ||
355 | __FILE__, | ||
356 | __LINE__, curl_easy_strerror (msg->data.result)); | ||
357 | curl_multi_remove_handle (multi, c); | ||
358 | curl_multi_cleanup (multi); | ||
359 | curl_easy_cleanup (c); | ||
360 | c = NULL; | ||
361 | multi = NULL; | ||
362 | } | ||
363 | } | ||
364 | MHD_run (d); | ||
365 | } | ||
366 | if (multi != NULL) | ||
367 | { | ||
368 | curl_multi_remove_handle (multi, c); | ||
369 | curl_easy_cleanup (c); | ||
370 | curl_multi_cleanup (multi); | ||
371 | } | ||
372 | curl_formfree(pd); | ||
373 | MHD_stop_daemon (d); | ||
374 | if (cbc.pos != strlen ("/hello_world")) | ||
375 | return 8192; | ||
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 | errorCount += testInternalPost (); | ||
392 | if (0) { | ||
393 | errorCount += testMultithreadedPost (); | ||
394 | errorCount += testExternalPost (); | ||
395 | } | ||
396 | if (errorCount != 0) | ||
397 | fprintf (stderr, "Error (code: %u)\n", errorCount); | ||
398 | curl_global_cleanup (); | ||
399 | return errorCount != 0; /* 0 == pass */ | ||
400 | } | ||