|
Packit |
875988 |
/*
|
|
Packit |
875988 |
This file is part of libmicrohttpd
|
|
Packit |
875988 |
Copyright (C) 2007, 2009, 2011 Christian Grothoff
|
|
Packit |
875988 |
|
|
Packit |
875988 |
libmicrohttpd is free software; you can redistribute it and/or modify
|
|
Packit |
875988 |
it under the terms of the GNU General Public License as published
|
|
Packit |
875988 |
by the Free Software Foundation; either version 2, or (at your
|
|
Packit |
875988 |
option) any later version.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
libmicrohttpd is distributed in the hope that it will be useful, but
|
|
Packit |
875988 |
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
875988 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
875988 |
General Public License for more details.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
You should have received a copy of the GNU General Public License
|
|
Packit |
875988 |
along with libmicrohttpd; see the file COPYING. If not, write to the
|
|
Packit |
875988 |
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
Packit |
875988 |
Boston, MA 02110-1301, USA.
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* @file daemontest_urlparse.c
|
|
Packit |
875988 |
* @brief Testcase for libmicrohttpd url parsing
|
|
Packit |
875988 |
* @author Christian Grothoff
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#include "MHD_config.h"
|
|
Packit |
875988 |
#include "platform.h"
|
|
Packit |
875988 |
#include <curl/curl.h>
|
|
Packit |
875988 |
#include <microhttpd.h>
|
|
Packit |
875988 |
#include <stdlib.h>
|
|
Packit |
875988 |
#include <string.h>
|
|
Packit |
875988 |
#include <time.h>
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifdef _WIN32
|
|
Packit |
875988 |
#ifndef WIN32_LEAN_AND_MEAN
|
|
Packit |
875988 |
#define WIN32_LEAN_AND_MEAN 1
|
|
Packit |
875988 |
#endif /* !WIN32_LEAN_AND_MEAN */
|
|
Packit |
875988 |
#include <windows.h>
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifndef WINDOWS
|
|
Packit |
875988 |
#include <unistd.h>
|
|
Packit |
875988 |
#include <sys/socket.h>
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static int oneone;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static int matches;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
struct CBC
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
char *buf;
|
|
Packit |
875988 |
size_t pos;
|
|
Packit |
875988 |
size_t size;
|
|
Packit |
875988 |
};
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static size_t
|
|
Packit |
875988 |
copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
struct CBC *cbc = ctx;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (cbc->pos + size * nmemb > cbc->size)
|
|
Packit |
875988 |
return 0; /* overflow */
|
|
Packit |
875988 |
memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
|
|
Packit |
875988 |
cbc->pos += size * nmemb;
|
|
Packit |
875988 |
return size * nmemb;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static int
|
|
Packit |
875988 |
test_values (void *cls,
|
|
Packit |
875988 |
enum MHD_ValueKind kind,
|
|
Packit |
875988 |
const char *key,
|
|
Packit |
875988 |
const char *value)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
(void)cls;(void)kind; /* Unused. Silent compiler warning. */
|
|
Packit |
875988 |
if ( (0 == strcmp (key, "a")) &&
|
|
Packit |
875988 |
(0 == strcmp (value, "b")) )
|
|
Packit |
875988 |
matches += 1;
|
|
Packit |
875988 |
if ( (0 == strcmp (key, "c")) &&
|
|
Packit |
875988 |
(0 == strcmp (value, "")) )
|
|
Packit |
875988 |
matches += 2;
|
|
Packit |
875988 |
if ( (0 == strcmp (key, "d")) &&
|
|
Packit |
875988 |
(NULL == value) )
|
|
Packit |
875988 |
matches += 4;
|
|
Packit |
875988 |
return MHD_YES;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static int
|
|
Packit |
875988 |
ahc_echo (void *cls,
|
|
Packit |
875988 |
struct MHD_Connection *connection,
|
|
Packit |
875988 |
const char *url,
|
|
Packit |
875988 |
const char *method,
|
|
Packit |
875988 |
const char *version,
|
|
Packit |
875988 |
const char *upload_data, size_t *upload_data_size,
|
|
Packit |
875988 |
void **unused)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
static int ptr;
|
|
Packit |
875988 |
const char *me = cls;
|
|
Packit |
875988 |
struct MHD_Response *response;
|
|
Packit |
875988 |
int ret;
|
|
Packit |
875988 |
(void)version;(void)upload_data;(void)upload_data_size; /* Unused. Silent compiler warning. */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (0 != strcmp (me, method))
|
|
Packit |
875988 |
return MHD_NO; /* unexpected method */
|
|
Packit |
875988 |
if (&ptr != *unused)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
*unused = &pt;;
|
|
Packit |
875988 |
return MHD_YES;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
MHD_get_connection_values (connection,
|
|
Packit |
875988 |
MHD_GET_ARGUMENT_KIND,
|
|
Packit |
875988 |
&test_values,
|
|
Packit |
875988 |
NULL);
|
|
Packit |
875988 |
*unused = NULL;
|
|
Packit |
875988 |
response = MHD_create_response_from_buffer (strlen (url),
|
|
Packit |
875988 |
(void *) url,
|
|
Packit |
875988 |
MHD_RESPMEM_MUST_COPY);
|
|
Packit |
875988 |
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
|
|
Packit |
875988 |
MHD_destroy_response (response);
|
|
Packit |
875988 |
if (ret == MHD_NO)
|
|
Packit |
875988 |
abort ();
|
|
Packit |
875988 |
return ret;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static int
|
|
Packit |
875988 |
testInternalGet (int poll_flag)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
struct MHD_Daemon *d;
|
|
Packit |
875988 |
CURL *c;
|
|
Packit |
875988 |
char buf[2048];
|
|
Packit |
875988 |
struct CBC cbc;
|
|
Packit |
875988 |
CURLcode errornum;
|
|
Packit |
875988 |
int port;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
|
|
Packit |
875988 |
port = 0;
|
|
Packit |
875988 |
else
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
port = 1510;
|
|
Packit |
875988 |
if (oneone)
|
|
Packit |
875988 |
port += 5;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
cbc.buf = buf;
|
|
Packit |
875988 |
cbc.size = 2048;
|
|
Packit |
875988 |
cbc.pos = 0;
|
|
Packit |
875988 |
d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | poll_flag,
|
|
Packit |
875988 |
port, NULL, NULL, &ahc_echo, "GET", MHD_OPTION_END);
|
|
Packit |
875988 |
if (d == NULL)
|
|
Packit |
875988 |
return 1;
|
|
Packit |
875988 |
if (0 == port)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
const union MHD_DaemonInfo *dinfo;
|
|
Packit |
875988 |
dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
|
|
Packit |
875988 |
if (NULL == dinfo || 0 == dinfo->port)
|
|
Packit |
875988 |
{ MHD_stop_daemon (d); return 32; }
|
|
Packit |
875988 |
port = (int)dinfo->port;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
c = curl_easy_init ();
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world?a=b&c=&d";;
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_PORT, (long)port);
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer);
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
|
|
Packit |
875988 |
if (oneone)
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
|
|
Packit |
875988 |
else
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
|
|
Packit |
875988 |
/* NOTE: use of CONNECTTIMEOUT without also
|
|
Packit |
875988 |
setting NOSIGNAL results in really weird
|
|
Packit |
875988 |
crashes on my system!*/
|
|
Packit |
875988 |
curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
|
|
Packit |
875988 |
if (CURLE_OK != (errornum = curl_easy_perform (c)))
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
fprintf (stderr,
|
|
Packit |
875988 |
"curl_easy_perform failed: `%s'\n",
|
|
Packit |
875988 |
curl_easy_strerror (errornum));
|
|
Packit |
875988 |
curl_easy_cleanup (c);
|
|
Packit |
875988 |
MHD_stop_daemon (d);
|
|
Packit |
875988 |
return 2;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
curl_easy_cleanup (c);
|
|
Packit |
875988 |
MHD_stop_daemon (d);
|
|
Packit |
875988 |
if (cbc.pos != strlen ("/hello_world"))
|
|
Packit |
875988 |
return 4;
|
|
Packit |
875988 |
if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
|
|
Packit |
875988 |
return 8;
|
|
Packit |
875988 |
if (matches != 7)
|
|
Packit |
875988 |
return 16;
|
|
Packit |
875988 |
return 0;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
int
|
|
Packit |
875988 |
main (int argc, char *const *argv)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
unsigned int errorCount = 0;
|
|
Packit |
875988 |
(void)argc; /* Unused. Silent compiler warning. */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
oneone = (NULL != strrchr (argv[0], (int) '/')) ?
|
|
Packit |
875988 |
(NULL != strstr (strrchr (argv[0], (int) '/'), "11")) : 0;
|
|
Packit |
875988 |
if (0 != curl_global_init (CURL_GLOBAL_WIN32))
|
|
Packit |
875988 |
return 2;
|
|
Packit |
875988 |
errorCount += testInternalGet (0);
|
|
Packit |
875988 |
if (errorCount != 0)
|
|
Packit |
875988 |
fprintf (stderr, "Error (code: %u)\n", errorCount);
|
|
Packit |
875988 |
curl_global_cleanup ();
|
|
Packit |
875988 |
return errorCount != 0; /* 0 == pass */
|
|
Packit |
875988 |
}
|