Christian Grothoff 18 роки тому
батько
коміт
5e9c5b7621

+ 10 - 1
configure.ac

@@ -134,6 +134,12 @@ then
  AC_DEFINE([HAVE_MESSAGES],[1],[Include error messages])
 fi
 
+# optional: have zzuf, socat?
+AC_CHECK_PROG([HAVE_ZZUF],[zzuf], 1, 0)
+AC_CHECK_PROG([HAVE_SOCAT],[socat], 1, 0)
+
+AM_CONDITIONAL(HAVE_ZZUF, test 0 != $HAVE_ZZUF)
+AM_CONDITIONAL(HAVE_SOCAT, test 0 != $HAVE_SOCAT)
 
 AC_SUBST(CPPFLAGS)
 AC_SUBST(LIBS)
@@ -147,7 +153,10 @@ doc/Makefile
 m4/Makefile
 src/Makefile
 src/include/Makefile
-src/daemon/Makefile])
+src/daemon/Makefile
+src/examples/Makefile
+src/testcurl/Makefile
+src/testzzuf/Makefile])
 AC_OUTPUT
 
 if test "$curl" != 1

Різницю між файлами не показано, бо вона завелика
+ 604 - 164
doc/texinfo.tex


+ 9 - 1
src/Makefile.am

@@ -1 +1,9 @@
-SUBDIRS = include daemon . 
+if HAVE_CURL
+curltests = testcurl
+if HAVE_ZZUF
+if HAVE_SOCAT
+zzuftests = testzzuf
+endif
+endif
+endif
+SUBDIRS = include daemon examples $(curltests) $(zzuftests) . 

+ 2 - 128
src/daemon/Makefile.am

@@ -23,42 +23,9 @@ libmicrohttpd_la_SOURCES = \
   postprocessor.c \
   response.c response.h 
 
-# example programs
-
-noinst_PROGRAMS = minimal_example fileserver_example
-
-minimal_example_SOURCES = \
- minimal_example.c 
-minimal_example_LDADD = \
- $(top_builddir)/src/daemon/libmicrohttpd.la 
-
-fileserver_example_SOURCES = \
- fileserver_example.c 
-fileserver_example_LDADD = \
- $(top_builddir)/src/daemon/libmicrohttpd.la 
-
-
-# No curl, no testcases
-if HAVE_CURL
-
 check_PROGRAMS = \
-  postprocessor_test \
-  daemontest \
-  daemontest_get \
-  daemontest_post \
-  daemontest_postform \
-  daemontest_post_loop \
-  daemontest_put \
-  daemontest_large_put \
-  daemontest_get11 \
-  daemontest_post11 \
-  daemontest_postform11 \
-  daemontest_post_loop11 \
-  daemontest_put11 \
-  daemontest_large_put11 \
-  daemontest_long_header \
-  daemontest_get_chunked \
-  daemontest_put_chunked 
+  postprocessor_test 
+  daemontest 
 
 TESTS = $(check_PROGRAMS)
 
@@ -71,96 +38,3 @@ postprocessor_test_SOURCES = \
   postprocessor_test.c
 postprocessor_test_LDADD = \
   $(top_builddir)/src/daemon/libmicrohttpd.la 
-
-daemontest_get_SOURCES = \
-  daemontest_get.c
-daemontest_get_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_get_chunked_SOURCES = \
-  daemontest_get_chunked.c
-daemontest_get_chunked_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_post_SOURCES = \
-  daemontest_post.c
-daemontest_post_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_postform_SOURCES = \
-  daemontest_postform.c
-daemontest_postform_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_post_loop_SOURCES = \
-  daemontest_post_loop.c
-daemontest_post_loop_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_put_SOURCES = \
-  daemontest_put.c
-daemontest_put_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_put_chunked_SOURCES = \
-  daemontest_put_chunked.c
-daemontest_put_chunked_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_get11_SOURCES = \
-  daemontest_get.c
-daemontest_get11_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_post11_SOURCES = \
-  daemontest_post.c
-daemontest_post11_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_postform11_SOURCES = \
-  daemontest_postform.c
-daemontest_postform11_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_post_loop11_SOURCES = \
-  daemontest_post_loop.c
-daemontest_post_loop11_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_put11_SOURCES = \
-  daemontest_put.c
-daemontest_put11_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-
-daemontest_large_put_SOURCES = \
-  daemontest_large_put.c
-daemontest_large_put_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_large_put11_SOURCES = \
-  daemontest_large_put.c
-daemontest_large_put11_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-daemontest_long_header_SOURCES = \
-  daemontest_long_header.c
-daemontest_long_header_LDADD = \
-  $(top_builddir)/src/daemon/libmicrohttpd.la \
-  @LIBCURL@ 
-
-endif

+ 18 - 0
src/examples/Makefile.am

@@ -0,0 +1,18 @@
+SUBDIRS  = .
+
+INCLUDES = -I$(top_srcdir)/src/include
+
+# example programs
+
+noinst_PROGRAMS = minimal_example fileserver_example
+
+minimal_example_SOURCES = \
+ minimal_example.c 
+minimal_example_LDADD = \
+ $(top_builddir)/src/daemon/libmicrohttpd.la 
+
+fileserver_example_SOURCES = \
+ fileserver_example.c 
+fileserver_example_LDADD = \
+ $(top_builddir)/src/daemon/libmicrohttpd.la 
+

+ 0 - 0
src/daemon/fileserver_example.c → src/examples/fileserver_example.c


+ 0 - 0
src/daemon/minimal_example.c → src/examples/minimal_example.c


+ 126 - 0
src/testcurl/Makefile.am

