Files
ZLMediaKit/src/Common/Parser.h

160 lines
5.3 KiB
C++
Raw Normal View History

2020-04-04 20:30:09 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2020-04-04 20:30:09 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2020-04-04 20:30:09 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* 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
#ifndef ZLMEDIAKIT_PARSER_H
#define ZLMEDIAKIT_PARSER_H
#include <map>
#include <string>
#include "Util/util.h"
2021-09-30 16:10:09 +08:00
namespace mediakit {
2019-06-28 16:48:02 +08:00
// 从字符串中提取子字符串 [AUTO-TRANSLATED:8493b6a5]
// Extract substring from string
std::string findSubString(const char *buf, const char *start, const char *end, size_t buf_size = 0);
// 把url解析为主机地址和端口号,兼容ipv4/ipv6/dns [AUTO-TRANSLATED:0cfa4a6c]
// Parse url to host address and port number, compatible with ipv4/ipv6/dns
2023-06-10 11:04:52 +08:00
void splitUrl(const std::string &url, std::string &host, uint16_t &port);
// 解析proxy url,仅支持http [AUTO-TRANSLATED:194b49d7]
// Parse proxy url, only supports http
void parseProxyUrl(const std::string &proxy_url, std::string &proxy_host, uint16_t &proxy_port, std::string &proxy_auth);
2019-06-28 16:48:02 +08:00
struct StrCaseCompare {
2023-06-10 11:04:52 +08:00
bool operator()(const std::string &__x, const std::string &__y) const { return strcasecmp(__x.data(), __y.data()) < 0; }
2019-06-28 16:48:02 +08:00
};
class StrCaseMap : public std::multimap<std::string, std::string, StrCaseCompare> {
2021-09-30 16:10:09 +08:00
public:
using Super = std::multimap<std::string, std::string, StrCaseCompare>;
2019-07-17 14:50:24 +08:00
std::string &operator[](const std::string &k) {
2019-12-28 13:39:25 +08:00
auto it = find(k);
2021-09-30 16:10:09 +08:00
if (it == end()) {
it = Super::emplace(k, "");
2019-06-28 16:48:02 +08:00
}
return it->second;
}
2023-06-10 12:22:28 +08:00
template <typename K, typename V>
void emplace(K &&k, V &&v) {
2019-12-28 13:39:25 +08:00
auto it = find(k);
2021-09-30 16:10:09 +08:00
if (it != end()) {
2019-06-28 16:48:02 +08:00
return;
}
2023-06-10 12:22:28 +08:00
Super::emplace(std::forward<K>(k), std::forward<V>(v));
2019-06-28 16:48:02 +08:00
}
2023-06-10 12:22:28 +08:00
template <typename K, typename V>
void emplace_force(K &&k, V &&v) {
Super::emplace(std::forward<K>(k), std::forward<V>(v));
2019-06-28 16:48:02 +08:00
}
};
// rtsp/http/sip解析类 [AUTO-TRANSLATED:188ca500]
// rtsp/http/sip parsing class
2019-06-28 16:48:02 +08:00
class Parser {
2020-04-20 18:13:45 +08:00
public:
// 解析http/rtsp/sip请求需要确保buf以\0结尾 [AUTO-TRANSLATED:552953af]
// Parse http/rtsp/sip request, ensure buf ends with \0
2023-06-10 12:22:28 +08:00
void parse(const char *buf, size_t size);
2021-09-30 16:10:09 +08:00
// 获取命令字如GET/POST [AUTO-TRANSLATED:34750f3d]
// Get command word, such as GET/POST
2023-06-10 11:04:52 +08:00
const std::string &method() const;
2021-09-30 16:10:09 +08:00
// 请求时获取中间url不包含?后面的参数 [AUTO-TRANSLATED:c259f1ed]
// When requesting, get the middle url, excluding the parameters after ?
2023-06-10 11:04:52 +08:00
const std::string &url() const;
// 回复时获取状态码如200/404 [AUTO-TRANSLATED:ac3f8ed4]
// When replying, get the status code, such as 200/404
2023-06-10 11:04:52 +08:00
const std::string &status() const;
2021-09-30 16:10:09 +08:00
// 获取中间url包含?后面的参数 [AUTO-TRANSLATED:ca1fec1a]
// Get the middle url, including the parameters after ?
2023-06-10 11:04:52 +08:00
std::string fullUrl() const;
2021-09-30 16:10:09 +08:00
// 请求时获取协议名如HTTP/1.1 [AUTO-TRANSLATED:7410fed6]
// When requesting, get the protocol name, such as HTTP/1.1
2023-06-10 11:04:52 +08:00
const std::string &protocol() const;
// 回复时,获取状态字符串,如 OK/Not Found [AUTO-TRANSLATED:d245247a]
// When replying, get the status string, such as OK/Not Found
2023-06-10 11:04:52 +08:00
const std::string &statusStr() const;
2021-09-30 16:10:09 +08:00
// 根据header key名获取请求header value值 [AUTO-TRANSLATED:5cbc9ac7]
// Get the request header value according to the header key name
const std::string &operator[](const char *name) const;
2021-09-30 16:10:09 +08:00
// 获取http body或sdp [AUTO-TRANSLATED:d6fd1803]
// Get http body or sdp
2023-06-10 11:04:52 +08:00
const std::string &content() const;
2021-09-30 16:10:09 +08:00
// 清空,为了重用 [AUTO-TRANSLATED:cb7a16dd]
// Clear, for reuse
2023-06-10 11:04:52 +08:00
void clear();
2021-09-30 16:10:09 +08:00
// 获取?后面的参数 [AUTO-TRANSLATED:4ada90e1]
// Get the parameters after ?
2023-06-10 11:04:52 +08:00
const std::string &params() const;
2021-09-30 16:10:09 +08:00
// 重新设置url [AUTO-TRANSLATED:4829ba8e]
// Reset url
void setUrl(std::string url);
2021-09-30 16:10:09 +08:00
// 重新设置content [AUTO-TRANSLATED:ac8fc8c0]
// Reset content
void setContent(std::string content);
2021-09-30 16:10:09 +08:00
// 获取header列表 [AUTO-TRANSLATED:90d90b03]
// Get header list
2020-04-20 18:13:45 +08:00
StrCaseMap &getHeader() const;
2021-09-30 16:10:09 +08:00
// 获取url参数列表 [AUTO-TRANSLATED:da1df48a]
// Get url parameter list
2020-04-20 18:13:45 +08:00
StrCaseMap &getUrlArgs() const;
2021-09-30 16:10:09 +08:00
// 解析?后面的参数 [AUTO-TRANSLATED:38692051]
// Parse the parameters after ?
static StrCaseMap parseArgs(const std::string &str, const char *pair_delim = "&", const char *key_delim = "=");
2021-09-30 16:10:09 +08:00
2023-06-10 11:04:52 +08:00
static std::string mergeUrl(const std::string &base_url, const std::string &path);
2019-06-28 16:55:28 +08:00
private:
2023-06-10 12:22:28 +08:00
std::string _method;
std::string _url;
std::string _protocol;
std::string _content;
std::string _params;
2023-06-10 12:22:28 +08:00
mutable StrCaseMap _headers;
mutable StrCaseMap _url_args;
2019-06-28 16:48:02 +08:00
};
// 解析rtsp url的工具类 [AUTO-TRANSLATED:0d31ae01]
// Utility class for parsing rtsp url
2023-06-10 11:04:52 +08:00
class RtspUrl {
2022-05-08 16:33:33 +08:00
public:
bool _is_ssl;
uint16_t _port;
std::string _url;
std::string _user;
std::string _passwd;
std::string _host;
public:
void parse(const std::string &url);
private:
2023-06-10 11:04:52 +08:00
void setup(bool, const std::string &, const std::string &, const std::string &);
2022-05-08 16:33:33 +08:00
};
2023-06-10 11:04:52 +08:00
} // namespace mediakit
2019-06-28 16:48:02 +08:00
2023-06-10 11:04:52 +08:00
#endif // ZLMEDIAKIT_PARSER_H