From ce0cc3513f16816f4837f1934c980cfcd48426e7 Mon Sep 17 00:00:00 2001
From: chenyong <1521761801@qq.com>
Date: Thu, 11 Apr 2019 13:40:54 +0800
Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E6=96=87?=
=?UTF-8?q?=E4=BB=B6=E6=88=96=E6=96=87=E4=BB=B6=E5=90=8D=E6=89=93=E5=BC=80?=
=?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: chenyong <1521761801@qq.com>
---
module/wn_module_index.c | 6 ++++++
src/wn_module.c | 10 +++++-----
2 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/module/wn_module_index.c b/module/wn_module_index.c
index 6322610..3c3e200 100644
--- a/module/wn_module_index.c
+++ b/module/wn_module_index.c
@@ -43,6 +43,7 @@ int webnet_module_dirindex(struct webnet_session* session, int event)
if (event == WEBNET_EVENT_URI_POST)
{
DIR *dir;
+ struct stat file_stat;
struct webnet_request *request;
static const char* header = "
Index of %sIndex of %s
";
static const char* foot = "
WebNet/%s (RT-Thread)";
@@ -51,6 +52,11 @@ int webnet_module_dirindex(struct webnet_session* session, int event)
request = session->request;
RT_ASSERT(request != RT_NULL);
+ if (stat(request->path, &file_stat) < 0 || !S_ISDIR(file_stat.st_mode))
+ {
+ return WEBNET_MODULE_CONTINUE;
+ }
+
dir = opendir(request->path);
if (dir != RT_NULL)
{
diff --git a/src/wn_module.c b/src/wn_module.c
index 17276f9..0b9af0f 100644
--- a/src/wn_module.c
+++ b/src/wn_module.c
@@ -146,6 +146,7 @@ static const struct webnet_session_ops _dofile_ops =
int webnet_module_system_dofile(struct webnet_session *session)
{
int fd = -1; /* file descriptor */
+ struct stat file_stat;
const char *mimetype;
rt_size_t file_length;
struct webnet_request *request;
@@ -242,7 +243,7 @@ int webnet_module_system_dofile(struct webnet_session *session)
/* .gz not exist, use raw. */
#endif /* WEBNET_USING_GZIP */
- if (fd < 0)
+ if (fd < 0 && stat(request->path, &file_stat) >= 0 && !S_ISDIR(file_stat.st_mode))
{
fd = open(request->path, O_RDONLY, 0);
}
@@ -536,17 +537,16 @@ int webnet_module_handle_uri(struct webnet_session *session)
index = 0;
while (default_files[index] != RT_NULL)
{
+ struct stat file_stat;
+
/* made a full path */
rt_snprintf(full_path, WEBNET_PATH_MAX, "%s/%s%s",
webnet_get_root(), request->path, default_files[index]);
/* normalize path */
str_normalize_path(full_path);
- fd = open(full_path, O_RDONLY, 0);
- if (fd >= 0)
+ if (stat(full_path, &file_stat) >= 0 && !S_ISDIR(file_stat.st_mode))
{
- /* close file descriptor */
- close(fd);
break;
}