@@ -0,0 +1,126 @@
+SUBDIRS  = .
+
+INCLUDES = -I$(top_srcdir)/src/include
+
+check_PROGRAMS = \
+  postprocessor_test \
+  daemontest \
+  daemontest_get \
+  daemontest_post \
+  daemontest_postform \
+  daemontest_post_loop \
+  daemontest_put \
+  daemontest_large_put \
+  daemontest_get11 \
+  daemontest_post11 \
+  daemontest_postform11 \
+  daemontest_post_loop11 \
+  daemontest_put11 \
+  daemontest_large_put11 \
+  daemontest_long_header \
+  daemontest_get_chunked \
+  daemontest_put_chunked 
+
+TESTS = $(check_PROGRAMS)
+
+daemontest_SOURCES = \
+  daemontest.c
+daemontest_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la 
+
+postprocessor_test_SOURCES = \
+  postprocessor_test.c
+postprocessor_test_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la 
+
+daemontest_get_SOURCES = \
+  daemontest_get.c
+daemontest_get_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_get_chunked_SOURCES = \
+  daemontest_get_chunked.c
+daemontest_get_chunked_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_post_SOURCES = \
+  daemontest_post.c
+daemontest_post_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_postform_SOURCES = \
+  daemontest_postform.c
+daemontest_postform_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_post_loop_SOURCES = \
+  daemontest_post_loop.c
+daemontest_post_loop_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_put_SOURCES = \
+  daemontest_put.c
+daemontest_put_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_put_chunked_SOURCES = \
+  daemontest_put_chunked.c
+daemontest_put_chunked_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_get11_SOURCES = \
+  daemontest_get.c
+daemontest_get11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_post11_SOURCES = \
+  daemontest_post.c
+daemontest_post11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_postform11_SOURCES = \
+  daemontest_postform.c
+daemontest_postform11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_post_loop11_SOURCES = \
+  daemontest_post_loop.c
+daemontest_post_loop11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_put11_SOURCES = \
+  daemontest_put.c
+daemontest_put11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+
+daemontest_large_put_SOURCES = \
+  daemontest_large_put.c
+daemontest_large_put_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_large_put11_SOURCES = \
+  daemontest_large_put.c
+daemontest_large_put11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_long_header_SOURCES = \
+  daemontest_long_header.c
+daemontest_long_header_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+

+ 0 - 0
src/daemon/daemontest.c → src/testcurl/daemontest.c


+ 0 - 0
src/daemon/daemontest_get.c → src/testcurl/daemontest_get.c


+ 0 - 0
src/daemon/daemontest_get_chunked.c → src/testcurl/daemontest_get_chunked.c


+ 0 - 0
src/daemon/daemontest_large_put.c → src/testcurl/daemontest_large_put.c


+ 0 - 0
src/daemon/daemontest_long_header.c → src/testcurl/daemontest_long_header.c


+ 0 - 0
src/daemon/daemontest_put.c → src/testcurl/daemontest_put.c


+ 0 - 0
src/daemon/daemontest_put_chunked.c → src/testcurl/daemontest_put_chunked.c


+ 125 - 0
src/testzzuf/Makefile.am

@@ -0,0 +1,125 @@
+SUBDIRS  = .
+
+INCLUDES = -I$(top_srcdir)/src/include
+
+check_PROGRAMS = \
+  postprocessor_test \
+  daemontest \
+  daemontest_get \
+  daemontest_post \
+  daemontest_postform \
+  daemontest_post_loop \
+  daemontest_put \
+  daemontest_large_put \
+  daemontest_get11 \
+  daemontest_post11 \
+  daemontest_postform11 \
+  daemontest_post_loop11 \
+  daemontest_put11 \
+  daemontest_large_put11 \
+  daemontest_long_header \
+  daemontest_get_chunked \
+  daemontest_put_chunked 
+
+TESTS = $(check_PROGRAMS)
+
+daemontest_SOURCES = \
+  daemontest.c
+daemontest_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la 
+
+postprocessor_test_SOURCES = \
+  postprocessor_test.c
+postprocessor_test_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la 
+
+daemontest_get_SOURCES = \
+  daemontest_get.c
+daemontest_get_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_get_chunked_SOURCES = \
+  daemontest_get_chunked.c
+daemontest_get_chunked_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_post_SOURCES = \
+  daemontest_post.c
+daemontest_post_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_postform_SOURCES = \
+  daemontest_postform.c
+daemontest_postform_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_post_loop_SOURCES = \
+  daemontest_post_loop.c
+daemontest_post_loop_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_put_SOURCES = \
+  daemontest_put.c
+daemontest_put_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_put_chunked_SOURCES = \
+  daemontest_put_chunked.c
+daemontest_put_chunked_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_get11_SOURCES = \
+  daemontest_get.c
+daemontest_get11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_post11_SOURCES = \
+  daemontest_post.c
+daemontest_post11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_postform11_SOURCES = \
+  daemontest_postform.c
+daemontest_postform11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_post_loop11_SOURCES = \
+  daemontest_post_loop.c
+daemontest_post_loop11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_put11_SOURCES = \
+  daemontest_put.c
+daemontest_put11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+
+daemontest_large_put_SOURCES = \
+  daemontest_large_put.c
+daemontest_large_put_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_large_put11_SOURCES = \
+  daemontest_large_put.c
+daemontest_large_put11_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 
+
+daemontest_long_header_SOURCES = \
+  daemontest_long_header.c
+daemontest_long_header_LDADD = \
+  $(top_builddir)/src/daemon/libmicrohttpd.la \
+  @LIBCURL@ 

+ 163 - 0
src/testzzuf/daemontest.c

