libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 6136f0adf6a5a2592dcbe3d9819f7790e89cac5d
parent 5edea01df8860942fcc05839e1a4dded1ccee7c7
Author: Christian Grothoff <christian@grothoff.org>
Date:   Fri,  9 Sep 2011 13:21:25 +0000

testcase

Diffstat:
MChangeLog | 4++++
Msrc/testcurl/Makefile.am | 7+++++++
Asrc/testcurl/daemontest_get_response_cleanup.c | 282+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 293 insertions(+), 0 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,3 +1,7 @@ +Fri Sep 9 13:42:20 CEST 2011 + Added testcase to demonstrate that response cleanup calling is + working. No bug was found. CG + Thu Aug 18 11:05:16 CEST 2011 Fixed bug with wrong state transition if callback returned MHD_CONTENT_READER_END_OF_STREAM causing spurious extra callbacks diff --git a/src/testcurl/Makefile.am b/src/testcurl/Makefile.am @@ -21,6 +21,7 @@ $(LIBCURL_CPPFLAGS) if !HAVE_W32 PERF_GET_CONCURRENT=perf_get_concurrent +CURL_FORK_TEST=daemontest_get_response_cleanup endif check_PROGRAMS = \ @@ -49,6 +50,7 @@ check_PROGRAMS = \ daemontest_termination \ daemontest_timeout \ test_callback \ + $(CURL_FORK_TEST) \ perf_get $(PERF_GET_CONCURRENT) @@ -110,6 +112,11 @@ daemontest_get_sendfile_LDADD = \ $(top_builddir)/src/daemon/libmicrohttpd.la \ @LIBCURL@ +daemontest_get_response_cleanup_SOURCES = \ + daemontest_get_response_cleanup.c +daemontest_get_response_cleanup_LDADD = \ + $(top_builddir)/src/daemon/libmicrohttpd.la + daemontest_get_chunked_SOURCES = \ daemontest_get_chunked.c daemontest_get_chunked_LDADD = \ diff --git a/src/testcurl/daemontest_get_response_cleanup.c b/src/testcurl/daemontest_get_response_cleanup.c @@ -0,0 +1,282 @@ +/* DO NOT CHANGE THIS LINE */ +/* + This file is part of libmicrohttpd + (C) 2007, 2009 Christian Grothoff + + libmicrohttpd is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 2, or (at your + option) any later version. + + libmicrohttpd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libmicrohttpd; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file daemontest_get_response_cleanup.c + * @brief Testcase for libmicrohttpd response cleanup + * @author Christian Grothoff + */ + +#include "MHD_config.h" +#include "platform.h" +#include <microhttpd.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <fcntl.h> + +#ifndef WINDOWS +#include <sys/socket.h> +#include <unistd.h> +#endif + +#define TESTSTR "/* DO NOT CHANGE THIS LINE */" + +static int oneone; + +static int ok; + + +static pid_t +fork_curl (const char *url) +{ + pid_t ret; + + ret = fork(); + if (ret != 0) + return ret; + execlp ("curl", "curl", "-s", "-N", "-o", "/dev/null", "-GET", url, NULL); + _exit (-1); +} + +static void +kill_curl (pid_t pid) +{ + int status; + + // fprintf (stderr, "Killing curl\n"); + kill (pid, SIGTERM); + waitpid (pid, &status, 0); +} + + +static ssize_t +push_callback (void *cls, uint64_t pos, char *buf, size_t max) +{ + if (max == 0) + return 0; + buf[0] = 'd'; + return 1; +} + +static void +push_free_callback (void *cls) +{ + int *ok = cls; + + // fprintf (stderr, "Cleanup callback called!\n"); + *ok = 0; +} + + +static int +ahc_echo (void *cls, + struct MHD_Connection *connection, + const char *url, + const char *method, + const char *version, + const char *upload_data, size_t *upload_data_size, + void **unused) +{ + static int ptr; + const char *me = cls; + struct MHD_Response *response; + int ret; + + if (0 != strcmp (me, method)) + return MHD_NO; /* unexpected method */ + if (&ptr != *unused) + { + *unused = &ptr; + return MHD_YES; + } + *unused = NULL; + response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, + 32 * 1024, + &push_callback, + &ok, + &push_free_callback); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); + if (ret == MHD_NO) + abort (); + return ret; +} + + +static int +testInternalGet () +{ + struct MHD_Daemon *d; + pid_t curl; + + ok = 1; + d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, + 11080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END); + if (d == NULL) + return 1; + curl = fork_curl ("http://localhost:11080/"); + sleep (1); + kill_curl (curl); + sleep (1); + // fprintf (stderr, "Stopping daemon!\n"); + MHD_stop_daemon (d); + if (ok != 0) + return 2; + return 0; +} + +static int +testMultithreadedGet () +{ + struct MHD_Daemon *d; + pid_t curl; + + d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, + 1081, NULL, NULL, &ahc_echo, "GET", + MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 2, + MHD_OPTION_END); + if (d == NULL) + return 16; + ok = 1; + curl = fork_curl ("http://localhost:1081/"); + sleep (1); + kill_curl (curl); + sleep (1); + curl = fork_curl ("http://localhost:1081/"); + sleep (1); + if (ok != 0) + { + kill_curl (curl); + MHD_stop_daemon (d); + return 64; + } + kill_curl (curl); + sleep (1); + // fprintf (stderr, "Stopping daemon!\n"); + MHD_stop_daemon (d); + if (ok != 0) + return 32; + + return 0; +} + +static int +testMultithreadedPoolGet () +{ + struct MHD_Daemon *d; + pid_t curl; + + d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, + 1081, NULL, NULL, &ahc_echo, "GET", + MHD_OPTION_THREAD_POOL_SIZE, 4, MHD_OPTION_END); + if (d == NULL) + return 64; + ok = 1; + curl = fork_curl ("http://localhost:1081/"); + sleep (1); + kill_curl (curl); + sleep (1); + // fprintf (stderr, "Stopping daemon!\n"); + MHD_stop_daemon (d); + if (ok != 0) + return 128; + return 0; +} + +static int +testExternalGet () +{ + struct MHD_Daemon *d; + fd_set rs; + fd_set ws; + fd_set es; + int max; + time_t start; + struct timeval tv; + pid_t curl; + + d = MHD_start_daemon (MHD_USE_DEBUG, + 1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END); + if (d == NULL) + return 256; + curl = fork_curl ("http://localhost:1082/"); + + start = time (NULL); + while ((time (NULL) - start < 2)) + { + max = 0; + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); + if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) + { + MHD_stop_daemon (d); + return 4096; + } + tv.tv_sec = 0; + tv.tv_usec = 1000; + select (max + 1, &rs, &ws, &es, &tv); + MHD_run (d); + } + kill_curl (curl); + start = time (NULL); + while ((time (NULL) - start < 2)) + { + max = 0; + FD_ZERO (&rs); + FD_ZERO (&ws); + FD_ZERO (&es); + if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) + { + MHD_stop_daemon (d); + return 4096; + } + tv.tv_sec = 0; + tv.tv_usec = 1000; + select (max + 1, &rs, &ws, &es, &tv); + MHD_run (d); + } + // fprintf (stderr, "Stopping daemon!\n"); + MHD_stop_daemon (d); + if (ok != 0) + return 1024; + return 0; +} + + +int +main (int argc, char *const *argv) +{ + unsigned int errorCount = 0; + + oneone = NULL != strstr (argv[0], "11"); + errorCount += testInternalGet (); + errorCount += testMultithreadedGet (); + errorCount += testMultithreadedPoolGet (); + errorCount += testExternalGet (); + if (errorCount != 0) + fprintf (stderr, "Error (code: %u)\n", errorCount); + return errorCount != 0; /* 0 == pass */ +}