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
This commit is contained in:
patacongo 2012-09-18 18:45:39 +00:00
parent 0e61e7ff2a
commit 45875a1993
3 changed files with 66 additions and 9 deletions

View File

@ -104,16 +104,20 @@
# define CONFIG_NETUTILS_HTTPD_TIMEOUT 0 # define CONFIG_NETUTILS_HTTPD_TIMEOUT 0
#endif #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 * 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 "; static const char g_httpcmdget[] = "GET ";
/**************************************************************************** /****************************************************************************
@ -131,6 +135,33 @@ static int httpd_open(const char *name, struct httpd_fs_file *file)
#endif #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) static int httpd_close(struct httpd_fs_file *file)
{ {
#if defined(CONFIG_NETUTILS_HTTPD_SENDFILE) #if defined(CONFIG_NETUTILS_HTTPD_SENDFILE)
@ -371,7 +402,7 @@ static int httpd_senderror(struct httpd_state *pstate, int status)
return ERROR; return ERROR;
} }
if (httpd_open(pstate->ht_filename, &pstate->ht_file) != OK) if (httpd_openindex(pstate) != OK)
{ {
char s[10 + 1]; char s[10 + 1];
@ -418,7 +449,7 @@ static int httpd_sendfile(struct httpd_state *pstate)
} }
#endif #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); ndbg("[%d] '%s' not found\n", pstate->ht_sockfd, pstate->ht_filename);
return httpd_senderror(pstate, 404); 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); ndbg("[%d] Missing path\n", pstate->ht_sockfd);
return httpd_senderror(pstate, 400); return httpd_senderror(pstate, 400);
} }
#if !defined(CONFIG_NETUTILS_HTTPD_SENDFILE) && !defined(CONFIG_NETUTILS_HTTPD_MMAP)
else if (pstate->ht_buffer[5] == ISO_space) 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 else
{ {
for (i = 0; for (i = 0;

View File

@ -89,6 +89,18 @@ int httpd_mmap_open(const char *name, struct httpd_fs_file *file)
return ERROR; 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) if (st.st_size > INT_MAX)
{ {
errno = EFBIG; errno = EFBIG;

View File

@ -89,6 +89,18 @@ int httpd_sendfile_open(const char *name, struct httpd_fs_file *file)
return ERROR; 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) if (st.st_size > INT_MAX || st.st_size > SIZE_MAX)
{ {
errno = EFBIG; errno = EFBIG;