diff options
Diffstat (limited to 'src/testcurl/test_urlparse.c')
-rw-r--r-- | src/testcurl/test_urlparse.c | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/src/testcurl/test_urlparse.c b/src/testcurl/test_urlparse.c new file mode 100644 index 00000000..8a809073 --- /dev/null +++ b/src/testcurl/test_urlparse.c | |||
@@ -0,0 +1,187 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007, 2009, 2011 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_urlparse.c | ||
23 | * @brief Testcase for libmicrohttpd url parsing | ||
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 | #ifdef __MINGW32__ | ||
36 | #define usleep(usec) (Sleep ((usec) / 1000),0) | ||
37 | #endif | ||
38 | |||
39 | #ifndef WINDOWS | ||
40 | #include <unistd.h> | ||
41 | #include <sys/socket.h> | ||
42 | #endif | ||
43 | |||
44 | static int oneone; | ||
45 | |||
46 | static int matches; | ||
47 | |||
48 | struct CBC | ||
49 | { | ||
50 | char *buf; | ||
51 | size_t pos; | ||
52 | size_t size; | ||
53 | }; | ||
54 | |||
55 | static size_t | ||
56 | copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) | ||
57 | { | ||
58 | struct CBC *cbc = ctx; | ||
59 | |||
60 | if (cbc->pos + size * nmemb > cbc->size) | ||
61 | return 0; /* overflow */ | ||
62 | memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); | ||
63 | cbc->pos += size * nmemb; | ||
64 | return size * nmemb; | ||
65 | } | ||
66 | |||
67 | static int | ||
68 | test_values (void *cls, | ||
69 | enum MHD_ValueKind kind, | ||
70 | const char *key, | ||
71 | const char *value) | ||
72 | { | ||
73 | if ( (0 == strcmp (key, "a")) && | ||
74 | (0 == strcmp (value, "b")) ) | ||
75 | matches += 1; | ||
76 | if ( (0 == strcmp (key, "c")) && | ||
77 | (0 == strcmp (value, "")) ) | ||
78 | matches += 2; | ||
79 | if ( (0 == strcmp (key, "d")) && | ||
80 | (NULL == value) ) | ||
81 | matches += 4; | ||
82 | return MHD_YES; | ||
83 | } | ||
84 | |||
85 | static int | ||
86 | ahc_echo (void *cls, | ||
87 | struct MHD_Connection *connection, | ||
88 | const char *url, | ||
89 | const char *method, | ||
90 | const char *version, | ||
91 | const char *upload_data, size_t *upload_data_size, | ||
92 | void **unused) | ||
93 | { | ||
94 | static int ptr; | ||
95 | const char *me = cls; | ||
96 | struct MHD_Response *response; | ||
97 | int ret; | ||
98 | |||
99 | if (0 != strcmp (me, method)) | ||
100 | return MHD_NO; /* unexpected method */ | ||
101 | if (&ptr != *unused) | ||
102 | { | ||
103 | *unused = &ptr; | ||
104 | return MHD_YES; | ||
105 | } | ||
106 | MHD_get_connection_values (connection, | ||
107 | MHD_GET_ARGUMENT_KIND, | ||
108 | &test_values, | ||
109 | NULL); | ||
110 | *unused = NULL; | ||
111 | response = MHD_create_response_from_buffer (strlen (url), | ||
112 | (void *) url, | ||
113 | MHD_RESPMEM_MUST_COPY); | ||
114 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
115 | MHD_destroy_response (response); | ||
116 | if (ret == MHD_NO) | ||
117 | abort (); | ||
118 | return ret; | ||
119 | } | ||
120 | |||
121 | |||
122 | static int | ||
123 | testInternalGet (int poll_flag) | ||
124 | { | ||
125 | struct MHD_Daemon *d; | ||
126 | CURL *c; | ||
127 | char buf[2048]; | ||
128 | struct CBC cbc; | ||
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 | poll_flag, | ||
135 | 11080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END); | ||
136 | if (d == NULL) | ||
137 | return 1; | ||
138 | c = curl_easy_init (); | ||
139 | curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1:11080/hello_world?a=b&c=&d"); | ||
140 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
141 | curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); | ||
142 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
143 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); | ||
144 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); | ||
145 | if (oneone) | ||
146 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
147 | else | ||
148 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
149 | /* NOTE: use of CONNECTTIMEOUT without also | ||
150 | setting NOSIGNAL results in really weird | ||
151 | crashes on my system!*/ | ||
152 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
153 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
154 | { | ||
155 | fprintf (stderr, | ||
156 | "curl_easy_perform failed: `%s'\n", | ||
157 | curl_easy_strerror (errornum)); | ||
158 | curl_easy_cleanup (c); | ||
159 | MHD_stop_daemon (d); | ||
160 | return 2; | ||
161 | } | ||
162 | curl_easy_cleanup (c); | ||
163 | MHD_stop_daemon (d); | ||
164 | if (cbc.pos != strlen ("/hello_world")) | ||
165 | return 4; | ||
166 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
167 | return 8; | ||
168 | if (matches != 7) | ||
169 | return 16; | ||
170 | return 0; | ||
171 | } | ||
172 | |||
173 | |||
174 | int | ||
175 | main (int argc, char *const *argv) | ||
176 | { | ||
177 | unsigned int errorCount = 0; | ||
178 | |||
179 | oneone = NULL != strstr (argv[0], "11"); | ||
180 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | ||
181 | return 2; | ||
182 | errorCount += testInternalGet (0); | ||
183 | if (errorCount != 0) | ||
184 | fprintf (stderr, "Error (code: %u)\n", errorCount); | ||
185 | curl_global_cleanup (); | ||
186 | return errorCount != 0; /* 0 == pass */ | ||
187 | } | ||