THTTPD CGI works

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2063 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2009-09-16 15:04:58 +00:00
parent ebd9543ff2
commit 0640aa9fdf
8 changed files with 335 additions and 45 deletions

View File

@ -33,7 +33,7 @@
#
############################################################################
SUBDIRS = hello
SUBDIRS = hello tasks
INSTALL_FILES = index.html style.css
THTTPD_DIR = $(TOPDIR)/examples/thttpd

View File

@ -52,18 +52,29 @@ int main(int argc, char *argv[])
{
fprintf(stderr, "Hello requested from: %s\n", getenv("REMOTE_ADDR"));
(void)printf(
puts(
"Content-type: text/html\r\n"
"Status: 200/html\r\n"
"\r\n"
"<HTML>"
"<HEAD>"
"<TITLE>Hello</TITLE>"
"</HEAD>\r\n"
"<BODY>"
"<H2>Hello, World!</H2>"
"<P>Requested by: %s</P>"
"</BODY></HTML>\r\n",
getenv("REMOTE_ADDR"));
"<html>\r\n"
"<head>\r\n"
"<title>Hello!</title>\r\n"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/style.css\">\r\n"
"</head>\r\n"
"<body bgcolor=\"#fffeec\" text=\"black\">\r\n"
"<div class=\"menu\">\r\n"
"<div class=\"menubox\"><a href=\"/index.html\">Front page</a></div>\r\n"
"<div class=\"menubox\"><a href=\"hello\">Say Hello</a></div>\r\n"
"<div class=\"menubox\"><a href=\"tasks\">Tasks</a></div>\r\n"
"<div class=\"menubox\"><a href=\"netstat\">Network status</a></div>\r\n"
"<br>\r\n"
"</div>\r\n"
"<div class=\"contentblock\">\r\n");
printf(
"<h2>Hello, World!</h2><p>Requested by: %s</p>\r\n",
getenv("REMOTE_ADDR"));
puts(
"</body>\r\n"
"</html>\r\n");
return 0;
}

View File

@ -1,7 +1,7 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>NuttX examples/thttpd!</title>
<title>NuttX examples/thttpd</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="#fffeec" text="black">
@ -9,8 +9,8 @@
<div class="menu">
<div class="menubox"><a href="index.html">Front page</a></div>
<div class="menubox"><a href="cgi-bin/hello">Say Hello</a></div>
<div class="menubox"><a href="cgi-bin/test1">Test1</a></div>
<div class="menubox"><a href="cgi-bin/test2">Test2</a></div>
<div class="menubox"><a href="cgi-bin/tasks">Tasks</a></div>
<div class="menubox"><a href="cgi-bin/netstat">Network status</a></div>
<br>
</div>

View File

@ -0,0 +1,78 @@
############################################################################
# examples/thttpd/content/tasks/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
BIN = tasks
R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
all: $(BIN)
$(R1OBJS): %.o: %.c
@echo "CC: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(BIN) $(R2SRC) *.o *.r1 *.r2 *~ .*.swp core
install:
@install -m 0755 -D $(BIN) $(CGI_DIR)/$(BIN)

View File

@ -0,0 +1,168 @@
/****************************************************************************
* examples/thttpd/tasks.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static const char *g_statenames[] =
{
"INVALID ",
"PENDING ",
"READY ",
"RUNNING ",
"INACTIVE",
"WAITSEM ",
#ifndef CONFIG_DISABLE_MQUEUE
"WAITSIG ",
#endif
#ifndef CONFIG_DISABLE_MQUEUE
"MQNEMPTY",
"MQNFULL "
#endif
};
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: show_task
****************************************************************************/
/* NOTE: One limitation in the use of NXFLAT is that functions that are
* referenced as a pointer-to-a-function must have global scope. Otherwise
* ARM GCC will generate some bad logic.
*/
/* static */ void show_task(FAR _TCB *tcb, FAR void *arg)
{
int i;
/* Show task status */
printf("%5d %3d %4s %7s%c%c %8s ",
tcb->pid, tcb->sched_priority,
tcb->flags & TCB_FLAG_ROUND_ROBIN ? "RR " : "FIFO",
tcb->flags & TCB_FLAG_PTHREAD ? "PTHREAD" : "TASK ",
tcb->flags & TCB_FLAG_NONCANCELABLE ? 'N' : ' ',
tcb->flags & TCB_FLAG_CANCEL_PENDING ? 'P' : ' ',
g_statenames[tcb->task_state]);
/* Show task name and arguments */
printf("%s(", tcb->argv[0]);
/* Special case 1st argument (no comma) */
if (tcb->argv[1])
{
printf("%p", tcb->argv[1]);
}
/* Then any additional arguments */
#if CONFIG_MAX_TASK_ARGS > 2
for (i = 2; i <= CONFIG_MAX_TASK_ARGS && tcb->argv[i]; i++)
{
printf(", %p", tcb->argv[i]);
}
#endif
printf(")\n");
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char *argv[])
{
puts(
"Content-type: text/html\r\n"
"Status: 200/html\r\n"
"\r\n"
"<html>\r\n"
"<head>\r\n"
"<title>Hello!</title>\r\n"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/style.css\">\r\n"
"</head>\r\n"
"<body bgcolor=\"#fffeec\" text=\"black\">\r\n"
"<div class=\"menu\">\r\n"
"<div class=\"menubox\"><a href=\"/index.html\">Front page</a></div>\r\n"
"<div class=\"menubox\"><a href=\"hello\">Say Hello</a></div>\r\n"
"<div class=\"menubox\"><a href=\"tasks\">Tasks</a></div>\r\n"
"<div class=\"menubox\"><a href=\"netstat\">Network status</a></div>\r\n"
"<br>\r\n"
"</div>\r\n"
"<div class=\"contentblock\">\r\n"
"<pre>\r\n"
"PID PRI SCHD TYPE NP STATE NAME\r\n");
sched_foreach(show_task, NULL);
puts(
"</pre>\r\n"
"</body>\r\n"
"</html>\r\n");
return 0;
}

