Przeglądaj źródła

【修复】文件或文件名打开失败问题

Signed-off-by: chenyong <1521761801@qq.com>
chenyong 6 lat temu
rodzic
commit
ce0cc3513f
2 zmienionych plików z 11 dodań i 5 usunięć
  1. 6 0
      module/wn_module_index.c
  2. 5 5
      src/wn_module.c

+ 6 - 0
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 = "<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>";
@@ -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)
         {

+ 5 - 5
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;
         }