From cadff93d4d9257d60a6f878e13a9cc7bad48f215 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 30 Nov 2019 14:29:44 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81hls=20cookie=E6=94=B9=E6=88=9060?= =?UTF-8?q?=E7=A7=92=E6=9C=89=E6=95=88=E6=9C=9F=E5=B9=B6=E4=B8=94=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E6=97=B6=E5=88=B7=E6=96=B0cookie=202=E3=80=81?= =?UTF-8?q?=E5=8E=BB=E9=99=A4keep-alive=E4=B8=8B=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=AC=A1=E6=95=B0=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/config.ini | 2 -- src/Common/config.cpp | 4 ---- src/Common/config.h | 2 -- src/Http/HttpFileManager.cpp | 36 +++++++++++++++++++++++------------- src/Http/HttpSession.cpp | 18 +++++------------- src/Http/HttpSession.h | 1 - 6 files changed, 28 insertions(+), 35 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index c6bebc7d..8df1dd3b 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -96,8 +96,6 @@ timeoutSec=10 charSet=utf-8 #http链接超时时间 keepAliveSecond=10 -#keep-alive类型的链接最多复用次数 -maxReqCount=100 #http请求体最大字节数,如果post的body太大,则不适合缓存body在内存 maxReqSize=4096 #404网页内容,用户可以自定义404网页 diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 39336e69..37597b96 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -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 diff --git a/src/Common/config.h b/src/Common/config.h index ba171c99..3f597c5a 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -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 服务器根目录 diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index e4aebcf6..21097021 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -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(); auto cookiePath = (*cookie)[kCookiePathKey].get(); + auto is_hls = (*cookie)[kAccessHls].get(); 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(cookie_path); //记录能否访问 (*cookie)[kAccessErrKey].set(errMsg); + //记录访问的是否为hls + (*cookie)[kAccessHls].set(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()); } 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); }); diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index bb2b3b5a..1c36e5cc 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -194,9 +194,7 @@ bool HttpSession::checkLiveFlvStream(const function &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 weakSelf = dynamic_pointer_cast(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 weakSelf = dynamic_pointer_cast(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 weakSelf = dynamic_pointer_cast(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 recvedContentLen = std::make_shared(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; diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 1d9ead78..207b061b 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -123,7 +123,6 @@ private: string _origin; Parser _parser; Ticker _ticker; - uint32_t _iReqCnt = 0; //消耗的总流量 uint64_t _ui64TotalBytes = 0; //flv over http