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
0bff2ad1
Commit
0bff2ad1
authored
Oct 29, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化精简Http服务器代码
parent
43e1cc69
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
39 行增加
和
44 行删除
+39
-44
src/Http/HttpSession.cpp
+39
-44
没有找到文件。
src/Http/HttpSession.cpp
查看文件 @
0bff2ad1
...
...
@@ -138,49 +138,52 @@ HttpResponseInvokerImp::HttpResponseInvokerImp(const HttpResponseInvokerImp::Htt
void
HttpResponseInvokerImp
::
responseFile
(
const
StrCaseMap
&
requestHeader
,
const
StrCaseMap
&
responseHeader
,
const
string
&
filePath
)
const
{
try
{
struct
stat
tFileStat
;
if
(
filePath
.
empty
()
||
0
!=
stat
(
filePath
.
data
(),
&
tFileStat
))
{
//文件不存在
throw
std
::
runtime_error
(
"file not exited"
);
StrCaseMap
&
httpHeader
=
const_cast
<
StrCaseMap
&>
(
responseHeader
);
do
{
std
::
shared_ptr
<
FILE
>
fp
(
fopen
(
filePath
.
data
(),
"rb"
),
[](
FILE
*
fp
)
{
if
(
fp
)
{
fclose
(
fp
);
}
});
if
(
!
fp
)
{
//打开文件失败
break
;
}
auto
&
strRange
=
const_cast
<
StrCaseMap
&>
(
requestHeader
)[
"Range"
];
int64_t
iRangeStart
=
0
,
iRangeEnd
=
0
;
iRangeStart
=
atoll
(
FindField
(
strRange
.
data
(),
"bytes="
,
"-"
).
data
());
iRangeEnd
=
atoll
(
FindField
(
strRange
.
data
(),
"-"
,
"
\r\n
"
).
data
());
if
(
iRangeEnd
==
0
)
{
iRangeEnd
=
tFileStat
.
st_size
-
1
;
}
int64_t
iRangeStart
=
0
;
int64_t
iRangeEnd
=
0
;
int64_t
fileSize
=
HttpMultiFormBody
::
fileSize
(
fp
.
get
());
const
char
*
pcHttpResult
=
NULL
;
StrCaseMap
&
httpHeader
=
const_cast
<
StrCaseMap
&>
(
responseHeader
);
if
(
strRange
.
size
()
==
0
)
{
//全部下载
pcHttpResult
=
"200 OK"
;
iRangeEnd
=
fileSize
-
1
;
}
else
{
//分节下载
pcHttpResult
=
"206 Partial Content"
;
//分节下载返回Content-Range头
httpHeader
.
emplace
(
"Content-Range"
,
StrPrinter
<<
"bytes "
<<
iRangeStart
<<
"-"
<<
iRangeEnd
<<
"/"
<<
tFileStat
.
st_size
<<
endl
);
}
std
::
shared_ptr
<
FILE
>
fp
(
fopen
(
filePath
.
data
(),
"rb"
),
[](
FILE
*
fp
)
{
if
(
fp
)
{
fclose
(
fp
);
iRangeStart
=
atoll
(
FindField
(
strRange
.
data
(),
"bytes="
,
"-"
).
data
());
iRangeEnd
=
atoll
(
FindField
(
strRange
.
data
(),
"-"
,
"
\r\n
"
).
data
());
if
(
iRangeEnd
==
0
)
{
iRangeEnd
=
fileSize
-
1
;
}
});
if
(
!
fp
)
{
//打开文件失败
throw
std
::
runtime_error
(
StrPrinter
<<
"open file not failed:"
<<
get_uv_errmsg
(
false
));
//分节下载返回Content-Range头
httpHeader
.
emplace
(
"Content-Range"
,
StrPrinter
<<
"bytes "
<<
iRangeStart
<<
"-"
<<
iRangeEnd
<<
"/"
<<
fileSize
<<
endl
);
}
//回复文件
HttpBody
::
Ptr
fileBody
=
std
::
make_shared
<
HttpFileBody
>
(
fp
,
iRangeStart
,
iRangeEnd
-
iRangeStart
+
1
);
(
*
this
)(
pcHttpResult
,
httpHeader
,
fileBody
);
return
;
}
while
(
false
);
}
catch
(
std
::
exception
&
ex
){
(
*
this
)(
"404 Not Found"
,
responseHeader
,
ex
.
what
());
}
GET_CONFIG
(
string
,
notFound
,
Http
::
kNotFound
);
GET_CONFIG
(
string
,
charSet
,
Http
::
kCharSet
);
auto
strContentType
=
StrPrinter
<<
"text/html; charset="
<<
charSet
<<
endl
;
httpHeader
[
"Content-Type"
]
=
strContentType
;
(
*
this
)(
"404 Not Found"
,
httpHeader
,
notFound
);
}
HttpResponseInvokerImp
::
operator
bool
(){
...
...
@@ -378,7 +381,7 @@ bool HttpSession::checkLiveFlvStream(const function<void()> &cb){
if
(
!
cb
)
{
//找到rtmp源,发送http头,负载后续发送
sendResponse
(
"200 OK"
,
false
,
get_mime_type
(
".flv"
)
,
KeyValue
(),
nullptr
,
false
);
sendResponse
(
"200 OK"
,
false
,
"video/x-flv"
,
KeyValue
(),
nullptr
,
false
);
}
else
{
cb
();
}
...
...
@@ -583,11 +586,16 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) {
return
;
}
//先看看该http事件是否被拦截
if
(
emitHttpEvent
(
false
)){
//拦截http api事件
return
;
}
if
(
checkLiveFlvStream
()){
//拦截http-flv播放器
return
;
}
//事件未被拦截,则认为是http下载请求
auto
fullUrl
=
string
(
HTTP_SCHEMA
)
+
"://"
+
_parser
[
"Host"
]
+
_parser
.
FullUrl
();
_mediaInfo
.
parse
(
fullUrl
);
...
...
@@ -633,22 +641,9 @@ void HttpSession::Handle_Req_GET(int64_t &content_len) {
}
}
while
(
0
);
//访问的是文件
struct
stat
tFileStat
;
if
(
0
!=
stat
(
strFile
.
data
(),
&
tFileStat
))
{
//再看看是否为http-flv直播请求
if
(
checkLiveFlvStream
()){
//若是,return!
return
;
}
//文件不存在
sendNotFound
(
bClose
);
throw
SockException
(
bClose
?
Err_shutdown
:
Err_success
,
"close connection after send 404 not found on file"
);
}
auto
parser
=
_parser
;
//判断是否有权限访问该文件
canAccessPath
(
_parser
.
Url
(),
false
,[
this
,
parser
,
tFileStat
,
bClose
,
strFile
](
const
string
&
errMsg
,
const
HttpServerCookie
::
Ptr
&
cookie
){
canAccessPath
(
_parser
.
Url
(),
false
,[
this
,
parser
,
bClose
,
strFile
](
const
string
&
errMsg
,
const
HttpServerCookie
::
Ptr
&
cookie
){
if
(
!
errMsg
.
empty
()){
KeyValue
headerOut
;
if
(
cookie
){
...
...
@@ -813,8 +808,8 @@ void HttpSession::sendResponse(const char *pcStatus,
}
if
(
set_content_len
&&
size
>=
0
){
//文件长度为定值或者,且不是http-flv设置Content-Length
headerOut
.
emplace
(
"Content-Length"
,
StrPrinter
<<
size
<<
endl
)
;
//文件长度为定值或者,且不是http-flv
强制
设置Content-Length
headerOut
[
"Content-Length"
]
=
StrPrinter
<<
size
<<
endl
;
}
if
(
size
&&
!
pcContentType
){
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论