@@ -0,0 +1,163 @@
+/*
+     This file is part of libmicrohttpd
+     (C) 2007 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.c
+ * @brief  Testcase for libmicrohttpd starts and stops
+ * @author Christian Grothoff
+ */
+
+#include "config.h"
+#include "microhttpd.h"
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#ifndef WINDOWS
+#include <unistd.h>
+#endif
+
+
+static int
+testStartError ()
+{
+  struct MHD_Daemon *d;
+
+  d = MHD_start_daemon (MHD_USE_DEBUG, 0, NULL, NULL, NULL, NULL);
+  if (d != NULL)
+    return 1;
+  return 0;
+}
+
+static int
+apc_nothing (void *cls, const struct sockaddr *addr, socklen_t addrlen)
+{
+  return MHD_NO;
+}
+
+static int
+apc_all (void *cls, const struct sockaddr *addr, socklen_t addrlen)
+{
+  return MHD_YES;
+}
+
+static int
+ahc_nothing (void *cls,
+             struct MHD_Connection *connection,
+             const char *url,
+             const char *method,
+             const char *version,
+             const char *upload_data, unsigned int *upload_data_size,
+             void **unused)
+{
+  return MHD_NO;
+}
+
+static int
+testStartStop ()
+{
+  struct MHD_Daemon *d;
+
+  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
+                        1080,
+                        &apc_nothing,
+                        NULL, &ahc_nothing, NULL, MHD_OPTION_END);
+  if (d == NULL)
+    return 2;
+  MHD_stop_daemon (d);
+  return 0;
+}
+
+static int
+testExternalRun ()
+{
+  struct MHD_Daemon *d;
+  fd_set rs;
+  int maxfd;
+  int i;
+
+  d = MHD_start_daemon (MHD_USE_DEBUG,
+                        1081,
+                        &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END);
+
+  if (d == NULL)
+    return 4;
+  i = 0;
+  while (i < 15)
+    {
+      maxfd = 0;
+      FD_ZERO (&rs);
+      MHD_get_fdset (d, &rs, &rs, &rs, &maxfd);
+      if (MHD_run (d) == MHD_NO)
+        {
+          MHD_stop_daemon (d);
+          return 8;
+        }
+      i++;
+    }
+  MHD_stop_daemon (d);
+  return 0;
+}
+
+static int
+testThread ()
+{
+  struct MHD_Daemon *d;
+  d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SELECT_INTERNALLY,
+                        1082,
+                        &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END);
+
+  if (d == NULL)
+    return 16;
+  if (MHD_run (d) != MHD_NO)
+    return 32;
+  MHD_stop_daemon (d);
+  return 0;
+}
+
+static int
+testMultithread ()
+{
+  struct MHD_Daemon *d;
+  d = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_THREAD_PER_CONNECTION,
+                        1083,
+                        &apc_all, NULL, &ahc_nothing, NULL, MHD_OPTION_END);
+
+  if (d == NULL)
+    return 64;
+  if (MHD_run (d) != MHD_NO)
+    return 128;
+  MHD_stop_daemon (d);
+  return 0;
+}
+
+int
+main (int argc, char *const *argv)
+{
+  unsigned int errorCount = 0;
+  errorCount += testStartError ();
+  errorCount += testStartStop ();
+  errorCount += testExternalRun ();
+  errorCount += testThread ();
+  errorCount += testMultithread ();
+  if (errorCount != 0)
+    fprintf (stderr, "Error (code: %u)\n", errorCount);
+  return errorCount != 0;       /* 0 == pass */
+}

+ 330 - 0
src/testzzuf/daemontest_get.c

@@ -0,0 +1,330 @@
+/*
+     This file is part of libmicrohttpd
+     (C) 2007 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.c
+ * @brief  Testcase for libmicrohttpd GET operations
+ *         TODO: test parsing of query
+ * @author Christian Grothoff
+ */
+
+#include "config.h"
+#include <curl/curl.h>
+#include <microhttpd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#ifndef WINDOWS
+#include <unistd.h>
+#endif
+
+static int oneone;
+
+struct CBC
+{
+  char *buf;
+  size_t pos;
+  size_t size;
+};
+
+static size_t
+copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
+{
+  struct CBC *cbc = ctx;
+
+  if (cbc->pos + size * nmemb > cbc->size)
+    return 0;                   /* overflow */
+  memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
+  cbc->pos += size * nmemb;
+  return size * nmemb;
+}
+
+static int
+ahc_echo (void *cls,
+          struct MHD_Connection *connection,
+          const char *url,
+          const char *method,
+          const char *version,
+          const char *upload_data, unsigned int *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_data (strlen (url),
+                                            (void *) url, MHD_NO, MHD_YES);
+  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;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  CURLcode errornum;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  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;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11080/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 2;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 4;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 8;
+  return 0;
+}
+
+static int
+testMultithreadedGet ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  CURLcode errornum;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
+                        1081, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
+  if (d == NULL)
+    return 16;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 32;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 64;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 128;
+  return 0;
+}
+
+
+static int
+testExternalGet ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  CURLM *multi;
+  CURLMcode mret;
+  fd_set rs;
+  fd_set ws;
+  fd_set es;
+  int max;
+  int running;
+  struct CURLMsg *msg;
+  time_t start;
+  struct timeval tv;
+
+  multi = NULL;
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_DEBUG,
+                        1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
+  if (d == NULL)
+    return 256;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+
+
+  multi = curl_multi_init ();
+  if (multi == NULL)
+    {
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 512;
+    }
+  mret = curl_multi_add_handle (multi, c);
+  if (mret != CURLM_OK)
+    {
+      curl_multi_cleanup (multi);
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 1024;
+    }
+  start = time (NULL);
+  while ((time (NULL) - start < 5) && (multi != NULL))
+    {
+      max = 0;
+      FD_ZERO (&rs);
+      FD_ZERO (&ws);
+      FD_ZERO (&es);
+      curl_multi_perform (multi, &running);
+      mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
+      if (mret != CURLM_OK)
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 2048;
+        }
+      if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 4096;
+        }
+      tv.tv_sec = 0;
+      tv.tv_usec = 1000;
+      select (max + 1, &rs, &ws, &es, &tv);
+      curl_multi_perform (multi, &running);
+      if (running == 0)
+        {
+          msg = curl_multi_info_read (multi, &running);
+          if (msg == NULL)
+            break;
+          if (msg->msg == CURLMSG_DONE)
+            {
+              if (msg->data.result != CURLE_OK)
+                printf ("%s failed at %s:%d: `%s'\n",
+                        "curl_multi_perform",
+                        __FILE__,
+                        __LINE__, curl_easy_strerror (msg->data.result));
+              curl_multi_remove_handle (multi, c);
+              curl_multi_cleanup (multi);
+              curl_easy_cleanup (c);
+              c = NULL;
+              multi = NULL;
+            }
+        }
+      MHD_run (d);
+    }
+  if (multi != NULL)
+    {
+      curl_multi_remove_handle (multi, c);
+      curl_easy_cleanup (c);
+      curl_multi_cleanup (multi);
+    }
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 8192;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 16384;
+  return 0;
+}
+
+
+
+int
+main (int argc, char *const *argv)
+{
+  unsigned int errorCount = 0;
+
+  oneone = NULL != strstr (argv[0], "11");
+  if (0 != curl_global_init (CURL_GLOBAL_WIN32))
+    return 2;
+  errorCount += testInternalGet ();
+  errorCount += testMultithreadedGet ();
+  errorCount += testExternalGet ();
+  if (errorCount != 0)
+    fprintf (stderr, "Error (code: %u)\n", errorCount);
+  curl_global_cleanup ();
+  return errorCount != 0;       /* 0 == pass */
+}

+ 364 - 0
src/testzzuf/daemontest_get_chunked.c

