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

View File

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

View File

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

View File

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

View File

@ -194,9 +194,7 @@ bool HttpSession::checkLiveFlvStream(const function<void()> &cb){
return false; return false;
} }
_mediaInfo._streamid.erase(_mediaInfo._streamid.size() - 4);//去除.flv后缀 _mediaInfo._streamid.erase(_mediaInfo._streamid.size() - 4);//去除.flv后缀
bool bClose = !strcasecmp(_parser["Connection"].data(),"close");
GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount);
bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt);
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this()); 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){ 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; return;
} }
GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount); bool bClose = !strcasecmp(_parser["Connection"].data(),"close");
bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt);
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this()); 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, 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 ){ bool set_content_len ){
GET_CONFIG(string,charSet,Http::kCharSet); GET_CONFIG(string,charSet,Http::kCharSet);
GET_CONFIG(uint32_t,keepAliveSec,Http::kKeepAliveSecond); GET_CONFIG(uint32_t,keepAliveSec,Http::kKeepAliveSecond);
GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount);
//body默认为空 //body默认为空
int64_t size = 0; int64_t size = 0;
@ -343,7 +339,7 @@ void HttpSession::sendResponse(const char *pcStatus,
headerOut.emplace("Server", SERVER_NAME); headerOut.emplace("Server", SERVER_NAME);
headerOut.emplace("Connection", bClose ? "close" : "keep-alive"); headerOut.emplace("Connection", bClose ? "close" : "keep-alive");
if(!bClose){ 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()){ if(!_origin.empty()){
@ -463,10 +459,7 @@ void HttpSession::urlDecode(Parser &parser){
} }
bool HttpSession::emitHttpEvent(bool doInvoke){ bool HttpSession::emitHttpEvent(bool doInvoke){
///////////////////是否断开本链接/////////////////////// bool bClose = !strcasecmp(_parser["Connection"].data(),"close");
GET_CONFIG(uint32_t,reqCnt,Http::kMaxReqCount);
bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt);
/////////////////////异步回复Invoker/////////////////////////////// /////////////////////异步回复Invoker///////////////////////////////
weak_ptr<HttpSession> weakSelf = dynamic_pointer_cast<HttpSession>(shared_from_this()); 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){ 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) { void HttpSession::Handle_Req_POST(int64_t &content_len) {
GET_CONFIG(uint64_t,maxReqSize,Http::kMaxReqSize); 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()); 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; content_len = -1;
auto parserCopy = _parser; auto parserCopy = _parser;
std::shared_ptr<uint64_t> recvedContentLen = std::make_shared<uint64_t>(0); 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){ _contentCallBack = [this,parserCopy,totalContentLen,recvedContentLen,bClose](const char *data,uint64_t len){
*(recvedContentLen) += len; *(recvedContentLen) += len;

View File

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