-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
支持中文URL和中文静态文件名 #559
支持中文URL和中文静态文件名 #559
Conversation
http/server/FileCache.cpp
Outdated
stat_interval = 10; // s | ||
expired_time = 60; // s | ||
#ifdef OS_WIN | ||
std::setlocale(LC_CTYPE, "en_US.utf8"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看了下,你的主要实现就是调用了std::setlocale(LC_CTYPE, "en_US.utf8");
这个你也可以自行在外部调用吧,不必要非要在libhv内部去调用,可能不是所有用户都想如此设置。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
大神言之有理。那我改成windows宽字节版本的open调用再发一个PR吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commit已修改,使用如下方式支持非英文路径
#ifdef OS_WIN
static 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
http/server/FileCache.cpp
Outdated
expired_time = 60; // s | ||
} | ||
|
||
int hv_open(char const* filepath,int flags){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
内部函数加static,{前加空格
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改
http/server/FileCache.cpp
Outdated
|
||
int hv_open(char const* filepath,int flags){ | ||
#ifdef OS_WIN | ||
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个需要声明为static吗,多线程安全吗?
http/server/FileCache.cpp
Outdated
|
||
int hv_open(char const* filepath,int flags){ | ||
#ifdef OS_WIN | ||
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个需要声明为static吗,多线程安全吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一开始想着外部已经加了锁了,static一下性能好一点。不过从代码规范和安全角度,是不应该声明为static
http/server/FileCache.cpp
Outdated
int hv_open(char const* filepath,int flags){ | ||
#ifdef OS_WIN | ||
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv; | ||
auto wfilepath=conv.from_bytes(filepath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=前后加空格
PS: 中国人的软件必须支持中文O(∩_∩)O Signed-off-by: xth <xth@live.com>
已按要求一一修改。另外我想问一下 |
file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) { |
是有点大,该处不仅仅是锁cached_files这个map,锁住整个函数,是因为下面有对fc内部成员做修改的操作,为了避免多线程同时修改导致问题 |
针对这次的代码合并,我提2个缺点 2.这个改动之前,按照微软的常规方案,单字节字符串的编码,默认根据本机地区设置而匹配了一种单字节字符串编码方案,比如我是简体中文,那么他就视为我传入的是gbk编码的单字节字符串,如果我是韩国地区,他会视我传入的是韩文对应的单字节字符串编码。而这种微软的历史约定,可能已经使得历史的接口使用者已经确保传入了的单字节字符串为相关地区的单字节字符串编码。但是本次提交,使得本库在windows环境下,调用hv_open传入的const char*filepath要求调用者必须使用utf8格式对单字节字符串进行编码。这很可能会导致之前的库的使用者在直接升级libhv版本的情况下,莫名的遇到了文件路径无效而突然无法访问的问题。 微软是有相关的api供我们实现【utf8】 【utf16】 【本机默认单字节字符串编码】三者之间进行转换的。 另外这个是C语言标准里提供的转换函数https://zh.cppreference.com/w/c/string/multibyte |
综上,建议暂时撤销本次改动。至于如何改,需要再议 |
是的,这个废弃声明我也注意到了,建议调研下nginx、apache是怎么处理中文编码问题的,欢迎提PR优化下 |
Bug:
Windows下,对中文路径支持不佳,Http Server返回404
Fixed:
静态文件和目录支持中文
PS:
中国人的软件必须支持中文O(∩_∩)O