aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/daemontest_post.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/daemontest_post.c')
-rw-r--r--src/testcurl/daemontest_post.c376
1 files changed, 376 insertions, 0 deletions
diff --git a/src/testcurl/daemontest_post.c b/src/testcurl/daemontest_post.c
new file mode 100644
index 00000000..335377e6
--- /dev/null
+++ b/src/testcurl/daemontest_post.c
@@ -0,0 +1,376 @@
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 using URL-encoding
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
40static int oneone;
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 * 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 */
66static int
67post_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
86static int
87ahc_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
129static int
130testInternalPost ()
131{
132 struct MHD_Daemon *d;
133 CURL *c;
134 char buf[2048];
135 struct CBC cbc;
136 CURLcode errornum;
137
138 cbc.buf = buf;
139 cbc.size = 2048;
140 cbc.pos = 0;
141 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
142 1080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
143 if (d == NULL)
144 return 1;
145 c = curl_easy_init ();
146 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
147 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
148 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
149 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
150 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
151 curl_easy_setopt (c, CURLOPT_POST, 1L);
152 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
153 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
154 if (oneone)
155 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
156 else
157 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
158 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
159 // NOTE: use of CONNECTTIMEOUT without also
160 // setting NOSIGNAL results in really weird
161 // crashes on my system!
162 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
163 if (CURLE_OK != (errornum = curl_easy_perform (c)))
164 {
165 fprintf (stderr,
166 "curl_easy_perform failed: `%s'\n",
167 curl_easy_strerror (errornum));
168 curl_easy_cleanup (c);
169 MHD_stop_daemon (d);
170 return 2;
171 }
172 curl_easy_cleanup (c);
173 MHD_stop_daemon (d);
174 if (cbc.pos != strlen ("/hello_world"))
175 return 4;
176 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
177 return 8;
178 return 0;
179}
180
181static int
182testMultithreadedPost ()
183{
184 struct MHD_Daemon *d;
185 CURL *c;
186 char buf[2048];
187 struct CBC cbc;
188 CURLcode errornum;
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 1081, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
195 if (d == NULL)
196 return 16;
197 c = curl_easy_init ();
198 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
199 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
200 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
201 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
202 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
203 curl_easy_setopt (c, CURLOPT_POST, 1L);
204 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
205 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
206 if (oneone)
207 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
208 else
209 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
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 return 0;
231}
232
233
234static int
235testExternalPost ()
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
252 multi = NULL;
253 cbc.buf = buf;
254 cbc.size = 2048;
255 cbc.pos = 0;
256 d = MHD_start_daemon (MHD_USE_DEBUG,
257 1082, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
258 if (d == NULL)
259 return 256;
260 c = curl_easy_init ();
261 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
262 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
263 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
264 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
265 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
266 curl_easy_setopt (c, CURLOPT_POST, 1L);
267 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
268 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
269 if (oneone)
270 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
271 else
272 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
273 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
274 // NOTE: use of CONNECTTIMEOUT without also
275 // setting NOSIGNAL results in really weird
276 // crashes on my system!
277 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
278
279
280 multi = curl_multi_init ();
281 if (multi == NULL)
282 {
283 curl_easy_cleanup (c);
284 MHD_stop_daemon (d);
285 return 512;
286 }
287 mret = curl_multi_add_handle (multi, c);
288 if (mret != CURLM_OK)
289 {
290 curl_multi_cleanup (multi);
291 curl_easy_cleanup (c);
292 MHD_stop_daemon (d);
293 return 1024;
294 }
295 start = time (NULL);
296 while ((time (NULL) - start < 5) && (multi != NULL))
297 {
298 max = 0;
299 FD_ZERO (&rs);
300 FD_ZERO (&ws);
301 FD_ZERO (&es);
302 curl_multi_perform (multi, &running);
303 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
304 if (mret != CURLM_OK)
305 {
306 curl_multi_remove_handle (multi, c);
307 curl_multi_cleanup (multi);
308 curl_easy_cleanup (c);
309 MHD_stop_daemon (d);
310 return 2048;
311 }
312 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
313 {
314 curl_multi_remove_handle (multi, c);
315 curl_multi_cleanup (multi);
316 curl_easy_cleanup (c);
317 MHD_stop_daemon (d);
318 return 4096;
319 }
320 tv.tv_sec = 0;
321 tv.tv_usec = 1000;
322 select (max + 1, &rs, &ws, &es, &tv);
323 curl_multi_perform (multi, &running);
324 if (running == 0)
325 {
326 msg = curl_multi_info_read (multi, &running);
327 if (msg == NULL)
328 break;
329 if (msg->msg == CURLMSG_DONE)
330 {
331 if (msg->data.result != CURLE_OK)
332 printf ("%s failed at %s:%d: `%s'\n",
333 "curl_multi_perform",
334 __FILE__,
335 __LINE__, curl_easy_strerror (msg->data.result));
336 curl_multi_remove_handle (multi, c);
337 curl_multi_cleanup (multi);
338 curl_easy_cleanup (c);
339 c = NULL;
340 multi = NULL;
341 }
342 }
343 MHD_run (d);
344 }
345 if (multi != NULL)
346 {
347 curl_multi_remove_handle (multi, c);
348 curl_easy_cleanup (c);
349 curl_multi_cleanup (multi);
350 }
351 MHD_stop_daemon (d);
352 if (cbc.pos != strlen ("/hello_world"))
353 return 8192;
354 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
355 return 16384;
356 return 0;
357}
358
359
360
361int
362main (int argc, char *const *argv)
363{
364 unsigned int errorCount = 0;
365
366 oneone = NULL != strstr (argv[0], "11");
367 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
368 return 2;
369 errorCount += testInternalPost ();
370 errorCount += testMultithreadedPost ();
371 errorCount += testExternalPost ();
372 if (errorCount != 0)
373 fprintf (stderr, "Error (code: %u)\n", errorCount);
374 curl_global_cleanup ();
375 return errorCount != 0; /* 0 == pass */
376}