2020-04-04 20:30:09 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2020-04-04 20:30:09 +08:00
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
2019-06-28 16:48:02 +08:00
|
|
|
|
|
2022-05-13 20:48:22 +08:00
|
|
|
|
#include <cinttypes>
|
2019-06-28 16:48:02 +08:00
|
|
|
|
#include "Parser.h"
|
2023-06-10 18:46:30 +08:00
|
|
|
|
#include "strCoding.h"
|
2022-05-08 16:33:33 +08:00
|
|
|
|
#include "macros.h"
|
|
|
|
|
#include "Network/sockutil.h"
|
2023-06-10 12:22:28 +08:00
|
|
|
|
#include "Common/macros.h"
|
2019-06-28 16:48:02 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2023-06-10 12:22:28 +08:00
|
|
|
|
namespace mediakit {
|
2019-06-28 16:48:02 +08:00
|
|
|
|
|
2023-06-10 12:28:49 +08:00
|
|
|
|
string findSubString(const char *buf, const char *start, const char *end, size_t buf_size) {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
if (buf_size <= 0) {
|
|
|
|
|
buf_size = strlen(buf);
|
2019-06-28 16:48:02 +08:00
|
|
|
|
}
|
2023-06-10 12:28:49 +08:00
|
|
|
|
auto msg_start = buf;
|
|
|
|
|
auto msg_end = buf + buf_size;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t len = 0;
|
2019-06-28 16:48:02 +08:00
|
|
|
|
if (start != NULL) {
|
|
|
|
|
len = strlen(start);
|
|
|
|
|
msg_start = strstr(buf, start);
|
|
|
|
|
}
|
|
|
|
|
if (msg_start == NULL) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
msg_start += len;
|
|
|
|
|
if (end != NULL) {
|
|
|
|
|
msg_end = strstr(msg_start, end);
|
|
|
|
|
if (msg_end == NULL) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return string(msg_start, msg_end);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 12:22:28 +08:00
|
|
|
|
void Parser::parse(const char *buf, size_t size) {
|
2023-06-10 11:04:52 +08:00
|
|
|
|
clear();
|
2023-06-10 12:22:28 +08:00
|
|
|
|
auto ptr = buf;
|
2020-04-20 18:13:45 +08:00
|
|
|
|
while (true) {
|
2023-07-22 17:31:02 +08:00
|
|
|
|
auto next_line = strchr(ptr, '\n');
|
|
|
|
|
auto offset = 1;
|
|
|
|
|
CHECK(next_line && next_line > ptr);
|
|
|
|
|
if (*(next_line - 1) == '\r') {
|
|
|
|
|
next_line -= 1;
|
|
|
|
|
offset = 2;
|
|
|
|
|
}
|
2023-06-10 12:22:28 +08:00
|
|
|
|
if (ptr == buf) {
|
|
|
|
|
auto blank = strchr(ptr, ' ');
|
|
|
|
|
CHECK(blank > ptr && blank < next_line);
|
2023-07-06 20:07:49 +08:00
|
|
|
|
_method = std::string(ptr, blank - ptr);
|
2023-06-10 12:22:28 +08:00
|
|
|
|
auto next_blank = strchr(blank + 1, ' ');
|
|
|
|
|
CHECK(next_blank && next_blank < next_line);
|
|
|
|
|
_url.assign(blank + 1, next_blank);
|
|
|
|
|
auto pos = _url.find('?');
|
|
|
|
|
if (pos != string::npos) {
|
|
|
|
|
_params = _url.substr(pos + 1);
|
|
|
|
|
_url_args = parseArgs(_params);
|
|
|
|
|
_url = _url.substr(0, pos);
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
2023-06-10 12:22:28 +08:00
|
|
|
|
_protocol = std::string(next_blank + 1, next_line);
|
2020-04-20 18:13:45 +08:00
|
|
|
|
} else {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
auto pos = strchr(ptr, ':');
|
|
|
|
|
CHECK(pos > ptr && pos < next_line);
|
2023-07-06 20:07:49 +08:00
|
|
|
|
std::string key { ptr, static_cast<std::size_t>(pos - ptr) };
|
2023-06-10 12:22:28 +08:00
|
|
|
|
std::string value;
|
|
|
|
|
if (pos[1] == ' ') {
|
|
|
|
|
value.assign(pos + 2, next_line);
|
|
|
|
|
} else {
|
|
|
|
|
value.assign(pos + 1, next_line);
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
2023-06-10 12:22:28 +08:00
|
|
|
|
_headers.emplace_force(trim(std::move(key)), trim(std::move(value)));
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
2023-07-22 17:31:02 +08:00
|
|
|
|
ptr = next_line + offset;
|
2023-06-10 12:22:28 +08:00
|
|
|
|
if (strncmp(ptr, "\r\n", 2) == 0) { // 协议解析完毕
|
|
|
|
|
_content.assign(ptr + 2, buf + size);
|
2020-04-20 18:13:45 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 11:04:52 +08:00
|
|
|
|
const string &Parser::method() const {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
return _method;
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 11:04:52 +08:00
|
|
|
|
const string &Parser::url() const {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
return _url;
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 11:04:52 +08:00
|
|
|
|
const std::string &Parser::status() const {
|
|
|
|
|
return url();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string Parser::fullUrl() const {
|
2021-08-26 19:10:04 +08:00
|
|
|
|
if (_params.empty()) {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
return _url;
|
2021-08-26 19:10:04 +08:00
|
|
|
|
}
|
2023-06-10 12:22:28 +08:00
|
|
|
|
return _url + "?" + _params;
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 11:04:52 +08:00
|
|
|
|
const string &Parser::protocol() const {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
return _protocol;
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 11:04:52 +08:00
|
|
|
|
const std::string &Parser::statusStr() const {
|
|
|
|
|
return protocol();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 12:22:28 +08:00
|
|
|
|
static std::string kNull;
|
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
const string &Parser::operator[](const char *name) const {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
auto it = _headers.find(name);
|
|
|
|
|
if (it == _headers.end()) {
|
|
|
|
|
return kNull;
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 11:04:52 +08:00
|
|
|
|
const string &Parser::content() const {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
return _content;
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 11:04:52 +08:00
|
|
|
|
void Parser::clear() {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
_method.clear();
|
|
|
|
|
_url.clear();
|
2020-04-20 18:13:45 +08:00
|
|
|
|
_params.clear();
|
2023-06-10 12:22:28 +08:00
|
|
|
|
_protocol.clear();
|
|
|
|
|
_content.clear();
|
|
|
|
|
_headers.clear();
|
|
|
|
|
_url_args.clear();
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 11:04:52 +08:00
|
|
|
|
const string &Parser::params() const {
|
2020-04-20 18:13:45 +08:00
|
|
|
|
return _params;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
void Parser::setUrl(string url) {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
_url = std::move(url);
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
void Parser::setContent(string content) {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
_content = std::move(content);
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StrCaseMap &Parser::getHeader() const {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
return _headers;
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StrCaseMap &Parser::getUrlArgs() const {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
return _url_args;
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StrCaseMap Parser::parseArgs(const string &str, const char *pair_delim, const char *key_delim) {
|
|
|
|
|
StrCaseMap ret;
|
|
|
|
|
auto arg_vec = split(str, pair_delim);
|
2023-06-10 12:22:28 +08:00
|
|
|
|
for (auto &key_val : arg_vec) {
|
2021-09-03 17:58:23 +08:00
|
|
|
|
if (key_val.empty()) {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
// 忽略
|
2021-09-03 17:58:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-06-10 12:22:28 +08:00
|
|
|
|
auto pos = key_val.find(key_delim);
|
|
|
|
|
if (pos != string::npos) {
|
|
|
|
|
auto key = trim(std::string(key_val, 0, pos));
|
|
|
|
|
auto val = trim(key_val.substr(pos + strlen(key_delim)));
|
|
|
|
|
ret.emplace_force(std::move(key), std::move(val));
|
2020-12-27 18:11:10 +08:00
|
|
|
|
} else {
|
2021-09-03 18:11:20 +08:00
|
|
|
|
trim(key_val);
|
|
|
|
|
if (!key_val.empty()) {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
ret.emplace_force(std::move(key_val), "");
|
2021-09-03 18:11:20 +08:00
|
|
|
|
}
|
2020-12-27 18:11:10 +08:00
|
|
|
|
}
|
2020-04-20 18:13:45 +08:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2023-06-10 12:22:28 +08:00
|
|
|
|
|
2023-06-10 11:04:52 +08:00
|
|
|
|
std::string Parser::mergeUrl(const string &base_url, const string &path) {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
// 以base_url为基础, 合并path路径生成新的url, path支持相对路径和绝对路径
|
2023-02-20 10:52:00 +08:00
|
|
|
|
if (base_url.empty()) {
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
if (path.empty()) {
|
|
|
|
|
return base_url;
|
|
|
|
|
}
|
|
|
|
|
// 如果包含协议,则直接返回
|
|
|
|
|
if (path.find("://") != string::npos) {
|
|
|
|
|
return path;
|
|
|
|
|
}
|
2020-04-20 18:13:45 +08:00
|
|
|
|
|
2023-02-20 10:52:00 +08:00
|
|
|
|
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以"/"开头,则直接拼接协议和主机
|
|
|
|
|
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()) {
|
|
|
|
|
if (path_parts.size() > 1 || protocol_end == string::npos) {
|
|
|
|
|
path_parts.pop_back();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (part != "." && !part.empty()) {
|
|
|
|
|
path_parts.emplace_back(part);
|
|
|
|
|
}
|
|
|
|
|
pos = next_pos + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string part = path.substr(pos);
|
|
|
|
|
if (part != ".." && part != "." && !part.empty()) {
|
|
|
|
|
path_parts.emplace_back(part);
|
|
|
|
|
}
|
|
|
|
|
stringstream final_url;
|
|
|
|
|
for (size_t i = 0; i < path_parts.size(); ++i) {
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
final_url << path_parts[i];
|
|
|
|
|
} else {
|
|
|
|
|
final_url << '/' << path_parts[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return final_url.str();
|
|
|
|
|
}
|
2023-06-10 12:22:28 +08:00
|
|
|
|
|
2022-05-08 16:33:33 +08:00
|
|
|
|
void RtspUrl::parse(const string &strUrl) {
|
2023-06-10 12:28:49 +08:00
|
|
|
|
auto schema = findSubString(strUrl.data(), nullptr, "://");
|
2022-05-08 16:33:33 +08:00
|
|
|
|
bool is_ssl = strcasecmp(schema.data(), "rtsps") == 0;
|
2023-06-10 12:22:28 +08:00
|
|
|
|
// 查找"://"与"/"之间的字符串,用于提取用户名密码
|
2023-06-10 12:28:49 +08:00
|
|
|
|
auto middle_url = findSubString(strUrl.data(), "://", "/");
|
2022-05-08 16:33:33 +08:00
|
|
|
|
if (middle_url.empty()) {
|
2023-06-10 12:28:49 +08:00
|
|
|
|
middle_url = findSubString(strUrl.data(), "://", nullptr);
|
2022-05-08 16:33:33 +08:00
|
|
|
|
}
|
|
|
|
|
auto pos = middle_url.rfind('@');
|
|
|
|
|
if (pos == string::npos) {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
// 并没有用户名密码
|
2022-05-08 16:33:33 +08:00
|
|
|
|
return setup(is_ssl, strUrl, "", "");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 12:22:28 +08:00
|
|
|
|
// 包含用户名密码
|
2022-05-08 16:33:33 +08:00
|
|
|
|
auto user_pwd = middle_url.substr(0, pos);
|
|
|
|
|
auto suffix = strUrl.substr(schema.size() + 3 + pos + 1);
|
|
|
|
|
auto url = StrPrinter << "rtsp://" << suffix << endl;
|
|
|
|
|
if (user_pwd.find(":") == string::npos) {
|
|
|
|
|
return setup(is_ssl, url, user_pwd, "");
|
|
|
|
|
}
|
2023-06-10 12:28:49 +08:00
|
|
|
|
auto user = findSubString(user_pwd.data(), nullptr, ":");
|
|
|
|
|
auto pwd = findSubString(user_pwd.data(), ":", nullptr);
|
2022-05-08 16:33:33 +08:00
|
|
|
|
return setup(is_ssl, url, user, pwd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtspUrl::setup(bool is_ssl, const string &url, const string &user, const string &passwd) {
|
2023-06-10 12:28:49 +08:00
|
|
|
|
auto ip = findSubString(url.data(), "://", "/");
|
2022-05-08 16:33:33 +08:00
|
|
|
|
if (ip.empty()) {
|
2023-06-10 12:28:49 +08:00
|
|
|
|
ip = split(findSubString(url.data(), "://", NULL), "?")[0];
|
2022-05-08 16:33:33 +08:00
|
|
|
|
}
|
|
|
|
|
uint16_t port = is_ssl ? 322 : 554;
|
|
|
|
|
splitUrl(ip, ip, port);
|
|
|
|
|
|
2023-06-10 18:46:30 +08:00
|
|
|
|
|
2022-05-08 16:33:33 +08:00
|
|
|
|
_url = std::move(url);
|
2023-06-10 18:46:30 +08:00
|
|
|
|
_user = strCoding::UrlDecode(std::move(user));
|
|
|
|
|
_passwd = strCoding::UrlDecode(std::move(passwd));
|
2022-05-08 16:33:33 +08:00
|
|
|
|
_host = std::move(ip);
|
|
|
|
|
_port = port;
|
|
|
|
|
_is_ssl = is_ssl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void inline checkHost(std::string &host) {
|
|
|
|
|
if (host.back() == ']' && host.front() == '[') {
|
|
|
|
|
// ipv6去除方括号
|
|
|
|
|
host.pop_back();
|
|
|
|
|
host.erase(0, 1);
|
|
|
|
|
CHECK(SockUtil::is_ipv6(host.data()), "not a ipv6 address:", host);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void splitUrl(const std::string &url, std::string &host, uint16_t &port) {
|
2022-05-29 21:36:34 +08:00
|
|
|
|
CHECK(!url.empty(), "empty url");
|
2022-05-08 16:33:33 +08:00
|
|
|
|
auto pos = url.rfind(':');
|
|
|
|
|
if (pos == string::npos || url.back() == ']') {
|
2023-06-10 12:22:28 +08:00
|
|
|
|
// 没有冒号,未指定端口;或者是纯粹的ipv6地址
|
2022-05-08 16:33:33 +08:00
|
|
|
|
host = url;
|
|
|
|
|
checkHost(host);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-04 16:21:08 +08:00
|
|
|
|
CHECK(pos > 0, "invalid url:", url);
|
2022-05-08 16:33:33 +08:00
|
|
|
|
CHECK(sscanf(url.data() + pos + 1, "%" SCNu16, &port) == 1, "parse port from url failed:", url);
|
|
|
|
|
host = url.substr(0, pos);
|
|
|
|
|
checkHost(host);
|
|
|
|
|
}
|
|
|
|
|
#if 0
|
|
|
|
|
//测试代码
|
|
|
|
|
static onceToken token([](){
|
|
|
|
|
string host;
|
|
|
|
|
uint16_t port;
|
|
|
|
|
splitUrl("www.baidu.com:8880", host, port);
|
|
|
|
|
splitUrl("192.168.1.1:8880", host, port);
|
|
|
|
|
splitUrl("[::]:8880", host, port);
|
|
|
|
|
splitUrl("[fe80::604d:4173:76e9:1009]:8880", host, port);
|
|
|
|
|
});
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-06-10 12:22:28 +08:00
|
|
|
|
} // namespace mediakit
|