diff options
Diffstat (limited to 'src/testzzuf/test_post.c')
-rw-r--r-- | src/testzzuf/test_post.c | 372 |
1 files changed, 372 insertions, 0 deletions
diff --git a/src/testzzuf/test_post.c b/src/testzzuf/test_post.c new file mode 100644 index 00000000..66077b4f --- /dev/null +++ b/src/testzzuf/test_post.c | |||
@@ -0,0 +1,372 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007, 2008 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 using URL-encoding | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "MHD_config.h" | ||
28 | #include "platform.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 | |||
40 | #include "socat.c" | ||
41 | |||
42 | #define POST_DATA "name=daniel&project=curl" | ||
43 | |||
44 | static int oneone; | ||
45 | |||
46 | struct CBC | ||
47 | { | ||
48 | char *buf; | ||
49 | size_t pos; | ||
50 | size_t size; | ||
51 | }; | ||
52 | |||
53 | static size_t | ||
54 | copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) | ||
55 | { | ||
56 | struct CBC *cbc = ctx; | ||
57 | |||
58 | if (cbc->pos + size * nmemb > cbc->size) | ||
59 | return 0; /* overflow */ | ||
60 | memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); | ||
61 | cbc->pos += size * nmemb; | ||
62 | return size * nmemb; | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Note that this post_iterator is not perfect | ||
67 | * in that it fails to support incremental processing. | ||
68 | * (to be fixed in the future) | ||
69 | */ | ||
70 | static int | ||
71 | post_iterator (void *cls, | ||
72 | enum MHD_ValueKind kind, | ||
73 | const char *key, | ||
74 | const char *filename, | ||
75 | const char *content_type, | ||
76 | const char *transfer_encoding, | ||
77 | const char *value, uint64_t off, size_t size) | ||
78 | { | ||
79 | int *eok = cls; | ||
80 | |||
81 | if ((0 == strcmp (key, "name")) && | ||
82 | (size == strlen ("daniel")) && (0 == strncmp (value, "daniel", size))) | ||
83 | (*eok) |= 1; | ||
84 | if ((0 == strcmp (key, "project")) && | ||
85 | (size == strlen ("curl")) && (0 == strncmp (value, "curl", size))) | ||
86 | (*eok) |= 2; | ||
87 | return MHD_YES; | ||
88 | } | ||
89 | |||
90 | static int | ||
91 | ahc_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, size_t *upload_data_size, | ||
97 | void **unused) | ||
98 | { | ||
99 | static int eok; | ||
100 | struct MHD_Response *response; | ||
101 | struct MHD_PostProcessor *pp; | ||
102 | int ret; | ||
103 | |||
104 | if (0 != strcmp ("POST", method)) | ||
105 | { | ||
106 | return MHD_NO; /* unexpected method */ | ||
107 | } | ||
108 | pp = *unused; | ||
109 | if (pp == NULL) | ||
110 | { | ||
111 | eok = 0; | ||
112 | pp = MHD_create_post_processor (connection, 1024, &post_iterator, &eok); | ||
113 | *unused = pp; | ||
114 | } | ||
115 | MHD_post_process (pp, upload_data, *upload_data_size); | ||
116 | if ((eok == 3) && (0 == *upload_data_size)) | ||
117 | { | ||
118 | response = MHD_create_response_from_buffer (strlen (url), | ||
119 | (void *) url, | ||
120 | MHD_RESPMEM_MUST_COPY); | ||
121 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
122 | MHD_destroy_response (response); | ||
123 | MHD_destroy_post_processor (pp); | ||
124 | *unused = NULL; | ||
125 | return ret; | ||
126 | } | ||
127 | *upload_data_size = 0; | ||
128 | return MHD_YES; | ||
129 | } | ||
130 | |||
131 | |||
132 | static int | ||
133 | testInternalPost () | ||
134 | { | ||
135 | struct MHD_Daemon *d; | ||
136 | CURL *c; | ||
137 | char buf[2048]; | ||
138 | struct CBC cbc; | ||
139 | int i; | ||
140 | |||
141 | cbc.buf = buf; | ||
142 | cbc.size = 2048; | ||
143 | cbc.pos = 0; | ||
144 | d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */ , | ||
145 | 11080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); | ||
146 | if (d == NULL) | ||
147 | return 1; | ||
148 | zzuf_socat_start (); | ||
149 | for (i = 0; i < LOOP_COUNT; i++) | ||
150 | { | ||
151 | fprintf (stderr, "."); | ||
152 | |||
153 | c = curl_easy_init (); | ||
154 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11081/hello_world"); | ||
155 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
156 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
157 | curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); | ||
158 | curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); | ||
159 | curl_easy_setopt (c, CURLOPT_POST, 1L); | ||
160 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
161 | curl_easy_setopt (c, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT); | ||
162 | if (oneone) | ||
163 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
164 | else | ||
165 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
166 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT_MS, CURL_TIMEOUT); | ||
167 | // NOTE: use of CONNECTTIMEOUT without also | ||
168 | // setting NOSIGNAL results in really weird | ||
169 | // crashes on my system! | ||
170 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
171 | curl_easy_perform (c); | ||
172 | curl_easy_cleanup (c); | ||
173 | } | ||
174 | fprintf (stderr, "\n"); | ||
175 | zzuf_socat_stop (); | ||
176 | MHD_stop_daemon (d); | ||
177 | |||
178 | return 0; | ||
179 | } | ||
180 | |||
181 | static int | ||
182 | testMultithreadedPost () | ||
183 | { | ||
184 | struct MHD_Daemon *d; | ||
185 | CURL *c; | ||
186 | char buf[2048]; | ||
187 | struct CBC cbc; | ||
188 | int i; | ||
189 | |||
190 | cbc.buf = buf; | ||
191 | cbc.size = 2048; | ||
192 | cbc.pos = 0; | ||
193 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION /* | MHD_USE_DEBUG */ , | ||
194 | 11080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); | ||
195 | if (d == NULL) | ||
196 | return 16; | ||
197 | |||
198 | zzuf_socat_start (); | ||
199 | for (i = 0; i < LOOP_COUNT; i++) | ||
200 | { | ||
201 | fprintf (stderr, "."); | ||
202 | |||
203 | c = curl_easy_init (); | ||
204 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11081/hello_world"); | ||
205 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
206 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
207 | curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); | ||
208 | curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); | ||
209 | curl_easy_setopt (c, CURLOPT_POST, 1L); | ||
210 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
211 | curl_easy_setopt (c, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT); | ||
212 | if (oneone) | ||
213 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
214 | else | ||
215 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
216 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT_MS, CURL_TIMEOUT); | ||
217 | // NOTE: use of CONNECTTIMEOUT without also | ||
218 | // setting NOSIGNAL results in really weird | ||
219 | // crashes on my system! | ||
220 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
221 | curl_easy_perform (c); | ||
222 | curl_easy_cleanup (c); | ||
223 | } | ||
224 | fprintf (stderr, "\n"); | ||
225 | zzuf_socat_stop (); | ||
226 | |||
227 | MHD_stop_daemon (d); | ||
228 | return 0; | ||
229 | } | ||
230 | |||
231 | |||
232 | static int | ||
233 | testExternalPost () | ||
234 | { | ||
235 | struct MHD_Daemon *d; | ||
236 | CURL *c; | ||
237 | char buf[2048]; | ||
238 | struct CBC cbc; | ||
239 | CURLM *multi; | ||
240 | CURLMcode mret; | ||
241 | fd_set rs; | ||
242 | fd_set ws; | ||
243 | fd_set es; | ||
244 | int max; | ||
245 | int running; | ||
246 | time_t start; | ||
247 | struct timeval tv; | ||
248 | int i; | ||
249 | |||
250 | multi = NULL; | ||
251 | cbc.buf = buf; | ||
252 | cbc.size = 2048; | ||
253 | cbc.pos = 0; | ||
254 | d = MHD_start_daemon (MHD_NO_FLAG /* | MHD_USE_DEBUG */ , | ||
255 | 1082, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); | ||
256 | if (d == NULL) | ||
257 | return 256; | ||
258 | multi = curl_multi_init (); | ||
259 | if (multi == NULL) | ||
260 | { | ||
261 | MHD_stop_daemon (d); | ||
262 | return 512; | ||
263 | } | ||
264 | |||
265 | zzuf_socat_start (); | ||
266 | for (i = 0; i < LOOP_COUNT; i++) | ||
267 | { | ||
268 | fprintf (stderr, "."); | ||
269 | |||
270 | |||
271 | c = curl_easy_init (); | ||
272 | curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world"); | ||
273 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
274 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
275 | curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA); | ||
276 | curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA)); | ||
277 | curl_easy_setopt (c, CURLOPT_POST, 1L); | ||
278 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
279 | curl_easy_setopt (c, CURLOPT_TIMEOUT_MS, CURL_TIMEOUT); | ||
280 | if (oneone) | ||
281 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
282 | else | ||
283 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
284 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT_MS, CURL_TIMEOUT); | ||
285 | // NOTE: use of CONNECTTIMEOUT without also | ||
286 | // setting NOSIGNAL results in really weird | ||
287 | // crashes on my system! | ||
288 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
289 | |||
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 | zzuf_socat_stop (); | ||
297 | MHD_stop_daemon (d); | ||
298 | return 1024; | ||
299 | } | ||
300 | start = time (NULL); | ||
301 | while ((time (NULL) - start < 5) && (c != 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 | zzuf_socat_stop (); | ||
315 | MHD_stop_daemon (d); | ||
316 | return 2048; | ||
317 | } | ||
318 | if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) | ||
319 | { | ||
320 | curl_multi_remove_handle (multi, c); | ||
321 | curl_multi_cleanup (multi); | ||
322 | curl_easy_cleanup (c); | ||
323 | zzuf_socat_stop (); | ||
324 | MHD_stop_daemon (d); | ||
325 | return 4096; | ||
326 | } | ||
327 | tv.tv_sec = 0; | ||
328 | tv.tv_usec = 1000; | ||
329 | select (max + 1, &rs, &ws, &es, &tv); | ||
330 | curl_multi_perform (multi, &running); | ||
331 | if (running == 0) | ||
332 | { | ||
333 | curl_multi_info_read (multi, &running); | ||
334 | curl_multi_remove_handle (multi, c); | ||
335 | curl_easy_cleanup (c); | ||
336 | c = NULL; | ||
337 | } | ||
338 | MHD_run (d); | ||
339 | } | ||
340 | if (c != NULL) | ||
341 | { | ||
342 | curl_multi_remove_handle (multi, c); | ||
343 | curl_easy_cleanup (c); | ||
344 | } | ||
345 | |||
346 | } | ||
347 | fprintf (stderr, "\n"); | ||
348 | curl_multi_cleanup (multi); | ||
349 | zzuf_socat_stop (); | ||
350 | |||
351 | MHD_stop_daemon (d); | ||
352 | return 0; | ||
353 | } | ||
354 | |||
355 | |||
356 | |||
357 | int | ||
358 | main (int argc, char *const *argv) | ||
359 | { | ||
360 | unsigned int errorCount = 0; | ||
361 | |||
362 | oneone = NULL != strstr (argv[0], "11"); | ||
363 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | ||
364 | return 2; | ||
365 | errorCount += testInternalPost (); | ||
366 | errorCount += testMultithreadedPost (); | ||
367 | errorCount += testExternalPost (); | ||
368 | if (errorCount != 0) | ||
369 | fprintf (stderr, "Error (code: %u)\n", errorCount); | ||
370 | curl_global_cleanup (); | ||
371 | return errorCount != 0; /* 0 == pass */ | ||
372 | } | ||