aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2008-01-21 19:01:07 +0000
committerChristian Grothoff <christian@grothoff.org>2008-01-21 19:01:07 +0000
commit6c34675ab9161454957cf1b4caf94f9bb2e4ba70 (patch)
treefdd3c64318abd6f8c145f70c22dff45ceb059dd6
parent425a9ca5ba6c855b00249f0918310e8aa0f94980 (diff)
downloadlibmicrohttpd-6c34675ab9161454957cf1b4caf94f9bb2e4ba70.tar.gz
libmicrohttpd-6c34675ab9161454957cf1b4caf94f9bb2e4ba70.zip
adding option
-rw-r--r--ChangeLog4
-rw-r--r--README7
-rw-r--r--src/daemon/daemon.c43
-rw-r--r--src/daemon/daemontest_get_chunked.c364
-rw-r--r--src/daemon/internal.h6
-rw-r--r--src/include/microhttpd.h14
6 files changed, 435 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 5275ba46..154b28ce 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
1Mon Jan 21 11:59:46 MST 2008
2 Added option to limit number of concurrent connections
3 accepted from the same IP address. -CG
4
1Fri Jan 4 16:02:08 MST 2008 5Fri Jan 4 16:02:08 MST 2008
2 Fix to properly close connection if application signals 6 Fix to properly close connection if application signals
3 problem handling the request. - AS 7 problem handling the request. - AS
diff --git a/README b/README
index cf941c2e..5f4820d6 100644
--- a/README
+++ b/README
@@ -54,6 +54,9 @@ For POST:
54========= 54=========
55- add support to decode multipart/form-data with 55- add support to decode multipart/form-data with
56 nesting (used for files) -- #1221, TEST 56 nesting (used for files) -- #1221, TEST
57- http://en.wikipedia.org/wiki/HTTP_pipelining:
58 "Only idempotent requests should be pipelined, such as GET and HEAD requests."
59 We should make sure that we force a close after POSTs, PUTs and DELETEs.
57 60
58For SSL: 61For SSL:
59======== 62========
@@ -64,7 +67,9 @@ microhttpd.h:
64Missing Testcases: 67Missing Testcases:
65================== 68==================
66- add testcases for http/1.1 pipelining (need 69- add testcases for http/1.1 pipelining (need
67 to figure out how to ensure curl pipelines) 70 to figure out how to ensure curl pipelines
71 -- and it seems libcurl has issues with pipelining,
72 see http://curl.haxx.se/mail/lib-2007-12/0248.html)
68- add testcases for resource limit enforcement 73- add testcases for resource limit enforcement
69- add testcases for client queuing early response, 74- add testcases for client queuing early response,
70 suppressing 100 CONTINUE 75 suppressing 100 CONTINUE
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 93017ae3..e971c9b2 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -172,10 +172,12 @@ MHD_handle_connection (void *data)
172static int 172static int
173MHD_accept_connection (struct MHD_Daemon *daemon) 173MHD_accept_connection (struct MHD_Daemon *daemon)
174{ 174{
175 struct MHD_Connection *pos;
175 struct MHD_Connection *connection; 176 struct MHD_Connection *connection;
176 struct sockaddr_in6 addr6; 177 struct sockaddr_in6 addr6;
177 struct sockaddr *addr = (struct sockaddr *) &addr6; 178 struct sockaddr *addr = (struct sockaddr *) &addr6;
178 socklen_t addrlen; 179 socklen_t addrlen;
180 unsigned int have;
179 int s; 181 int s;
180#if OSX 182#if OSX
181 static int on = 1; 183 static int on = 1;
@@ -202,7 +204,42 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
202#if DEBUG_CONNECT 204#if DEBUG_CONNECT
203 MHD_DLOG (daemon, "Accepted connection on socket %d\n", s); 205 MHD_DLOG (daemon, "Accepted connection on socket %d\n", s);
204#endif 206#endif
205 if (daemon->max_connections == 0) 207 have = 0;
208 if ( (daemon->per_ip_connection_limit != 0) &&
209 (daemon->max_connections > 0) )
210 {
211 pos = daemon->connections;
212 while (pos != NULL)
213 {
214 if ( (pos->addr != NULL) &&
215 (pos->addr_len == addrlen) )
216 {
217 if (addrlen == sizeof(struct sockaddr_in))
218 {
219 const struct sockaddr_in * a1 = (const struct sockaddr_in *) &addr;
220 const struct sockaddr_in * a2 = (const struct sockaddr_in *) pos->addr;
221 if (0 == memcmp(&a1->sin_addr,
222 &a2->sin_addr,
223 sizeof(struct in_addr)))
224 have++;
225 }
226 if (addrlen == sizeof(struct sockaddr_in6))
227 {
228 const struct sockaddr_in6 * a1 = (const struct sockaddr_in6 *) &addr;
229 const struct sockaddr_in6 * a2 = (const struct sockaddr_in6 *) pos->addr;
230 if (0 == memcmp(&a1->sin6_addr,
231 &a2->sin6_addr,
232 sizeof(struct in6_addr)))
233 have++;
234 }
235 }
236 pos = pos->next;
237 }
238 }
239
240 if ( (daemon->max_connections == 0) ||
241 ( (daemon->per_ip_connection_limit != 0) &&
242 (daemon->per_ip_connection_limit <= have) ) )
206 { 243 {
207 /* above connection limit - reject */ 244 /* above connection limit - reject */
208#if HAVE_MESSAGES 245#if HAVE_MESSAGES
@@ -631,6 +668,10 @@ MHD_start_daemon (unsigned int options,
631 va_arg (ap, MHD_RequestCompletedCallback); 668 va_arg (ap, MHD_RequestCompletedCallback);
632 retVal->notify_completed_cls = va_arg (ap, void *); 669 retVal->notify_completed_cls = va_arg (ap, void *);
633 break; 670 break;
671 case MHD_OPTION_PER_IP_CONNECTION_LIMIT:
672 retVal->per_ip_connection_limit
673 = va_arg (ap, unsigned int);
674 break;
634 default: 675 default:
635#if HAVE_MESSAGES 676#if HAVE_MESSAGES
636 fprintf (stderr, 677 fprintf (stderr,
diff --git a/src/daemon/daemontest_get_chunked.c b/src/daemon/daemontest_get_chunked.c
new file mode 100644
index 00000000..4b2739cf
--- /dev/null
+++ b/src/daemon/daemontest_get_chunked.c
@@ -0,0 +1,364 @@
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_get_chunked.c
23 * @brief Testcase for libmicrohttpd GET operations with chunked content encoding
24 * TODO:
25 * - how to test that chunking was actually used?
26 * - use CURLOPT_HEADERFUNCTION to validate
27 * footer was sent
28 * @author Christian Grothoff
29 */
30
31#include "config.h"
32#include <curl/curl.h>
33#include <microhttpd.h>
34#include <stdlib.h>
35#include <string.h>
36#include <time.h>
37
38#ifndef WINDOWS
39#include <unistd.h>
40#endif
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 * MHD content reader callback that returns
63 * data in chunks.
64 */
65static int
66crc (void *cls, size_t pos, char *buf, int max)
67{
68 struct MHD_Response **responseptr = cls;
69
70 if (pos == 128 * 10)
71 {
72 MHD_add_response_header (*responseptr, "Footer", "working");
73 return -1; /* end of stream */
74 }
75 if (max < 128)
76 abort (); /* should not happen in this testcase... */
77 memset (buf, 'A' + (pos / 128), 128);
78 return 128;
79}
80
81/**
82 * Dummy function that does nothing.
83 */
84static void
85crcf (void *ptr)
86{
87 free (ptr);
88}
89
90static int
91ahc_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, unsigned int *upload_data_size, void **ptr)
97{
98 static int aptr;
99 const char *me = cls;
100 struct MHD_Response *response;
101 struct MHD_Response **responseptr;
102 int ret;
103
104 if (0 != strcmp (me, method))
105 return MHD_NO; /* unexpected method */
106 if (&aptr != *ptr)
107 {
108 /* do never respond on first call */
109 *ptr = &aptr;
110 return MHD_YES;
111 }
112 responseptr = malloc (sizeof (struct MHD_Response *));
113 response = MHD_create_response_from_callback (-1,
114 1024,
115 &crc, responseptr, &crcf);
116 *responseptr = response;
117 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
118 MHD_destroy_response (response);
119 return ret;
120}
121
122static int
123validate (struct CBC cbc, int ebase)
124{
125 int i;
126 char buf[128];
127
128 if (cbc.pos != 128 * 10)
129 return ebase;
130
131 for (i = 0; i < 10; i++)
132 {
133 memset (buf, 'A' + i, 128);
134 if (0 != memcmp (buf, &cbc.buf[i * 128], 128))
135 {
136 fprintf (stderr,
137 "Got `%.*s'\nWant `%.*s'\n",
138 128, buf, 128, &cbc.buf[i * 128]);
139 return ebase * 2;
140 }
141 }
142 return 0;
143}
144
145static int
146testInternalGet ()
147{
148 struct MHD_Daemon *d;
149 CURL *c;
150 char buf[2048];
151 struct CBC cbc;
152 CURLcode errornum;
153
154 cbc.buf = buf;
155 cbc.size = 2048;
156 cbc.pos = 0;
157 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
158 1080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
159 if (d == NULL)
160 return 1;
161 c = curl_easy_init ();
162 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
163 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
164 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
165 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
166 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
167 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
168 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
169 // NOTE: use of CONNECTTIMEOUT without also
170 // setting NOSIGNAL results in really weird
171 // crashes on my system!
172 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
173 if (CURLE_OK != (errornum = curl_easy_perform (c)))
174 {
175 fprintf (stderr,
176 "curl_easy_perform failed: `%s'\n",
177 curl_easy_strerror (errornum));
178 curl_easy_cleanup (c);
179 MHD_stop_daemon (d);
180 return 2;
181 }
182 curl_easy_cleanup (c);
183 MHD_stop_daemon (d);
184 return validate (cbc, 4);
185}
186
187static int
188testMultithreadedGet ()
189{
190 struct MHD_Daemon *d;
191 CURL *c;
192 char buf[2048];
193 struct CBC cbc;
194 CURLcode errornum;
195
196 cbc.buf = buf;
197 cbc.size = 2048;
198 cbc.pos = 0;
199 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
200 1081, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
201 if (d == NULL)
202 return 16;
203 c = curl_easy_init ();
204 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
205 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
206 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
207 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
208 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
209 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
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 return validate (cbc, 64);
227}
228
229
230static int
231testExternalGet ()
232{
233 struct MHD_Daemon *d;
234 CURL *c;
235 char buf[2048];
236 struct CBC cbc;
237 CURLM *multi;
238 CURLMcode mret;
239 fd_set rs;
240 fd_set ws;
241 fd_set es;
242 int max;
243 int running;
244 struct CURLMsg *msg;
245 time_t start;
246 struct timeval tv;
247
248 multi = NULL;
249 cbc.buf = buf;
250 cbc.size = 2048;
251 cbc.pos = 0;
252 d = MHD_start_daemon (MHD_USE_DEBUG,
253 1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
254 if (d == NULL)
255 return 256;
256 c = curl_easy_init ();
257 curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
258 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
259 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
260 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
261 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
262 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
263 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L);
264 // NOTE: use of CONNECTTIMEOUT without also
265 // setting NOSIGNAL results in really weird
266 // crashes on my system!
267 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
268
269
270 multi = curl_multi_init ();
271 if (multi == NULL)
272 {
273 curl_easy_cleanup (c);
274 MHD_stop_daemon (d);
275 return 512;
276 }
277 mret = curl_multi_add_handle (multi, c);
278 if (mret != CURLM_OK)
279 {
280 curl_multi_cleanup (multi);
281 curl_easy_cleanup (c);
282 MHD_stop_daemon (d);
283 return 1024;
284 }
285 start = time (NULL);
286 while ((time (NULL) - start < 5) && (multi != NULL))
287 {
288 max = 0;
289 FD_ZERO (&rs);
290 FD_ZERO (&ws);
291 FD_ZERO (&es);
292 curl_multi_perform (multi, &running);
293 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
294 if (mret != CURLM_OK)
295 {
296 curl_multi_remove_handle (multi, c);
297 curl_multi_cleanup (multi);
298 curl_easy_cleanup (c);
299 MHD_stop_daemon (d);
300 return 2048;
301 }
302 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
303 {
304 curl_multi_remove_handle (multi, c);
305 curl_multi_cleanup (multi);
306 curl_easy_cleanup (c);
307 MHD_stop_daemon (d);
308 return 4096;
309 }
310 tv.tv_sec = 0;
311 tv.tv_usec = 1000;
312 select (max + 1, &rs, &ws, &es, &tv);
313 curl_multi_perform (multi, &running);
314 if (running == 0)
315 {
316 msg = curl_multi_info_read (multi, &running);
317 if (msg == NULL)
318 break;
319 if (msg->msg == CURLMSG_DONE)
320 {
321 if (msg->data.result != CURLE_OK)
322 printf ("%s failed at %s:%d: `%s'\n",
323 "curl_multi_perform",
324 __FILE__,
325 __LINE__, curl_easy_strerror (msg->data.result));
326 curl_multi_remove_handle (multi, c);
327 curl_multi_cleanup (multi);
328 curl_easy_cleanup (c);
329 c = NULL;
330 multi = NULL;
331 }
332 }
333 MHD_run (d);
334 }
335 if (multi != NULL)
336 {
337 curl_multi_remove_handle (multi, c);
338 curl_easy_cleanup (c);
339 curl_multi_cleanup (multi);
340 }
341 MHD_stop_daemon (d);
342 return validate (cbc, 8192);
343}
344
345
346
347int
348main (int argc, char *const *argv)
349{
350 unsigned int errorCount = 0;
351
352 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
353 return 2;
354 errorCount += testInternalGet ();
355 if (0)
356 {
357 errorCount += testMultithreadedGet ();
358 errorCount += testExternalGet ();
359 }
360 if (errorCount != 0)
361 fprintf (stderr, "Error (code: %u)\n", errorCount);
362 curl_global_cleanup ();
363 return errorCount != 0; /* 0 == pass */
364}
diff --git a/src/daemon/internal.h b/src/daemon/internal.h
index 43599149..e22de32f 100644
--- a/src/daemon/internal.h
+++ b/src/daemon/internal.h
@@ -572,6 +572,12 @@ struct MHD_Daemon
572 unsigned int connection_timeout; 572 unsigned int connection_timeout;
573 573
574 /** 574 /**
575 * Maximum number of connections per IP, or 0 for
576 * unlimited.
577 */
578 unsigned int per_ip_connection_limit;
579
580 /**
575 * Daemon's options. 581 * Daemon's options.
576 */ 582 */
577 enum MHD_OPTION options; 583 enum MHD_OPTION options;
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
index b8dfc426..76e64e96 100644
--- a/src/include/microhttpd.h
+++ b/src/include/microhttpd.h
@@ -84,7 +84,7 @@ extern "C"
84/** 84/**
85 * Current version of the library. 85 * Current version of the library.
86 */ 86 */
87#define MHD_VERSION 0x00000200 87#define MHD_VERSION 0x00000201
88 88
89/** 89/**
90 * MHD-internal return codes. 90 * MHD-internal return codes.
@@ -333,6 +333,18 @@ enum MHD_OPTION
333 */ 333 */
334 MHD_OPTION_NOTIFY_COMPLETED = 4, 334 MHD_OPTION_NOTIFY_COMPLETED = 4,
335 335
336 /**
337 * Limit on the number of (concurrent) connections made to the
338 * server from the same IP address. Can be used to prevent one
339 * IP from taking over all of the allowed connections. If the
340 * same IP tries to establish more than the specified number of
341 * connections, they will be immediately rejected. The option
342 * should be followed by an "unsigned int". The default is
343 * zero, which means no limit on the number of connections
344 * from the same IP address.
345 */
346 MHD_OPTION_PER_IP_CONNECTION_LIMIT = 5,
347
336}; 348};
337 349
338/** 350/**