View File

@ -56,6 +56,37 @@
* Pre-Processor Definitions
****************************************************************************/
/* Debug output from this file is normally suppressed. If enabled, be aware
* that output to stdout will interfere with CGI programs (you could use the
* the low-level debug (lldbg) functions which probably do not use stdout
*/
#ifdef CONFIG_THTTPD_FDWATCH_DEBUG
# ifdef CONFIG_CPP_HAVE_VARARGS
# define fwdbg(format, arg...) ndbg(format, ##arg)
# define fwlldbg(format, arg...) nlldbg(format, ##arg)
# define fwvdbg(format, arg...) nvdbg(format, ##arg)
# define fwllvdbg(format, arg...) nllvdbg(format, ##arg)
# else
# define fwdbg ndbg
# define fwlldbg nlldbg
# define fwvdbg nvdbg
# define fwllvdbg nllvdbg
# endif
#else
# ifdef CONFIG_CPP_HAVE_VARARGS
# define fwdbg(x...)
# define fwlldbg(x...)
# define fwvdbg(x...)
# define fwllvdbg(x...)
# else
# define fwdbg (void)
# define fwlldbg (void)
# define fwvdbg (void)
# define fwllvdbg (void)
# endif
#endif
#ifndef MIN
# define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
@ -77,18 +108,18 @@ static void fdwatch_dump(const char *msg, FAR struct fdwatch_s *fw)
{
int i;
nvdbg("%s\n", msg);
nvdbg("nwatched: %d nfds: %d\n", fw->nwatched, fw->nfds);
fwvdbg("%s\n", msg);
fwvdbg("nwatched: %d nfds: %d\n", fw->nwatched, fw->nfds);
for (i = 0; i < fw->nwatched; i++)
{
nvdbg("%2d. pollfds: {fd: %d events: %02x revents: %02x} client: %p\n",
i+1, fw->pollfds[i].fd, fw->pollfds[i].events,
fw->pollfds[i].revents, fw->client[i]);
fwvdbg("%2d. pollfds: {fd: %d events: %02x revents: %02x} client: %p\n",
i+1, fw->pollfds[i].fd, fw->pollfds[i].events,
fw->pollfds[i].revents, fw->client[i]);
}
nvdbg("nactive: %d next: %d\n", fw->nactive, fw->next);
fwvdbg("nactive: %d next: %d\n", fw->nactive, fw->next);
for (i = 0; i < fw->nactive; i++)
{
nvdbg("%2d. %d active\n", i, fw->ready[i]);
fwvdbg("%2d. %d active\n", i, fw->ready[i]);
}
}
#else
@ -105,12 +136,12 @@ static int fdwatch_pollndx(FAR struct fdwatch_s *fw, int fd)
{
if (fw->pollfds[pollndx].fd == fd)
{
nvdbg("pollndx: %d\n", pollndx);
fwvdbg("pollndx: %d\n", pollndx);
return pollndx;
}
}
ndbg("No poll index for fd %d: %d\n", fd);
fwdbg("No poll index for fd %d: %d\n", fd);
return -1;
}
@ -129,7 +160,7 @@ struct fdwatch_s *fdwatch_initialize(int nfds)
fw = (struct fdwatch_s*)zalloc(sizeof(struct fdwatch_s));
if (!fw)
{
ndbg("Failed to allocate fdwatch\n");
fwdbg("Failed to allocate fdwatch\n");
return NULL;
}
@ -193,12 +224,12 @@ void fdwatch_uninitialize(struct fdwatch_s *fw)
void fdwatch_add_fd(struct fdwatch_s *fw, int fd, void *client_data)
{
nvdbg("fd: %d client_data: %p\n", fd, client_data);
fwvdbg("fd: %d client_data: %p\n", fd, client_data);
fdwatch_dump("Before adding:", fw);
if (fw->nwatched >= fw->nfds)
{
ndbg("too many fds\n");
fwdbg("too many fds\n");
return;
}
@ -220,7 +251,7 @@ void fdwatch_del_fd(struct fdwatch_s *fw, int fd)
{
int pollndx;
nvdbg("fd: %d\n", fd);
fwvdbg("fd: %d\n", fd);
fdwatch_dump("Before deleting:", fw);
/* Get the index associated with the fd */
@ -261,11 +292,11 @@ int fdwatch(struct fdwatch_s *fw, long timeout_msecs)
*/
fdwatch_dump("Before waiting:", fw);
nvdbg("Waiting... (timeout %d)\n", timeout_msecs);
fwvdbg("Waiting... (timeout %d)\n", timeout_msecs);
fw->nactive = 0;
fw->next = 0;
ret = poll(fw->pollfds, fw->nwatched, (int)timeout_msecs);
nvdbg("Awakened: %d\n", ret);
fwvdbg("Awakened: %d\n", ret);
/* Look through all of the descriptors and make a list of all of them than
* have activity.
@ -281,7 +312,7 @@ int fdwatch(struct fdwatch_s *fw, long timeout_msecs)
{
/* Yes... save it in a shorter list */
nvdbg("pollndx: %d fd: %d revents: %04x\n",
fwvdbg("pollndx: %d fd: %d revents: %04x\n",
i, fw->pollfds[i].fd, fw->pollfds[i].revents);
fw->ready[fw->nactive++] = fw->pollfds[i].fd;
@ -297,7 +328,7 @@ int fdwatch(struct fdwatch_s *fw, long timeout_msecs)
/* Return the number of descriptors with activity */
nvdbg("nactive: %d\n", fw->nactive);
fwvdbg("nactive: %d\n", fw->nactive);
fdwatch_dump("After wakeup:", fw);
return ret;
}
@ -308,7 +339,7 @@ int fdwatch_check_fd(struct fdwatch_s *fw, int fd)
{
int pollndx;
nvdbg("fd: %d\n", fd);
fwvdbg("fd: %d\n", fd);
fdwatch_dump("Checking:", fw);
/* Get the index associated with the fd */
@ -319,7 +350,7 @@ int fdwatch_check_fd(struct fdwatch_s *fw, int fd)
return fw->pollfds[pollndx].revents & (POLLIN | POLLHUP | POLLNVAL);
}
nvdbg("POLLERR fd: %d\n", fd);
fwvdbg("POLLERR fd: %d\n", fd);
return 0;
}
@ -328,11 +359,11 @@ void *fdwatch_get_next_client_data(struct fdwatch_s *fw)
fdwatch_dump("Before getting client data:", fw);
if (fw->next >= fw->nwatched)
{
nvdbg("All client data returned: %d\n", fw->next);
fwvdbg("All client data returned: %d\n", fw->next);
return (void*)-1;
}
nvdbg("client_data[%d]: %p\n", fw->next, fw->client[fw->next]);
fwvdbg("client_data[%d]: %p\n", fw->next, fw->client[fw->next]);
return fw->client[fw->next++];
}

