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
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string FindField(const char *buf, const char *start, const char *end, size_t bufSize = 0);
|
2019-06-28 16:48:02 +08:00
|
|
|
|
|
|
|
|
|
struct StrCaseCompare {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
bool operator()(const std::string &__x, const std::string &__y) const {
|
2019-07-17 14:50:24 +08:00
|
|
|
|
return strcasecmp(__x.data(), __y.data()) < 0;
|
|
|
|
|
}
|
2019-06-28 16:48:02 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class StrCaseMap : public std::multimap<std::string, std::string, StrCaseCompare> {
|
2021-09-30 16:10:09 +08:00
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using Super = multimap<std::string, std::string, StrCaseCompare>;
|
2019-06-28 16:48:02 +08:00
|
|
|
|
StrCaseMap() = default;
|
|
|
|
|
~StrCaseMap() = default;
|
2019-07-17 14:50:24 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
template<typename V>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void emplace(const std::string &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;
|
|
|
|
|
}
|
2021-09-30 16:10:09 +08:00
|
|
|
|
Super::emplace(k, std::forward<V>(v));
|
2019-06-28 16:48:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
template<typename V>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void emplace_force(const std::string k, V &&v) {
|
2021-09-30 16:10:09 +08:00
|
|
|
|
Super::emplace(k, std::forward<V>(v));
|
2019-06-28 16:48:02 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//rtsp/http/sip解析类
|
2019-06-28 16:48:02 +08:00
|
|
|
|
class Parser {
|
2020-04-20 18:13:45 +08:00
|
|
|
|
public:
|
|
|
|
|
Parser();
|
|
|
|
|
~Parser();
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//解析信令
|
|
|
|
|
void Parse(const char *buf);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//获取命令字
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &Method() const;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//获取中间url,不包含?后面的参数
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &Url() const;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//获取中间url,包含?后面的参数
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string FullUrl() const;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//获取命令协议名
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &Tail() const;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//根据header key名,获取请求header value值
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &operator[](const char *name) const;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//获取http body或sdp
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &Content() const;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//清空,为了重用
|
|
|
|
|
void Clear();
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//获取?后面的参数
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &Params() const;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//重新设置url
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void setUrl(std::string url);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//重新设置content
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void setContent(std::string content);
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//获取header列表
|
|
|
|
|
StrCaseMap &getHeader() const;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//获取url参数列表
|
|
|
|
|
StrCaseMap &getUrlArgs() const;
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2020-04-20 18:13:45 +08:00
|
|
|
|
//解析?后面的参数
|
2022-02-02 20:34:50 +08:00
|
|
|
|
static StrCaseMap parseArgs(const std::string &str, const char *pair_delim = "&", const char *key_delim = "=");
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2019-06-28 16:55:28 +08:00
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _strMethod;
|
|
|
|
|
std::string _strUrl;
|
|
|
|
|
std::string _strTail;
|
|
|
|
|
std::string _strContent;
|
|
|
|
|
std::string _strNull;
|
|
|
|
|
std::string _params;
|
2019-06-28 16:48:02 +08:00
|
|
|
|
mutable StrCaseMap _mapHeaders;
|
|
|
|
|
mutable StrCaseMap _mapUrlArgs;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_PARSER_H
|