mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 02:34:26 +08:00
优化Parser代码命名
This commit is contained in:
parent
52c7bc1d34
commit
af0ebf4633
@ -86,17 +86,17 @@ API_EXPORT const char* API_CALL mk_mp4_info_get_stream(const mk_mp4_info ctx){
|
||||
API_EXPORT const char* API_CALL mk_parser_get_method(const mk_parser ctx){
|
||||
assert(ctx);
|
||||
Parser *parser = (Parser *)ctx;
|
||||
return parser->Method().c_str();
|
||||
return parser->method().c_str();
|
||||
}
|
||||
API_EXPORT const char* API_CALL mk_parser_get_url(const mk_parser ctx){
|
||||
assert(ctx);
|
||||
Parser *parser = (Parser *)ctx;
|
||||
return parser->Url().c_str();
|
||||
return parser->url().c_str();
|
||||
}
|
||||
API_EXPORT const char* API_CALL mk_parser_get_url_params(const mk_parser ctx){
|
||||
assert(ctx);
|
||||
Parser *parser = (Parser *)ctx;
|
||||
return parser->Params().c_str();
|
||||
return parser->params().c_str();
|
||||
}
|
||||
API_EXPORT const char* API_CALL mk_parser_get_url_param(const mk_parser ctx,const char *key){
|
||||
assert(ctx && key);
|
||||
@ -106,7 +106,7 @@ API_EXPORT const char* API_CALL mk_parser_get_url_param(const mk_parser ctx,cons
|
||||
API_EXPORT const char* API_CALL mk_parser_get_tail(const mk_parser ctx){
|
||||
assert(ctx);
|
||||
Parser *parser = (Parser *)ctx;
|
||||
return parser->Tail().c_str();
|
||||
return parser->protocol().c_str();
|
||||
}
|
||||
API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const char *key){
|
||||
assert(ctx && key);
|
||||
@ -117,9 +117,9 @@ API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx, size_
|
||||
assert(ctx);
|
||||
Parser *parser = (Parser *)ctx;
|
||||
if(length){
|
||||
*length = parser->Content().size();
|
||||
*length = parser->content().size();
|
||||
}
|
||||
return parser->Content().c_str();
|
||||
return parser->content().c_str();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////MediaInfo/////////////////////////////////////////////
|
||||
|
@ -108,7 +108,7 @@ API_EXPORT void API_CALL mk_http_requester_add_header(mk_http_requester ctx,cons
|
||||
API_EXPORT const char* API_CALL mk_http_requester_get_response_status(mk_http_requester ctx){
|
||||
assert(ctx);
|
||||
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
|
||||
return (*obj)->response().Url().c_str();
|
||||
return (*obj)->response().status().c_str();
|
||||
}
|
||||
|
||||
API_EXPORT const char* API_CALL mk_http_requester_get_response_header(mk_http_requester ctx,const char *key){
|
||||
@ -121,9 +121,9 @@ API_EXPORT const char* API_CALL mk_http_requester_get_response_body(mk_http_requ
|
||||
assert(ctx);
|
||||
HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
|
||||
if(length){
|
||||
*length = (*obj)->response().Content().size();
|
||||
*length = (*obj)->response().content().size();
|
||||
}
|
||||
return (*obj)->response().Content().c_str();
|
||||
return (*obj)->response().content().c_str();
|
||||
}
|
||||
|
||||
API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester ctx){
|
||||
|
@ -56,8 +56,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
if (argc < 3) {
|
||||
ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n"
|
||||
<< "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n"
|
||||
<< endl;
|
||||
<< "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -130,7 +130,7 @@ static HttpApi toApi(const function<void(API_ARGS_JSON_ASYNC)> &cb) {
|
||||
//参数解析成json对象然后处理
|
||||
Json::Value args;
|
||||
Json::Reader reader;
|
||||
reader.parse(parser.Content(), args);
|
||||
reader.parse(parser.content(), args);
|
||||
|
||||
cb(sender, headerOut, HttpAllArgs<decltype(args)>(parser, args), val, invoker);
|
||||
};
|
||||
@ -152,7 +152,7 @@ static HttpApi toApi(const function<void(API_ARGS_STRING_ASYNC)> &cb) {
|
||||
Json::Value val;
|
||||
val["code"] = API::Success;
|
||||
|
||||
cb(sender, headerOut, HttpAllArgs<string>(parser, (string &)parser.Content()), val, invoker);
|
||||
cb(sender, headerOut, HttpAllArgs<string>(parser, (string &)parser.content()), val, invoker);
|
||||
};
|
||||
}
|
||||
|
||||
@ -191,13 +191,13 @@ void api_regist(const string &api_path, const function<void(API_ARGS_STRING_ASYN
|
||||
static ApiArgsType getAllArgs(const Parser &parser) {
|
||||
ApiArgsType allArgs;
|
||||
if (parser["Content-Type"].find("application/x-www-form-urlencoded") == 0) {
|
||||
auto contentArgs = parser.parseArgs(parser.Content());
|
||||
auto contentArgs = parser.parseArgs(parser.content());
|
||||
for (auto &pr : contentArgs) {
|
||||
allArgs[pr.first] = HttpSession::urlDecode(pr.second);
|
||||
}
|
||||
} else if (parser["Content-Type"].find("application/json") == 0) {
|
||||
try {
|
||||
stringstream ss(parser.Content());
|
||||
stringstream ss(parser.content());
|
||||
Value jsonArgs;
|
||||
ss >> jsonArgs;
|
||||
auto keys = jsonArgs.getMemberNames();
|
||||
@ -231,7 +231,7 @@ static inline void addHttpListener(){
|
||||
GET_CONFIG(bool, api_debug, API::kApiDebug);
|
||||
//注册监听kBroadcastHttpRequest事件
|
||||
NoticeCenter::Instance().addListener(&web_api_tag, Broadcast::kBroadcastHttpRequest, [](BroadcastHttpRequestArgs) {
|
||||
auto it = s_map_api.find(parser.Url());
|
||||
auto it = s_map_api.find(parser.url());
|
||||
if (it == s_map_api.end()) {
|
||||
return;
|
||||
}
|
||||
@ -248,14 +248,14 @@ static inline void addHttpListener(){
|
||||
}
|
||||
|
||||
LogContextCapture log(getLogger(), toolkit::LTrace, __FILE__, "http api debug", __LINE__);
|
||||
log << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n";
|
||||
log << "\r\n# request:\r\n" << parser.method() << " " << parser.fullUrl() << "\r\n";
|
||||
log << "# header:\r\n";
|
||||
|
||||
for (auto &pr : parser.getHeader()) {
|
||||
log << pr.first << " : " << pr.second << "\r\n";
|
||||
}
|
||||
|
||||
auto &content = parser.Content();
|
||||
auto &content = parser.content();
|
||||
log << "# content:\r\n" << (content.size() > 4 * 1024 ? content.substr(0, 4 * 1024) : content) << "\r\n";
|
||||
|
||||
if (size > 0 && size < 4 * 1024) {
|
||||
@ -1637,7 +1637,7 @@ void installWebApi() {
|
||||
CHECK_ARGS("app", "stream");
|
||||
|
||||
return StrPrinter << RTC_SCHEMA << "://" << _args["Host"] << "/" << _args["app"] << "/"
|
||||
<< _args["stream"] << "?" << _args.getParser().Params() + "&session=" + _session_id;
|
||||
<< _args["stream"] << "?" << _args.getParser().params() + "&session=" + _session_id;
|
||||
}
|
||||
|
||||
private:
|
||||
@ -1700,7 +1700,7 @@ void installWebApi() {
|
||||
|
||||
api_regist(delete_webrtc_url, [](API_ARGS_MAP_ASYNC) {
|
||||
CHECK_ARGS("id", "token");
|
||||
CHECK(allArgs.getParser().Method() == "DELETE", "http method is not DELETE: " + allArgs.getParser().Method());
|
||||
CHECK(allArgs.getParser().method() == "DELETE", "http method is not DELETE: " + allArgs.getParser().method());
|
||||
auto obj = WebRtcTransportManager::Instance().getItem(allArgs["id"]);
|
||||
if (!obj) {
|
||||
invoker(404, headerOut, "id not found");
|
||||
|
@ -100,14 +100,14 @@ static void parse_http_response(const SockException &ex, const Parser &res, cons
|
||||
fun(Json::nullValue, errStr, should_retry);
|
||||
return;
|
||||
}
|
||||
if (res.Url() != "200") {
|
||||
auto errStr = StrPrinter << "[bad http status code]:" << res.Url() << endl;
|
||||
if (res.status() != "200") {
|
||||
auto errStr = StrPrinter << "[bad http status code]:" << res.status() << endl;
|
||||
fun(Json::nullValue, errStr, should_retry);
|
||||
return;
|
||||
}
|
||||
Value result;
|
||||
try {
|
||||
stringstream ss(res.Content());
|
||||
stringstream ss(res.content());
|
||||
ss >> result;
|
||||
} catch (std::exception &ex) {
|
||||
auto errStr = StrPrinter << "[parse json failed]:" << ex.what() << endl;
|
||||
@ -614,7 +614,7 @@ void installWebHook() {
|
||||
// 追踪用户的目的是为了缓存上次鉴权结果,减少鉴权次数,提高性能
|
||||
NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastHttpAccess, [](BroadcastHttpAccessArgs) {
|
||||
GET_CONFIG(string, hook_http_access, Hook::kOnHttpAccess);
|
||||
if (sender.get_peer_ip() == "127.0.0.1" || parser.Params() == hook_adminparams) {
|
||||
if (sender.get_peer_ip() == "127.0.0.1" || parser.params() == hook_adminparams) {
|
||||
// 如果是本机或超级管理员访问,那么不做访问鉴权;权限有效期1个小时
|
||||
invoker("", "", 60 * 60);
|
||||
return;
|
||||
@ -632,7 +632,7 @@ void installWebHook() {
|
||||
body["id"] = sender.getIdentifier();
|
||||
body["path"] = path;
|
||||
body["is_dir"] = is_dir;
|
||||
body["params"] = parser.Params();
|
||||
body["params"] = parser.params();
|
||||
for (auto &pr : parser.getHeader()) {
|
||||
body[string("header.") + pr.first] = pr.second;
|
||||
}
|
||||
|
@ -363,8 +363,8 @@ int start_main(int argc,char *argv[]) {
|
||||
#endif//defined(ENABLE_SRT)
|
||||
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << "端口占用或无权限:" << ex.what() << endl;
|
||||
ErrorL << "程序启动失败,请修改配置文件中端口号后重试!" << endl;
|
||||
WarnL << "端口占用或无权限:" << ex.what();
|
||||
ErrorL << "程序启动失败,请修改配置文件中端口号后重试!";
|
||||
sleep(1);
|
||||
#if !defined(_WIN32)
|
||||
if (pid != getpid() && kill_parent_if_failed) {
|
||||
|
@ -44,7 +44,7 @@ string FindField(const char* buf, const char* start, const char *end ,size_t buf
|
||||
void Parser::Parse(const char *buf) {
|
||||
//解析
|
||||
const char *start = buf;
|
||||
Clear();
|
||||
clear();
|
||||
while (true) {
|
||||
auto line = FindField(start, NULL, "\r\n");
|
||||
if (line.size() == 0) {
|
||||
@ -77,25 +77,33 @@ void Parser::Parse(const char *buf) {
|
||||
}
|
||||
}
|
||||
|
||||
const string &Parser::Method() const {
|
||||
const string &Parser::method() const {
|
||||
return _strMethod;
|
||||
}
|
||||
|
||||
const string &Parser::Url() const {
|
||||
const string &Parser::url() const {
|
||||
return _strUrl;
|
||||
}
|
||||
|
||||
string Parser::FullUrl() const {
|
||||
const std::string &Parser::status() const {
|
||||
return url();
|
||||
}
|
||||
|
||||
string Parser::fullUrl() const {
|
||||
if (_params.empty()) {
|
||||
return _strUrl;
|
||||
}
|
||||
return _strUrl + "?" + _params;
|
||||
}
|
||||
|
||||
const string &Parser::Tail() const {
|
||||
const string &Parser::protocol() const {
|
||||
return _strTail;
|
||||
}
|
||||
|
||||
const std::string &Parser::statusStr() const {
|
||||
return protocol();
|
||||
}
|
||||
|
||||
const string &Parser::operator[](const char *name) const {
|
||||
auto it = _mapHeaders.find(name);
|
||||
if (it == _mapHeaders.end()) {
|
||||
@ -104,11 +112,11 @@ const string &Parser::operator[](const char *name) const {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
const string &Parser::Content() const {
|
||||
const string &Parser::content() const {
|
||||
return _strContent;
|
||||
}
|
||||
|
||||
void Parser::Clear() {
|
||||
void Parser::clear() {
|
||||
_strMethod.clear();
|
||||
_strUrl.clear();
|
||||
_params.clear();
|
||||
@ -118,7 +126,7 @@ void Parser::Clear() {
|
||||
_mapUrlArgs.clear();
|
||||
}
|
||||
|
||||
const string &Parser::Params() const {
|
||||
const string &Parser::params() const {
|
||||
return _params;
|
||||
}
|
||||
|
||||
@ -159,7 +167,7 @@ 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) {
|
||||
std::string Parser::mergeUrl(const string &base_url, const string &path) {
|
||||
//以base_url为基础, 合并path路径生成新的url, path支持相对路径和绝对路径
|
||||
if (base_url.empty()) {
|
||||
return path;
|
||||
|
@ -17,15 +17,13 @@
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
//从字符串中提取子字符串
|
||||
// 从字符串中提取子字符串
|
||||
std::string FindField(const char *buf, const char *start, const char *end, size_t bufSize = 0);
|
||||
//把url解析为主机地址和端口号,兼容ipv4/ipv6/dns
|
||||
void splitUrl(const std::string &url, std::string &host, uint16_t& port);
|
||||
// 把url解析为主机地址和端口号,兼容ipv4/ipv6/dns
|
||||
void splitUrl(const std::string &url, std::string &host, uint16_t &port);
|
||||
|
||||
struct StrCaseCompare {
|
||||
bool operator()(const std::string &__x, const std::string &__y) const {
|
||||
return strcasecmp(__x.data(), __y.data()) < 0;
|
||||
}
|
||||
bool operator()(const std::string &__x, const std::string &__y) const { return strcasecmp(__x.data(), __y.data()) < 0; }
|
||||
};
|
||||
|
||||
class StrCaseMap : public std::multimap<std::string, std::string, StrCaseCompare> {
|
||||
@ -42,7 +40,7 @@ public:
|
||||
return it->second;
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
template <typename V>
|
||||
void emplace(const std::string &k, V &&v) {
|
||||
auto it = find(k);
|
||||
if (it != end()) {
|
||||
@ -51,61 +49,65 @@ public:
|
||||
Super::emplace(k, std::forward<V>(v));
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
template <typename V>
|
||||
void emplace_force(const std::string k, V &&v) {
|
||||
Super::emplace(k, std::forward<V>(v));
|
||||
}
|
||||
};
|
||||
|
||||
//rtsp/http/sip解析类
|
||||
// rtsp/http/sip解析类
|
||||
class Parser {
|
||||
public:
|
||||
Parser() = default;
|
||||
~Parser() = default;
|
||||
|
||||
//解析信令
|
||||
// 解析信令
|
||||
void Parse(const char *buf);
|
||||
|
||||
//获取命令字
|
||||
const std::string &Method() const;
|
||||
// 获取命令字
|
||||
const std::string &method() const;
|
||||
|
||||
//获取中间url,不包含?后面的参数
|
||||
const std::string &Url() const;
|
||||
// 请求时,获取中间url,不包含?后面的参数
|
||||
const std::string &url() const;
|
||||
// 回复时,获取状态码
|
||||
const std::string &status() const;
|
||||
|
||||
//获取中间url,包含?后面的参数
|
||||
std::string FullUrl() const;
|
||||
// 获取中间url,包含?后面的参数
|
||||
std::string fullUrl() const;
|
||||
|
||||
//获取命令协议名
|
||||
const std::string &Tail() const;
|
||||
// 请求时,获取协议名
|
||||
const std::string &protocol() const;
|
||||
// 回复时,获取状态字符串
|
||||
const std::string &statusStr() const;
|
||||
|
||||
//根据header key名,获取请求header value值
|
||||
// 根据header key名,获取请求header value值
|
||||
const std::string &operator[](const char *name) const;
|
||||
|
||||
//获取http body或sdp
|
||||
const std::string &Content() const;
|
||||
// 获取http body或sdp
|
||||
const std::string &content() const;
|
||||
|
||||
//清空,为了重用
|
||||
void Clear();
|
||||
// 清空,为了重用
|
||||
void clear();
|
||||
|
||||
//获取?后面的参数
|
||||
const std::string &Params() const;
|
||||
// 获取?后面的参数
|
||||
const std::string ¶ms() const;
|
||||
|
||||
//重新设置url
|
||||
// 重新设置url
|
||||
void setUrl(std::string url);
|
||||
|
||||
//重新设置content
|
||||
// 重新设置content
|
||||
void setContent(std::string content);
|
||||
|
||||
//获取header列表
|
||||
// 获取header列表
|
||||
StrCaseMap &getHeader() const;
|
||||
|
||||
//获取url参数列表
|
||||
// 获取url参数列表
|
||||
StrCaseMap &getUrlArgs() const;
|
||||
|
||||
//解析?后面的参数
|
||||
// 解析?后面的参数
|
||||
static StrCaseMap parseArgs(const std::string &str, const char *pair_delim = "&", const char *key_delim = "=");
|
||||
|
||||
static std::string merge_url(const std::string &base_url, const std::string &path);
|
||||
static std::string mergeUrl(const std::string &base_url, const std::string &path);
|
||||
|
||||
private:
|
||||
std::string _strMethod;
|
||||
@ -118,8 +120,8 @@ private:
|
||||
mutable StrCaseMap _mapUrlArgs;
|
||||
};
|
||||
|
||||
//解析rtsp url的工具类
|
||||
class RtspUrl{
|
||||
// 解析rtsp url的工具类
|
||||
class RtspUrl {
|
||||
public:
|
||||
bool _is_ssl;
|
||||
uint16_t _port;
|
||||
@ -134,9 +136,9 @@ public:
|
||||
void parse(const std::string &url);
|
||||
|
||||
private:
|
||||
void setup(bool,const std::string &, const std::string &, const std::string &);
|
||||
void setup(bool, const std::string &, const std::string &, const std::string &);
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
} // namespace mediakit
|
||||
|
||||
#endif //ZLMEDIAKIT_PARSER_H
|
||||
#endif // ZLMEDIAKIT_PARSER_H
|
||||
|
@ -37,7 +37,7 @@ bool HlsParser::parse(const string &http_url, const string &m3u8) {
|
||||
|
||||
if ((_is_m3u8_inner || extinf_dur != 0) && line[0] != '#') {
|
||||
segment.duration = extinf_dur;
|
||||
segment.url = Parser::merge_url(http_url, line);
|
||||
segment.url = Parser::mergeUrl(http_url, line);
|
||||
if (!_is_m3u8_inner) {
|
||||
//ts按照先后顺序排序
|
||||
ts_map.emplace(index++, segment);
|
||||
|
@ -100,7 +100,7 @@ void HttpClient::clearResponse() {
|
||||
_header_recved = false;
|
||||
_recved_body_size = 0;
|
||||
_total_body_size = 0;
|
||||
_parser.Clear();
|
||||
_parser.clear();
|
||||
_chunked_splitter = nullptr;
|
||||
_wait_header.resetTime();
|
||||
_wait_body.resetTime();
|
||||
@ -182,19 +182,19 @@ void HttpClient::onError(const SockException &ex) {
|
||||
|
||||
ssize_t HttpClient::onRecvHeader(const char *data, size_t len) {
|
||||
_parser.Parse(data);
|
||||
if (_parser.Url() == "302" || _parser.Url() == "301" || _parser.Url() == "303") {
|
||||
auto new_url = Parser::merge_url(_url, _parser["Location"]);
|
||||
if (_parser.status() == "302" || _parser.status() == "301" || _parser.status() == "303") {
|
||||
auto new_url = Parser::mergeUrl(_url, _parser["Location"]);
|
||||
if (new_url.empty()) {
|
||||
throw invalid_argument("未找到Location字段(跳转url)");
|
||||
}
|
||||
if (onRedirectUrl(new_url, _parser.Url() == "302")) {
|
||||
if (onRedirectUrl(new_url, _parser.status() == "302")) {
|
||||
HttpClient::sendRequest(new_url);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
checkCookie(_parser.getHeader());
|
||||
onResponseHeader(_parser.Url(), _parser.getHeader());
|
||||
onResponseHeader(_parser.status(), _parser.getHeader());
|
||||
_header_recved = true;
|
||||
|
||||
if (_parser["Transfer-Encoding"] == "chunked") {
|
||||
|
@ -240,8 +240,8 @@ public:
|
||||
static void canAccessPath(Session &sender, const Parser &parser, const MediaInfo &media_info, bool is_dir,
|
||||
const function<void(const string &err_msg, const HttpServerCookie::Ptr &cookie)> &callback) {
|
||||
//获取用户唯一id
|
||||
auto uid = parser.Params();
|
||||
auto path = parser.Url();
|
||||
auto uid = parser.params();
|
||||
auto path = parser.url();
|
||||
|
||||
//先根据http头中的cookie字段获取cookie
|
||||
HttpServerCookie::Ptr cookie = HttpCookieManager::Instance().getCookie(kCookieName, parser.getHeader());
|
||||
@ -268,7 +268,7 @@ static void canAccessPath(Session &sender, const Parser &parser, const MediaInfo
|
||||
return;
|
||||
}
|
||||
//上次鉴权失败,但是如果url参数发生变更,那么也重新鉴权下
|
||||
if (parser.Params().empty() || parser.Params() == cookie->getUid()) {
|
||||
if (parser.params().empty() || parser.params() == cookie->getUid()) {
|
||||
//url参数未变,或者本来就没有url参数,那么判断本次请求为重复请求,无访问权限
|
||||
callback(attach._err_msg, update_cookie ? cookie : nullptr);
|
||||
return;
|
||||
@ -469,11 +469,11 @@ static string getFilePath(const Parser &parser,const MediaInfo &media_info, Sess
|
||||
if (it != virtualPathMap.end()) {
|
||||
//访问的是virtualPath
|
||||
path = it->second;
|
||||
url = parser.Url().substr(1 + media_info.app.size());
|
||||
url = parser.url().substr(1 + media_info.app.size());
|
||||
} else {
|
||||
//访问的是rootPath
|
||||
path = rootPath;
|
||||
url = parser.Url();
|
||||
url = parser.url();
|
||||
}
|
||||
for (auto &ch : url) {
|
||||
if (ch == '\\') {
|
||||
@ -493,7 +493,7 @@ static string getFilePath(const Parser &parser,const MediaInfo &media_info, Sess
|
||||
* @param cb 回调对象
|
||||
*/
|
||||
void HttpFileManager::onAccessPath(Session &sender, Parser &parser, const HttpFileManager::invoker &cb) {
|
||||
auto fullUrl = string(HTTP_SCHEMA) + "://" + parser["Host"] + parser.FullUrl();
|
||||
auto fullUrl = string(HTTP_SCHEMA) + "://" + parser["Host"] + parser.fullUrl();
|
||||
MediaInfo media_info(fullUrl);
|
||||
auto file_path = getFilePath(parser, media_info, sender);
|
||||
if (file_path.size() == 0) {
|
||||
@ -506,13 +506,13 @@ void HttpFileManager::onAccessPath(Session &sender, Parser &parser, const HttpFi
|
||||
if (!indexFile.empty()) {
|
||||
//发现该文件夹下有index文件
|
||||
file_path = pathCat(file_path, indexFile);
|
||||
parser.setUrl(pathCat(parser.Url(), indexFile));
|
||||
parser.setUrl(pathCat(parser.url(), indexFile));
|
||||
accessFile(sender, parser, media_info, file_path, cb);
|
||||
return;
|
||||
}
|
||||
string strMenu;
|
||||
//生成文件夹菜单索引
|
||||
if (!makeFolderMenu(parser.Url(), file_path, strMenu)) {
|
||||
if (!makeFolderMenu(parser.url(), file_path, strMenu)) {
|
||||
//文件夹不存在
|
||||
sendNotFound(cb);
|
||||
return;
|
||||
|
@ -64,10 +64,10 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) {
|
||||
});
|
||||
|
||||
_parser.Parse(header);
|
||||
CHECK(_parser.Url()[0] == '/');
|
||||
CHECK(_parser.url()[0] == '/');
|
||||
|
||||
urlDecode(_parser);
|
||||
string cmd = _parser.Method();
|
||||
string cmd = _parser.method();
|
||||
auto it = s_func_map.find(cmd);
|
||||
if (it == s_func_map.end()) {
|
||||
WarnP(this) << "不支持该命令:" << cmd;
|
||||
@ -80,7 +80,7 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) {
|
||||
(this->*(it->second))(content_len);
|
||||
|
||||
// 清空解析器节省内存
|
||||
_parser.Clear();
|
||||
_parser.clear();
|
||||
// 返回content长度
|
||||
return content_len;
|
||||
}
|
||||
@ -173,7 +173,7 @@ bool HttpSession::checkWebSocket() {
|
||||
}
|
||||
|
||||
bool HttpSession::checkLiveStream(const string &schema, const string &url_suffix, const function<void(const MediaSource::Ptr &src)> &cb) {
|
||||
std::string url = _parser.Url();
|
||||
std::string url = _parser.url();
|
||||
auto it = _parser.getUrlArgs().find("schema");
|
||||
if (it != _parser.getUrlArgs().end()) {
|
||||
if (strcasecmp(it->second.c_str(), schema.c_str())) {
|
||||
@ -191,9 +191,9 @@ bool HttpSession::checkLiveStream(const string &schema, const string &url_suffix
|
||||
}
|
||||
|
||||
// 带参数的url
|
||||
if (!_parser.Params().empty()) {
|
||||
if (!_parser.params().empty()) {
|
||||
url += "?";
|
||||
url += _parser.Params();
|
||||
url += _parser.params();
|
||||
}
|
||||
|
||||
// 解析带上协议+参数完整的url
|
||||
@ -616,7 +616,7 @@ string HttpSession::urlDecode(const string &str) {
|
||||
}
|
||||
|
||||
void HttpSession::urlDecode(Parser &parser) {
|
||||
parser.setUrl(urlDecode(parser.Url()));
|
||||
parser.setUrl(urlDecode(parser.url()));
|
||||
for (auto &pr : _parser.getUrlArgs()) {
|
||||
const_cast<string &>(pr.second) = urlDecode(pr.second);
|
||||
}
|
||||
@ -682,7 +682,7 @@ void HttpSession::Handle_Req_POST(ssize_t &content_len) {
|
||||
// 触发http事件,emitHttpEvent内部会选择是否关闭连接
|
||||
emitHttpEvent(true);
|
||||
// 清空数据,节省内存
|
||||
_parser.Clear();
|
||||
_parser.clear();
|
||||
// content已经接收完毕
|
||||
return false;
|
||||
};
|
||||
|
@ -27,7 +27,7 @@ namespace mediakit {
|
||||
RtmpPlayer::RtmpPlayer(const EventPoller::Ptr &poller) : TcpClient(poller) {}
|
||||
|
||||
RtmpPlayer::~RtmpPlayer() {
|
||||
DebugL << endl;
|
||||
DebugL;
|
||||
}
|
||||
|
||||
void RtmpPlayer::teardown() {
|
||||
|
@ -27,7 +27,7 @@ public:
|
||||
RtmpPlayerImp(const toolkit::EventPoller::Ptr &poller) : Super(poller) {};
|
||||
|
||||
~RtmpPlayerImp() override {
|
||||
DebugL << std::endl;
|
||||
DebugL;
|
||||
}
|
||||
|
||||
float getProgress() const override {
|
||||
|
@ -27,7 +27,7 @@ RtmpPusher::RtmpPusher(const EventPoller::Ptr &poller, const RtmpMediaSource::Pt
|
||||
|
||||
RtmpPusher::~RtmpPusher() {
|
||||
teardown();
|
||||
DebugL << endl;
|
||||
DebugL;
|
||||
}
|
||||
|
||||
void RtmpPusher::teardown() {
|
||||
|
@ -33,7 +33,7 @@ RtspPlayer::RtspPlayer(const EventPoller::Ptr &poller)
|
||||
: TcpClient(poller) {}
|
||||
|
||||
RtspPlayer::~RtspPlayer(void) {
|
||||
DebugL << endl;
|
||||
DebugL;
|
||||
}
|
||||
|
||||
void RtspPlayer::sendTeardown() {
|
||||
@ -170,11 +170,11 @@ bool RtspPlayer::handleAuthenticationFailure(const string ¶msStr) {
|
||||
bool RtspPlayer::handleResponse(const string &cmd, const Parser &parser) {
|
||||
string authInfo = parser["WWW-Authenticate"];
|
||||
// 发送DESCRIBE命令后的回复
|
||||
if ((parser.Url() == "401") && handleAuthenticationFailure(authInfo)) {
|
||||
if ((parser.status() == "401") && handleAuthenticationFailure(authInfo)) {
|
||||
sendOptions();
|
||||
return false;
|
||||
}
|
||||
if (parser.Url() == "302" || parser.Url() == "301") {
|
||||
if (parser.status() == "302" || parser.status() == "301") {
|
||||
auto newUrl = parser["Location"];
|
||||
if (newUrl.empty()) {
|
||||
throw std::runtime_error("未找到Location字段(跳转url)");
|
||||
@ -182,8 +182,8 @@ bool RtspPlayer::handleResponse(const string &cmd, const Parser &parser) {
|
||||
play(newUrl);
|
||||
return false;
|
||||
}
|
||||
if (parser.Url() != "200") {
|
||||
throw std::runtime_error(StrPrinter << cmd << ":" << parser.Url() << " " << parser.Tail() << endl);
|
||||
if (parser.status() != "200") {
|
||||
throw std::runtime_error(StrPrinter << cmd << ":" << parser.status() << " " << parser.statusStr() << endl);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -201,7 +201,7 @@ void RtspPlayer::handleResDESCRIBE(const Parser &parser) {
|
||||
}
|
||||
|
||||
// 解析sdp
|
||||
SdpParser sdpParser(parser.Content());
|
||||
SdpParser sdpParser(parser.content());
|
||||
|
||||
string sdp;
|
||||
auto play_track = (TrackType)((int)(*this)[Client::kPlayTrack] - 1);
|
||||
@ -267,8 +267,8 @@ void RtspPlayer::sendSetup(unsigned int track_idx) {
|
||||
}
|
||||
|
||||
void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) {
|
||||
if (parser.Url() != "200") {
|
||||
throw std::runtime_error(StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl);
|
||||
if (parser.status() != "200") {
|
||||
throw std::runtime_error(StrPrinter << "SETUP:" << parser.status() << " " << parser.statusStr() << endl);
|
||||
}
|
||||
if (track_idx == 0) {
|
||||
_session_id = parser["Session"];
|
||||
@ -446,14 +446,14 @@ void RtspPlayer::speed(float speed) {
|
||||
}
|
||||
|
||||
void RtspPlayer::handleResPAUSE(const Parser &parser, int type) {
|
||||
if (parser.Url() != "200") {
|
||||
if (parser.status() != "200") {
|
||||
switch (type) {
|
||||
case type_pause: WarnL << "Pause failed:" << parser.Url() << " " << parser.Tail() << endl; break;
|
||||
case type_pause: WarnL << "Pause failed:" << parser.status() << " " << parser.statusStr(); break;
|
||||
case type_play:
|
||||
WarnL << "Play failed:" << parser.Url() << " " << parser.Tail() << endl;
|
||||
onPlayResult_l(SockException(Err_shutdown, StrPrinter << "rtsp play failed:" << parser.Url() << " " << parser.Tail()), !_play_check_timer);
|
||||
WarnL << "Play failed:" << parser.status() << " " << parser.statusStr();
|
||||
onPlayResult_l(SockException(Err_shutdown, StrPrinter << "rtsp play failed:" << parser.status() << " " << parser.statusStr()), !_play_check_timer);
|
||||
break;
|
||||
case type_seek: WarnL << "Seek failed:" << parser.Url() << " " << parser.Tail() << endl; break;
|
||||
case type_seek: WarnL << "Seek failed:" << parser.status() << " " << parser.statusStr(); break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -487,7 +487,7 @@ void RtspPlayer::onWholeRtspPacket(Parser &parser) {
|
||||
if (func) {
|
||||
func(parser);
|
||||
}
|
||||
parser.Clear();
|
||||
parser.clear();
|
||||
} catch (std::exception &err) {
|
||||
// 定时器_pPlayTimer为空后表明握手结束了
|
||||
onPlayResult_l(SockException(Err_other, err.what()), !_play_check_timer);
|
||||
|
@ -28,7 +28,7 @@ public:
|
||||
RtspPlayerImp(const toolkit::EventPoller::Ptr &poller) : Super(poller) {}
|
||||
|
||||
~RtspPlayerImp() override {
|
||||
DebugL << std::endl;
|
||||
DebugL;
|
||||
}
|
||||
|
||||
float getProgress() const override {
|
||||
|
@ -26,7 +26,7 @@ RtspPusher::RtspPusher(const EventPoller::Ptr &poller, const RtspMediaSource::Pt
|
||||
|
||||
RtspPusher::~RtspPusher() {
|
||||
teardown();
|
||||
DebugL << endl;
|
||||
DebugL;
|
||||
}
|
||||
|
||||
void RtspPusher::sendTeardown(){
|
||||
@ -147,7 +147,7 @@ void RtspPusher::onWholeRtspPacket(Parser &parser) {
|
||||
if (func) {
|
||||
func(parser);
|
||||
}
|
||||
parser.Clear();
|
||||
parser.clear();
|
||||
}
|
||||
|
||||
void RtspPusher::onRtpPacket(const char *data, size_t len) {
|
||||
@ -188,11 +188,11 @@ void RtspPusher::sendAnnounce() {
|
||||
void RtspPusher::handleResAnnounce(const Parser &parser) {
|
||||
string authInfo = parser["WWW-Authenticate"];
|
||||
//发送DESCRIBE命令后的回复
|
||||
if ((parser.Url() == "401") && handleAuthenticationFailure(authInfo)) {
|
||||
if ((parser.status() == "401") && handleAuthenticationFailure(authInfo)) {
|
||||
sendAnnounce();
|
||||
return;
|
||||
}
|
||||
if (parser.Url() == "302") {
|
||||
if (parser.status() == "302") {
|
||||
auto newUrl = parser["Location"];
|
||||
if (newUrl.empty()) {
|
||||
throw std::runtime_error("未找到Location字段(跳转url)");
|
||||
@ -200,8 +200,8 @@ void RtspPusher::handleResAnnounce(const Parser &parser) {
|
||||
publish(newUrl);
|
||||
return;
|
||||
}
|
||||
if (parser.Url() != "200") {
|
||||
throw std::runtime_error(StrPrinter << "ANNOUNCE:" << parser.Url() << " " << parser.Tail());
|
||||
if (parser.status() != "200") {
|
||||
throw std::runtime_error(StrPrinter << "ANNOUNCE:" << parser.status() << " " << parser.statusStr());
|
||||
}
|
||||
_content_base = parser["Content-Base"];
|
||||
|
||||
@ -285,8 +285,8 @@ void RtspPusher::sendSetup(unsigned int track_idx) {
|
||||
}
|
||||
|
||||
void RtspPusher::handleResSetup(const Parser &parser, unsigned int track_idx) {
|
||||
if (parser.Url() != "200") {
|
||||
throw std::runtime_error(StrPrinter << "SETUP:" << parser.Url() << " " << parser.Tail() << endl);
|
||||
if (parser.status() != "200") {
|
||||
throw std::runtime_error(StrPrinter << "SETUP:" << parser.status() << " " << parser.statusStr() << endl);
|
||||
}
|
||||
if (track_idx == 0) {
|
||||
_session_id = parser["Session"];
|
||||
|
@ -128,11 +128,11 @@ void RtspSession::onRecv(const Buffer::Ptr &buf) {
|
||||
}
|
||||
|
||||
void RtspSession::onWholeRtspPacket(Parser &parser) {
|
||||
string method = parser.Method(); //提取出请求命令字
|
||||
string method = parser.method(); //提取出请求命令字
|
||||
_cseq = atoi(parser["CSeq"].data());
|
||||
if (_content_base.empty() && method != "GET") {
|
||||
_content_base = parser.Url();
|
||||
_media_info.parse(parser.FullUrl());
|
||||
_content_base = parser.url();
|
||||
_media_info.parse(parser.fullUrl());
|
||||
_media_info.schema = RTSP_SCHEMA;
|
||||
}
|
||||
|
||||
@ -160,7 +160,7 @@ void RtspSession::onWholeRtspPacket(Parser &parser) {
|
||||
}
|
||||
|
||||
(this->*(it->second))(parser);
|
||||
parser.Clear();
|
||||
parser.clear();
|
||||
}
|
||||
|
||||
void RtspSession::onRtpPacket(const char *data, size_t len) {
|
||||
@ -187,7 +187,7 @@ void RtspSession::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, const char *
|
||||
}
|
||||
|
||||
ssize_t RtspSession::getContentLength(Parser &parser) {
|
||||
if(parser.Method() == "POST"){
|
||||
if(parser.method() == "POST"){
|
||||
//http post请求的content数据部分是base64编码后的rtsp请求信令包
|
||||
return remainDataSize();
|
||||
}
|
||||
@ -200,7 +200,7 @@ void RtspSession::handleReq_Options(const Parser &parser) {
|
||||
}
|
||||
|
||||
void RtspSession::handleReq_ANNOUNCE(const Parser &parser) {
|
||||
auto full_url = parser.FullUrl();
|
||||
auto full_url = parser.fullUrl();
|
||||
_content_base = full_url;
|
||||
if (end_with(full_url, ".sdp")) {
|
||||
//去除.sdp后缀,防止EasyDarwin推流器强制添加.sdp后缀
|
||||
@ -250,7 +250,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) {
|
||||
throw SockException(Err_shutdown, err);
|
||||
}
|
||||
|
||||
SdpParser sdpParser(parser.Content());
|
||||
SdpParser sdpParser(parser.content());
|
||||
_sessionid = makeRandStr(12);
|
||||
_sdp_track = sdpParser.getAvailableTrack();
|
||||
if (_sdp_track.empty()) {
|
||||
@ -270,7 +270,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) {
|
||||
//获取所有权
|
||||
_push_src_ownership = _push_src->getOwnership();
|
||||
_push_src->setProtocolOption(option);
|
||||
_push_src->setSdp(parser.Content());
|
||||
_push_src->setSdp(parser.content());
|
||||
}
|
||||
|
||||
_push_src->setListener(static_pointer_cast<RtspSession>(shared_from_this()));
|
||||
@ -628,7 +628,7 @@ void RtspSession::send_SessionNotFound() {
|
||||
|
||||
void RtspSession::handleReq_Setup(const Parser &parser) {
|
||||
//处理setup命令,该函数可能进入多次
|
||||
int trackIdx = getTrackIndexByControlUrl(parser.FullUrl());
|
||||
int trackIdx = getTrackIndexByControlUrl(parser.fullUrl());
|
||||
SdpTrack::Ptr &trackRef = _sdp_track[trackIdx];
|
||||
if (trackRef->_inited) {
|
||||
//已经初始化过该Track
|
||||
@ -915,9 +915,9 @@ void RtspSession::handleReq_Post(const Parser &parser) {
|
||||
});
|
||||
};
|
||||
|
||||
if(!parser.Content().empty()){
|
||||
if(!parser.content().empty()){
|
||||
//http poster后面的粘包
|
||||
_on_recv(std::make_shared<BufferString>(parser.Content()));
|
||||
_on_recv(std::make_shared<BufferString>(parser.content()));
|
||||
}
|
||||
|
||||
sendRtspResponse("200 OK",
|
||||
|
@ -65,7 +65,7 @@ ssize_t RtspSplitter::onRecvHeader(const char *data, size_t len) {
|
||||
auto ret = getContentLength(_parser);
|
||||
if(ret == 0){
|
||||
onWholeRtspPacket(_parser);
|
||||
_parser.Clear();
|
||||
_parser.clear();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@ -73,7 +73,7 @@ ssize_t RtspSplitter::onRecvHeader(const char *data, size_t len) {
|
||||
void RtspSplitter::onRecvContent(const char *data, size_t len) {
|
||||
_parser.setContent(string(data,len));
|
||||
onWholeRtspPacket(_parser);
|
||||
_parser.Clear();
|
||||
_parser.clear();
|
||||
}
|
||||
|
||||
void RtspSplitter::enableRecvRtp(bool enable) {
|
||||
|
@ -43,7 +43,7 @@ void initEventListener(){
|
||||
static onceToken s_token([](){
|
||||
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastHttpRequest,[](BroadcastHttpRequestArgs){
|
||||
//const Parser &parser,HttpSession::HttpResponseInvoker &invoker,bool &consumed
|
||||
if(strstr(parser.Url().data(),"/api/") != parser.Url().data()){
|
||||
if(strstr(parser.url().data(),"/api/") != parser.url().data()){
|
||||
return;
|
||||
}
|
||||
//url以"/api/起始,说明是http api"
|
||||
@ -51,11 +51,11 @@ void initEventListener(){
|
||||
|
||||
_StrPrinter printer;
|
||||
////////////////method////////////////////
|
||||
printer << "\r\nmethod:\r\n\t" << parser.Method();
|
||||
printer << "\r\nmethod:\r\n\t" << parser.method();
|
||||
////////////////url/////////////////
|
||||
printer << "\r\nurl:\r\n\t" << parser.Url();
|
||||
printer << "\r\nurl:\r\n\t" << parser.url();
|
||||
////////////////protocol/////////////////
|
||||
printer << "\r\nprotocol:\r\n\t" << parser.Tail();
|
||||
printer << "\r\nprotocol:\r\n\t" << parser.protocol();
|
||||
///////////////args//////////////////
|
||||
printer << "\r\nargs:\r\n";
|
||||
for(auto &pr : parser.getUrlArgs()){
|
||||
@ -67,7 +67,7 @@ void initEventListener(){
|
||||
printer << "\t" << pr.first << " : " << pr.second << "\r\n";
|
||||
}
|
||||
////////////////content/////////////////
|
||||
printer << "\r\ncontent:\r\n" << parser.Content();
|
||||
printer << "\r\ncontent:\r\n" << parser.content();
|
||||
auto contentOut = printer << endl;
|
||||
|
||||
////////////////我们测算异步回复,当然你也可以同步回复/////////////////
|
||||
@ -113,7 +113,7 @@ int main(int argc,char *argv[]){
|
||||
TcpServer::Ptr httpsSrv(new TcpServer());
|
||||
httpsSrv->start<HttpsSession>(mINI::Instance()[Http::kSSLPort]);//默认443
|
||||
|
||||
InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1" << endl;
|
||||
InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1";
|
||||
|
||||
sem.wait();
|
||||
return 0;
|
||||
|
@ -86,9 +86,9 @@ int main(int argc, char *argv[]) {
|
||||
for (auto &pr: parser.getHeader()) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "status:" << parser.Url() << "\r\n"
|
||||
InfoL << "status:" << parser.status() << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << parser.Content();
|
||||
<< "\r\nbody:" << parser.content();
|
||||
}
|
||||
});
|
||||
|
||||
@ -122,9 +122,9 @@ int main(int argc, char *argv[]) {
|
||||
for (auto &pr: parser.getHeader()) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "status:" << parser.Url() << "\r\n"
|
||||
InfoL << "status:" << parser.status() << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << parser.Content();
|
||||
<< "\r\nbody:" << parser.content();
|
||||
}
|
||||
});
|
||||
|
||||
@ -159,9 +159,9 @@ int main(int argc, char *argv[]) {
|
||||
for (auto &pr: parser.getHeader()) {
|
||||
printer << pr.first << ":" << pr.second << "\r\n";
|
||||
}
|
||||
InfoL << "status:" << parser.Url() << "\r\n"
|
||||
InfoL << "status:" << parser.status() << "\r\n"
|
||||
<< "header:\r\n" << (printer << endl)
|
||||
<< "\r\nbody:" << parser.Content();
|
||||
<< "\r\nbody:" << parser.content();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -83,7 +83,7 @@ struct EchoSessionCreator {
|
||||
//返回的Session必须派生于SendInterceptor,可以返回null(拒绝连接)
|
||||
Session::Ptr operator()(const Parser &header, const HttpSession &parent, const Socket::Ptr &pSock) {
|
||||
// return nullptr;
|
||||
if (header.Url() == "/") {
|
||||
if (header.url() == "/") {
|
||||
return std::make_shared<SessionTypeImp<EchoSession> >(header, parent, pSock);
|
||||
}
|
||||
return std::make_shared<SessionTypeImp<EchoSessionWithUrl> >(header, parent, pSock);
|
||||
|
@ -323,7 +323,7 @@ void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tup
|
||||
if (RTC::StunPacket::IsStun((const uint8_t *)buf, len)) {
|
||||
std::unique_ptr<RTC::StunPacket> packet(RTC::StunPacket::Parse((const uint8_t *)buf, len));
|
||||
if (!packet) {
|
||||
WarnL << "parse stun error" << std::endl;
|
||||
WarnL << "parse stun error";
|
||||
return;
|
||||
}
|
||||
_ice_server->ProcessStunPacket(packet.get(), tuple);
|
||||
|
Loading…
Reference in New Issue
Block a user