summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-09-18 18:45:39 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2012-09-18 18:45:39 +0000
commit93ee8262f526db8d9b73dcb21b1a94054d5a064b (patch)
tree57e05816aff54967e701523653dd1dfd3155be7f
parent9d045b959ec699d0bc459ed965e042a185307a37 (diff)
downloadnuttx-93ee8262f526db8d9b73dcb21b1a94054d5a064b.tar.gz
nuttx-93ee8262f526db8d9b73dcb21b1a94054d5a064b.tar.bz2
nuttx-93ee8262f526db8d9b73dcb21b1a94054d5a064b.zip
Add default file name if URL is a directory, giving index.html behavior. From Kate
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5162 42af7a65-404d-4744-a932-0658087f49c3
-rw-r--r--apps/netutils/webserver/httpd.c51
-rw-r--r--apps/netutils/webserver/httpd_mmap.c12
-rw-r--r--apps/netutils/webserver/httpd_sendfile.c12
3 files changed, 66 insertions, 9 deletions
diff --git a/apps/netutils/webserver/httpd.c b/apps/netutils/webserver/httpd.c
index 3421a43c9..29bc078ba 100644
--- a/apps/netutils/webserver/httpd.c
+++ b/apps/netutils/webserver/httpd.c
@@ -104,16 +104,20 @@
# define CONFIG_NETUTILS_HTTPD_TIMEOUT 0
#endif
+#if !defined(CONFIG_NETUTILS_HTTPD_SENDFILE) && !defined(CONFIG_NETUTILS_HTTPD_MMAP)
+# ifndef CONFIG_NETUTILS_HTTPD_INDEX
+# ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
+# define CONFIG_NETUTILS_HTTPD_INDEX "index.shtml"
+# else
+# define CONFIG_NETUTILS_HTTPD_INDEX "index.html"
+# endif
+# endif
+#endif
+
/****************************************************************************
* Private Data
****************************************************************************/
-#ifndef CONFIG_NETUTILS_HTTPD_SCRIPT_DISABLE
-static const char g_httpindexpath[] = "/index.shtml";
-#else
-static const char g_httpindexpath[] = "/index.html";
-#endif
-
static const char g_httpcmdget[] = "GET ";
/****************************************************************************
@@ -131,6 +135,33 @@ static int httpd_open(const char *name, struct httpd_fs_file *file)
#endif
}
+static int httpd_openindex(struct httpd_state *pstate)
+{
+ int ret;
+ size_t z;
+
+ z = strlen(pstate->ht_filename);
+ if (z > 0 && pstate->ht_filename[z - 1] == '/')
+ {
+ pstate->ht_filename[--z] = '\0';
+ }
+
+ ret = httpd_open(pstate->ht_filename, &pstate->ht_file);
+#if defined(CONFIG_NETUTILS_HTTPD_SENDFILE) || defined(CONFIG_NETUTILS_HTTPD_MMAP)
+# if defined(CONFIG_NETUTILS_HTTPD_INDEX)
+ if (ret == ERROR && errno == EISDIR)
+ {
+ (void) snprintf(pstate->ht_filename + z, sizeof pstate->ht_filename - z, "/%s",
+ CONFIG_NETUTILS_HTTPD_INDEX);
+
+ ret = httpd_open(pstate->ht_filename, &pstate->ht_file);
+ }
+# endif
+#endif
+
+ return ret;
+}
+
static int httpd_close(struct httpd_fs_file *file)
{
#if defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
@@ -371,7 +402,7 @@ static int httpd_senderror(struct httpd_state *pstate, int status)
return ERROR;
}
- if (httpd_open(pstate->ht_filename, &pstate->ht_file) != OK)
+ if (httpd_openindex(pstate) != OK)
{
char s[10 + 1];
@@ -418,7 +449,7 @@ static int httpd_sendfile(struct httpd_state *pstate)
}
#endif
- if (httpd_open(pstate->ht_filename, &pstate->ht_file) != OK)
+ if (httpd_openindex(pstate) != OK)
{
ndbg("[%d] '%s' not found\n", pstate->ht_sockfd, pstate->ht_filename);
return httpd_senderror(pstate, 404);
@@ -493,10 +524,12 @@ static inline int httpd_cmd(struct httpd_state *pstate)
ndbg("[%d] Missing path\n", pstate->ht_sockfd);
return httpd_senderror(pstate, 400);
}
+#if !defined(CONFIG_NETUTILS_HTTPD_SENDFILE) && !defined(CONFIG_NETUTILS_HTTPD_MMAP)
else if (pstate->ht_buffer[5] == ISO_space)
{
- strncpy(pstate->ht_filename, g_httpindexpath, strlen(g_httpindexpath));
+ strncpy(pstate->ht_filename, "/" CONFIG_NETUTILS_HTTPD_INDEX, strlen("/" CONFIG_NETUTILS_HTTPD_INDEX));
}
+#endif
else
{
for (i = 0;
diff --git a/apps/netutils/webserver/httpd_mmap.c b/apps/netutils/webserver/httpd_mmap.c
index 9777df3b3..042c9932f 100644
--- a/apps/netutils/webserver/httpd_mmap.c
+++ b/apps/netutils/webserver/httpd_mmap.c
@@ -89,6 +89,18 @@ int httpd_mmap_open(const char *name, struct httpd_fs_file *file)
return ERROR;
}
+ if (S_ISDIR(st.st_mode))
+ {
+ errno = EISDIR;
+ return ERROR;
+ }
+
+ if (!S_ISREG(st.st_mode))
+ {
+ errno = ENOENT;
+ return ERROR;
+ }
+
if (st.st_size > INT_MAX)
{
errno = EFBIG;
diff --git a/apps/netutils/webserver/httpd_sendfile.c b/apps/netutils/webserver/httpd_sendfile.c
index 6a6ec24ab..a60144d04 100644
--- a/apps/netutils/webserver/httpd_sendfile.c
+++ b/apps/netutils/webserver/httpd_sendfile.c
@@ -89,6 +89,18 @@ int httpd_sendfile_open(const char *name, struct httpd_fs_file *file)
return ERROR;
}
+ if (S_ISDIR(st.st_mode))
+ {
+ errno = EISDIR;
+ return ERROR;
+ }
+
+ if (!S_ISREG(st.st_mode))
+ {
+ errno = ENOENT;
+ return ERROR;
+ }
+
if (st.st_size > INT_MAX || st.st_size > SIZE_MAX)
{
errno = EFBIG;