View File

@ -491,7 +491,7 @@ static inline int cgi_interpose_output(struct cgi_conn_s *cc)
httpd_realloc_str(&cc->outbuf.buffer, &cc->outbuf.size, cc->outbuf.len + nbytes_read);
(void)memcpy(&(cc->outbuf.buffer[cc->outbuf.len]), cc->inbuf.buffer, nbytes_read);
cc->outbuf.len += nbytes_read;
cc->outbuf.len += nbytes_read;
cc->outbuf.buffer[cc->outbuf.len] = '\0';
nllvdbg("Header bytes accumulated: %d\n", cc->outbuf.len);
@ -505,15 +505,17 @@ static inline int cgi_interpose_output(struct cgi_conn_s *cc)
}
else
{
/* Return. We will be called again when more data is available
* on the socket.
/* All of the headers have not yet been read ... Return. We
* will be called again when more data is available in the pipe
* connected to the CGI task.
*/
return 0;
}
}
}
break;
/* Otherwise, fall through and parse status in the HTTP headers */
case CGI_OUTBUFFER_HEADERREAD:
{
@ -587,7 +589,7 @@ static inline int cgi_interpose_output(struct cgi_conn_s *cc)
title = err404title;
break;
case 408:
case 408:
title = httpd_err408title;
break;
@ -650,6 +652,10 @@ static inline int cgi_interpose_output(struct cgi_conn_s *cc)
return 1;
}
}
else
{
cgi_dumpbuffer("Received from CGI:", cc->inbuf.buffer, nbytes_read);
}
}
while (nbytes_read < 0);
@ -667,7 +673,7 @@ static inline int cgi_interpose_output(struct cgi_conn_s *cc)
{
/* Forward the data from the CGI program to the client */
(void)httpd_write(cc->connfd, cc->inbuf.buffer, strlen(cc->inbuf.buffer));
(void)httpd_write(cc->connfd, cc->inbuf.buffer, nbytes_read);
}
}
break;

View File

@ -233,8 +233,6 @@ Timer *tmr_create(struct timeval *now, TimerProc *timer_proc,
/* Add the new timer to the proper active list. */
l_add(tmr);
nvdbg("Return: %p\n", tmr);
return tmr;
}
@ -330,8 +328,6 @@ void tmr_run(struct timeval *now)
void tmr_cancel(Timer *tmr)
{
nvdbg("tmr: %p\n", tmr);
/* Remove it from its active list. */
l_remove(tmr);