Skip to content

Commit e909353

Browse files
authored
fix http url chinese directory bug
Optimize Windows Not Found request performance
1 parent 4712d97 commit e909353

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

http/server/FileCache.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "httpdef.h" // import http_content_type_str_by_suffix
99
#include "http_page.h" // import make_index_of_page
1010

11-
#ifdef _MSC_VER
11+
#ifdef OS_WIN
1212
#include <codecvt>
1313
#endif
1414

@@ -19,27 +19,27 @@ FileCache::FileCache() {
1919
expired_time = 60; // s
2020
}
2121

22-
static int hv_open(char const* filepath, int flags) {
23-
#ifdef _MSC_VER
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-
3322
file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
3423
std::lock_guard<std::mutex> locker(mutex_);
3524
file_cache_ptr fc = Get(filepath);
25+
#ifdef OS_WIN
26+
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
27+
std::wstring wfilepath;
28+
#endif
3629
bool modified = false;
3730
if (fc) {
3831
time_t now = time(NULL);
3932
if (now - fc->stat_time > stat_interval) {
40-
modified = fc->is_modified();
4133
fc->stat_time = now;
4234
fc->stat_cnt++;
35+
#ifdef OS_WIN
36+
wfilepath = conv.from_bytes(filepath);
37+
now = fc->st.st_mtime;
38+
_wstat(wfilepath.c_str(), (struct _stat*)&fc->st);
39+
modified = now != fc->st.st_mtime;
40+
#else
41+
modified = fc->is_modified();
42+
#endif
4343
}
4444
if (param->need_read) {
4545
if (!modified && fc->is_complete()) {
@@ -48,31 +48,35 @@ file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
4848
}
4949
}
5050
if (fc == NULL || modified || param->need_read) {
51+
struct stat st;
5152
int flags = O_RDONLY;
5253
#ifdef O_BINARY
5354
flags |= O_BINARY;
5455
#endif
55-
int fd = hv_open(filepath, flags);
56-
if (fd < 0) {
56+
int fd = -1;
5757
#ifdef OS_WIN
58-
// NOTE: open(dir) return -1 on windows
59-
if (!hv_isdir(filepath)) {
60-
param->error = ERR_OPEN_FILE;
61-
return NULL;
62-
}
63-
#else
58+
if(wfilepath.empty()) wfilepath = conv.from_bytes(filepath);
59+
// NOTE: Optimize Windows Not Found request performance
60+
if(_wstat(wfilepath.c_str(), (struct _stat*)&st) != 0) {
6461
param->error = ERR_OPEN_FILE;
6562
return NULL;
63+
}
64+
// NOTE: open(dir) return -1 on windows
65+
if(S_ISREG(st.st_mode)) {
66+
fd = _wopen(wfilepath.c_str(), flags);
67+
}else if(S_ISDIR(st.st_mode)) {
68+
fd = 0;
69+
}
70+
#else
71+
fd = open(filepath, flags);
6672
#endif
73+
if (fd < 0) {
74+
param->error = ERR_OPEN_FILE;
75+
return NULL;
6776
}
6877
defer(if (fd > 0) { close(fd); })
6978
if (fc == NULL) {
70-
struct stat st;
71-
if (fd > 0) {
72-
fstat(fd, &st);
73-
} else {
74-
stat(filepath, &st);
75-
}
79+
if (fd > 0) fstat(fd, &st);
7680
if (S_ISREG(st.st_mode) ||
7781
(S_ISDIR(st.st_mode) &&
7882
filepath[strlen(filepath)-1] == '/')) {

0 commit comments

Comments
 (0)