@@ -0,0 +1,364 @@
+/*
+     This file is part of libmicrohttpd
+     (C) 2007 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_chunked.c
+ * @brief  Testcase for libmicrohttpd GET operations with chunked content encoding
+ *         TODO:
+ *         - how to test that chunking was actually used?
+ *         - use CURLOPT_HEADERFUNCTION to validate
+ *           footer was sent
+ * @author Christian Grothoff
+ */
+
+#include "config.h"
+#include <curl/curl.h>
+#include <microhttpd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#ifndef WINDOWS
+#include <unistd.h>
+#endif
+
+struct CBC
+{
+  char *buf;
+  size_t pos;
+  size_t size;
+};
+
+static size_t
+copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
+{
+  struct CBC *cbc = ctx;
+
+  if (cbc->pos + size * nmemb > cbc->size)
+    return 0;                   /* overflow */
+  memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
+  cbc->pos += size * nmemb;
+  return size * nmemb;
+}
+
+/**
+ * MHD content reader callback that returns
+ * data in chunks.
+ */
+static int
+crc (void *cls, size_t pos, char *buf, int max)
+{
+  struct MHD_Response **responseptr = cls;
+
+  if (pos == 128 * 10)
+    {
+      MHD_add_response_header (*responseptr, "Footer", "working");
+      return -1;                /* end of stream */
+    }
+  if (max < 128)
+    abort ();                   /* should not happen in this testcase... */
+  memset (buf, 'A' + (pos / 128), 128);
+  return 128;
+}
+
+/**
+ * Dummy function that does nothing.
+ */
+static void
+crcf (void *ptr)
+{
+  free (ptr);
+}
+
+static int
+ahc_echo (void *cls,
+          struct MHD_Connection *connection,
+          const char *url,
+          const char *method,
+          const char *version,
+          const char *upload_data, unsigned int *upload_data_size, void **ptr)
+{
+  static int aptr;
+  const char *me = cls;
+  struct MHD_Response *response;
+  struct MHD_Response **responseptr;
+  int ret;
+
+  if (0 != strcmp (me, method))
+    return MHD_NO;              /* unexpected method */
+  if (&aptr != *ptr)
+    {
+      /* do never respond on first call */
+      *ptr = &aptr;
+      return MHD_YES;
+    }
+  responseptr = malloc (sizeof (struct MHD_Response *));
+  response = MHD_create_response_from_callback (-1,
+                                                1024,
+                                                &crc, responseptr, &crcf);
+  *responseptr = response;
+  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
+  MHD_destroy_response (response);
+  return ret;
+}
+
+static int
+validate (struct CBC cbc, int ebase)
+{
+  int i;
+  char buf[128];
+
+  if (cbc.pos != 128 * 10)
+    return ebase;
+
+  for (i = 0; i < 10; i++)
+    {
+      memset (buf, 'A' + i, 128);
+      if (0 != memcmp (buf, &cbc.buf[i * 128], 128))
+        {
+          fprintf (stderr,
+                   "Got  `%.*s'\nWant `%.*s'\n",
+                   128, buf, 128, &cbc.buf[i * 128]);
+          return ebase * 2;
+        }
+    }
+  return 0;
+}
+
+static int
+testInternalGet ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  CURLcode errornum;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
+                        1080, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
+  if (d == NULL)
+    return 1;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 2;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  return validate (cbc, 4);
+}
+
+static int
+testMultithreadedGet ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  CURLcode errornum;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
+                        1081, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
+  if (d == NULL)
+    return 16;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 32;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  return validate (cbc, 64);
+}
+
+
+static int
+testExternalGet ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  CURLM *multi;
+  CURLMcode mret;
+  fd_set rs;
+  fd_set ws;
+  fd_set es;
+  int max;
+  int running;
+  struct CURLMsg *msg;
+  time_t start;
+  struct timeval tv;
+
+  multi = NULL;
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_DEBUG,
+                        1082, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
+  if (d == NULL)
+    return 256;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+
+
+  multi = curl_multi_init ();
+  if (multi == NULL)
+    {
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 512;
+    }
+  mret = curl_multi_add_handle (multi, c);
+  if (mret != CURLM_OK)
+    {
+      curl_multi_cleanup (multi);
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 1024;
+    }
+  start = time (NULL);
+  while ((time (NULL) - start < 5) && (multi != NULL))
+    {
+      max = 0;
+      FD_ZERO (&rs);
+      FD_ZERO (&ws);
+      FD_ZERO (&es);
+      curl_multi_perform (multi, &running);
+      mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
+      if (mret != CURLM_OK)
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 2048;
+        }
+      if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 4096;
+        }
+      tv.tv_sec = 0;
+      tv.tv_usec = 1000;
+      select (max + 1, &rs, &ws, &es, &tv);
+      curl_multi_perform (multi, &running);
+      if (running == 0)
+        {
+          msg = curl_multi_info_read (multi, &running);
+          if (msg == NULL)
+            break;
+          if (msg->msg == CURLMSG_DONE)
+            {
+              if (msg->data.result != CURLE_OK)
+                printf ("%s failed at %s:%d: `%s'\n",
+                        "curl_multi_perform",
+                        __FILE__,
+                        __LINE__, curl_easy_strerror (msg->data.result));
+              curl_multi_remove_handle (multi, c);
+              curl_multi_cleanup (multi);
+              curl_easy_cleanup (c);
+              c = NULL;
+              multi = NULL;
+            }
+        }
+      MHD_run (d);
+    }
+  if (multi != NULL)
+    {
+      curl_multi_remove_handle (multi, c);
+      curl_easy_cleanup (c);
+      curl_multi_cleanup (multi);
+    }
+  MHD_stop_daemon (d);
+  return validate (cbc, 8192);
+}
+
+
+
+int
+main (int argc, char *const *argv)
+{
+  unsigned int errorCount = 0;
+
+  if (0 != curl_global_init (CURL_GLOBAL_WIN32))
+    return 2;
+  errorCount += testInternalGet ();
+  if (0)
+    {
+      errorCount += testMultithreadedGet ();
+      errorCount += testExternalGet ();
+    }
+  if (errorCount != 0)
+    fprintf (stderr, "Error (code: %u)\n", errorCount);
+  curl_global_cleanup ();
+  return errorCount != 0;       /* 0 == pass */
+}

+ 404 - 0
src/testzzuf/daemontest_large_put.c

