Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
cadff93d
Commit
cadff93d
authored
Nov 30, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1、hls cookie改成60秒有效期并且访问时刷新cookie
2、去除keep-alive下最大请求次数限制
parent
d46b67a5
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
26 行增加
和
33 行删除
+26
-33
conf/config.ini
+0
-2
src/Common/config.cpp
+0
-4
src/Common/config.h
+0
-2
src/Http/HttpFileManager.cpp
+21
-11
src/Http/HttpSession.cpp
+5
-13
src/Http/HttpSession.h
+0
-1
没有找到文件。
conf/config.ini
查看文件 @
cadff93d
...
@@ -96,8 +96,6 @@ timeoutSec=10
...
@@ -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网页
...
...
src/Common/config.cpp
查看文件 @
cadff93d
...
@@ -107,8 +107,6 @@ const string kSendBufSize = HTTP_FIELD"sendBufSize";
...
@@ -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([](){
...
@@ -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
...
...
src/Common/config.h
查看文件 @
cadff93d
...
@@ -199,8 +199,6 @@ extern const string kSendBufSize;
...
@@ -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 服务器根目录
...
...
src/Http/HttpFileManager.cpp
查看文件 @
cadff93d
...
@@ -35,12 +35,17 @@
...
@@ -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
&
get
Mime
Type
(
const
char
*
name
)
{
static
const
string
&
get
Content
Type
(
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){
...
@@ -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){
...
@@ -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
...
@@ -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,8 +282,9 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI
...
@@ -275,8 +282,9 @@ 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
;
...
@@ -295,11 +303,13 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI
...
@@ -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
...
@@ -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
(),
get
Mime
Type
(
strFile
.
data
()),
headerOut
,
body
);
cb
(
codeOut
.
data
(),
get
Content
Type
(
strFile
.
data
()),
headerOut
,
body
);
};
};
invoker
.
responseFile
(
parser
.
getValues
(),
httpHeader
,
strFile
);
invoker
.
responseFile
(
parser
.
getValues
(),
httpHeader
,
strFile
);
});
});
...
...
src/Http/HttpSession.cpp
查看文件 @
cadff93d
...
@@ -194,9 +194,7 @@ bool HttpSession::checkLiveFlvStream(const function<void()> &cb){
...
@@ -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) {
...
@@ -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,
...
@@ -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,
...
@@ -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){
...
@@ -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){
...
@@ -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) {
...
@@ -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
;
...
...
src/Http/HttpSession.h
查看文件 @
cadff93d
...
@@ -123,7 +123,6 @@ private:
...
@@ -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
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论