的确有不少问题
抱歉, 最近一些工具类代码都是chatgpt辅助写的. 我仔细看了下的确代码有问题, 我已经重新修改了.
This commit is contained in:
parent
a34c9079ba
commit
9f50b9159c
|
|
@ -160,48 +160,79 @@ StrCaseMap Parser::parseArgs(const string &str, const char *pair_delim, const ch
|
|||
return ret;
|
||||
}
|
||||
std::string Parser::merge_url(const string &base_url, const string &path) {
|
||||
if (path.empty()) return base_url;
|
||||
if (path.find("://") != string::npos) return path; // 如果包含协议,则直接返回
|
||||
if (path.find("./") == 0) return base_url.substr(0, base_url.rfind('/') + 1) + path.substr(2);
|
||||
if (path.find("../") != 0) return base_url.substr(0, base_url.rfind('/') + 1) + path;
|
||||
vector<string> path_parts;
|
||||
size_t pos = 0, next_pos = 0;
|
||||
if (path[0] == '/') {
|
||||
path_parts.emplace_back(""); // 新的URL从根开始
|
||||
} else {
|
||||
while ((next_pos = base_url.find('/', pos)) != string::npos) {
|
||||
path_parts.emplace_back(base_url.substr(pos, next_pos - pos));
|
||||
pos = next_pos + 1;
|
||||
//以base_url为基础, 合并path路径生成新的url, path支持相对路径和绝对路径
|
||||
if (base_url.empty()) {
|
||||
return path;
|
||||
}
|
||||
if (path.empty()) {
|
||||
return base_url;
|
||||
}
|
||||
// 如果包含协议,则直接返回
|
||||
if (path.find("://") != string::npos) {
|
||||
return path;
|
||||
}
|
||||
|
||||
string protocol = "http://";
|
||||
size_t protocol_end = base_url.find("://");
|
||||
if (protocol_end != string::npos) {
|
||||
protocol = base_url.substr(0, protocol_end + 3);
|
||||
}
|
||||
// 如果path以"//"开头,则直接拼接协议
|
||||
if (path.find("//") == 0) {
|
||||
return protocol + path.substr(2);
|
||||
}
|
||||
string host;
|
||||
size_t pos = 0;
|
||||
if (protocol_end != string::npos) {
|
||||
pos = base_url.find('/', protocol_end + 3);
|
||||
host = base_url.substr(0, pos);
|
||||
if (pos == string::npos) {
|
||||
pos = base_url.size();
|
||||
} else {
|
||||
pos++;
|
||||
}
|
||||
// path_parts.pop_back(); // 去掉文件名部分
|
||||
}
|
||||
// 如果path以"/"开头,则直接拼接协议和主机
|
||||
if (path[0] == '/') {
|
||||
return host + path;
|
||||
}
|
||||
vector<string> path_parts;
|
||||
size_t next_pos = 0;
|
||||
if (!host.empty()) {
|
||||
path_parts.emplace_back(host);
|
||||
}
|
||||
while ((next_pos = base_url.find('/', pos)) != string::npos) {
|
||||
path_parts.emplace_back(base_url.substr(pos, next_pos - pos));
|
||||
pos = next_pos + 1;
|
||||
}
|
||||
pos = 0;
|
||||
while ((next_pos = path.find('/', pos)) != string::npos) {
|
||||
string part = path.substr(pos, next_pos - pos);
|
||||
if (part == "..") {
|
||||
if (!path_parts.empty() && !path_parts.back().empty()) {
|
||||
path_parts.pop_back();
|
||||
if (path_parts.size() > 1 || protocol_end == string::npos) {
|
||||
path_parts.pop_back();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} else if (part != "." && !part.empty()) {
|
||||
path_parts.emplace_back(part);
|
||||
}
|
||||
pos = next_pos + 1;
|
||||
}
|
||||
|
||||
string part = path.substr(pos);
|
||||
if (part != "..") {
|
||||
if (part != ".." && part != "." && !part.empty()) {
|
||||
path_parts.emplace_back(part);
|
||||
}
|
||||
|
||||
stringstream ss;
|
||||
stringstream final_url;
|
||||
for (size_t i = 0; i < path_parts.size(); ++i) {
|
||||
if (i == 0) {
|
||||
ss << path_parts[i];
|
||||
final_url << path_parts[i];
|
||||
} else {
|
||||
ss << '/' << path_parts[i];
|
||||
final_url << '/' << path_parts[i];
|
||||
}
|
||||
}
|
||||
return ss.str();
|
||||
return final_url.str();
|
||||
}
|
||||
void RtspUrl::parse(const string &strUrl) {
|
||||
auto schema = FindField(strUrl.data(), nullptr, "://");
|
||||
|
|
@ -281,4 +312,4 @@ static onceToken token([](){
|
|||
});
|
||||
#endif
|
||||
|
||||
}//namespace mediakit
|
||||
}//namespace mediakit
|
||||
|
|
|
|||
Loading…
Reference in New Issue