Skip to content

Commit bc78d00

Browse files
authored
Merge pull request #9 from Lawlieta/master
【修复】文件或文件名打开失败问题
2 parents 2bc618a + ce0cc35 commit bc78d00

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

module/wn_module_index.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int webnet_module_dirindex(struct webnet_session* session, int event)
4343
if (event == WEBNET_EVENT_URI_POST)
4444
{
4545
DIR *dir;
46+
struct stat file_stat;
4647
struct webnet_request *request;
4748
static const char* header = "<html><head><title>Index of %s</title></head><body bgcolor=\"white\"><h1>Index of %s</h1><hr><pre>";
4849
static const char* foot = "</pre><hr>WebNet/%s (RT-Thread)</body></html>";
@@ -51,6 +52,11 @@ int webnet_module_dirindex(struct webnet_session* session, int event)
5152
request = session->request;
5253
RT_ASSERT(request != RT_NULL);
5354

55+
if (stat(request->path, &file_stat) < 0 || !S_ISDIR(file_stat.st_mode))
56+
{
57+
return WEBNET_MODULE_CONTINUE;
58+
}
59+
5460
dir = opendir(request->path);
5561
if (dir != RT_NULL)
5662
{

src/wn_module.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static const struct webnet_session_ops _dofile_ops =
146146
int webnet_module_system_dofile(struct webnet_session *session)
147147
{
148148
int fd = -1; /* file descriptor */
149+
struct stat file_stat;
149150
const char *mimetype;
150151
rt_size_t file_length;
151152
struct webnet_request *request;
@@ -242,7 +243,7 @@ int webnet_module_system_dofile(struct webnet_session *session)
242243

243244
/* .gz not exist, use raw. */
244245
#endif /* WEBNET_USING_GZIP */
245-
if (fd < 0)
246+
if (fd < 0 && stat(request->path, &file_stat) >= 0 && !S_ISDIR(file_stat.st_mode))
246247
{
247248
fd = open(request->path, O_RDONLY, 0);
248249
}
@@ -536,17 +537,16 @@ int webnet_module_handle_uri(struct webnet_session *session)
536537
index = 0;
537538
while (default_files[index] != RT_NULL)
538539
{
540+
struct stat file_stat;
541+
539542
/* made a full path */
540543
rt_snprintf(full_path, WEBNET_PATH_MAX, "%s/%s%s",
541544
webnet_get_root(), request->path, default_files[index]);
542545
/* normalize path */
543546
str_normalize_path(full_path);
544547

545-
fd = open(full_path, O_RDONLY, 0);
546-
if (fd >= 0)
548+
if (stat(full_path, &file_stat) >= 0 && !S_ISDIR(file_stat.st_mode))
547549
{
548-
/* close file descriptor */
549-
close(fd);
550550
break;
551551
}
552552

0 commit comments

Comments
 (0)