2019-11-30 11:38:00 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-11-30 11:38:00 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-11-30 11:38:00 +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-11-30 11:38:00 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#endif //!defined(_WIN32)
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include "HttpFileManager.h"
|
|
|
|
|
#include "Util/File.h"
|
2021-01-02 21:24:06 +08:00
|
|
|
|
#include "HttpConst.h"
|
2019-11-30 11:38:00 +08:00
|
|
|
|
#include "HttpSession.h"
|
2019-12-28 18:50:56 +08:00
|
|
|
|
#include "Record/HlsMediaSource.h"
|
2021-08-25 14:30:31 +08:00
|
|
|
|
#include "Common/Parser.h"
|
2019-11-30 11:38:00 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2019-11-30 11:38:00 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2019-11-30 14:29:44 +08:00
|
|
|
|
// hls的播放cookie缓存时间默认60秒,
|
|
|
|
|
// 每次访问一次该cookie,那么将重新刷新cookie有效期
|
|
|
|
|
// 假如播放器在60秒内都未访问该cookie,那么将重新触发hls播放鉴权
|
|
|
|
|
static int kHlsCookieSecond = 60;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
static const string kCookieName = "ZL_COOKIE";
|
2019-11-30 14:29:44 +08:00
|
|
|
|
static const string kHlsSuffix = "/hls.m3u8";
|
2020-01-02 17:46:20 +08:00
|
|
|
|
|
2020-09-20 10:13:15 +08:00
|
|
|
|
class HttpCookieAttachment {
|
2020-01-02 17:46:20 +08:00
|
|
|
|
public:
|
|
|
|
|
//cookie生效作用域,本cookie只对该目录下的文件生效
|
|
|
|
|
string _path;
|
|
|
|
|
//上次鉴权失败信息,为空则上次鉴权成功
|
|
|
|
|
string _err_msg;
|
|
|
|
|
//本cookie是否为hls直播的
|
|
|
|
|
bool _is_hls = false;
|
|
|
|
|
//hls直播时的其他一些信息,主要用于播放器个数计数以及流量计数
|
|
|
|
|
HlsCookieData::Ptr _hls_data;
|
|
|
|
|
//如果是hls直播,那么判断该cookie是否使用过MediaSource::findAsync查找过
|
|
|
|
|
//如果程序未正常退出,会残余上次的hls文件,所以判断hls直播是否存在的关键不是文件存在与否
|
|
|
|
|
//而是应该判断HlsMediaSource是否已注册,但是这样会每次获取m3u8文件时都会用MediaSource::findAsync判断一次
|
|
|
|
|
//会导致程序性能低下,所以我们应该在cookie声明周期的第一次判断HlsMediaSource是否已经注册,后续通过文件存在与否判断
|
|
|
|
|
bool _have_find_media_source = false;
|
|
|
|
|
};
|
2019-11-30 11:38:00 +08:00
|
|
|
|
|
2020-01-02 18:24:11 +08:00
|
|
|
|
const string &HttpFileManager::getContentType(const char *name) {
|
2021-01-02 21:24:06 +08:00
|
|
|
|
return getHttpContentType(name);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static string searchIndexFile(const string &dir){
|
|
|
|
|
DIR *pDir;
|
|
|
|
|
dirent *pDirent;
|
|
|
|
|
if ((pDir = opendir(dir.data())) == NULL) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
set<string> setFile;
|
|
|
|
|
while ((pDirent = readdir(pDir)) != NULL) {
|
2020-09-20 10:13:15 +08:00
|
|
|
|
static set<const char *, StrCaseCompare> indexSet = {"index.html", "index.htm", "index"};
|
|
|
|
|
if (indexSet.find(pDirent->d_name) != indexSet.end()) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
string ret = pDirent->d_name;
|
|
|
|
|
closedir(pDir);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
closedir(pDir);
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool makeFolderMenu(const string &httpPath, const string &strFullPath, string &strRet) {
|
2020-06-30 09:16:02 +08:00
|
|
|
|
GET_CONFIG(bool, dirMenu, Http::kDirMenu);
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (!dirMenu) {
|
2020-06-30 09:16:02 +08:00
|
|
|
|
//不允许浏览文件夹
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-11-30 11:38:00 +08:00
|
|
|
|
string strPathPrefix(strFullPath);
|
2021-08-26 19:36:38 +08:00
|
|
|
|
//url后缀有没有'/'访问文件夹,处理逻辑不一致
|
2019-11-30 11:38:00 +08:00
|
|
|
|
string last_dir_name;
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (strPathPrefix.back() == '/') {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
strPathPrefix.pop_back();
|
2020-09-20 10:13:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
last_dir_name = split(strPathPrefix, "/").back();
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!File::is_dir(strPathPrefix.data())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
stringstream ss;
|
2021-08-26 19:36:38 +08:00
|
|
|
|
ss << "<html>\r\n"
|
|
|
|
|
"<head>\r\n"
|
|
|
|
|
"<title>文件索引</title>\r\n"
|
|
|
|
|
"</head>\r\n"
|
|
|
|
|
"<body>\r\n"
|
|
|
|
|
"<h1>文件索引:";
|
2019-11-30 11:38:00 +08:00
|
|
|
|
|
|
|
|
|
ss << httpPath;
|
|
|
|
|
ss << "</h1>\r\n";
|
|
|
|
|
if (httpPath != "/") {
|
|
|
|
|
ss << "<li><a href=\"";
|
|
|
|
|
ss << "/";
|
|
|
|
|
ss << "\">";
|
|
|
|
|
ss << "根目录";
|
|
|
|
|
ss << "</a></li>\r\n";
|
|
|
|
|
|
|
|
|
|
ss << "<li><a href=\"";
|
2021-08-26 19:36:38 +08:00
|
|
|
|
if (!last_dir_name.empty()) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
ss << "./";
|
2021-08-26 19:36:38 +08:00
|
|
|
|
} else {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
ss << "../";
|
|
|
|
|
}
|
|
|
|
|
ss << "\">";
|
|
|
|
|
ss << "上级目录";
|
|
|
|
|
ss << "</a></li>\r\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DIR *pDir;
|
|
|
|
|
dirent *pDirent;
|
|
|
|
|
if ((pDir = opendir(strPathPrefix.data())) == NULL) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-08-26 19:36:38 +08:00
|
|
|
|
multimap<string/*url name*/, std::pair<string/*note name*/, string/*file path*/> > file_map;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
while ((pDirent = readdir(pDir)) != NULL) {
|
|
|
|
|
if (File::is_special_dir(pDirent->d_name)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (pDirent->d_name[0] == '.') {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-08-26 19:36:38 +08:00
|
|
|
|
file_map.emplace(pDirent->d_name, std::make_pair(pDirent->d_name, strPathPrefix + "/" + pDirent->d_name));
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
2021-08-25 14:30:31 +08:00
|
|
|
|
//如果是root目录,添加虚拟目录
|
|
|
|
|
if (httpPath == "/") {
|
2021-08-27 11:05:26 +08:00
|
|
|
|
GET_CONFIG_FUNC(StrCaseMap, virtualPathMap, Http::kVirtualPath, [](const string &str) {
|
|
|
|
|
return Parser::parseArgs(str, ";", ",");
|
|
|
|
|
});
|
|
|
|
|
for (auto &pr : virtualPathMap) {
|
2021-08-26 19:36:38 +08:00
|
|
|
|
file_map.emplace(pr.first, std::make_pair(string("虚拟目录:") + pr.first, File::absolutePath("", pr.second)));
|
2021-08-25 14:30:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-30 11:38:00 +08:00
|
|
|
|
int i = 0;
|
2021-08-26 19:36:38 +08:00
|
|
|
|
for (auto &pr :file_map) {
|
|
|
|
|
auto &strAbsolutePath = pr.second.second;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
bool isDir = File::is_dir(strAbsolutePath.data());
|
|
|
|
|
ss << "<li><span>" << i++ << "</span>\t";
|
|
|
|
|
ss << "<a href=\"";
|
2021-08-26 19:36:38 +08:00
|
|
|
|
//路径链接地址
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (!last_dir_name.empty()) {
|
2021-08-26 19:36:38 +08:00
|
|
|
|
ss << last_dir_name << "/" << pr.first;
|
2020-09-20 10:13:15 +08:00
|
|
|
|
} else {
|
2021-08-26 19:36:38 +08:00
|
|
|
|
ss << pr.first;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (isDir) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
ss << "/";
|
|
|
|
|
}
|
|
|
|
|
ss << "\">";
|
2021-08-26 19:36:38 +08:00
|
|
|
|
//路径名称
|
|
|
|
|
ss << pr.second.first;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
if (isDir) {
|
|
|
|
|
ss << "/</a></li>\r\n";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//是文件
|
|
|
|
|
struct stat fileData;
|
|
|
|
|
if (0 == stat(strAbsolutePath.data(), &fileData)) {
|
|
|
|
|
auto &fileSize = fileData.st_size;
|
|
|
|
|
if (fileSize < 1024) {
|
|
|
|
|
ss << " (" << fileData.st_size << "B)" << endl;
|
|
|
|
|
} else if (fileSize < 1024 * 1024) {
|
|
|
|
|
ss << fixed << setprecision(2) << " (" << fileData.st_size / 1024.0 << "KB)";
|
|
|
|
|
} else if (fileSize < 1024 * 1024 * 1024) {
|
|
|
|
|
ss << fixed << setprecision(2) << " (" << fileData.st_size / 1024 / 1024.0 << "MB)";
|
|
|
|
|
} else {
|
|
|
|
|
ss << fixed << setprecision(2) << " (" << fileData.st_size / 1024 / 1024 / 1024.0 << "GB)";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ss << "</a></li>\r\n";
|
|
|
|
|
}
|
|
|
|
|
closedir(pDir);
|
|
|
|
|
ss << "<ul>\r\n";
|
|
|
|
|
ss << "</ul>\r\n</body></html>";
|
|
|
|
|
ss.str().swap(strRet);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//拦截hls的播放请求
|
2019-12-29 11:52:02 +08:00
|
|
|
|
static bool emitHlsPlayed(const Parser &parser, const MediaInfo &mediaInfo, const HttpSession::HttpAccessPathInvoker &invoker,TcpSession &sender){
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//访问的hls.m3u8结尾,我们转换成kBroadcastMediaPlayed事件
|
2020-09-20 10:13:15 +08:00
|
|
|
|
Broadcast::AuthInvoker auth_invoker = [invoker](const string &err) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//cookie有效期为kHlsCookieSecond
|
2020-09-20 10:13:15 +08:00
|
|
|
|
invoker(err, "", kHlsCookieSecond);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
};
|
2020-09-20 10:13:15 +08:00
|
|
|
|
bool flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed, mediaInfo, auth_invoker, static_cast<SockInfo &>(sender));
|
|
|
|
|
if (!flag) {
|
2020-09-12 19:14:35 +08:00
|
|
|
|
//未开启鉴权,那么允许播放
|
2020-09-20 10:13:15 +08:00
|
|
|
|
auth_invoker("");
|
2020-09-12 19:14:35 +08:00
|
|
|
|
}
|
|
|
|
|
return flag;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 22:04:59 +08:00
|
|
|
|
class SockInfoImp : public SockInfo{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<SockInfoImp> Ptr;
|
|
|
|
|
SockInfoImp() = default;
|
|
|
|
|
~SockInfoImp() override = default;
|
|
|
|
|
|
2020-09-20 10:13:15 +08:00
|
|
|
|
string get_local_ip() override {
|
2020-04-23 22:04:59 +08:00
|
|
|
|
return _local_ip;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 10:13:15 +08:00
|
|
|
|
uint16_t get_local_port() override {
|
2020-04-23 22:04:59 +08:00
|
|
|
|
return _local_port;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 10:13:15 +08:00
|
|
|
|
string get_peer_ip() override {
|
2020-04-23 22:04:59 +08:00
|
|
|
|
return _peer_ip;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 10:13:15 +08:00
|
|
|
|
uint16_t get_peer_port() override {
|
2020-04-23 22:04:59 +08:00
|
|
|
|
return _peer_port;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 10:13:15 +08:00
|
|
|
|
string getIdentifier() const override {
|
2020-04-23 22:04:59 +08:00
|
|
|
|
return _identifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string _local_ip;
|
|
|
|
|
string _peer_ip;
|
|
|
|
|
string _identifier;
|
|
|
|
|
uint16_t _local_port;
|
|
|
|
|
uint16_t _peer_port;
|
|
|
|
|
};
|
2019-11-30 11:38:00 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断http客户端是否有权限访问文件的逻辑步骤
|
|
|
|
|
* 1、根据http请求头查找cookie,找到进入步骤3
|
|
|
|
|
* 2、根据http url参数查找cookie,如果还是未找到cookie则进入步骤5
|
|
|
|
|
* 3、cookie标记是否有权限访问文件,如果有权限,直接返回文件
|
|
|
|
|
* 4、cookie中记录的url参数是否跟本次url参数一致,如果一致直接返回客户端错误码
|
|
|
|
|
* 5、触发kBroadcastHttpAccess事件
|
|
|
|
|
*/
|
|
|
|
|
static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaInfo &mediaInfo, bool is_dir,
|
|
|
|
|
const function<void(const string &errMsg, const HttpServerCookie::Ptr &cookie)> &callback) {
|
|
|
|
|
//获取用户唯一id
|
|
|
|
|
auto uid = parser.Params();
|
|
|
|
|
auto path = parser.Url();
|
|
|
|
|
|
|
|
|
|
//先根据http头中的cookie字段获取cookie
|
2020-04-20 18:13:45 +08:00
|
|
|
|
HttpServerCookie::Ptr cookie = HttpCookieManager::Instance().getCookie(kCookieName, parser.getHeader());
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//如果不是从http头中找到的cookie,我们让http客户端设置下cookie
|
|
|
|
|
bool cookie_from_header = true;
|
|
|
|
|
if (!cookie && !uid.empty()) {
|
|
|
|
|
//客户端请求中无cookie,再根据该用户的用户id获取cookie
|
|
|
|
|
cookie = HttpCookieManager::Instance().getCookieByUid(kCookieName, uid);
|
|
|
|
|
cookie_from_header = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cookie) {
|
|
|
|
|
//找到了cookie,对cookie上锁先
|
2022-02-11 14:33:11 +08:00
|
|
|
|
auto& attach = cookie->getAttach<HttpCookieAttachment>();
|
|
|
|
|
if (path.find(attach._path) == 0) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//上次cookie是限定本目录
|
2022-02-11 14:33:11 +08:00
|
|
|
|
if (attach._err_msg.empty()) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//上次鉴权成功
|
2022-02-11 14:33:11 +08:00
|
|
|
|
if (attach._is_hls) {
|
2019-12-29 17:55:02 +08:00
|
|
|
|
//如果播放的是hls,那么刷新hls的cookie(获取ts文件也会刷新)
|
2019-11-30 14:29:44 +08:00
|
|
|
|
cookie->updateTime();
|
|
|
|
|
cookie_from_header = false;
|
|
|
|
|
}
|
2019-11-30 11:38:00 +08:00
|
|
|
|
callback("", cookie_from_header ? nullptr : cookie);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//上次鉴权失败,但是如果url参数发生变更,那么也重新鉴权下
|
|
|
|
|
if (parser.Params().empty() || parser.Params() == cookie->getUid()) {
|
|
|
|
|
//url参数未变,或者本来就没有url参数,那么判断本次请求为重复请求,无访问权限
|
2022-02-11 14:33:11 +08:00
|
|
|
|
callback(attach._err_msg, cookie_from_header ? nullptr : cookie);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//如果url参数变了或者不是限定本目录,那么旧cookie失效,重新鉴权
|
|
|
|
|
HttpCookieManager::Instance().delCookie(cookie);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 11:52:02 +08:00
|
|
|
|
bool is_hls = mediaInfo._schema == HLS_SCHEMA;
|
2020-04-23 22:04:59 +08:00
|
|
|
|
|
|
|
|
|
SockInfoImp::Ptr info = std::make_shared<SockInfoImp>();
|
|
|
|
|
info->_identifier = sender.getIdentifier();
|
|
|
|
|
info->_peer_ip = sender.get_peer_ip();
|
|
|
|
|
info->_peer_port = sender.get_peer_port();
|
|
|
|
|
info->_local_ip = sender.get_local_ip();
|
|
|
|
|
info->_local_port = sender.get_local_port();
|
2020-02-13 11:33:59 +08:00
|
|
|
|
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//该用户从来未获取过cookie,这个时候我们广播是否允许该用户访问该http目录
|
2020-04-23 22:04:59 +08:00
|
|
|
|
HttpSession::HttpAccessPathInvoker accessPathInvoker = [callback, uid, path, is_dir, is_hls, mediaInfo, info]
|
2022-02-11 14:33:11 +08:00
|
|
|
|
(const string &err_msg, const string &cookie_path_in, int life_second) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
HttpServerCookie::Ptr cookie;
|
2022-02-11 14:33:11 +08:00
|
|
|
|
if (life_second) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//本次鉴权设置了有效期,我们把鉴权结果缓存在cookie中
|
|
|
|
|
string cookie_path = cookie_path_in;
|
|
|
|
|
if (cookie_path.empty()) {
|
|
|
|
|
//如果未设置鉴权目录,那么我们采用当前目录
|
|
|
|
|
cookie_path = is_dir ? path : path.substr(0, path.rfind("/") + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 14:33:11 +08:00
|
|
|
|
auto attach = std::make_shared<HttpCookieAttachment>();
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//记录用户能访问的路径
|
2022-02-11 14:33:11 +08:00
|
|
|
|
attach->_path = cookie_path;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//记录能否访问
|
2022-02-11 14:33:11 +08:00
|
|
|
|
attach->_err_msg = err_msg;
|
2019-11-30 14:29:44 +08:00
|
|
|
|
//记录访问的是否为hls
|
2022-02-11 14:33:11 +08:00
|
|
|
|
attach->_is_hls = is_hls;
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (is_hls) {
|
2022-02-11 14:33:11 +08:00
|
|
|
|
// hls相关信息
|
|
|
|
|
attach->_hls_data = std::make_shared<HlsCookieData>(mediaInfo, info);
|
|
|
|
|
// hls未查找MediaSource
|
|
|
|
|
attach->_have_find_media_source = false;
|
2019-12-28 16:48:11 +08:00
|
|
|
|
}
|
2022-02-11 14:33:11 +08:00
|
|
|
|
callback(err_msg, HttpCookieManager::Instance().addCookie(kCookieName, uid, life_second, attach));
|
2020-09-20 10:13:15 +08:00
|
|
|
|
} else {
|
2022-02-11 14:33:11 +08:00
|
|
|
|
callback(err_msg, nullptr);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-12 19:14:35 +08:00
|
|
|
|
if (is_hls) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//是hls的播放鉴权,拦截之
|
2020-09-12 19:14:35 +08:00
|
|
|
|
emitHlsPlayed(parser, mediaInfo, accessPathInvoker, sender);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//事件未被拦截,则认为是http下载请求
|
2020-04-23 21:38:44 +08:00
|
|
|
|
bool flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpAccess, parser, path, is_dir, accessPathInvoker, static_cast<SockInfo &>(sender));
|
2019-11-30 11:38:00 +08:00
|
|
|
|
if (!flag) {
|
|
|
|
|
//此事件无人监听,我们默认都有权限访问
|
|
|
|
|
callback("", nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送404 Not Found
|
|
|
|
|
*/
|
|
|
|
|
static void sendNotFound(const HttpFileManager::invoker &cb) {
|
2020-09-20 10:13:15 +08:00
|
|
|
|
GET_CONFIG(string, notFound, Http::kNotFound);
|
2021-01-02 21:24:06 +08:00
|
|
|
|
cb(404, "text/html", StrCaseMap(), std::make_shared<HttpStringBody>(notFound));
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 拼接文件路径
|
|
|
|
|
*/
|
|
|
|
|
static string pathCat(const string &a, const string &b){
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (a.back() == '/') {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
return a + b;
|
|
|
|
|
}
|
|
|
|
|
return a + '/' + b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 访问文件
|
|
|
|
|
* @param sender 事件触发者
|
|
|
|
|
* @param parser http请求
|
|
|
|
|
* @param mediaInfo http url信息
|
|
|
|
|
* @param strFile 文件绝对路径
|
|
|
|
|
* @param cb 回调对象
|
|
|
|
|
*/
|
|
|
|
|
static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo &mediaInfo, const string &strFile, const HttpFileManager::invoker &cb) {
|
2020-09-26 09:39:38 +08:00
|
|
|
|
bool is_hls = end_with(strFile, kHlsSuffix);
|
2019-12-29 14:00:20 +08:00
|
|
|
|
bool file_exist = File::is_file(strFile.data());
|
|
|
|
|
if (!is_hls && !file_exist) {
|
2019-12-29 11:52:02 +08:00
|
|
|
|
//文件不存在且不是hls,那么直接返回404
|
2019-11-30 11:38:00 +08:00
|
|
|
|
sendNotFound(cb);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-29 11:52:02 +08:00
|
|
|
|
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (is_hls) {
|
2019-12-29 11:52:02 +08:00
|
|
|
|
//hls,那么移除掉后缀获取真实的stream_id并且修改协议为HLS
|
|
|
|
|
const_cast<string &>(mediaInfo._schema) = HLS_SCHEMA;
|
|
|
|
|
replace(const_cast<string &>(mediaInfo._streamid), kHlsSuffix, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
weak_ptr<TcpSession> weakSession = sender.shared_from_this();
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//判断是否有权限访问该文件
|
2019-12-29 14:00:20 +08:00
|
|
|
|
canAccessPath(sender, parser, mediaInfo, false, [cb, strFile, parser, is_hls, mediaInfo, weakSession , file_exist](const string &errMsg, const HttpServerCookie::Ptr &cookie) {
|
2019-12-29 15:38:29 +08:00
|
|
|
|
auto strongSession = weakSession.lock();
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (!strongSession) {
|
2019-12-29 15:38:29 +08:00
|
|
|
|
//http客户端已经断开,不需要回复
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!errMsg.empty()) {
|
|
|
|
|
//文件鉴权失败
|
|
|
|
|
StrCaseMap headerOut;
|
|
|
|
|
if (cookie) {
|
2022-02-11 15:14:34 +08:00
|
|
|
|
headerOut["Set-Cookie"] = cookie->getCookie(cookie->getAttach<HttpCookieAttachment>()._path);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
2021-01-02 21:24:06 +08:00
|
|
|
|
cb(401, "text/html", headerOut, std::make_shared<HttpStringBody>(errMsg));
|
2019-12-29 15:38:29 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-11-30 11:38:00 +08:00
|
|
|
|
|
2021-12-22 15:42:03 +08:00
|
|
|
|
auto response_file = [file_exist, is_hls](const HttpServerCookie::Ptr &cookie, const HttpFileManager::invoker &cb, const string &strFile, const Parser &parser) {
|
2019-12-29 15:38:29 +08:00
|
|
|
|
StrCaseMap httpHeader;
|
|
|
|
|
if (cookie) {
|
2022-02-11 15:14:34 +08:00
|
|
|
|
httpHeader["Set-Cookie"] = cookie->getCookie(cookie->getAttach<HttpCookieAttachment>()._path);
|
2019-12-29 15:38:29 +08:00
|
|
|
|
}
|
2021-01-02 21:24:06 +08:00
|
|
|
|
HttpSession::HttpResponseInvoker invoker = [&](int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body) {
|
2019-12-29 17:55:02 +08:00
|
|
|
|
if (cookie && file_exist) {
|
2022-02-11 14:33:11 +08:00
|
|
|
|
auto& attach = cookie->getAttach<HttpCookieAttachment>();
|
|
|
|
|
if (attach._is_hls) {
|
|
|
|
|
attach._hls_data->addByteUsage(body->remainSize());
|
2019-12-29 11:52:02 +08:00
|
|
|
|
}
|
2019-12-29 15:38:29 +08:00
|
|
|
|
}
|
2021-01-02 21:24:06 +08:00
|
|
|
|
cb(code, HttpFileManager::getContentType(strFile.data()), headerOut, body);
|
2019-12-29 11:52:02 +08:00
|
|
|
|
};
|
2021-12-22 15:42:03 +08:00
|
|
|
|
invoker.responseFile(parser.getHeader(), httpHeader, strFile, !is_hls);
|
2019-12-29 15:38:29 +08:00
|
|
|
|
};
|
2019-12-29 11:52:02 +08:00
|
|
|
|
|
2019-12-29 15:38:29 +08:00
|
|
|
|
if (!is_hls) {
|
|
|
|
|
//不是hls,直接回复文件或404
|
|
|
|
|
response_file(cookie, cb, strFile, parser);
|
2020-09-20 10:13:15 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//是hls直播,判断HLS直播流是否已经注册
|
|
|
|
|
bool have_find_media_src = false;
|
|
|
|
|
if (cookie) {
|
2022-02-11 14:33:11 +08:00
|
|
|
|
auto& attach = cookie->getAttach<HttpCookieAttachment>();
|
|
|
|
|
have_find_media_src = attach._have_find_media_source;
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (!have_find_media_src) {
|
2022-02-11 14:33:11 +08:00
|
|
|
|
const_cast<HttpCookieAttachment &>(attach)._have_find_media_source = true;
|
2019-12-28 16:48:11 +08:00
|
|
|
|
}
|
2020-09-20 10:13:15 +08:00
|
|
|
|
}
|
|
|
|
|
if (have_find_media_src) {
|
|
|
|
|
//之前该cookie已经通过MediaSource::findAsync查找过了,所以现在只以文件系统查找结果为准
|
|
|
|
|
response_file(cookie, cb, strFile, parser);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//hls文件不存在,我们等待其生成并延后回复
|
|
|
|
|
MediaSource::findAsync(mediaInfo, strongSession, [response_file, cookie, cb, strFile, parser](const MediaSource::Ptr &src) {
|
|
|
|
|
if (cookie) {
|
|
|
|
|
//尝试添加HlsMediaSource的观看人数(HLS是按需生成的,这样可以触发HLS文件的生成)
|
2022-02-11 14:33:11 +08:00
|
|
|
|
cookie->getAttach<HttpCookieAttachment>()._hls_data->addByteUsage(0);
|
2020-09-20 10:13:15 +08:00
|
|
|
|
}
|
|
|
|
|
if (src && File::is_file(strFile.data())) {
|
|
|
|
|
//流和m3u8文件都存在,那么直接返回文件
|
|
|
|
|
response_file(cookie, cb, strFile, parser);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto hls = dynamic_pointer_cast<HlsMediaSource>(src);
|
|
|
|
|
if (!hls) {
|
|
|
|
|
//流不存在,那么直接返回文件(相当于纯粹的HLS文件服务器,但是会挂起播放器15秒左右(用于等待HLS流的注册))
|
2019-12-29 15:38:29 +08:00
|
|
|
|
response_file(cookie, cb, strFile, parser);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
|
2020-09-20 10:13:15 +08:00
|
|
|
|
//流存在,但是m3u8文件不存在,那么等待生成m3u8文件(HLS源注册后,并不会立即生成HLS文件,有人观看才会按需生成HLS文件)
|
|
|
|
|
hls->waitForFile([response_file, cookie, cb, strFile, parser]() {
|
|
|
|
|
response_file(cookie, cb, strFile, parser);
|
2019-12-29 15:38:29 +08:00
|
|
|
|
});
|
2020-09-20 10:13:15 +08:00
|
|
|
|
});
|
2019-11-30 11:38:00 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-23 12:47:04 +08:00
|
|
|
|
static string getFilePath(const Parser &parser,const MediaInfo &mediaInfo, TcpSession &sender){
|
|
|
|
|
GET_CONFIG(bool, enableVhost, General::kEnableVhost);
|
2021-08-27 11:05:26 +08:00
|
|
|
|
GET_CONFIG(string, rootPath, Http::kRootPath);
|
|
|
|
|
GET_CONFIG_FUNC(StrCaseMap, virtualPathMap, Http::kVirtualPath, [](const string &str) {
|
|
|
|
|
return Parser::parseArgs(str, ";", ",");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
string url, path;
|
|
|
|
|
auto it = virtualPathMap.find(mediaInfo._app);
|
|
|
|
|
if (it != virtualPathMap.end()) {
|
|
|
|
|
//访问的是virtualPath
|
|
|
|
|
path = it->second;
|
|
|
|
|
url = parser.Url().substr(1 + mediaInfo._app.size());
|
|
|
|
|
} else {
|
|
|
|
|
//访问的是rootPath
|
2021-08-26 19:36:38 +08:00
|
|
|
|
path = rootPath;
|
|
|
|
|
url = parser.Url();
|
2021-08-25 14:30:31 +08:00
|
|
|
|
}
|
2021-08-26 19:36:38 +08:00
|
|
|
|
auto ret = File::absolutePath(enableVhost ? mediaInfo._vhost + url : url, path);
|
2020-04-23 21:38:44 +08:00
|
|
|
|
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpBeforeAccess, parser, ret, static_cast<SockInfo &>(sender));
|
2020-09-21 14:32:56 +08:00
|
|
|
|
return ret;
|
2019-12-23 12:47:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-30 11:38:00 +08:00
|
|
|
|
/**
|
|
|
|
|
* 访问文件或文件夹
|
|
|
|
|
* @param sender 事件触发者
|
|
|
|
|
* @param parser http请求
|
|
|
|
|
* @param cb 回调对象
|
|
|
|
|
*/
|
|
|
|
|
void HttpFileManager::onAccessPath(TcpSession &sender, Parser &parser, const HttpFileManager::invoker &cb) {
|
|
|
|
|
auto fullUrl = string(HTTP_SCHEMA) + "://" + parser["Host"] + parser.FullUrl();
|
|
|
|
|
MediaInfo mediaInfo(fullUrl);
|
2019-12-23 12:47:04 +08:00
|
|
|
|
auto strFile = getFilePath(parser, mediaInfo, sender);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//访问的是文件夹
|
2019-11-30 11:56:40 +08:00
|
|
|
|
if (File::is_dir(strFile.data())) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
auto indexFile = searchIndexFile(strFile);
|
|
|
|
|
if (!indexFile.empty()) {
|
|
|
|
|
//发现该文件夹下有index文件
|
|
|
|
|
strFile = pathCat(strFile, indexFile);
|
|
|
|
|
parser.setUrl(pathCat(parser.Url(), indexFile));
|
|
|
|
|
accessFile(sender, parser, mediaInfo, strFile, cb);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
string strMenu;
|
|
|
|
|
//生成文件夹菜单索引
|
|
|
|
|
if (!makeFolderMenu(parser.Url(), strFile, strMenu)) {
|
|
|
|
|
//文件夹不存在
|
|
|
|
|
sendNotFound(cb);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//判断是否有权限访问该目录
|
2021-03-14 10:29:17 +08:00
|
|
|
|
canAccessPath(sender, parser, mediaInfo, true, [strMenu, cb](const string &errMsg, const HttpServerCookie::Ptr &cookie) mutable{
|
2019-11-30 11:38:00 +08:00
|
|
|
|
if (!errMsg.empty()) {
|
2021-03-14 10:29:17 +08:00
|
|
|
|
strMenu = errMsg;
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
StrCaseMap headerOut;
|
|
|
|
|
if (cookie) {
|
2022-02-11 15:14:34 +08:00
|
|
|
|
headerOut["Set-Cookie"] = cookie->getCookie(cookie->getAttach<HttpCookieAttachment>()._path);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
2021-01-02 21:24:06 +08:00
|
|
|
|
cb(errMsg.empty() ? 200 : 401, "text/html", headerOut, std::make_shared<HttpStringBody>(strMenu));
|
2019-11-30 11:38:00 +08:00
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//访问的是文件
|
|
|
|
|
accessFile(sender, parser, mediaInfo, strFile, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////HttpResponseInvokerImp//////////////////////////////////////
|
|
|
|
|
|
2021-08-12 21:02:07 +08:00
|
|
|
|
void HttpResponseInvokerImp::operator()(int code, const StrCaseMap &headerOut, const Buffer::Ptr &body) const {
|
|
|
|
|
return operator()(code, headerOut, std::make_shared<HttpBufferBody>(body));
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 21:24:06 +08:00
|
|
|
|
void HttpResponseInvokerImp::operator()(int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body) const{
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (_lambad) {
|
2021-01-02 21:24:06 +08:00
|
|
|
|
_lambad(code, headerOut, body);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 21:24:06 +08:00
|
|
|
|
void HttpResponseInvokerImp::operator()(int code, const StrCaseMap &headerOut, const string &body) const{
|
|
|
|
|
this->operator()(code, headerOut, std::make_shared<HttpStringBody>(body));
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpResponseInvokerImp::HttpResponseInvokerImp(const HttpResponseInvokerImp::HttpResponseInvokerLambda0 &lambda){
|
|
|
|
|
_lambad = lambda;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpResponseInvokerImp::HttpResponseInvokerImp(const HttpResponseInvokerImp::HttpResponseInvokerLambda1 &lambda){
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (!lambda) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
_lambad = nullptr;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-02 21:24:06 +08:00
|
|
|
|
_lambad = [lambda](int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
string str;
|
2020-09-20 10:13:15 +08:00
|
|
|
|
if (body && body->remainSize()) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
str = body->readData(body->remainSize())->toString();
|
|
|
|
|
}
|
2021-01-02 21:24:06 +08:00
|
|
|
|
lambda(code, headerOut, str);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
|
|
|
|
|
const StrCaseMap &responseHeader,
|
2021-12-22 15:42:03 +08:00
|
|
|
|
const string &filePath,
|
|
|
|
|
bool use_mmap) const {
|
2020-09-20 10:13:15 +08:00
|
|
|
|
StrCaseMap &httpHeader = const_cast<StrCaseMap &>(responseHeader);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
auto fileBody = std::make_shared<HttpFileBody>(filePath, use_mmap);
|
|
|
|
|
if (fileBody->remainSize() < 0) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//打开文件失败
|
2020-09-20 10:13:15 +08:00
|
|
|
|
GET_CONFIG(string, notFound, Http::kNotFound);
|
|
|
|
|
GET_CONFIG(string, charSet, Http::kCharSet);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
|
|
|
|
|
auto strContentType = StrPrinter << "text/html; charset=" << charSet << endl;
|
|
|
|
|
httpHeader["Content-Type"] = strContentType;
|
2021-01-02 21:24:06 +08:00
|
|
|
|
(*this)(404, httpHeader, notFound);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto &strRange = const_cast<StrCaseMap &>(requestHeader)["Range"];
|
2022-02-10 20:23:37 +08:00
|
|
|
|
int code = 200;
|
|
|
|
|
if (!strRange.empty()) {
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//分节下载
|
2021-01-02 21:24:06 +08:00
|
|
|
|
code = 206;
|
2022-02-10 20:23:37 +08:00
|
|
|
|
auto iRangeStart = atoll(FindField(strRange.data(), "bytes=", "-").data());
|
|
|
|
|
auto iRangeEnd = atoll(FindField(strRange.data(), "-", nullptr).data());
|
|
|
|
|
auto fileSize = fileBody->remainSize();
|
2019-11-30 11:38:00 +08:00
|
|
|
|
if (iRangeEnd == 0) {
|
|
|
|
|
iRangeEnd = fileSize - 1;
|
|
|
|
|
}
|
2022-02-10 20:23:37 +08:00
|
|
|
|
//设置文件范围
|
|
|
|
|
fileBody->setRange(iRangeStart, iRangeEnd - iRangeStart + 1);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
//分节下载返回Content-Range头
|
|
|
|
|
httpHeader.emplace("Content-Range", StrPrinter << "bytes " << iRangeStart << "-" << iRangeEnd << "/" << fileSize << endl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//回复文件
|
2021-01-02 21:24:06 +08:00
|
|
|
|
(*this)(code, httpHeader, fileBody);
|
2019-11-30 11:38:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpResponseInvokerImp::operator bool(){
|
|
|
|
|
return _lambad.operator bool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|