Skip to content

Commit ba162bd

Browse files
authored
fix defaultLargeFileHandler not support chinese path (#720)
add hv::utf8_to_ansi and hv::ansi_to_utf8 function to hstring.h
1 parent 05bd1ca commit ba162bd

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

cpputil/hstring.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,24 +226,25 @@ std::string NetAddr::to_string(const char* ip, int port) {
226226
}
227227

228228
#ifdef OS_WIN
229-
std::string wchar_to_utf8(const std::wstring &wstr) {
229+
std::string wchar_to_string(const UINT codePage, const std::wstring &wstr) {
230230
std::string str(4 * wstr.size() + 1, '\0');
231231
str.resize(WideCharToMultiByte(
232-
CP_UTF8, 0,
233-
wstr.c_str(), wstr.size(),
234-
const_cast<char*>(str.data()), str.size(),
232+
codePage, 0,
233+
wstr.c_str(), wstr.size(),
234+
const_cast<char*>(str.data()), str.size(),
235235
NULL, NULL));
236236
return str;
237237
}
238238

239-
std::wstring utf8_to_wchar(const std::string &str) {
240-
std::wstring wstr(2 * str.size() + 1, '\0');
239+
std::wstring string_to_wchar(const UINT codePage, const std::string &str) {
240+
std::wstring wstr(2 * str.size() + 2, '\0');
241241
wstr.resize(MultiByteToWideChar(
242-
CP_UTF8, 0,
243-
str.c_str(), str.size(),
242+
codePage, 0,
243+
str.c_str(), str.size(),
244244
const_cast<wchar_t*>(wstr.data()), wstr.size()));
245245
return wstr;
246246
}
247+
247248
#endif // OS_WIN
248249

249250
} // end namespace hv

cpputil/hstring.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,31 @@ struct HV_EXPORT NetAddr {
8888
static std::string to_string(const char* ip, int port);
8989
};
9090

91-
// windows wchar and utf8 conver
91+
// windows wchar and utf8/ansi conver
9292
#ifdef OS_WIN
93-
HV_EXPORT std::string wchar_to_utf8(const std::wstring &wstr);
94-
HV_EXPORT std::wstring utf8_to_wchar(const std::string &str);
93+
HV_EXPORT std::string wchar_to_string(const UINT codePage, const std::wstring &wstr);
94+
HV_EXPORT std::wstring string_to_wchar(const UINT codePage, const std::string &str);
95+
96+
HV_INLINE std::string wchar_to_utf8(const std::wstring &wstr) {
97+
return wchar_to_string(CP_UTF8, wstr);
98+
}
99+
100+
HV_INLINE std::string wchar_to_ansi(const std::wstring &wstr) {
101+
return wchar_to_string(CP_ACP, wstr);
102+
}
103+
104+
HV_INLINE std::wstring utf8_to_wchar(const std::string &str) {
105+
return string_to_wchar(CP_UTF8, str);
106+
}
107+
108+
HV_INLINE std::string utf8_to_ansi(const std::string &str) {
109+
return wchar_to_string(CP_ACP, string_to_wchar(CP_UTF8, str));
110+
}
111+
112+
HV_INLINE std::string ansi_to_utf8(const std::string &str) {
113+
return wchar_to_string(CP_UTF8, string_to_wchar(CP_ACP, str));
114+
}
115+
95116
#endif // OS_WIN
96117

97118
} // end namespace hv

http/server/HttpHandler.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ int HttpHandler::defaultStaticHandler() {
585585
if (service->largeFileHandler) {
586586
status_code = customHttpHandler(service->largeFileHandler);
587587
} else {
588-
status_code = defaultLargeFileHandler();
588+
status_code = defaultLargeFileHandler(filepath);
589589
}
590590
}
591591
return status_code;
@@ -604,7 +604,7 @@ int HttpHandler::defaultStaticHandler() {
604604
if (service->largeFileHandler) {
605605
status_code = customHttpHandler(service->largeFileHandler);
606606
} else {
607-
status_code = defaultLargeFileHandler();
607+
status_code = defaultLargeFileHandler(filepath);
608608
}
609609
} else {
610610
status_code = HTTP_STATUS_NOT_FOUND;
@@ -629,10 +629,9 @@ int HttpHandler::defaultStaticHandler() {
629629
return status_code;
630630
}
631631

632-
int HttpHandler::defaultLargeFileHandler() {
632+
int HttpHandler::defaultLargeFileHandler(const std::string &filepath) {
633633
if (!writer) return HTTP_STATUS_NOT_IMPLEMENTED;
634634
if (!isFileOpened()) {
635-
std::string filepath = service->GetStaticFilepath(req->Path().c_str());
636635
if (filepath.empty() || openFile(filepath.c_str()) != 0) {
637636
return HTTP_STATUS_NOT_FOUND;
638637
}
@@ -874,7 +873,11 @@ int HttpHandler::openFile(const char* filepath) {
874873
closeFile();
875874
file = new LargeFile;
876875
file->timer = INVALID_TIMER_ID;
876+
#ifdef OS_WIN
877+
return file->open(hv::utf8_to_ansi(filepath).c_str(), "rb");
878+
#else
877879
return file->open(filepath, "rb");
880+
#endif
878881
}
879882

880883
bool HttpHandler::isFileOpened() {

http/server/HttpHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class HttpHandler {
156156
// default handlers
157157
int defaultRequestHandler();
158158
int defaultStaticHandler();
159-
int defaultLargeFileHandler();
159+
int defaultLargeFileHandler(const std::string &filepath);
160160
int defaultErrorHandler();
161161
int customHttpHandler(const http_handler& handler);
162162
int invokeHttpHandler(const http_handler* handler);

0 commit comments

Comments
 (0)