2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-06-11 09:25:54 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-06-11 09:25:54 +08:00
|
|
|
|
*
|
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-11 09:25:54 +08:00
|
|
|
|
*/
|
2019-06-06 18:28:33 +08:00
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_WEBAPI_H
|
|
|
|
|
#define ZLMEDIAKIT_WEBAPI_H
|
|
|
|
|
|
2019-06-19 10:32:54 +08:00
|
|
|
|
#include <string>
|
2020-12-27 22:14:59 +08:00
|
|
|
|
#include <functional>
|
|
|
|
|
#include "jsoncpp/json.h"
|
|
|
|
|
#include "Common/Parser.h"
|
|
|
|
|
#include "Network/Socket.h"
|
|
|
|
|
#include "Http/HttpSession.h"
|
|
|
|
|
|
2019-06-19 10:32:54 +08:00
|
|
|
|
using namespace std;
|
2020-12-27 22:14:59 +08:00
|
|
|
|
using namespace Json;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
using namespace mediakit;
|
2019-06-19 10:32:54 +08:00
|
|
|
|
|
2020-12-27 22:14:59 +08:00
|
|
|
|
//配置文件路径
|
|
|
|
|
extern string g_ini_file;
|
2019-06-19 10:32:54 +08:00
|
|
|
|
|
2020-12-27 22:14:59 +08:00
|
|
|
|
namespace mediakit {
|
2019-06-06 18:28:33 +08:00
|
|
|
|
////////////RTSP服务器配置///////////
|
|
|
|
|
namespace Rtsp {
|
2019-06-19 10:32:54 +08:00
|
|
|
|
extern const string kPort;
|
2019-06-06 18:28:33 +08:00
|
|
|
|
} //namespace Rtsp
|
|
|
|
|
|
|
|
|
|
////////////RTMP服务器配置///////////
|
|
|
|
|
namespace Rtmp {
|
2019-06-19 10:32:54 +08:00
|
|
|
|
extern const string kPort;
|
2019-06-06 18:28:33 +08:00
|
|
|
|
} //namespace RTMP
|
|
|
|
|
} // namespace mediakit
|
|
|
|
|
|
2020-12-27 22:14:59 +08:00
|
|
|
|
namespace API {
|
|
|
|
|
typedef enum {
|
|
|
|
|
NotFound = -500,//未找到
|
|
|
|
|
Exception = -400,//代码抛异常
|
|
|
|
|
InvalidArgs = -300,//参数不合法
|
|
|
|
|
SqlFailed = -200,//sql执行失败
|
|
|
|
|
AuthFailed = -100,//鉴权失败
|
|
|
|
|
OtherFailed = -1,//业务代码执行失败,
|
|
|
|
|
Success = 0//执行成功
|
|
|
|
|
} ApiErr;
|
|
|
|
|
}//namespace API
|
|
|
|
|
|
|
|
|
|
class ApiRetException: public std::runtime_error {
|
|
|
|
|
public:
|
|
|
|
|
ApiRetException(const char *str = "success" ,int code = API::Success):runtime_error(str){
|
|
|
|
|
_code = code;
|
|
|
|
|
}
|
|
|
|
|
~ApiRetException() = default;
|
|
|
|
|
int code(){ return _code; }
|
|
|
|
|
private:
|
|
|
|
|
int _code;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AuthException : public ApiRetException {
|
|
|
|
|
public:
|
|
|
|
|
AuthException(const char *str):ApiRetException(str,API::AuthFailed){}
|
|
|
|
|
~AuthException() = default;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class InvalidArgsException: public ApiRetException {
|
|
|
|
|
public:
|
|
|
|
|
InvalidArgsException(const char *str):ApiRetException(str,API::InvalidArgs){}
|
|
|
|
|
~InvalidArgsException() = default;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SuccessException: public ApiRetException {
|
|
|
|
|
public:
|
|
|
|
|
SuccessException():ApiRetException("success",API::Success){}
|
|
|
|
|
~SuccessException() = default;
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
using ApiArgsType = map<string, string, StrCaseCompare>;
|
2020-12-27 22:14:59 +08:00
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
template<typename Args, typename First>
|
|
|
|
|
string getValue(Args &args, const First &first) {
|
|
|
|
|
return args[first];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First>
|
|
|
|
|
string getValue(Json::Value &args, const First &first) {
|
|
|
|
|
return args[first].asString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First>
|
|
|
|
|
string getValue(string &args, const First &first) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First>
|
|
|
|
|
string getValue(const Parser &parser, const First &first) {
|
|
|
|
|
auto ret = parser.getUrlArgs()[first];
|
|
|
|
|
if (!ret.empty()) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
return parser.getHeader()[first];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First>
|
|
|
|
|
string getValue(Parser &parser, const First &first) {
|
|
|
|
|
return getValue((const Parser &) parser, first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Args, typename First>
|
|
|
|
|
string getValue(const Parser &parser, Args &args, const First &first) {
|
|
|
|
|
auto ret = getValue(args, first);
|
|
|
|
|
if (!ret.empty()) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
return getValue(parser, first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Args>
|
|
|
|
|
class HttpAllArgs {
|
|
|
|
|
public:
|
|
|
|
|
HttpAllArgs(const Parser &parser, Args &args) {
|
|
|
|
|
_get_args = [&args]() {
|
|
|
|
|
return (void *) &args;
|
|
|
|
|
};
|
|
|
|
|
_get_parser = [&parser]() -> const Parser & {
|
|
|
|
|
return parser;
|
|
|
|
|
};
|
|
|
|
|
_get_value = [](HttpAllArgs &that, const string &key) {
|
|
|
|
|
return getValue(that.getParser(), that.getArgs(), key);
|
|
|
|
|
};
|
|
|
|
|
_clone = [&](HttpAllArgs &that) {
|
|
|
|
|
that._get_args = [args]() {
|
|
|
|
|
return (void *) &args;
|
|
|
|
|
};
|
|
|
|
|
that._get_parser = [parser]() -> const Parser & {
|
|
|
|
|
return parser;
|
|
|
|
|
};
|
|
|
|
|
that._get_value = [](HttpAllArgs &that, const string &key) {
|
|
|
|
|
return getValue(that.getParser(), that.getArgs(), key);
|
|
|
|
|
};
|
|
|
|
|
that._cache_able = true;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpAllArgs(const HttpAllArgs &that) {
|
|
|
|
|
if (that._cache_able) {
|
|
|
|
|
_get_args = that._get_args;
|
|
|
|
|
_get_parser = that._get_parser;
|
|
|
|
|
_get_value = that._get_value;
|
2021-08-18 20:37:10 +08:00
|
|
|
|
_cache_able = true;
|
2021-08-12 16:07:31 +08:00
|
|
|
|
} else {
|
|
|
|
|
that._clone(*this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~HttpAllArgs() = default;
|
|
|
|
|
|
|
|
|
|
template<typename Key>
|
|
|
|
|
variant operator[](const Key &key) const {
|
|
|
|
|
return (variant)_get_value(*(HttpAllArgs*)this, key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Parser &getParser() const {
|
|
|
|
|
return _get_parser();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Args &getArgs() {
|
|
|
|
|
return *((Args *) _get_args());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Args &getArgs() const {
|
|
|
|
|
return *((Args *) _get_args());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool _cache_able = false;
|
|
|
|
|
function<void *() > _get_args;
|
|
|
|
|
function<const Parser &() > _get_parser;
|
|
|
|
|
function<string(HttpAllArgs &that, const string &key)> _get_value;
|
|
|
|
|
function<void(HttpAllArgs &that) > _clone;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define API_ARGS_MAP SockInfo &sender, HttpSession::KeyValue &headerOut, const HttpAllArgs<ApiArgsType> &allArgs, Json::Value &val
|
2020-12-27 22:14:59 +08:00
|
|
|
|
#define API_ARGS_MAP_ASYNC API_ARGS_MAP, const HttpSession::HttpResponseInvoker &invoker
|
2021-08-12 16:07:31 +08:00
|
|
|
|
#define API_ARGS_JSON SockInfo &sender, HttpSession::KeyValue &headerOut, const HttpAllArgs<Json::Value> &allArgs, Json::Value &val
|
2020-12-27 22:14:59 +08:00
|
|
|
|
#define API_ARGS_JSON_ASYNC API_ARGS_JSON, const HttpSession::HttpResponseInvoker &invoker
|
2021-08-12 16:07:31 +08:00
|
|
|
|
#define API_ARGS_STRING SockInfo &sender, HttpSession::KeyValue &headerOut, const HttpAllArgs<string> &allArgs, Json::Value &val
|
2021-03-31 17:15:26 +08:00
|
|
|
|
#define API_ARGS_STRING_ASYNC API_ARGS_STRING, const HttpSession::HttpResponseInvoker &invoker
|
2021-08-12 16:07:31 +08:00
|
|
|
|
#define API_ARGS_VALUE sender, headerOut, allArgs, val
|
2020-12-27 22:14:59 +08:00
|
|
|
|
|
|
|
|
|
//注册http请求参数是map<string, variant, StrCaseCompare>类型的http api
|
|
|
|
|
void api_regist(const string &api_path, const function<void(API_ARGS_MAP)> &func);
|
|
|
|
|
//注册http请求参数是map<string, variant, StrCaseCompare>类型,但是可以异步回复的的http api
|
|
|
|
|
void api_regist(const string &api_path, const function<void(API_ARGS_MAP_ASYNC)> &func);
|
|
|
|
|
|
|
|
|
|
//注册http请求参数是Json::Value类型的http api(可以支持多级嵌套的json参数对象)
|
|
|
|
|
void api_regist(const string &api_path, const function<void(API_ARGS_JSON)> &func);
|
|
|
|
|
//注册http请求参数是Json::Value类型,但是可以异步回复的的http api
|
|
|
|
|
void api_regist(const string &api_path, const function<void(API_ARGS_JSON_ASYNC)> &func);
|
|
|
|
|
|
2021-03-31 17:15:26 +08:00
|
|
|
|
//注册http请求参数是http原始请求信息的http api
|
|
|
|
|
void api_regist(const string &api_path, const function<void(API_ARGS_STRING)> &func);
|
|
|
|
|
//注册http请求参数是http原始请求信息的异步回复的http api
|
|
|
|
|
void api_regist(const string &api_path, const function<void(API_ARGS_STRING_ASYNC)> &func);
|
|
|
|
|
|
2020-12-27 22:14:59 +08:00
|
|
|
|
template<typename Args, typename First>
|
2021-08-12 16:07:31 +08:00
|
|
|
|
bool checkArgs(Args &args, const First &first) {
|
2020-12-27 22:14:59 +08:00
|
|
|
|
return !args[first].empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Args, typename First, typename ...KeyTypes>
|
2021-08-12 16:07:31 +08:00
|
|
|
|
bool checkArgs(Args &args, const First &first, const KeyTypes &...keys) {
|
|
|
|
|
return checkArgs(args, first) && checkArgs(args, keys...);
|
2021-03-31 17:15:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
//检查http url中或body中或http header参数是否为空的宏
|
2020-12-27 22:14:59 +08:00
|
|
|
|
#define CHECK_ARGS(...) \
|
|
|
|
|
if(!checkArgs(allArgs,##__VA_ARGS__)){ \
|
|
|
|
|
throw InvalidArgsException("缺少必要参数:" #__VA_ARGS__); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//检查http参数中是否附带secret密钥的宏,127.0.0.1的ip不检查密钥
|
|
|
|
|
#define CHECK_SECRET() \
|
|
|
|
|
if(sender.get_peer_ip() != "127.0.0.1"){ \
|
|
|
|
|
CHECK_ARGS("secret"); \
|
|
|
|
|
if(api_secret != allArgs["secret"]){ \
|
|
|
|
|
throw AuthException("secret错误"); \
|
|
|
|
|
} \
|
|
|
|
|
}
|
2019-06-06 18:28:33 +08:00
|
|
|
|
|
2019-06-24 14:58:56 +08:00
|
|
|
|
void installWebApi();
|
|
|
|
|
void unInstallWebApi();
|
2021-04-08 17:34:13 +08:00
|
|
|
|
Value makeMediaSourceJson(MediaSource &media);
|
2021-12-27 17:40:15 +08:00
|
|
|
|
void getStatisticJson(const function<void(Value &val)> &cb);
|
2022-01-12 16:45:47 +08:00
|
|
|
|
void addStreamProxy(const string &vhost, const string &app, const string &stream, const string &url, int retry_count,
|
|
|
|
|
bool enable_hls, bool enable_mp4, int rtp_type, float timeout_sec,
|
|
|
|
|
const function<void(const SockException &ex, const string &key)> &cb);
|
2019-06-06 18:28:33 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_WEBAPI_H
|