Parcourir la source

fix: Windows下支持中文路径 (#559)

Signed-off-by: xth <xth@live.com>
xth il y a 1 an
Parent
commit
6900f97a50
3 fichiers modifiés avec 24 ajouts et 5 suppressions
  1. 2 0
      html/中文路径/中文名称.txt
  2. 21 1
      http/server/FileCache.cpp
  3. 1 4
      http/server/FileCache.h

+ 2 - 0
html/中文路径/中文名称.txt

@@ -0,0 +1,2 @@
+http://127.0.0.1:8080/中文路径/中文名称.txt
+用于Windows中文路径测试

+ 21 - 1
http/server/FileCache.cpp

@@ -8,8 +8,28 @@
 #include "httpdef.h"    // import http_content_type_str_by_suffix
 #include "http_page.h"  // import make_index_of_page
 
+#ifdef OS_WIN
+#include <codecvt>
+#endif
+
 #define ETAG_FMT    "\"%zx-%zx\""
 
+FileCache::FileCache() {
+    stat_interval = 10; // s
+    expired_time  = 60; // s
+}
+
+static int hv_open(char const* filepath, int flags) {
+#ifdef OS_WIN
+    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
+    auto wfilepath = conv.from_bytes(filepath);
+    int fd = _wopen(wfilepath.c_str(), flags);
+#else
+    int fd = open(filepath, flags);
+#endif
+    return fd;
+}
+
 file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
     std::lock_guard<std::mutex> locker(mutex_);
     file_cache_ptr fc = Get(filepath);
@@ -32,7 +52,7 @@ file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
 #ifdef O_BINARY
         flags |= O_BINARY;
 #endif
-        int fd = open(filepath, flags);
+        int fd = hv_open(filepath, flags);
         if (fd < 0) {
 #ifdef OS_WIN
             // NOTE: open(dir) return -1 on windows

+ 1 - 4
http/server/FileCache.h

@@ -64,10 +64,7 @@ public:
     int             stat_interval;
     int             expired_time;
 
-    FileCache() {
-        stat_interval = 10; // s
-        expired_time  = 60; // s
-    }
+    FileCache();
 
     struct OpenParam {
         bool need_read;