[Wifidog] libhttpd

Alexandre Carmel-Veilleux saruman at northernhacking.org
Fri Apr 2 11:54:07 EST 2004


On Fri, Apr 02, 2004 at 11:15:05AM -0500, Alexandre Carmel-Veilleux wrote:
> 
> 	I'm not sure how libhttpd will handle that. Conceptually, it's
> good.

	It's doable, proof of concept included, compile with:

gcc test_httpd.c -o test_httpd -lhttpd

Cheers,

Alex
-------------- next part --------------
/*
** Copyright (c) 2002  Hughes Technologies Pty Ltd.  All rights
** reserved.
**
** Terms under which this software may be used or copied are
** provided in the  specific license associated with this product.
**
** Hughes Technologies disclaims all warranties with regard to this
** software, including all implied warranties of merchantability and
** fitness, in no event shall Hughes Technologies be liable for any
** special, indirect or consequential damages or any damages whatsoever
** resulting from loss of use, data or profits, whether in an action of
** contract, negligence or other tortious action, arising out of or in
** connection with the use or performance of this software.
**
**
** $Id: test_httpd.c,v 1.10 2002/11/25 02:26:58 bambi Exp $
**
*/

#include <stdio.h>
#include <unistd.h>

#ifdef _WIN32
#  include <getopt.h>
#else
#  include <sys/time.h>
#endif

#include <httpd.h>

/*
** This is a static page of HTML.  It is loaded into the content
** tree using httpdAddStaticContent( ).
*/

/*
** Below are 2 dynamic pages, each generated by a C function.  The first
** is a simple page that offers a little dynamic info (the process ID)
** and the setups up a test link and a simple form.
** 
** The second page processes the form.  As you can see, you can access
** the form data from within your C code by accessing the symbol table
** using httpdGetVariableByName() (and other similar functions).  You
** can also include variables in the string passed to httpdOutput( ) and
** they will be expanded automatically.
*/
void index_html(server)
	httpd	*server;
{
	if (fork() == 0) {
		sleep(15);
		httpdPrintf(server,
		    "Welcome to the httpd server running in process number %d<P>\n",
		    getpid());
		httpdPrintf(server,
		    "Click <A HREF=/test1.html>here</A> to view a test page<P>\n");
		httpdPrintf(server,
		    "Click <A HREF=/login.html>here</A> to authenticate<P>\n");
		httpdPrintf(server,
		    "Or <A HREF=/wildcard/foo>here</A> for a test wildcard page<P>\n");
		httpdPrintf(server, "<P><FORM ACTION=test2.html METHOD=POST>\n");
		httpdPrintf(server, "Enter your name <INPUT NAME=name SIZE=10>\n");
		httpdPrintf(server, "<INPUT TYPE=SUBMIT VALUE=Click!><P></FORM>\n");
		httpdEndRequest(server);
		exit(0);
	}
	server->clientSock = -1; /* So we don't get shutdown, there's
				    no error handling in httpdEndRequest */
	return;
}

int main(argc, argv)
	int	argc;
	char	*argv[];
{
	httpd	*server;
	char 	*host;
	int 	port,
		errFlag,
		result;
	extern char *optarg;
	extern int optind, opterr, optopt;
	int c;
	struct	timeval timeout;

	host = NULL;
	port = 80;
	errFlag = 0;
	while ( (c=getopt(argc,argv,"h:p:")) != -1 )
	{
		switch ( c ) 
		{
			case 'h':
				host=optarg;
				break;

			case 'p':
				port = atoi(optarg);
				break;
			default:
				errFlag++;
		}
	}
	if (errFlag)
	{
		fprintf(stderr,"usage: [-h <host IP>] [ -p <port >]\n");
		fprintf(stderr,"\nLibHTTPD version %s\n\n",LIBHTTPD_VERSION);
		exit(1);
	}


	/*
	** Create a server and setup our logging
	*/
	server = httpdCreate(host,port);
	if (server == NULL)
	{
		perror("Can't create server");
		exit(1);
	}
	httpdSetAccessLog(server, stdout);
	httpdSetErrorLog(server, stdout);

	/*
	** Setup some content for the server
	*/
	httpdAddCContent(server,"/", "index.html", HTTP_TRUE, 
		NULL, index_html);

	/*
	** Go into our service loop
	*/
	timeout.tv_sec = 5;
	timeout.tv_usec = 0;

	while(1 == 1)
	{
		result = httpdGetConnection(server, &timeout);
		if (result == 0)
		{
			printf("Timeout ... \n");
			continue;
		}
		if (result < 0)
		{
			printf("Error ... \n");
			continue;
		}
		if(httpdReadRequest(server) < 0)
		{
			httpdEndRequest(server);
			continue;
		}
		httpdProcessRequest(server);
		httpdEndRequest(server);
	}
}
-------------- next part --------------
_______________________________________________
Wifidog mailing list
Wifidog at isf.waglo.com
http://isf.waglo.com/mailman/listinfo/wifidog_isf.waglo.com


More information about the Wifidog mailing list