Skip to content

Commit 6900f97

Browse files
authored
fix: Windows下支持中文路径 (#559)
Signed-off-by: xth <[email protected]>
1 parent 031684d commit 6900f97

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

html/中文路径/中文名称.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
http://127.0.0.1:8080/中文路径/中文名称.txt
2+
用于Windows中文路径测试

http/server/FileCache.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,28 @@
88
#include "httpdef.h" // import http_content_type_str_by_suffix
99
#include "http_page.h" // import make_index_of_page
1010

11+
#ifdef OS_WIN
12+
#include <codecvt>
13+
#endif
14+
1115
#define ETAG_FMT "\"%zx-%zx\""
1216

17+
FileCache::FileCache() {
18+
stat_interval = 10; // s
19+
expired_time = 60; // s
20+
}
21+
22+
static int hv_open(char const* filepath, int flags) {
23+
#ifdef OS_WIN
24+
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
25+
auto wfilepath = conv.from_bytes(filepath);
26+
int fd = _wopen(wfilepath.c_str(), flags);
27+
#else
28+
int fd = open(filepath, flags);
29+
#endif
30+
return fd;
31+
}
32+
1333
file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
1434
std::lock_guard<std::mutex> locker(mutex_);
1535
file_cache_ptr fc = Get(filepath);
@@ -32,7 +52,7 @@ file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
3252
#ifdef O_BINARY
3353
flags |= O_BINARY;
3454
#endif
35-
int fd = open(filepath, flags);
55+
int fd = hv_open(filepath, flags);
3656
if (fd < 0) {
3757
#ifdef OS_WIN
3858
// NOTE: open(dir) return -1 on windows

http/server/FileCache.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ class FileCache {
6464
int stat_interval;
6565
int expired_time;
6666

67-
FileCache() {
68-
stat_interval = 10; // s
69-
expired_time = 60; // s
70-
}
67+
FileCache();
7168

7269
struct OpenParam {
7370
bool need_read;

0 commit comments

Comments
 (0)