libmicrohttpd

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

commit a84f9bc7b3896a197a1e4c41a5311d6b52928034
parent 2d299ede39408b4f4b1f5c817d5df28f1f37a21b
Author: Christian Grothoff <christian@grothoff.org>
Date:   Tue, 23 Jan 2007 05:48:21 +0000

expanded testcases

Diffstat:
MINSTALL | 69++++++++++++++++++++++++++++++++++++++-------------------------------
Mdoc/Makefile.am | 1+
Msrc/daemon/daemon.c | 237+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/daemon/daemontest.c | 148+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
4 files changed, 421 insertions(+), 34 deletions(-)

diff --git a/INSTALL b/INSTALL @@ -1,13 +1,16 @@ -Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002 Free Software -Foundation, Inc. +Installation Instructions +************************* - This file is free documentation; the Free Software Foundation gives +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005 Free +Software Foundation, Inc. + +This file is free documentation; the Free Software Foundation gives unlimited permission to copy, distribute and modify it. Basic Installation ================== - These are generic installation instructions. +These are generic installation instructions. The `configure' shell script attempts to guess correct values for various system-dependent variables used during compilation. It uses @@ -67,9 +70,9 @@ The simplest way to compile this package is: Compilers and Options ===================== - Some systems require unusual options for compilation or linking that -the `configure' script does not know about. Run `./configure --help' -for details on some of the pertinent environment variables. +Some systems require unusual options for compilation or linking that the +`configure' script does not know about. Run `./configure --help' for +details on some of the pertinent environment variables. You can give `configure' initial values for configuration parameters by setting variables in the command line or in the environment. Here @@ -82,7 +85,7 @@ is an example: Compiling For Multiple Architectures ==================================== - You can compile the package for more than one kind of computer at the +You can compile the package for more than one kind of computer at the same time, by placing the object files for each architecture in their own directory. To do this, you must use a version of `make' that supports the `VPATH' variable, such as GNU `make'. `cd' to the @@ -99,19 +102,19 @@ for another architecture. Installation Names ================== - By default, `make install' will install the package's files in -`/usr/local/bin', `/usr/local/man', etc. You can specify an -installation prefix other than `/usr/local' by giving `configure' the -option `--prefix=PATH'. +By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX'. You can specify separate installation prefixes for architecture-specific files and architecture-independent files. If you -give `configure' the option `--exec-prefix=PATH', the package will use -PATH as the prefix for installing programs and libraries. -Documentation and other data files will still use the regular prefix. +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. In addition, if you use an unusual directory layout you can give -options like `--bindir=PATH' to specify different values for particular +options like `--bindir=DIR' to specify different values for particular kinds of files. Run `configure --help' for a list of the directories you can set and what kinds of files go in them. @@ -122,7 +125,7 @@ option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. Optional Features ================= - Some packages pay attention to `--enable-FEATURE' options to +Some packages pay attention to `--enable-FEATURE' options to `configure', where FEATURE indicates an optional part of the package. They may also pay attention to `--with-PACKAGE' options, where PACKAGE is something like `gnu-as' or `x' (for the X Window System). The @@ -137,11 +140,11 @@ you can use the `configure' options `--x-includes=DIR' and Specifying the System Type ========================== - There may be some features `configure' cannot figure out -automatically, but needs to determine by the type of machine the package -will run on. Usually, assuming the package is built to be run on the -_same_ architectures, `configure' can figure that out, but if it prints -a message saying it cannot guess the machine type, give it the +There may be some features `configure' cannot figure out automatically, +but needs to determine by the type of machine the package will run on. +Usually, assuming the package is built to be run on the _same_ +architectures, `configure' can figure that out, but if it prints a +message saying it cannot guess the machine type, give it the `--build=TYPE' option. TYPE can either be a short name for the system type, such as `sun4', or a canonical name which has the form: @@ -156,7 +159,7 @@ where SYSTEM can have one of these forms: need to know the machine type. If you are _building_ compiler tools for cross-compiling, you should -use the `--target=TYPE' option to select the type of system they will +use the option `--target=TYPE' to select the type of system they will produce code for. If you want to _use_ a cross compiler, that generates code for a @@ -167,9 +170,9 @@ eventually be run) with `--host=TYPE'. Sharing Defaults ================ - If you want to set default values for `configure' scripts to share, -you can create a site shell script called `config.site' that gives -default values for variables like `CC', `cache_file', and `prefix'. +If you want to set default values for `configure' scripts to share, you +can create a site shell script called `config.site' that gives default +values for variables like `CC', `cache_file', and `prefix'. `configure' looks for `PREFIX/share/config.site' if it exists, then `PREFIX/etc/config.site' if it exists. Or, you can set the `CONFIG_SITE' environment variable to the location of the site script. @@ -178,7 +181,7 @@ A warning: not all `configure' scripts look for a site script. Defining Variables ================== - Variables not defined in a site shell script can be set in the +Variables not defined in a site shell script can be set in the environment passed to `configure'. However, some packages may run configure again during the build, and the customized values of these variables may be lost. In order to avoid this problem, you should set @@ -186,14 +189,18 @@ them in the `configure' command line, using `VAR=value'. For example: ./configure CC=/usr/local2/bin/gcc -will cause the specified gcc to be used as the C compiler (unless it is -overridden in the site shell script). +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). Here is a another example: + + /bin/bash ./configure CONFIG_SHELL=/bin/bash + +Here the `CONFIG_SHELL=/bin/bash' operand causes subsequent +configuration-related scripts to be executed by `/bin/bash'. `configure' Invocation ====================== - `configure' recognizes the following options to control how it -operates. +`configure' recognizes the following options to control how it operates. `--help' `-h' diff --git a/doc/Makefile.am b/doc/Makefile.am @@ -1,2 +1,3 @@ + man_MANS = libmicrohttpd.3 EXTRA_DIST = $(man_MANS) diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c @@ -27,3 +27,240 @@ #include "config.h" #include "microhttpd.h" + +/** + * Start a webserver on the given port. + * @param port port to bind to + * @param apc callback to call to check which clients + * will be allowed to connect + * @param apc_cls extra argument to apc + * @param dh default handler for all URIs + * @param dh_cls extra argument to dh + * @return NULL on error, handle to daemon on success + */ +struct MHD_Daemon * +MHD_start_daemon(unsigned int options, + unsigned short port, + MHD_AcceptPolicyCallback apc, + void * apc_cls, + MHD_AccessHandlerCallback dh, + void * dh_cls) { + return NULL; +} + + + +/** + * Shutdown an http daemon. + */ +void +MHD_stop_daemon(struct MHD_Daemon * daemon) { +} + +/** + * Obtain the select sets for this daemon. + * + * @return MHD_YES on success, MHD_NO if this + * daemon was not started with the right + * options for this call. + */ +int +MHD_get_fdset(struct MHD_Daemon * daemon, + fd_set * read_fd_set, + fd_set * write_fd_set, + fd_set * except_fd_set, + int * max_fd) { + return 0; +} + +/** + * Run webserver operations (without blocking unless + * in client callbacks). This method should be called + * by clients in combination with MHD_get_fdset + * if the client-controlled select method is used. + * + * @return MHD_YES on success, MHD_NO if this + * daemon was not started with the right + * options for this call. + */ +int +MHD_run(struct MHD_Daemon * daemon) { + return 0; +} + + +/** + * Register an access handler for all URIs beginning with uri_prefix. + * + * @param uri_prefix + * @return MRI_NO if a handler for this exact prefix + * already exists + */ +int +MHD_register_handler(struct MHD_Daemon * daemon, + const char * uri_prefix, + MHD_AccessHandlerCallback dh, + void * dh_cls) { + return 0; +} + +/** + * Unregister an access handler for the URIs beginning with + * uri_prefix. + * + * @param uri_prefix + * @return MHD_NO if a handler for this exact prefix + * is not known for this daemon + */ +int +MHD_unregister_handler(struct MHD_Daemon * daemon, + const char * uri_prefix, + MHD_AccessHandlerCallback dh, + void * dh_cls) { + return 0; +} + +/** + * Get all of the headers from the request. + * + * @param iterator callback to call on each header; + * maybe NULL (then just count headers) + * @param iterator_cls extra argument to iterator + * @return number of entries iterated over + */ +int +MHD_get_session_values(struct MHD_Session * session, + enum MHD_ValueKind kind, + MHD_KeyValueIterator * iterator, + void * iterator_cls); + +/** + * Get a particular header value. If multiple + * values match the kind, return any one of them. + * + * @param key the header to look for + * @return NULL if no such item was found + */ +const char * +MHD_lookup_session_value(struct MHD_Session * session, + enum MHD_ValueKind kind, + const char * key) { + return NULL; +} + +/** + * Queue a response to be transmitted to the client (as soon as + * possible). + * + * @param session the session identifying the client + * @param status_code HTTP status code (i.e. 200 for OK) + * @param response response to transmit + * @return MHD_NO on error (i.e. reply already sent), + * MHD_YES on success or if message has been queued + */ +int +MHD_queue_response(struct MHD_Session * session, + unsigned int status_code, + struct MHD_Response * response) { + return 0; +} + + +/** + * Create a response object. The response object can be extended with + * header information and then be used any number of times. + * + * @param size size of the data portion of the response, -1 for unknown + * @param crc callback to use to obtain response data + * @param crc_cls extra argument to crc + * @param crfc callback to call to free crc_cls resources + * @return NULL on error (i.e. invalid arguments, out of memory) + */ +struct MHD_Response * +MHD_create_response_from_callback(size_t size, + MHD_ContentReaderCallback crc, + void * crc_cls, + MHD_ContentReaderFreeCallback crfc) { + return NULL; +} + +/** + * Create a response object. The response object can be extended with + * header information and then be used any number of times. + * + * @param size size of the data portion of the response + * @param data the data itself + * @param must_free libmicrohttpd should free data when done + * @param must_copy libmicrohttpd must make a copy of data + * right away, the data maybe released anytime after + * this call returns + * @return NULL on error (i.e. invalid arguments, out of memory) + */ +struct MHD_Response * +MHD_create_response_from_data(size_t size, + void * data, + int must_free, + int must_copy) { + return NULL; +} + +/** + * Destroy a response object and associated resources. Note that + * libmicrohttpd may keep some of the resources around if the response + * is still in the queue for some clients, so the memory may not + * necessarily be freed immediatley. + */ +void +MHD_destroy_response(struct MHD_Response * response) { +} + +/** + * Add a header line to the response. + * + * @return MHD_NO on error (i.e. invalid header or content format). + */ +int +MHD_add_response_header(struct MHD_Response * response, + const char * header, + const char * content) { + return 0; +} + +/** + * Delete a header line from the response. + * + * @return MHD_NO on error (no such header known) + */ +int +MHD_del_response_header(struct MHD_Response * response, + const char * header, + const char * content) { + return 0; +} + +/** + * Get all of the headers added to a response. + * + * @param iterator callback to call on each header; + * maybe NULL (then just count headers) + * @param iterator_cls extra argument to iterator + * @return number of entries iterated over + */ +int +MHD_get_response_headers(struct MHD_Response * response, + MHD_KeyValueIterator * iterator, + void * iterator_cls) { + return -1; +} + +/** + * @return -1 if no data uploaded; otherwise number of bytes + * read into buf; 0 for end of transmission + */ +int +MHD_read_file_upload(struct MHD_Session * session, + void * buf, + size_t len) { + return -1; +} + diff --git a/src/daemon/daemontest.c b/src/daemon/daemontest.c @@ -1,6 +1,6 @@ /* This file is part of libmicrohttpd - (C) 2007 YOUR NAME HERE + (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 @@ -21,14 +21,156 @@ /** * @file daemontest.c * @brief Testcase for libmicrohttpd - * @author FIXME + * @author Christian Grothoff */ #include "config.h" +#include "curl/curl.h" #include "microhttpd.h" +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +static int testStartError() { + return NULL != MHD_start_daemon(0, 0, NULL, NULL, NULL, NULL); +} + +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_Session * session, + const char * url, + const char * method) { + return MHD_NO; +} + +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_Session * session, + const char * url, + const char * method) { + 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(session, + MHD_HTTP_OK, + response); + MHD_destroy_response(response); + return ret; +} + +static int testStartStop() { + struct MHD_Daemon * d; + + d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv4, + 1080, + &apc_nothing, + NULL, + &ahc_nothing, + NULL); + if (d == NULL) + return 1; + MHD_stop_daemon(d); + return 0; +} + +static int testGet() { + struct MHD_Daemon * d; + CURL * c; + int ret; + char buf[2048]; + struct CBC cbc; + + cbc.buf = buf; + cbc.size = 2048; + cbc.pos = 0; + ret = 0; + d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv4, + 1080, + &apc_all, + NULL, + &ahc_echo, + "GET"); + 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_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 != curl_easy_perform(c)) + ret = 1; + curl_easy_cleanup(c); + if (cbc.pos != strlen("hello_world")) + ret = 2; + if (0 != strncmp("hello_world", + cbc.buf, + strlen("hello_world"))) + ret = 3; + MHD_stop_daemon(d); + return ret; +} int main(int argc, char * const * argv) { - return 0; /* 0 == pass */ + unsigned int errorCount = 0; + + errorCount += testStartError(); + errorCount += testStartStop(); + errorCount += testGet(); + return errorCount != 0; /* 0 == pass */ }