@@ -0,0 +1,404 @@
+/*
+     This file is part of libmicrohttpd
+     (C) 2007 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_put.c
+ * @brief  Testcase for libmicrohttpd PUT operations
+ * @author Christian Grothoff
+ */
+
+#include "config.h"
+#include <curl/curl.h>
+#include <microhttpd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#ifndef WINDOWS
+#include <unistd.h>
+#endif
+
+static int oneone;
+
+/**
+ * Do not make this much larger since we will hit the
+ * MHD default buffer limit and the test code is not
+ * written for incremental upload processing...
+ */
+#define PUT_SIZE (512 * 1024)
+
+static char *put_buffer;
+
+struct CBC
+{
+  char *buf;
+  size_t pos;
+  size_t size;
+};
+
+static size_t
+putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
+{
+  unsigned int *pos = ptr;
+  unsigned int wrt;
+
+  wrt = size * nmemb;
+  if (wrt > PUT_SIZE - (*pos))
+    wrt = PUT_SIZE - (*pos);
+  memcpy (stream, &put_buffer[*pos], wrt);
+  (*pos) += wrt;
+  return wrt;
+}
+
+static size_t
+copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
+{
+  struct CBC *cbc = ctx;
+
+  if (cbc->pos + size * nmemb > cbc->size)
+    return 0;                   /* overflow */
+  memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
+  cbc->pos += size * nmemb;
+  return size * nmemb;
+}
+
+static int
+ahc_echo (void *cls,
+          struct MHD_Connection *connection,
+          const char *url,
+          const char *method,
+          const char *version,
+          const char *upload_data, unsigned int *upload_data_size,
+          void **unused)
+{
+  int *done = cls;
+  struct MHD_Response *response;
+  int ret;
+
+  if (0 != strcmp ("PUT", method))
+    return MHD_NO;              /* unexpected method */
+  if ((*done) == 0)
+    {
+      if (*upload_data_size != PUT_SIZE)
+        {
+#if 0
+          fprintf (stderr,
+                   "Waiting for more data (%u/%u)...\n",
+                   *upload_data_size, PUT_SIZE);
+#endif
+          return MHD_YES;       /* not yet ready */
+        }
+      if (0 == memcmp (upload_data, put_buffer, PUT_SIZE))
+        {
+          *upload_data_size = 0;
+        }
+      else
+        {
+          printf ("Invalid upload data!\n");
+          return MHD_NO;
+        }
+      *done = 1;
+      return MHD_YES;
+    }
+  response = MHD_create_response_from_data (strlen (url),
+                                            (void *) url, MHD_NO, MHD_YES);
+  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
+  MHD_destroy_response (response);
+  return ret;
+}
+
+
+static int
+testInternalPut ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  struct CBC cbc;
+  unsigned int pos = 0;
+  int done_flag = 0;
+  CURLcode errornum;
+  char buf[2048];
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
+                        1080,
+                        NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
+  if (d == NULL)
+    return 1;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
+  curl_easy_setopt (c, CURLOPT_READDATA, &pos);
+  curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
+  curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 2;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 4;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 8;
+  return 0;
+}
+
+static int
+testMultithreadedPut ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  struct CBC cbc;
+  unsigned int pos = 0;
+  int done_flag = 0;
+  CURLcode errornum;
+  char buf[2048];
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
+                        1081,
+                        NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
+  if (d == NULL)
+    {
+      free (cbc.buf);
+      return 16;
+    }
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
+  curl_easy_setopt (c, CURLOPT_READDATA, &pos);
+  curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
+  curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 32;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    {
+      fprintf (stderr, "Got invalid response `%.*s'\n", cbc.pos, cbc.buf);
+      return 64;
+    }
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 128;
+  return 0;
+}
+
+
+static int
+testExternalPut ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  struct CBC cbc;
+  CURLM *multi;
+  CURLMcode mret;
+  fd_set rs;
+  fd_set ws;
+  fd_set es;
+  int max;
+  int running;
+  struct CURLMsg *msg;
+  time_t start;
+  struct timeval tv;
+  unsigned int pos = 0;
+  int done_flag = 0;
+  char buf[2048];
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  multi = NULL;
+  d = MHD_start_daemon (MHD_USE_DEBUG,
+                        1082,
+                        NULL, NULL, &ahc_echo, &done_flag,
+                        MHD_OPTION_CONNECTION_MEMORY_LIMIT,
+                        PUT_SIZE * 4, MHD_OPTION_END);
+  if (d == NULL)
+    return 256;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
+  curl_easy_setopt (c, CURLOPT_READDATA, &pos);
+  curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
+  curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+
+
+  multi = curl_multi_init ();
+  if (multi == NULL)
+    {
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 512;
+    }
+  mret = curl_multi_add_handle (multi, c);
+  if (mret != CURLM_OK)
+    {
+      curl_multi_cleanup (multi);
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 1024;
+    }
+  start = time (NULL);
+  while ((time (NULL) - start < 5) && (multi != NULL))
+    {
+      max = 0;
+      FD_ZERO (&rs);
+      FD_ZERO (&ws);
+      FD_ZERO (&es);
+      curl_multi_perform (multi, &running);
+      mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
+      if (mret != CURLM_OK)
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 2048;
+        }
+      if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 4096;
+        }
+      tv.tv_sec = 0;
+      tv.tv_usec = 1000;
+      select (max + 1, &rs, &ws, &es, &tv);
+      curl_multi_perform (multi, &running);
+      if (running == 0)
+        {
+          msg = curl_multi_info_read (multi, &running);
+          if (msg == NULL)
+            break;
+          if (msg->msg == CURLMSG_DONE)
+            {
+              if (msg->data.result != CURLE_OK)
+                printf ("%s failed at %s:%d: `%s'\n",
+                        "curl_multi_perform",
+                        __FILE__,
+                        __LINE__, curl_easy_strerror (msg->data.result));
+              curl_multi_remove_handle (multi, c);
+              curl_multi_cleanup (multi);
+              curl_easy_cleanup (c);
+              c = NULL;
+              multi = NULL;
+            }
+        }
+      MHD_run (d);
+    }
+  if (multi != NULL)
+    {
+      curl_multi_remove_handle (multi, c);
+      curl_easy_cleanup (c);
+      curl_multi_cleanup (multi);
+    }
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    {
+      fprintf (stderr, "Got invalid response `%.*s'\n", cbc.pos, cbc.buf);
+      return 8192;
+    }
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 16384;
+  return 0;
+}
+
+
+
+int
+main (int argc, char *const *argv)
+{
+  unsigned int errorCount = 0;
+
+  oneone = NULL != strstr (argv[0], "11");
+  if (0 != curl_global_init (CURL_GLOBAL_WIN32))
+    return 2;
+  put_buffer = malloc (PUT_SIZE);
+  memset (put_buffer, 1, PUT_SIZE);
+  if (0)
+    {
+      errorCount += testInternalPut ();
+      errorCount += testMultithreadedPut ();
+    }
+  errorCount += testExternalPut ();
+  free (put_buffer);
+  if (errorCount != 0)
+    fprintf (stderr, "Error (code: %u)\n", errorCount);
+  curl_global_cleanup ();
+  return errorCount != 0;       /* 0 == pass */
+}

+ 240 - 0
src/testzzuf/daemontest_long_header.c

@@ -0,0 +1,240 @@
+/*
+     This file is part of libmicrohttpd
+     (C) 2007 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_long_header.c
+ * @brief  Testcase for libmicrohttpd handling of very long headers
+ * @author Christian Grothoff
+ */
+
+#include "config.h"
+#include <curl/curl.h>
+#include <microhttpd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#ifndef WINDOWS
+#include <unistd.h>
+#endif
+
+/**
+ * We will set the memory available per connection to
+ * half of this value, so the actual value does not have
+ * to be big at all...
+ */
+#define VERY_LONG (1024*10)
+
+static int oneone;
+
+static int
+apc_all (void *cls, const struct sockaddr *addr, socklen_t addrlen)
+{
+  return MHD_YES;
+}
+
+struct CBC
+{
+  char *buf;
+  size_t pos;
+  size_t size;
+};
+
+static size_t
+copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
+{
+  return size * nmemb;
+}
+
+static int
+ahc_echo (void *cls,
+          struct MHD_Connection *connection,
+          const char *url,
+          const char *method,
+          const char *version,
+          const char *upload_data, unsigned int *upload_data_size,
+          void **unused)
+{
+  const char *me = cls;
+  struct MHD_Response *response;
+  int ret;
+
+  if (0 != strcmp (me, method))
+    return MHD_NO;              /* unexpected method */
+  response = MHD_create_response_from_data (strlen (url),
+                                            (void *) url, MHD_NO, MHD_YES);
+  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
+  MHD_destroy_response (response);
+  return ret;
+}
+
+
+static int
+testLongUrlGet ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  char *url;
+  long code;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */ ,
+                        1080,
+                        &apc_all,
+                        NULL,
+                        &ahc_echo,
+                        "GET",
+                        MHD_OPTION_CONNECTION_MEMORY_LIMIT,
+                        VERY_LONG / 2, MHD_OPTION_END);
+  if (d == NULL)
+    return 1;
+  c = curl_easy_init ();
+  url = malloc (VERY_LONG);
+  memset (url, 'a', VERY_LONG);
+  url[VERY_LONG - 1] = '\0';
+  memcpy (url, "http://localhost:1080/", strlen ("http://localhost:1080/"));
+  curl_easy_setopt (c, CURLOPT_URL, url);
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK == curl_easy_perform (c))
+    {
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      free (url);
+      return 2;
+    }
+  if (CURLE_OK != curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &code))
+    {
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      free (url);
+      return 4;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  free (url);
+  if (code != MHD_HTTP_REQUEST_URI_TOO_LONG)
+    return 8;
+  return 0;
+}
+
+
+static int
+testLongHeaderGet ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  char *url;
+  long code;
+  struct curl_slist *header = NULL;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY /* | MHD_USE_DEBUG */ ,
+                        1080,
+                        &apc_all,
+                        NULL,
+                        &ahc_echo,
+                        "GET",
+                        MHD_OPTION_CONNECTION_MEMORY_LIMIT,
+                        VERY_LONG / 2, MHD_OPTION_END);
+  if (d == NULL)
+    return 16;
+  c = curl_easy_init ();
+  url = malloc (VERY_LONG);
+  memset (url, 'a', VERY_LONG);
+  url[VERY_LONG - 1] = '\0';
+  url[VERY_LONG / 2] = ':';
+  url[VERY_LONG / 2 + 1] = ':';
+  header = curl_slist_append (header, url);
+
+  curl_easy_setopt (c, CURLOPT_HTTPHEADER, header);
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK == curl_easy_perform (c))
+    {
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      curl_slist_free_all (header);
+      free (url);
+      return 32;
+    }
+  if (CURLE_OK != curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &code))
+    {
+      curl_slist_free_all (header);
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      free (url);
+      return 64;
+    }
+  curl_slist_free_all (header);
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  free (url);
+  if (code != MHD_HTTP_REQUEST_ENTITY_TOO_LARGE)
+    return 128;
+  return 0;
+}
+
+int
+main (int argc, char *const *argv)
+{
+  unsigned int errorCount = 0;
+
+  oneone = NULL != strstr (argv[0], "11");
+  if (0 != curl_global_init (CURL_GLOBAL_WIN32))
+    return 2;
+  errorCount += testLongUrlGet ();
+  errorCount += testLongHeaderGet ();
+  if (errorCount != 0)
+    fprintf (stderr, "Error (code: %u)\n", errorCount);
+  curl_global_cleanup ();
+  return errorCount != 0;       /* 0 == pass */
+}

+ 375 - 0
src/testzzuf/daemontest_put.c

