Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions module/wn_module_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<html><head><title>Index of %s</title></head><body bgcolor=\"white\"><h1>Index of %s</h1><hr><pre>";
static const char* foot = "</pre><hr>WebNet/%s (RT-Thread)</body></html>";
Expand All @@ -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)
{
Expand Down
10 changes: 5 additions & 5 deletions src/wn_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down