Skip to content

Commit 60f6081

Browse files
committed
* 修复http://bbb.com/xxx/xxx.html 这类型url获取到的文件名不对的问题
1 parent f4f98d6 commit 60f6081

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

mbvip/common/UrlUtil.h

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,53 @@
11

2-
#ifndef common_UrlUtil_h
3-
#define common_UrlUtil_h
2+
#ifndef content_UrlUtil_h
3+
#define content_UrlUtil_h
44

5-
#include "common/StringUtil.h"
5+
#include "content/common/StringUtil.h"
6+
#include "content/common/mbchar.h"
7+
8+
namespace content {
69

710
class UrlUtil {
811
public:
12+
// 从后往前找这几个字符串
13+
static int backwardSearch(const std::string& input)
14+
{
15+
std::string targets[] = { "\\", "//", "/", "%2f", "%2F", "%5c", "%5C" };
16+
for (int i = input.length() - 1; i >= 0; --i) {
17+
for (const auto& target : targets) {
18+
if (i >= static_cast<int>(target.length()) - 1) {
19+
std::string substr = input.substr(i - target.length() + 1, target.length());
20+
if (substr == target) {
21+
return i + 1;
22+
}
23+
}
24+
}
25+
}
26+
return -1;
27+
}
28+
929
// "1234-sss.ss?sz" -> "1234-sss.ss"
1030
// "1234-sss.ss/" -> "1234-sss.ss"
11-
static std::wstring getSaveNameFromUrl(const std::string& url)
31+
static std::u16string getSaveNameFromUrl(const std::string& url)
1232
{
1333
if (0 == url.size())
14-
return L"";
15-
16-
size_t pos1 = url.find_last_of('\\');
17-
if (std::string::npos == pos1)
18-
pos1 = url.size() - 1;
19-
else
20-
pos1++;
21-
22-
size_t pos2 = url.find_last_of('/');
23-
if (std::string::npos == pos2)
24-
pos2 = url.size() - 1;
25-
else
26-
pos2++;
27-
28-
size_t pos = pos1 < pos2 ? pos1 : pos2;
29-
if (std::string::npos == pos || url.size() - 1 == pos)
34+
return std::u16string((const char16_t*)mbu16(""));
35+
36+
// size_t pos1 = url.find_last_of('\\');
37+
// if (std::string::npos == pos1)
38+
// pos1 = url.size() - 1;
39+
// else
40+
// pos1++;
41+
//
42+
// size_t pos2 = url.find_last_of('/');
43+
// if (std::string::npos == pos2)
44+
// pos2 = url.size() - 1;
45+
// else
46+
// pos2++;
47+
//
48+
// size_t pos = pos1 < pos2 ? pos1 : pos2;
49+
int pos = backwardSearch(url);
50+
if (-1 == pos || ((int)(url.size() - 1) == pos))
3051
pos = 0;
3152

3253
size_t pos3 = url.find('?', pos);
@@ -36,8 +57,10 @@ class UrlUtil {
3657
if (pos3 < pos)
3758
pos3 = url.size();
3859
std::string path = url.substr(pos, pos3 - pos);
39-
return common::utf8ToUtf16(path);
60+
return utf8ToUtf16(path);
4061
}
4162
};
4263

43-
#endif // common_UrlUtil_h
64+
}
65+
66+
#endif // content_UrlUtil_h

0 commit comments

Comments
 (0)