@@ -0,0 +1,375 @@
+/*
+     This file is part of libmicrohttpd
+     (C) 2007 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_put.c
+ * @brief  Testcase for libmicrohttpd PUT operations
+ * @author Christian Grothoff
+ */
+
+#include "config.h"
+#include <curl/curl.h>
+#include <microhttpd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#ifndef WINDOWS
+#include <unistd.h>
+#endif
+
+static int oneone;
+
+struct CBC
+{
+  char *buf;
+  size_t pos;
+  size_t size;
+};
+
+static size_t
+putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
+{
+  unsigned int *pos = ptr;
+  unsigned int wrt;
+
+  wrt = size * nmemb;
+  if (wrt > 8 - (*pos))
+    wrt = 8 - (*pos);
+  memcpy (stream, &("Hello123"[*pos]), wrt);
+  (*pos) += wrt;
+  return wrt;
+}
+
+static size_t
+copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
+{
+  struct CBC *cbc = ctx;
+
+  if (cbc->pos + size * nmemb > cbc->size)
+    return 0;                   /* overflow */
+  memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
+  cbc->pos += size * nmemb;
+  return size * nmemb;
+}
+
+static int
+ahc_echo (void *cls,
+          struct MHD_Connection *connection,
+          const char *url,
+          const char *method,
+          const char *version,
+          const char *upload_data, unsigned int *upload_data_size,
+          void **unused)
+{
+  int *done = cls;
+  struct MHD_Response *response;
+  int ret;
+
+  if (0 != strcmp ("PUT", method))
+    return MHD_NO;              /* unexpected method */
+  if ((*done) == 0)
+    {
+      if (*upload_data_size != 8)
+        return MHD_YES;         /* not yet ready */
+      if (0 == memcmp (upload_data, "Hello123", 8))
+        {
+          *upload_data_size = 0;
+        }
+      else
+        {
+          printf ("Invalid upload data `%8s'!\n", upload_data);
+          return MHD_NO;
+        }
+      *done = 1;
+      return MHD_YES;
+    }
+  response = MHD_create_response_from_data (strlen (url),
+                                            (void *) url, MHD_NO, MHD_YES);
+  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
+  MHD_destroy_response (response);
+  return ret;
+}
+
+
+static int
+testInternalPut ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  unsigned int pos = 0;
+  int done_flag = 0;
+  CURLcode errornum;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
+                        1080,
+                        NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
+  if (d == NULL)
+    return 1;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1080/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
+  curl_easy_setopt (c, CURLOPT_READDATA, &pos);
+  curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
+  curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 2;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 4;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 8;
+  return 0;
+}
+
+static int
+testMultithreadedPut ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  unsigned int pos = 0;
+  int done_flag = 0;
+  CURLcode errornum;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
+                        1081,
+                        NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
+  if (d == NULL)
+    return 16;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1081/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
+  curl_easy_setopt (c, CURLOPT_READDATA, &pos);
+  curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
+  curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 32;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 64;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 128;
+
+  return 0;
+}
+
+
+static int
+testExternalPut ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  CURLM *multi;
+  CURLMcode mret;
+  fd_set rs;
+  fd_set ws;
+  fd_set es;
+  int max;
+  int running;
+  struct CURLMsg *msg;
+  time_t start;
+  struct timeval tv;
+  unsigned int pos = 0;
+  int done_flag = 0;
+
+  multi = NULL;
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_DEBUG,
+                        1082,
+                        NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
+  if (d == NULL)
+    return 256;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:1082/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
+  curl_easy_setopt (c, CURLOPT_READDATA, &pos);
+  curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
+  curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  if (oneone)
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  else
+    curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+
+
+  multi = curl_multi_init ();
+  if (multi == NULL)
+    {
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 512;
+    }
+  mret = curl_multi_add_handle (multi, c);
+  if (mret != CURLM_OK)
+    {
+      curl_multi_cleanup (multi);
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 1024;
+    }
+  start = time (NULL);
+  while ((time (NULL) - start < 5) && (multi != NULL))
+    {
+      max = 0;
+      FD_ZERO (&rs);
+      FD_ZERO (&ws);
+      FD_ZERO (&es);
+      curl_multi_perform (multi, &running);
+      mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
+      if (mret != CURLM_OK)
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 2048;
+        }
+      if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 4096;
+        }
+      tv.tv_sec = 0;
+      tv.tv_usec = 1000;
+      select (max + 1, &rs, &ws, &es, &tv);
+      curl_multi_perform (multi, &running);
+      if (running == 0)
+        {
+          msg = curl_multi_info_read (multi, &running);
+          if (msg == NULL)
+            break;
+          if (msg->msg == CURLMSG_DONE)
+            {
+              if (msg->data.result != CURLE_OK)
+                printf ("%s failed at %s:%d: `%s'\n",
+                        "curl_multi_perform",
+                        __FILE__,
+                        __LINE__, curl_easy_strerror (msg->data.result));
+              curl_multi_remove_handle (multi, c);
+              curl_multi_cleanup (multi);
+              curl_easy_cleanup (c);
+              c = NULL;
+              multi = NULL;
+            }
+        }
+      MHD_run (d);
+    }
+  if (multi != NULL)
+    {
+      curl_multi_remove_handle (multi, c);
+      curl_easy_cleanup (c);
+      curl_multi_cleanup (multi);
+    }
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 8192;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 16384;
+  return 0;
+}
+
+
+
+int
+main (int argc, char *const *argv)
+{
+  unsigned int errorCount = 0;
+
+  oneone = NULL != strstr (argv[0], "11");
+  if (0 != curl_global_init (CURL_GLOBAL_WIN32))
+    return 2;
+  errorCount += testInternalPut ();
+  if (0)
+    {
+      errorCount += testMultithreadedPut ();
+      errorCount += testExternalPut ();
+    }
+  if (errorCount != 0)
+    fprintf (stderr, "Error (code: %u)\n", errorCount);
+  curl_global_cleanup ();
+  return errorCount != 0;       /* 0 == pass */
+}

+ 383 - 0
src/testzzuf/daemontest_put_chunked.c

