libmicrohttpd

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

commit 01541405d99549b3f973729ebc4fc1f48521f9d3
parent 89c4f4882727ed5ce8c3a0a13e6926eca195ec69
Author: Christian Grothoff <christian@grothoff.org>
Date:   Tue, 27 Sep 2011 11:13:04 +0000

addressing odd URL format described by Daniel Chiaramello on the MHD mailinglist today

Diffstat:
MChangeLog | 4++++
Mdoc/microhttpd.texi | 13+++++++++++--
Msrc/daemon/connection.c | 12+++++++++++-
Msrc/testcurl/Makefile.am | 7+++++++
Asrc/testcurl/daemontest_urlparse.c | 187+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 220 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,3 +1,7 @@ +Tue Sep 27 13:07:36 CEST 2011 + Added ability to access URL arguments of the form 'url?foo' (without + '='). Added testcase and updated documentation accordingly. -CG + Mon Sep 26 21:24:00 CEST 2011 Only run response cleanup testcase if curl binary was found by configure. -CG diff --git a/doc/microhttpd.texi b/doc/microhttpd.texi @@ -1128,7 +1128,7 @@ right now). @deftypefun int MHD_get_connection_values (struct MHD_Connection *connection, enum MHD_ValueKind kind, MHD_KeyValueIterator iterator, void *iterator_cls) -Get all the headers matching @var{kind} from the request. +Get all the headers matching @var{kind} from the request. The @var{iterator} callback is invoked once for each header, with @var{iterator_cls} as first argument. Return the number of entries @@ -1137,13 +1137,22 @@ iterating, @var{iterator} returns @code{MHD_NO}. @var{iterator} can be @mynull{}: in this case this function just counts and returns the number of headers. + +In the case of @code{MHD_GET_ARGUMENT_KIND}, the @var{value} argument +will be NULL if the URL contained a key without an equals operator. +For example, for a HTTP request to the URL ``http://foo/bar?key'', the +@var{value} argument is NULL; in contrast, a HTTP request to the URL +``http://foo/bar?key='', the @var{value} argument is the empty string. +The normal case is that the URL contains ``http://foo/bar?key=value'' +in which case @var{value} would be the string ``value'' and @var{key} +would contain the string ``key''. @end deftypefun @deftypefun int MHD_set_connection_value (struct MHD_Connection *connection, enum MHD_ValueKind kind, const char * key, const char * value) This function can be used to add an entry to the HTTP headers of a connection (so that the -MHD_get_connection_values function will return +@code{MHD_get_connection_values function} will return them -- and the MHD PostProcessor will also see them). This maybe required in certain situations (see Mantis #1399) where (broken) diff --git a/src/daemon/connection.c b/src/daemon/connection.c @@ -1056,7 +1056,17 @@ parse_arguments (enum MHD_ValueKind kind, { equals = strstr (args, "="); if (equals == NULL) - return MHD_NO; /* invalid, ignore */ + { + /* add with 'value' NULL */ + connection->daemon->unescape_callback (connection->daemon->unescape_callback_cls, + connection, + args); + + return connection_add_header (connection, + args, + NULL, + kind); + } equals[0] = '\0'; equals++; amper = strstr (equals, "&"); diff --git a/src/testcurl/Makefile.am b/src/testcurl/Makefile.am @@ -29,6 +29,7 @@ endif check_PROGRAMS = \ daemontest_get \ daemontest_get_sendfile \ + daemontest_urlparse \ daemontest_post \ daemontest_postform \ daemontest_post_loop \ @@ -114,6 +115,12 @@ daemontest_get_sendfile_LDADD = \ $(top_builddir)/src/daemon/libmicrohttpd.la \ @LIBCURL@ +daemontest_urlparse_SOURCES = \ + daemontest_urlparse.c +daemontest_urlparse_LDADD = \ + $(top_builddir)/src/daemon/libmicrohttpd.la \ + @LIBCURL@ + daemontest_get_response_cleanup_SOURCES = \ daemontest_get_response_cleanup.c daemontest_get_response_cleanup_LDADD = \ diff --git a/src/testcurl/daemontest_urlparse.c b/src/testcurl/daemontest_urlparse.c @@ -0,0 +1,187 @@ +/* + This file is part of libmicrohttpd + (C) 2007, 2009, 2011 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_urlparse.c + * @brief Testcase for libmicrohttpd url parsing + * @author Christian Grothoff + */ + +#include "MHD_config.h" +#include "platform.h" +#include <curl/curl.h> +#include <microhttpd.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#ifdef __MINGW32__ +#define usleep(usec) (Sleep ((usec) / 1000),0) +#endif + +#ifndef WINDOWS +#include <unistd.h> +#include <sys/socket.h> +#endif + +static int oneone; + +static int matches; + +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 +test_values (void *cls, + enum MHD_ValueKind kind, + const char *key, + const char *value) +{ + if ( (0 == strcmp (key, "a")) && + (0 == strcmp (value, "b")) ) + matches += 1; + if ( (0 == strcmp (key, "c")) && + (0 == strcmp (value, "")) ) + matches += 2; + if ( (0 == strcmp (key, "d")) && + (NULL == value) ) + matches += 4; + return MHD_YES; +} + +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; + } + MHD_get_connection_values (connection, + MHD_GET_ARGUMENT_KIND, + &test_values, + NULL); + *unused = NULL; + response = MHD_create_response_from_buffer (strlen (url), + (void *) url, + MHD_RESPMEM_MUST_COPY); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); + if (ret == MHD_NO) + abort (); + return ret; +} + + +static int +testInternalGet (int poll_flag) +{ + 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 | poll_flag, + 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?a=b&c=&d"); + 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; + if (matches != 7) + return 16; + 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 (0); + if (errorCount != 0) + fprintf (stderr, "Error (code: %u)\n", errorCount); + curl_global_cleanup (); + return errorCount != 0; /* 0 == pass */ +}