From 91c5b43fb0efbb6c523bb558d8ae4297e623b35b Mon Sep 17 00:00:00 2001 From: patacongo Date: Sun, 2 Aug 2009 15:08:09 +0000 Subject: Initialize THTTPD integration changes git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2006 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/examples/thttpd/content/Makefile | 4 +-- nuttx/examples/thttpd/main.c | 3 +- nuttx/net/net_close.c | 3 +- nuttx/netutils/thttpd/libhttpd.c | 51 ++++++++++++++++++++-------------- nuttx/netutils/thttpd/libhttpd.h | 3 +- nuttx/netutils/thttpd/thttpd.c | 7 ++++- 6 files changed, 43 insertions(+), 28 deletions(-) diff --git a/nuttx/examples/thttpd/content/Makefile b/nuttx/examples/thttpd/content/Makefile index 9df3ec93a..1e9937aa6 100644 --- a/nuttx/examples/thttpd/content/Makefile +++ b/nuttx/examples/thttpd/content/Makefile @@ -70,10 +70,10 @@ install: $(foreach DIR, $(SUBDIRS), $(DIR)_install) # Create the romfs directory $(ROMFS_DIR): - @mkdir $(ROMFS_DIR) + @mkdir -p $(ROMFS_DIR) $(ROMFSCGI_DIR): $(ROMFS_DIR) - @mkdir $(ROMFSCGI_DIR) + @mkdir -p $(ROMFSCGI_DIR) # Populate the romfs directory diff --git a/nuttx/examples/thttpd/main.c b/nuttx/examples/thttpd/main.c index 2e77b39d7..23c272565 100644 --- a/nuttx/examples/thttpd/main.c +++ b/nuttx/examples/thttpd/main.c @@ -228,7 +228,8 @@ int user_start(int argc, char *argv[]) g_thttpdsymtab = exports; g_thttpdnsymbols = NEXPORTS; - printf("Starting THTTPD\n"); + message("Starting THTTPD\n"); thttpd_main(1, &thttpd_argv); + message("THTTPD terminated\n"); return 0; } diff --git a/nuttx/net/net_close.c b/nuttx/net/net_close.c index 4a486d972..7432d2482 100644 --- a/nuttx/net/net_close.c +++ b/nuttx/net/net_close.c @@ -245,8 +245,9 @@ int net_close(int sockfd) { /* Yes... free the connection structure */ - uip_unlisten(conn); /* No longer accepting connections */ + uip_unlisten(conn); /* No longer accepting connections */ netclose_disconnect(psock); /* Break any current connections */ + conn->crefs = 0; /* No more references on the connection */ uip_tcpfree(conn); /* Free uIP resources */ } else diff --git a/nuttx/netutils/thttpd/libhttpd.c b/nuttx/netutils/thttpd/libhttpd.c index f35b6a767..32c147cac 100644 --- a/nuttx/netutils/thttpd/libhttpd.c +++ b/nuttx/netutils/thttpd/libhttpd.c @@ -272,16 +272,19 @@ char *httpd_err503form = "The requested URL '%s' is temporarily overloaded. static void free_httpd_server(httpd_server * hs) { - if (hs->binding_hostname) + if (hs) { - free((void *)hs->binding_hostname); - } + if (hs->hostname) + { + free(hs->hostname); + } - if (hs->cwd) - { - free((void *)hs->cwd); + if (hs->cwd) + { + free(hs->cwd); + } + free(hs); } - free((void *)hs); } static int initialize_listen_socket(httpd_sockaddr *saP) @@ -300,10 +303,11 @@ static int initialize_listen_socket(httpd_sockaddr *saP) /* Create socket. */ + nvdbg("Create listen socket\n"); listen_fd = socket(saP->sin_family, SOCK_STREAM, 0); if (listen_fd < 0) { - ndbg("socket %s: %d\n", httpd_ntoa(saP), errno); + ndbg("socket failed: %d\n", errno); return -1; } @@ -312,14 +316,14 @@ static int initialize_listen_socket(httpd_sockaddr *saP) on = 1; if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) { - ndbg("setsockopt SO_REUSEADDR: %d\n", errno); + ndbg("setsockopt(SO_REUSEADDR) failed: %d\n", errno); } /* Bind to it. */ if (bind(listen_fd, (struct sockaddr*)&saP, sockaddr_len(saP)) < 0) { - ndbg("bind %s: %d\n", httpd_ntoa(saP), errno); + ndbg("bind to %s failed: %d\n", httpd_ntoa(saP), errno); (void)close(listen_fd); return -1; } @@ -329,14 +333,14 @@ static int initialize_listen_socket(httpd_sockaddr *saP) flags = fcntl(listen_fd, F_GETFL, 0); if (flags == -1) { - ndbg("fcntl F_GETFL: %d\n", errno); + ndbg("fcntl(F_GETFL) failed: %d\n", errno); (void)close(listen_fd); return -1; } if (fcntl(listen_fd, F_SETFL, flags | O_NDELAY) < 0) { - ndbg("fcntl O_NDELAY: %d\n", errno); + ndbg("fcntl(O_NDELAY) failed: %d\n", errno); (void)close(listen_fd); return -1; } @@ -345,7 +349,7 @@ static int initialize_listen_socket(httpd_sockaddr *saP) if (listen(listen_fd, CONFIG_THTTPD_LISTEN_BACKLOG) < 0) { - ndbg("listen: %d\n", errno); + ndbg("listen failed: %d\n", errno); (void)close(listen_fd); return -1; } @@ -2053,7 +2057,7 @@ static void create_environment(httpd_conn *hc) else #endif { - cp = hc->hs->server_hostname; + cp = hc->hs->hostname; } if (cp) @@ -3163,7 +3167,7 @@ static int check_referer(httpd_conn *hc) else #endif { - cp = hc->hs->server_hostname; + cp = hc->hs->hostname; } if (cp == NULL) @@ -3247,7 +3251,7 @@ static int really_check_referer(httpd_conn *hc) #ifndef CONFIG_THTTPD_VHOST /* Not vhosting, use the server name. */ - lp = hs->server_hostname; + lp = hs->hostname; if (!lp) { /* Couldn't figure out local hostname - give up. */ @@ -3332,13 +3336,15 @@ FAR httpd_server *httpd_initialize(FAR httpd_sockaddr *sa, FAR const char *cwd) { FAR httpd_server *hs; + nvdbg("cwd: %s\n", cwd); + /* Save the PID of the main thread */ main_thread = getpid(); /* Allocate the server structure */ - hs = NEW(httpd_server, 1); + hs = (FAR httpd_server *)zalloc(sizeof(httpd_server)); if (!hs) { ndbg("out of memory allocating an httpd_server\n"); @@ -3346,12 +3352,13 @@ FAR httpd_server *httpd_initialize(FAR httpd_sockaddr *sa, FAR const char *cwd) } #ifdef CONFIG_THTTPD_HOSTNAME - hs->server_hostname = strdup(CONFIG_THTTPD_HOSTNAME); + hs->hostname = strdup(CONFIG_THTTPD_HOSTNAME); #else - hs->server_hostname = strdup(httpd_ntoa(sa)); + hs->hostname = strdup(httpd_ntoa(sa)); #endif + nvdbg("hostname: %s\n", hs->hostname); - if (!hs->server_hostname) + if (!hs->hostname) { ndbg("out of memory copying hostname\n"); return NULL; @@ -3370,10 +3377,12 @@ FAR httpd_server *httpd_initialize(FAR httpd_sockaddr *sa, FAR const char *cwd) hs->listen_fd = initialize_listen_socket(sa); if (hs->listen_fd == -1) { + ndbg("Failed to create listen socket\n"); free_httpd_server(hs); - return (httpd_server *) 0; + return NULL; } + nvdbg("Calling init_mime()\n"); init_mime(); /* Done initializing. */ diff --git a/nuttx/netutils/thttpd/libhttpd.h b/nuttx/netutils/thttpd/libhttpd.h index aed4b8194..fc0aa490b 100644 --- a/nuttx/netutils/thttpd/libhttpd.h +++ b/nuttx/netutils/thttpd/libhttpd.h @@ -114,8 +114,7 @@ typedef struct sockaddr_in httpd_sockaddr; typedef struct { - char *binding_hostname; - char *server_hostname; + char *hostname; int cgi_count; char *cwd; int listen_fd; diff --git a/nuttx/netutils/thttpd/thttpd.c b/nuttx/netutils/thttpd/thttpd.c index a2aa1c330..aac9e4425 100644 --- a/nuttx/netutils/thttpd/thttpd.c +++ b/nuttx/netutils/thttpd/thttpd.c @@ -175,7 +175,7 @@ static void shut_down(void) } } - if (hs != (httpd_server *) 0) + if (hs) { httpd_server *ths = hs; hs = (httpd_server *) 0; @@ -747,6 +747,8 @@ int thttpd_main(int argc, char **argv) int ret; #endif + nvdbg("THTTPD started\n"); + /* Setup host address */ #ifdef CONFIG_NET_IPv6 @@ -803,9 +805,11 @@ int thttpd_main(int argc, char **argv) /* Initialize the HTTP layer */ + nvdbg("Calling httpd_initialize()\n"); hs = httpd_initialize(&sa, cwd); if (!hs) { + ndbg("httpd_initialize() failed\n"); exit(1); } @@ -870,6 +874,7 @@ int thttpd_main(int argc, char **argv) /* Main loop */ + nvdbg("Entering the main loop\n"); (void)gettimeofday(&tv, (struct timezone *)0); while ((!terminate) || num_connects > 0) { -- cgit v1.2.3