-
Notifications
You must be signed in to change notification settings - Fork 25
Open
Description
无法中文下载原因:
浏览器请求中文文件时,url会将中文自动编码为unicode,因此在下载中文文件时需要进行解码
解决方法,在httprequest.cpp中增加解码函数
//unicode解码
unsigned char FromHex(unsigned char x)
{
unsigned char y;
if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
else if (x >= '0' && x <= '9') y = x - '0';
else assert(0);
return y;
}
std::string UrlDecode(const std::string& str)
{
std::string strTemp = "";
size_t length = str.length();
for (size_t i = 0; i < length; i++)
{
if (str[i] == '+') strTemp += ' ';
else if (str[i] == '%')
{
assert(i + 2 < length);
unsigned char high = FromHex((unsigned char)str[++i]);
unsigned char low = FromHex((unsigned char)str[++i]);
strTemp += high * 16 + low;
}
else strTemp += str[i];
}
return strTemp;
}
const static std::regex regex_filesPath(".files.+");
然后在parsepath中增加一个条件判断
/*
解析地址 */
void HttpRequest::parsePath()
{
if (path == "/") path = "/index.html";
else if (DEFAULT_HTML.count(path))
path += ".html";
else if (path == "/list.json")
{
auto files = getFiles("./resources/files");
Json::Value root;
Json::Value file;
for (int i = 0; i < (int)files.size(); i ++)
{
file["filename"] = files[i];
root.append(file);
}
write_json("./resources/list.json", root);
}
// else if(path == "/files/+")
else if(regex_match(path,regex_filesPath)){
// cout<<"匹配到files"<<endl;
string newpath = "/files/";
string tobedecode = path.substr(7);
cout<<tobedecode<<endl;
newpath+=UrlDecode(tobedecode);
path = newpath;
}
}
这样可以解决get请求无法获取中文文件的问题
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels