Merged in masayuki2009/nuttx.apps/fix_webserver_with_period (pull request #183)

netutils/webserver: Fix directory listing containing period in name

Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>

Approved-by: Gregory Nutt <gnutt@nuttx.org>
This commit is contained in:
Masayuki Ishikawa 2019-07-06 13:05:45 +00:00 committed by Gregory Nutt
parent 2bb4834775
commit 9eff955556
3 changed files with 42 additions and 4 deletions

View File

@ -199,6 +199,7 @@ uint16_t httpd_fs_count(char *name);
int httpd_send_datachunk(int sockfd, void *data, int len, bool chunked);
#ifdef CONFIG_NETUTILS_HTTPD_DIRLIST
bool httpd_is_file(FAR const char *filename);
ssize_t httpd_dirlist(int outfd, FAR struct httpd_fs_file *file);
#endif

View File

@ -494,11 +494,7 @@ static int send_headers(struct httpd_state *pstate, int status, int len)
ptr = strrchr(pstate->ht_filename, ISO_period);
if (ptr == NULL)
{
#ifdef CONFIG_NETUTILS_HTTPD_DIRLIST
mime = "text/html";
#else
mime = "application/octet-stream";
#endif
}
else
{
@ -514,6 +510,15 @@ static int send_headers(struct httpd_state *pstate, int status, int len)
}
}
#ifdef CONFIG_NETUTILS_HTTPD_DIRLIST
if (false == httpd_is_file(pstate->ht_filename))
{
/* we assume that it's a directory */
mime = "text/html";
}
#endif
if (len >= 0)
{
(void)snprintf(contentlen, HTTPD_MAX_CONTENTLEN,

View File

@ -114,6 +114,38 @@
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: httpd_is_file
****************************************************************************/
bool httpd_is_file(FAR const char *filename)
{
char *path;
int fd;
bool ret = false;
path = malloc(CONFIG_NAME_MAX);
ASSERT(path);
snprintf(path, CONFIG_NAME_MAX, "%s/%s",
CONFIG_NETUTILS_HTTPD_PATH, filename);
fd = open(path, "O_RDONLY");
if (-1 != fd)
{
close(fd);
ret = true;
}
free(path);
return ret;
}
/****************************************************************************
* Name: httpd_dirlist
****************************************************************************/
ssize_t httpd_dirlist(int outfd, FAR struct httpd_fs_file *file)
{
struct dirent *dent;