Skip to content

Commit

Permalink
fix: Windows下支持中文路径 (#559)
Browse files Browse the repository at this point in the history
Signed-off-by: xth <xth@live.com>
  • Loading branch information
xth authored May 29, 2024
1 parent 031684d commit 6900f97
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions html/中文路径/中文名称.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
http://127.0.0.1:8080/中文路径/中文名称.txt
用于Windows中文路径测试
22 changes: 21 additions & 1 deletion http/server/FileCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions http/server/FileCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ class FileCache {
int stat_interval;
int expired_time;

FileCache() {
stat_interval = 10; // s
expired_time = 60; // s
}
FileCache();

struct OpenParam {
bool need_read;
Expand Down

0 comments on commit 6900f97

Please sign in to comment.