1、hls cookie改成60秒有效期并且访问时刷新cookie

2、去除keep-alive下最大请求次数限制
This commit is contained in:
xiongziliang 2019-11-30 14:29:44 +08:00
parent d46b67a5cf
commit cadff93d4d
6 changed files with 28 additions and 35 deletions

View File

@ -96,8 +96,6 @@ timeoutSec=10
charSet=utf-8
#http链接超时时间
keepAliveSecond=10
#keep-alive类型的链接最多复用次数
maxReqCount=100
#http请求体最大字节数如果post的body太大则不适合缓存body在内存
maxReqSize=4096
#404网页内容用户可以自定义404网页

View File

@ -107,8 +107,6 @@ const string kSendBufSize = HTTP_FIELD"sendBufSize";
const string kMaxReqSize = HTTP_FIELD"maxReqSize";
//http keep-alive秒数
const string kKeepAliveSecond = HTTP_FIELD"keepAliveSecond";
//http keep-alive最大请求数
const string kMaxReqCount = HTTP_FIELD"maxReqCount";
//http 字符编码
const string kCharSet = HTTP_FIELD"charSet";
//http 服务器根目录
@ -120,8 +118,6 @@ onceToken token([](){
mINI::Instance()[kSendBufSize] = 64 * 1024;
mINI::Instance()[kMaxReqSize] = 4*1024;
mINI::Instance()[kKeepAliveSecond] = 15;
mINI::Instance()[kMaxReqCount] = 100;
#if defined(_WIN32)
mINI::Instance()[kCharSet] = "gb2312";
#else

View File

@ -199,8 +199,6 @@ extern const string kSendBufSize;
extern const string kMaxReqSize;
//http keep-alive秒数
extern const string kKeepAliveSecond;
//http keep-alive最大请求数
extern const string kMaxReqCount;
//http 字符编码
extern const string kCharSet;
//http 服务器根目录

View File

@ -35,12 +35,17 @@
namespace mediakit {
static int kHlsCookieSecond = 10 * 60;
// hls的播放cookie缓存时间默认60秒
// 每次访问一次该cookie那么将重新刷新cookie有效期
// 假如播放器在60秒内都未访问该cookie那么将重新触发hls播放鉴权
static int kHlsCookieSecond = 60;
static const string kCookieName = "ZL_COOKIE";
static const string kCookiePathKey = "kCookiePathKey";
static const string kAccessErrKey = "kAccessErrKey";
static const string kAccessHls = "kAccessHls";
static const string kHlsSuffix = "/hls.m3u8";
static const string &getMimeType(const char *name) {
static const string &getContentType(const char *name) {
const char *dot;
dot = strrchr(name, '.');
static StrCaseMap mapType;
@ -211,11 +216,7 @@ static bool end_of(const string &str, const string &substr){
};
//拦截hls的播放请求
static bool checkHls(BroadcastHttpAccessArgs){
if(!end_of(args._streamid,("/hls.m3u8"))) {
//不是hls
return false;
}
static bool emitHlsPlayed(BroadcastHttpAccessArgs){
//访问的hls.m3u8结尾我们转换成kBroadcastMediaPlayed事件
Broadcast::AuthInvoker mediaAuthInvoker = [invoker,path](const string &err){
//cookie有效期为kHlsCookieSecond
@ -223,7 +224,7 @@ static bool checkHls(BroadcastHttpAccessArgs){
};
auto args_copy = args;
replace(args_copy._streamid,"/hls.m3u8","");
replace(args_copy._streamid,kHlsSuffix,"");
return NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaPlayed,args_copy,mediaAuthInvoker,sender);
}
@ -257,10 +258,16 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI
auto lck = cookie->getLock();
auto accessErr = (*cookie)[kAccessErrKey].get<string>();
auto cookiePath = (*cookie)[kCookiePathKey].get<string>();
auto is_hls = (*cookie)[kAccessHls].get<bool>();
if (path.find(cookiePath) == 0) {
//上次cookie是限定本目录
if (accessErr.empty()) {
//上次鉴权成功
if(is_hls){
//如果播放的是hls那么刷新hls的cookie
cookie->updateTime();
cookie_from_header = false;
}
callback("", cookie_from_header ? nullptr : cookie);
return;
}
@ -275,10 +282,11 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI
HttpCookieManager::Instance().delCookie(cookie);
}
bool is_hls = end_of(path,kHlsSuffix);
//该用户从来未获取过cookie这个时候我们广播是否允许该用户访问该http目录
HttpSession::HttpAccessPathInvoker accessPathInvoker = [callback, uid, path, is_dir](const string &errMsg,
const string &cookie_path_in,
int cookieLifeSecond) {
auto accessPathInvoker = [callback, uid, path, is_dir, is_hls](const string &errMsg,
const string &cookie_path_in,
int cookieLifeSecond) {
HttpServerCookie::Ptr cookie;
if (cookieLifeSecond) {
//本次鉴权设置了有效期我们把鉴权结果缓存在cookie中
@ -295,11 +303,13 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI
(*cookie)[kCookiePathKey].set<string>(cookie_path);
//记录能否访问
(*cookie)[kAccessErrKey].set<string>(errMsg);
//记录访问的是否为hls
(*cookie)[kAccessHls].set<bool>(is_hls);
}
callback(errMsg, cookie);
};
if (checkHls(parser, mediaInfo, path, is_dir, accessPathInvoker, sender)) {
if (is_hls && emitHlsPlayed(parser, mediaInfo, path, is_dir, accessPathInvoker, sender)) {
//是hls的播放鉴权,拦截之
return;
}
@ -359,7 +369,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
httpHeader["Set-Cookie"] = cookie->getCookie((*cookie)[kCookiePathKey].get<string>());
}
HttpSession::HttpResponseInvoker invoker = [&](const string &codeOut, const StrCaseMap &headerOut, const HttpBody::Ptr &body) {
cb(codeOut.data(), getMimeType(strFile.data()), headerOut, body);
cb(codeOut.data(), getContentType(strFile.data()), headerOut, body);
};
invoker.responseFile(parser.getValues(), httpHeader, strFile);
});

View File

@ -194,9 +194,7 @@ bool HttpSession::checkLiveFlvStream(const function<void()> &cb){
return false;
}
_mediaInfo._streamid.erase(_mediaInfo._streamid.size() - 4);//去除.flv后缀
GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount);
bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt);
bool bClose = !strcasecmp(_parser["Connection"].data(),"close");
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this());
MediaSource::findAsync(_mediaInfo,weakSelf.lock(), true,[weakSelf,bClose,this,cb](const MediaSource::Ptr &src){
@ -284,8 +282,7 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) {
return;
}
GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount);
bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt);
bool bClose = !strcasecmp(_parser["Connection"].data(),"close");
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this());
HttpFileManager::onAccessPath(*this, _parser, [weakSelf, bClose](const string &status_code, const string &content_type,
@ -319,7 +316,6 @@ void HttpSession::sendResponse(const char *pcStatus,
bool set_content_len ){
GET_CONFIG(string,charSet,Http::kCharSet);
GET_CONFIG(uint32_t,keepAliveSec,Http::kKeepAliveSecond);
GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount);
//body默认为空
int64_t size = 0;
@ -343,7 +339,7 @@ void HttpSession::sendResponse(const char *pcStatus,
headerOut.emplace("Server", SERVER_NAME);
headerOut.emplace("Connection", bClose ? "close" : "keep-alive");
if(!bClose){
headerOut.emplace("Keep-Alive",StrPrinter << "timeout=" << keepAliveSec << ", max=" << reqCnt << endl);
headerOut.emplace("Keep-Alive",StrPrinter << "timeout=" << keepAliveSec << ", max=100" << endl);
}
if(!_origin.empty()){
@ -463,10 +459,7 @@ void HttpSession::urlDecode(Parser &parser){
}
bool HttpSession::emitHttpEvent(bool doInvoke){
///////////////////是否断开本链接///////////////////////
GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount);
bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt);
bool bClose = !strcasecmp(_parser["Connection"].data(),"close");
/////////////////////异步回复Invoker///////////////////////////////
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this());
HttpResponseInvoker invoker = [weakSelf,bClose](const string &codeOut, const KeyValue &headerOut, const HttpBody::Ptr &body){
@ -495,7 +488,6 @@ bool HttpSession::emitHttpEvent(bool doInvoke){
void HttpSession::Handle_Req_POST(int64_t &content_len) {
GET_CONFIG(uint64_t,maxReqSize,Http::kMaxReqSize);
GET_CONFIG(int,maxReqCnt,Http::kMaxReqCount);
int64_t totalContentLen = _parser["Content-Length"].empty() ? -1 : atoll(_parser["Content-Length"].data());
@ -535,7 +527,7 @@ void HttpSession::Handle_Req_POST(int64_t &content_len) {
content_len = -1;
auto parserCopy = _parser;
std::shared_ptr<uint64_t> recvedContentLen = std::make_shared<uint64_t>(0);
bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > maxReqCnt);
bool bClose = !strcasecmp(_parser["Connection"].data(),"close");
_contentCallBack = [this,parserCopy,totalContentLen,recvedContentLen,bClose](const char *data,uint64_t len){
*(recvedContentLen) += len;

View File

@ -123,7 +123,6 @@ private:
string _origin;
Parser _parser;
Ticker _ticker;
uint32_t _iReqCnt = 0;
//消耗的总流量
uint64_t _ui64TotalBytes = 0;
//flv over http