aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/daemontest_long_header.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/daemontest_long_header.c')
-rw-r--r--src/testcurl/daemontest_long_header.c240
1 files changed, 240 insertions, 0 deletions
diff --git a/src/testcurl/daemontest_long_header.c b/src/testcurl/daemontest_long_header.c
new file mode 100644
index 00000000..385b9776
--- /dev/null
+++ b/src/testcurl/daemontest_long_header.c
@@ -0,0 +1,240 @@
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_long_header.c
23 * @brief Testcase for libmicrohttpd handling of very long headers
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/**
39 * We will set the memory available per connection to
40 * half of this value, so the actual value does not have
41 * to be big at all...
42 */
43#define VERY_LONG (1024*10)
44
45static int oneone;
46
47static int
48apc_all (void *cls, const struct sockaddr *addr, socklen_t addrlen)
49{
50 return MHD_YES;
51}
52
53struct CBC
54{
55 char *buf;
56 size_t pos;
57 size_t size;
58};
59
60static size_t
61copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
62{
63 return size * nmemb;
64}
65
66static int
67ahc_echo (void *cls,
68 struct MHD_Connection *connection,
69 const char *url,
70 const char *method,
71 const char *version,
72 const char *upload_data, unsigned int *upload_data_size,
73 void **unused)
74{
75 const char *me = cls;
76 struct MHD_Response *response;
77 int ret;
78
79 if (0 != strcmp (me, method))
80 return MHD_NO; /* unexpected method */
81 response = MHD_create_response_from_data (strlen (url),
82 (void *) url, MHD_NO, MHD_YES);
83 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
84 MHD_destroy_response (response);
85 return ret;
86}
87
88
89static int
90testLongUrlGet ()
91{
92 struct MHD_Daemon *d;
93 CURL *c;
94 char buf[2048];
95 struct CBC cbc;
96 char *url;
97 long code;
98
99 cbc.buf = buf;
100 cbc.size = 2048;
101 cbc.pos = 0;
102 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */ ,
103 1080,
104 &apc_all,
105 NULL,
106 &ahc_echo,
107 "GET",
108 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
109 VERY_LONG / 2, MHD_OPTION_END);
110 if (d == NULL)
111 return 1;
112 c = curl_easy_init ();
113 url = malloc (VERY_LONG);
114 memset (url, 'a', VERY_LONG);
115 url[VERY_LONG - 1] = '\0';
116 memcpy (url, "http://localhost:1080/", strlen ("http://localhost:1080/"));
117 curl_easy_setopt (c, CURLOPT_URL, url);
118 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
119 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
120 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
121 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
122 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
123 if (oneone)
124 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
125 else
126 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
127 // NOTE: use of CONNECTTIMEOUT without also
128 // setting NOSIGNAL results in really weird
129 // crashes on my system!
130 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
131 if (CURLE_OK == curl_easy_perform (c))
132 {
133 curl_easy_cleanup (c);
134 MHD_stop_daemon (d);
135 free (url);
136 return 2;
137 }
138 if (CURLE_OK != curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &code))
139 {
140 curl_easy_cleanup (c);
141 MHD_stop_daemon (d);
142 free (url);
143 return 4;
144 }
145 curl_easy_cleanup (c);
146 MHD_stop_daemon (d);
147 free (url);
148 if (code != MHD_HTTP_REQUEST_URI_TOO_LONG)
149 return 8;
150 return 0;
151}
152
153
154static int
155testLongHeaderGet ()
156{
157 struct MHD_Daemon *d;
158 CURL *c;
159 char buf[2048];
160 struct CBC cbc;
161 char *url;
162 long code;
163 struct curl_slist *header = NULL;
164
165 cbc.buf = buf;
166 cbc.size = 2048;
167 cbc.pos = 0;
168 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */ ,
169 1080,
170 &apc_all,
171 NULL,
172 &ahc_echo,
173 "GET",
174 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
175 VERY_LONG / 2, MHD_OPTION_END);
176 if (d == NULL)
177 return 16;
178 c = curl_easy_init ();
179 url = malloc (VERY_LONG);
180 memset (url, 'a', VERY_LONG);
181 url[VERY_LONG - 1] = '\0';
182 url[VERY_LONG / 2] = ':';
183 url[VERY_LONG / 2 + 1] = ':';
184 header = curl_slist_append (header, url);
185
186 curl_easy_setopt (c, CURLOPT_HTTPHEADER, header);
187 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
188 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
189 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
190 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
191 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
192 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
193 if (oneone)
194 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
195 else
196 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
197 // NOTE: use of CONNECTTIMEOUT without also
198 // setting NOSIGNAL results in really weird
199 // crashes on my system!
200 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
201 if (CURLE_OK == curl_easy_perform (c))
202 {
203 curl_easy_cleanup (c);
204 MHD_stop_daemon (d);
205 curl_slist_free_all (header);
206 free (url);
207 return 32;
208 }
209 if (CURLE_OK != curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &code))
210 {
211 curl_slist_free_all (header);
212 curl_easy_cleanup (c);
213 MHD_stop_daemon (d);
214 free (url);
215 return 64;
216 }
217 curl_slist_free_all (header);
218 curl_easy_cleanup (c);
219 MHD_stop_daemon (d);
220 free (url);
221 if (code != MHD_HTTP_REQUEST_ENTITY_TOO_LARGE)
222 return 128;
223 return 0;
224}
225
226int
227main (int argc, char *const *argv)
228{
229 unsigned int errorCount = 0;
230
231 oneone = NULL != strstr (argv[0], "11");
232 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
233 return 2;
234 errorCount += testLongUrlGet ();
235 errorCount += testLongHeaderGet ();
236 if (errorCount != 0)
237 fprintf (stderr, "Error (code: %u)\n", errorCount);
238 curl_global_cleanup ();
239 return errorCount != 0; /* 0 == pass */
240}