@@ -0,0 +1,383 @@
+/*
+     This file is part of libmicrohttpd
+     (C) 2007 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_put_chunked.c
+ * @brief Testcase for libmicrohttpd PUT operations with chunked encoding
+ *        for the upload data
+ * @author Christian Grothoff
+ */
+
+#include "config.h"
+#include <curl/curl.h>
+#include <microhttpd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#ifndef WINDOWS
+#include <unistd.h>
+#endif
+
+struct CBC
+{
+  char *buf;
+  size_t pos;
+  size_t size;
+};
+
+static size_t
+putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
+{
+  unsigned int *pos = ptr;
+  unsigned int wrt;
+
+  wrt = size * nmemb;
+  if (wrt > 8 - (*pos))
+    wrt = 8 - (*pos);
+  if (wrt > 4)
+    wrt = 4;                    /* only send half at first => force multiple chunks! */
+  memcpy (stream, &("Hello123"[*pos]), wrt);
+  (*pos) += wrt;
+  return wrt;
+}
+
+static size_t
+copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
+{
+  struct CBC *cbc = ctx;
+
+  if (cbc->pos + size * nmemb > cbc->size)
+    return 0;                   /* overflow */
+  memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
+  cbc->pos += size * nmemb;
+  return size * nmemb;
+}
+
+static int
+ahc_echo (void *cls,
+          struct MHD_Connection *connection,
+          const char *url,
+          const char *method,
+          const char *version,
+          const char *upload_data, unsigned int *upload_data_size,
+          void **unused)
+{
+  int *done = cls;
+  struct MHD_Response *response;
+  int ret;
+  int have;
+
+  if (0 != strcmp ("PUT", method))
+    return MHD_NO;              /* unexpected method */
+  if ((*done) < 8)
+    {
+      have = *upload_data_size;
+      if (have + *done > 8)
+        {
+          printf ("Invalid upload data `%8s'!\n", upload_data);
+          return MHD_NO;
+        }
+      if (0 == memcmp (upload_data, &"Hello123"[*done], have))
+        {
+          *done += have;
+          *upload_data_size = 0;
+        }
+      else
+        {
+          printf ("Invalid upload data `%8s'!\n", upload_data);
+          return MHD_NO;
+        }
+#if 0
+      fprintf (stderr, "Not ready for response: %u/%u\n", *done, 8);
+#endif
+      return MHD_YES;
+    }
+  response = MHD_create_response_from_data (strlen (url),
+                                            (void *) url, MHD_NO, MHD_YES);
+  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
+  MHD_destroy_response (response);
+  return ret;
+}
+
+
+static int
+testInternalPut ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  unsigned int pos = 0;
+  int done_flag = 0;
+  CURLcode errornum;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
+                        11080,
+                        NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
+  if (d == NULL)
+    return 1;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11080/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
+  curl_easy_setopt (c, CURLOPT_READDATA, &pos);
+  curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
+  /*
+     // by not giving the file size, we force chunking!
+     curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
+   */
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 2;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 4;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 8;
+  return 0;
+}
+
+static int
+testMultithreadedPut ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  unsigned int pos = 0;
+  int done_flag = 0;
+  CURLcode errornum;
+
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
+                        11081,
+                        NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
+  if (d == NULL)
+    return 16;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11081/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
+  curl_easy_setopt (c, CURLOPT_READDATA, &pos);
+  curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
+  /*
+     // by not giving the file size, we force chunking!
+     curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
+   */
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+  if (CURLE_OK != (errornum = curl_easy_perform (c)))
+    {
+      fprintf (stderr,
+               "curl_easy_perform failed: `%s'\n",
+               curl_easy_strerror (errornum));
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 32;
+    }
+  curl_easy_cleanup (c);
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 64;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 128;
+
+  return 0;
+}
+
+
+static int
+testExternalPut ()
+{
+  struct MHD_Daemon *d;
+  CURL *c;
+  char buf[2048];
+  struct CBC cbc;
+  CURLM *multi;
+  CURLMcode mret;
+  fd_set rs;
+  fd_set ws;
+  fd_set es;
+  int max;
+  int running;
+  struct CURLMsg *msg;
+  time_t start;
+  struct timeval tv;
+  unsigned int pos = 0;
+  int done_flag = 0;
+
+  multi = NULL;
+  cbc.buf = buf;
+  cbc.size = 2048;
+  cbc.pos = 0;
+  d = MHD_start_daemon (MHD_USE_DEBUG,
+                        11082,
+                        NULL, NULL, &ahc_echo, &done_flag, MHD_OPTION_END);
+  if (d == NULL)
+    return 256;
+  c = curl_easy_init ();
+  curl_easy_setopt (c, CURLOPT_URL, "http://localhost:11082/hello_world");
+  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
+  curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
+  curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
+  curl_easy_setopt (c, CURLOPT_READDATA, &pos);
+  curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
+  /*
+     // by not giving the file size, we force chunking!
+     curl_easy_setopt (c, CURLOPT_INFILESIZE_LARGE, (curl_off_t) 8L);
+   */
+  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
+  curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
+  curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
+  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 15L);
+  // NOTE: use of CONNECTTIMEOUT without also
+  //   setting NOSIGNAL results in really weird
+  //   crashes on my system!
+  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
+
+
+  multi = curl_multi_init ();
+  if (multi == NULL)
+    {
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 512;
+    }
+  mret = curl_multi_add_handle (multi, c);
+  if (mret != CURLM_OK)
+    {
+      curl_multi_cleanup (multi);
+      curl_easy_cleanup (c);
+      MHD_stop_daemon (d);
+      return 1024;
+    }
+  start = time (NULL);
+  while ((time (NULL) - start < 5) && (multi != NULL))
+    {
+      max = 0;
+      FD_ZERO (&rs);
+      FD_ZERO (&ws);
+      FD_ZERO (&es);
+      curl_multi_perform (multi, &running);
+      mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
+      if (mret != CURLM_OK)
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 2048;
+        }
+      if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
+        {
+          curl_multi_remove_handle (multi, c);
+          curl_multi_cleanup (multi);
+          curl_easy_cleanup (c);
+          MHD_stop_daemon (d);
+          return 4096;
+        }
+      tv.tv_sec = 0;
+      tv.tv_usec = 1000;
+      select (max + 1, &rs, &ws, &es, &tv);
+      curl_multi_perform (multi, &running);
+      if (running == 0)
+        {
+          msg = curl_multi_info_read (multi, &running);
+          if (msg == NULL)
+            break;
+          if (msg->msg == CURLMSG_DONE)
+            {
+              if (msg->data.result != CURLE_OK)
+                printf ("%s failed at %s:%d: `%s'\n",
+                        "curl_multi_perform",
+                        __FILE__,
+                        __LINE__, curl_easy_strerror (msg->data.result));
+              curl_multi_remove_handle (multi, c);
+              curl_multi_cleanup (multi);
+              curl_easy_cleanup (c);
+              c = NULL;
+              multi = NULL;
+            }
+        }
+      MHD_run (d);
+    }
+  if (multi != NULL)
+    {
+      curl_multi_remove_handle (multi, c);
+      curl_easy_cleanup (c);
+      curl_multi_cleanup (multi);
+    }
+  MHD_stop_daemon (d);
+  if (cbc.pos != strlen ("/hello_world"))
+    return 8192;
+  if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
+    return 16384;
+  return 0;
+}
+
+
+
+int
+main (int argc, char *const *argv)
+{
+  unsigned int errorCount = 0;
+
+  if (0 != curl_global_init (CURL_GLOBAL_WIN32))
+    return 2;
+  errorCount += testInternalPut ();
+  if (0)
+    {
+      errorCount += testMultithreadedPut ();
+      errorCount += testExternalPut ();
+    }
+  if (errorCount != 0)
+    fprintf (stderr, "Error (code: %u)\n", errorCount);
+  curl_global_cleanup ();
+  return errorCount != 0;       /* 0 == pass */
+}

Деякі файли не було показано, через те що забагато файлів було змінено