From 4322c23e43a0fd043156645382c8431bf55884e3 Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Sun, 2 Aug 2009 15:08:09 +0000
Subject: [PATCH] Initialize THTTPD integration changes

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2006 42af7a65-404d-4744-a932-0658087f49c3
---
 examples/thttpd/content/Makefile |  4 +--
 examples/thttpd/main.c           |  3 +-
 net/net_close.c                  |  3 +-
 netutils/thttpd/libhttpd.c       | 51 +++++++++++++++++++-------------
 netutils/thttpd/libhttpd.h       |  3 +-
 netutils/thttpd/thttpd.c         |  7 ++++-
 6 files changed, 43 insertions(+), 28 deletions(-)

diff --git a/examples/thttpd/content/Makefile b/examples/thttpd/content/Makefile
index 9df3ec93a1..1e9937aa65 100644
--- a/examples/thttpd/content/Makefile
+++ b/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/examples/thttpd/main.c b/examples/thttpd/main.c
index 2e77b39d7b..23c2725654 100644
--- a/examples/thttpd/main.c
+++ b/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/net/net_close.c b/net/net_close.c
index 4a486d9721..7432d2482c 100644
--- a/net/net_close.c
+++ b/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/netutils/thttpd/libhttpd.c b/netutils/thttpd/libhttpd.c
index f35b6a7677..32c147cac8 100644
--- a/netutils/thttpd/libhttpd.c
+++ b/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/netutils/thttpd/libhttpd.h b/netutils/thttpd/libhttpd.h
index aed4b81948..fc0aa490be 100644
--- a/netutils/thttpd/libhttpd.h
+++ b/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/netutils/thttpd/thttpd.c b/netutils/thttpd/thttpd.c
index a2aa1c330a..aac9e4425d 100644
--- a/netutils/thttpd/thttpd.c
+++ b/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)
     {
-- 
GitLab