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
ba213346
Commit
ba213346
authored
Feb 10, 2022
by
ziyue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http文件服务器mmap方案采用共享方式
parent
72caa43c
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
135 行增加
和
136 行删除
+135
-136
src/Http/HttpBody.cpp
+114
-104
src/Http/HttpBody.h
+12
-12
src/Http/HttpFileManager.cpp
+9
-20
没有找到文件。
src/Http/HttpBody.cpp
查看文件 @
ba213346
...
@@ -9,16 +9,23 @@
...
@@ -9,16 +9,23 @@
*/
*/
#include <csignal>
#include <csignal>
#include "HttpBody.h"
#include "Util/util.h"
#ifndef _WIN32
#include <sys/mman.h>
#endif
#if defined(__linux__) || defined(__linux)
#include <sys/sendfile.h>
#endif
#include "Util/File.h"
#include "Util/File.h"
#include "Util/uv_errno.h"
#include "Util/logger.h"
#include "Util/logger.h"
#include "Util/onceToken.h"
#include "Util/onceToken.h"
#include "Util/util.h"
#include "Util/uv_errno.h"
#include "HttpBody.h"
#include "HttpClient.h"
#include "HttpClient.h"
#ifndef _WIN32
#include "Common/macros.h"
#include <sys/mman.h>
#endif
#ifndef _WIN32
#ifndef _WIN32
#define ENABLE_MMAP
#define ENABLE_MMAP
...
@@ -29,7 +36,7 @@ using namespace toolkit;
...
@@ -29,7 +36,7 @@ using namespace toolkit;
namespace
mediakit
{
namespace
mediakit
{
HttpStringBody
::
HttpStringBody
(
string
str
){
HttpStringBody
::
HttpStringBody
(
string
str
)
{
_str
=
std
::
move
(
str
);
_str
=
std
::
move
(
str
);
}
}
...
@@ -39,84 +46,92 @@ ssize_t HttpStringBody::remainSize() {
...
@@ -39,84 +46,92 @@ ssize_t HttpStringBody::remainSize() {
Buffer
::
Ptr
HttpStringBody
::
readData
(
size_t
size
)
{
Buffer
::
Ptr
HttpStringBody
::
readData
(
size_t
size
)
{
size
=
MIN
((
size_t
)
remainSize
(),
size
);
size
=
MIN
((
size_t
)
remainSize
(),
size
);
if
(
!
size
)
{
if
(
!
size
)
{
//没有剩余字节了
//没有剩余字节了
return
nullptr
;
return
nullptr
;
}
}
auto
ret
=
std
::
make_shared
<
BufferString
>
(
_str
,
_offset
,
size
);
auto
ret
=
std
::
make_shared
<
BufferString
>
(
_str
,
_offset
,
size
);
_offset
+=
size
;
_offset
+=
size
;
return
ret
;
return
ret
;
}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
HttpFileBody
::
HttpFileBody
(
const
string
&
filePath
,
bool
use_mmap
)
{
#ifdef ENABLE_MMAP
std
::
shared_ptr
<
FILE
>
fp
(
fopen
(
filePath
.
data
(),
"rb"
),
[](
FILE
*
fp
)
{
static
std
::
shared_ptr
<
char
>
getSharedMmap
(
const
string
&
file_path
,
const
std
::
shared_ptr
<
FILE
>
&
fp
,
uint64_t
max_size
)
{
static
mutex
s_mtx
;
static
unordered_map
<
string
/*file_path*/
,
weak_ptr
<
char
>
/*mmap*/
>
s_shared_mmap
;
{
lock_guard
<
mutex
>
lck
(
s_mtx
);
auto
it
=
s_shared_mmap
.
find
(
file_path
);
if
(
it
!=
s_shared_mmap
.
end
())
{
auto
ret
=
it
->
second
.
lock
();
if
(
ret
)
{
//命中mmap缓存
return
ret
;
}
}
}
int
fd
=
fileno
(
fp
.
get
());
if
(
fd
<
0
)
{
WarnL
<<
"fileno failed:"
<<
get_uv_errmsg
(
false
);
return
nullptr
;
}
auto
ptr
=
(
char
*
)
mmap
(
NULL
,
max_size
,
PROT_READ
,
MAP_SHARED
,
fd
,
0
);
if
(
ptr
==
MAP_FAILED
)
{
WarnL
<<
"mmap "
<<
file_path
<<
" failed:"
<<
get_uv_errmsg
(
false
);
return
nullptr
;
}
std
::
shared_ptr
<
char
>
ret
(
ptr
,
[
max_size
,
fp
](
char
*
ptr
)
{
munmap
(
ptr
,
max_size
);
});
{
lock_guard
<
mutex
>
lck
(
s_mtx
);
s_shared_mmap
[
file_path
]
=
ret
;
}
return
ret
;
}
#endif
HttpFileBody
::
HttpFileBody
(
const
string
&
file_path
,
bool
use_mmap
)
{
_fp
.
reset
(
fopen
(
file_path
.
data
(),
"rb"
),
[](
FILE
*
fp
)
{
if
(
fp
)
{
if
(
fp
)
{
fclose
(
fp
);
fclose
(
fp
);
}
}
});
});
if
(
!
fp
)
{
if
(
!
_
fp
)
{
init
(
fp
,
0
,
0
,
use_mmap
);
//文件不存在
}
else
{
_read_to
=
-
1
;
init
(
fp
,
0
,
File
::
fileSize
(
fp
.
get
()),
use_mmap
)
;
return
;
}
}
_read_to
=
File
::
fileSize
(
_fp
.
get
());
#ifdef ENABLE_MMAP
if
(
use_mmap
&&
_read_to
)
{
_map_addr
=
getSharedMmap
(
file_path
,
_fp
,
_read_to
);
}
#endif
}
}
HttpFileBody
::
HttpFileBody
(
const
std
::
shared_ptr
<
FILE
>
&
fp
,
size_t
offset
,
size_t
max_size
,
bool
use_mmap
)
{
void
HttpFileBody
::
setRange
(
uint64_t
offset
,
uint64_t
max_size
)
{
init
(
fp
,
offset
,
max_size
,
use_mmap
);
CHECK
(
offset
<=
_read_to
&&
max_size
+
offset
<=
_read_to
);
_read_to
=
max_size
+
offset
;
_file_offset
=
offset
;
if
(
_fp
&&
!
_map_addr
)
{
fseek64
(
_fp
.
get
(),
_file_offset
,
SEEK_SET
);
}
}
}
#if defined(__linux__) || defined(__linux)
#include <sys/sendfile.h>
#endif
int
HttpFileBody
::
sendFile
(
int
fd
)
{
int
HttpFileBody
::
sendFile
(
int
fd
)
{
#if defined(__linux__) || defined(__linux)
#if defined(__linux__) || defined(__linux)
static
onceToken
s_token
([]()
{
static
onceToken
s_token
([]()
{
signal
(
SIGPIPE
,
SIG_IGN
);
});
signal
(
SIGPIPE
,
SIG_IGN
);
});
off_t
off
=
_file_offset
;
off_t
off
=
_file_offset
;
return
sendfile
(
fd
,
fileno
(
_fp
.
get
()),
&
off
,
_
max_size
);
return
sendfile
(
fd
,
fileno
(
_fp
.
get
()),
&
off
,
_
read_to
-
_file_offset
);
#else
#else
return
-
1
;
return
-
1
;
#endif
#endif
}
}
void
HttpFileBody
::
init
(
const
std
::
shared_ptr
<
FILE
>
&
fp
,
size_t
offset
,
size_t
max_size
,
bool
use_mmap
)
{
class
BufferMmap
:
public
Buffer
{
_fp
=
fp
;
_max_size
=
max_size
;
_file_offset
=
offset
;
#ifdef ENABLE_MMAP
if
(
use_mmap
)
{
do
{
if
(
!
_fp
)
{
//文件不存在
break
;
}
int
fd
=
fileno
(
fp
.
get
());
if
(
fd
<
0
)
{
WarnL
<<
"fileno failed:"
<<
get_uv_errmsg
(
false
);
break
;
}
auto
ptr
=
(
char
*
)
mmap
(
NULL
,
max_size
,
PROT_READ
,
MAP_SHARED
,
fd
,
offset
);
if
(
ptr
==
MAP_FAILED
)
{
WarnL
<<
"mmap failed:"
<<
get_uv_errmsg
(
false
);
break
;
}
_map_addr
.
reset
(
ptr
,
[
max_size
,
fp
](
char
*
ptr
)
{
munmap
(
ptr
,
max_size
);
});
}
while
(
false
);
}
#endif
if
(
!
_map_addr
&&
offset
&&
fp
.
get
())
{
//未映射,那么fseek设置偏移量
fseek64
(
fp
.
get
(),
offset
,
SEEK_SET
);
}
}
class
BufferMmap
:
public
Buffer
{
public
:
public
:
typedef
std
::
shared_ptr
<
BufferMmap
>
Ptr
;
typedef
std
::
shared_ptr
<
BufferMmap
>
Ptr
;
BufferMmap
(
const
std
::
shared_ptr
<
char
>
&
map_addr
,
size_t
offset
,
size_t
size
)
{
BufferMmap
(
const
std
::
shared_ptr
<
char
>
&
map_addr
,
size_t
offset
,
size_t
size
)
{
...
@@ -124,103 +139,96 @@ public:
...
@@ -124,103 +139,96 @@ public:
_data
=
map_addr
.
get
()
+
offset
;
_data
=
map_addr
.
get
()
+
offset
;
_size
=
size
;
_size
=
size
;
}
}
~
BufferMmap
()
override
{};
~
BufferMmap
()
override
{};
//返回数据长度
//返回数据长度
char
*
data
()
const
override
{
char
*
data
()
const
override
{
return
_data
;
}
return
_data
;
size_t
size
()
const
override
{
return
_size
;
}
}
size_t
size
()
const
override
{
return
_size
;
}
private
:
private
:
std
::
shared_ptr
<
char
>
_map_addr
;
char
*
_data
;
char
*
_data
;
size_t
_size
;
size_t
_size
;
std
::
shared_ptr
<
char
>
_map_addr
;
};
};
ssize_t
HttpFileBody
::
remainSize
()
{
ssize_t
HttpFileBody
::
remainSize
()
{
return
_
max_size
-
_offset
;
return
_
read_to
-
_file
_offset
;
}
}
Buffer
::
Ptr
HttpFileBody
::
readData
(
size_t
size
)
{
Buffer
::
Ptr
HttpFileBody
::
readData
(
size_t
size
)
{
size
=
MIN
((
size_t
)
remainSize
(),
size
);
size
=
MIN
((
size_t
)
remainSize
(),
size
);
if
(
!
size
)
{
if
(
!
size
)
{
//没有剩余字节了
//没有剩余字节了
return
nullptr
;
return
nullptr
;
}
}
if
(
!
_map_addr
)
{
if
(
!
_map_addr
)
{
//fread模式
//
fread模式
ssize_t
iRead
;
ssize_t
iRead
;
auto
ret
=
_pool
.
obtain2
();
auto
ret
=
_pool
.
obtain2
();
ret
->
setCapacity
(
size
+
1
);
ret
->
setCapacity
(
size
+
1
);
do
{
do
{
iRead
=
fread
(
ret
->
data
(),
1
,
size
,
_fp
.
get
());
iRead
=
fread
(
ret
->
data
(),
1
,
size
,
_fp
.
get
());
}
while
(
-
1
==
iRead
&&
UV_EINTR
==
get_uv_error
(
false
));
}
while
(
-
1
==
iRead
&&
UV_EINTR
==
get_uv_error
(
false
));
if
(
iRead
>
0
)
{
if
(
iRead
>
0
)
{
//读到数据了
//读到数据了
ret
->
setSize
(
iRead
);
ret
->
setSize
(
iRead
);
_offset
+=
iRead
;
_
file_
offset
+=
iRead
;
return
std
::
move
(
ret
);
return
std
::
move
(
ret
);
}
}
//读取文件异常,文件真实长度小于声明长度
//读取文件异常,文件真实长度小于声明长度
_
offset
=
_max_size
;
_
file_offset
=
_read_to
;
WarnL
<<
"read file err:"
<<
get_uv_errmsg
();
WarnL
<<
"read file err:"
<<
get_uv_errmsg
();
return
nullptr
;
return
nullptr
;
}
}
//mmap模式
//
mmap模式
auto
ret
=
std
::
make_shared
<
BufferMmap
>
(
_map_addr
,
_offset
,
size
);
auto
ret
=
std
::
make_shared
<
BufferMmap
>
(
_map_addr
,
_file_offset
,
size
);
_offset
+=
size
;
_
file_
offset
+=
size
;
return
ret
;
return
ret
;
}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
HttpMultiFormBody
::
HttpMultiFormBody
(
const
HttpArgs
&
args
,
const
string
&
filePath
,
const
string
&
boundary
){
std
::
shared_ptr
<
FILE
>
fp
(
fopen
(
filePath
.
data
(),
"rb"
),
[](
FILE
*
fp
)
{
HttpMultiFormBody
::
HttpMultiFormBody
(
const
HttpArgs
&
args
,
const
string
&
filePath
,
const
string
&
boundary
)
{
if
(
fp
){
_fileBody
=
std
::
make_shared
<
HttpFileBody
>
(
filePath
);
fclose
(
fp
);
if
(
_fileBody
->
remainSize
()
<
0
)
{
}
});
if
(
!
fp
){
throw
std
::
invalid_argument
(
StrPrinter
<<
"open file failed:"
<<
filePath
<<
" "
<<
get_uv_errmsg
());
throw
std
::
invalid_argument
(
StrPrinter
<<
"open file failed:"
<<
filePath
<<
" "
<<
get_uv_errmsg
());
}
}
_fileBody
=
std
::
make_shared
<
HttpFileBody
>
(
fp
,
0
,
File
::
fileSize
(
fp
.
get
()));
auto
fileName
=
filePath
;
auto
fileName
=
filePath
;
auto
pos
=
filePath
.
rfind
(
'/'
);
auto
pos
=
filePath
.
rfind
(
'/'
);
if
(
pos
!=
string
::
npos
)
{
if
(
pos
!=
string
::
npos
)
{
fileName
=
filePath
.
substr
(
pos
+
1
);
fileName
=
filePath
.
substr
(
pos
+
1
);
}
}
_bodyPrefix
=
multiFormBodyPrefix
(
args
,
boundary
,
fileName
);
_bodyPrefix
=
multiFormBodyPrefix
(
args
,
boundary
,
fileName
);
_bodySuffix
=
multiFormBodySuffix
(
boundary
);
_bodySuffix
=
multiFormBodySuffix
(
boundary
);
_totalSize
=
_bodyPrefix
.
size
()
+
_bodySuffix
.
size
()
+
_fileBody
->
remainSize
();
_totalSize
=
_bodyPrefix
.
size
()
+
_bodySuffix
.
size
()
+
_fileBody
->
remainSize
();
}
}
ssize_t
HttpMultiFormBody
::
remainSize
()
{
ssize_t
HttpMultiFormBody
::
remainSize
()
{
return
_totalSize
-
_offset
;
return
_totalSize
-
_offset
;
}
}
Buffer
::
Ptr
HttpMultiFormBody
::
readData
(
size_t
size
){
Buffer
::
Ptr
HttpMultiFormBody
::
readData
(
size_t
size
)
{
if
(
_bodyPrefix
.
size
())
{
if
(
_bodyPrefix
.
size
())
{
auto
ret
=
std
::
make_shared
<
BufferString
>
(
_bodyPrefix
);
auto
ret
=
std
::
make_shared
<
BufferString
>
(
_bodyPrefix
);
_offset
+=
_bodyPrefix
.
size
();
_offset
+=
_bodyPrefix
.
size
();
_bodyPrefix
.
clear
();
_bodyPrefix
.
clear
();
return
ret
;
return
ret
;
}
}
if
(
_fileBody
->
remainSize
())
{
if
(
_fileBody
->
remainSize
())
{
auto
ret
=
_fileBody
->
readData
(
size
);
auto
ret
=
_fileBody
->
readData
(
size
);
if
(
!
ret
)
{
if
(
!
ret
)
{
//读取文件出现异常,提前中断
//读取文件出现异常,提前中断
_offset
=
_totalSize
;
_offset
=
_totalSize
;
}
else
{
}
else
{
_offset
+=
ret
->
size
();
_offset
+=
ret
->
size
();
}
}
return
ret
;
return
ret
;
}
}
if
(
_bodySuffix
.
size
())
{
if
(
_bodySuffix
.
size
())
{
auto
ret
=
std
::
make_shared
<
BufferString
>
(
_bodySuffix
);
auto
ret
=
std
::
make_shared
<
BufferString
>
(
_bodySuffix
);
_offset
=
_totalSize
;
_offset
=
_totalSize
;
_bodySuffix
.
clear
();
_bodySuffix
.
clear
();
...
@@ -230,7 +238,7 @@ Buffer::Ptr HttpMultiFormBody::readData(size_t size){
...
@@ -230,7 +238,7 @@ Buffer::Ptr HttpMultiFormBody::readData(size_t size){
return
nullptr
;
return
nullptr
;
}
}
string
HttpMultiFormBody
::
multiFormBodySuffix
(
const
string
&
boundary
){
string
HttpMultiFormBody
::
multiFormBodySuffix
(
const
string
&
boundary
)
{
string
MPboundary
=
string
(
"--"
)
+
boundary
;
string
MPboundary
=
string
(
"--"
)
+
boundary
;
string
endMPboundary
=
MPboundary
+
"--"
;
string
endMPboundary
=
MPboundary
+
"--"
;
_StrPrinter
body
;
_StrPrinter
body
;
...
@@ -238,21 +246,23 @@ string HttpMultiFormBody::multiFormBodySuffix(const string &boundary){
...
@@ -238,21 +246,23 @@ string HttpMultiFormBody::multiFormBodySuffix(const string &boundary){
return
std
::
move
(
body
);
return
std
::
move
(
body
);
}
}
string
HttpMultiFormBody
::
multiFormContentType
(
const
string
&
boundary
){
string
HttpMultiFormBody
::
multiFormContentType
(
const
string
&
boundary
)
{
return
StrPrinter
<<
"multipart/form-data; boundary="
<<
boundary
;
return
StrPrinter
<<
"multipart/form-data; boundary="
<<
boundary
;
}
}
string
HttpMultiFormBody
::
multiFormBodyPrefix
(
const
HttpArgs
&
args
,
const
string
&
boundary
,
const
string
&
fileName
)
{
string
HttpMultiFormBody
::
multiFormBodyPrefix
(
const
HttpArgs
&
args
,
const
string
&
boundary
,
const
string
&
fileName
)
{
string
MPboundary
=
string
(
"--"
)
+
boundary
;
string
MPboundary
=
string
(
"--"
)
+
boundary
;
_StrPrinter
body
;
_StrPrinter
body
;
for
(
auto
&
pr
:
args
)
{
for
(
auto
&
pr
:
args
)
{
body
<<
MPboundary
<<
"
\r\n
"
;
body
<<
MPboundary
<<
"
\r\n
"
;
body
<<
"Content-Disposition: form-data; name=
\"
"
<<
pr
.
first
<<
"
\"\r\n\r\n
"
;
body
<<
"Content-Disposition: form-data; name=
\"
"
<<
pr
.
first
<<
"
\"\r\n\r\n
"
;
body
<<
pr
.
second
<<
"
\r\n
"
;
body
<<
pr
.
second
<<
"
\r\n
"
;
}
}
body
<<
MPboundary
<<
"
\r\n
"
;
body
<<
MPboundary
<<
"
\r\n
"
;
body
<<
"Content-Disposition: form-data; name=
\"
"
<<
"file"
<<
"
\"
;filename=
\"
"
<<
fileName
<<
"
\"\r\n
"
;
body
<<
"Content-Disposition: form-data; name=
\"
"
body
<<
"Content-Type: application/octet-stream
\r\n\r\n
"
;
<<
"file"
<<
"
\"
;filename=
\"
"
<<
fileName
<<
"
\"\r\n
"
;
body
<<
"Content-Type: application/octet-stream
\r\n\r\n
"
;
return
std
::
move
(
body
);
return
std
::
move
(
body
);
}
}
...
@@ -268,4 +278,4 @@ Buffer::Ptr HttpBufferBody::readData(size_t size) {
...
@@ -268,4 +278,4 @@ Buffer::Ptr HttpBufferBody::readData(size_t size) {
return
Buffer
::
Ptr
(
std
::
move
(
_buffer
));
return
Buffer
::
Ptr
(
std
::
move
(
_buffer
));
}
}
}
//
namespace mediakit
}
//
namespace mediakit
src/Http/HttpBody.h
查看文件 @
ba213346
...
@@ -104,32 +104,32 @@ private:
...
@@ -104,32 +104,32 @@ private:
/**
/**
* 文件类型的content
* 文件类型的content
*/
*/
class
HttpFileBody
:
public
HttpBody
{
class
HttpFileBody
:
public
HttpBody
{
public
:
public
:
typedef
std
::
shared_ptr
<
HttpFileBody
>
Ptr
;
typedef
std
::
shared_ptr
<
HttpFileBody
>
Ptr
;
/**
/**
* 构造函数
* 构造函数
* @param fp 文件句柄,文件的偏移量必须为0
* @param file_path 文件路径
* @param offset 相对文件头的偏移量
* @param max_size 最大读取字节数,未判断是否大于文件真实大小
* @param use_mmap 是否使用mmap方式访问文件
* @param use_mmap 是否使用mmap方式访问文件
*/
*/
HttpFileBody
(
const
std
::
shared_ptr
<
FILE
>
&
fp
,
size_t
offset
,
size_t
max_size
,
bool
use_mmap
=
true
);
HttpFileBody
(
const
std
::
string
&
file_path
,
bool
use_mmap
=
true
);
HttpFileBody
(
const
std
::
string
&
file_path
,
bool
use_mmap
=
true
);
~
HttpFileBody
()
override
=
default
;
~
HttpFileBody
()
override
=
default
;
ssize_t
remainSize
()
override
;
/**
* 设置读取范围
* @param offset 相对文件头的偏移量
* @param max_size 最大读取字节数
*/
void
setRange
(
uint64_t
offset
,
uint64_t
max_size
);
ssize_t
remainSize
()
override
;
toolkit
::
Buffer
::
Ptr
readData
(
size_t
size
)
override
;
toolkit
::
Buffer
::
Ptr
readData
(
size_t
size
)
override
;
int
sendFile
(
int
fd
)
override
;
int
sendFile
(
int
fd
)
override
;
private
:
private
:
void
init
(
const
std
::
shared_ptr
<
FILE
>
&
fp
,
size_t
offset
,
size_t
max_size
,
bool
use_mmap
);
int64_t
_read_to
=
0
;
uint64_t
_file_offset
=
0
;
private
:
size_t
_max_size
;
size_t
_offset
=
0
;
size_t
_file_offset
=
0
;
std
::
shared_ptr
<
FILE
>
_fp
;
std
::
shared_ptr
<
FILE
>
_fp
;
std
::
shared_ptr
<
char
>
_map_addr
;
std
::
shared_ptr
<
char
>
_map_addr
;
toolkit
::
ResourcePool
<
toolkit
::
BufferRaw
>
_pool
;
toolkit
::
ResourcePool
<
toolkit
::
BufferRaw
>
_pool
;
...
...
src/Http/HttpFileManager.cpp
查看文件 @
ba213346
...
@@ -582,13 +582,8 @@ void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
...
@@ -582,13 +582,8 @@ void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
const
string
&
filePath
,
const
string
&
filePath
,
bool
use_mmap
)
const
{
bool
use_mmap
)
const
{
StrCaseMap
&
httpHeader
=
const_cast
<
StrCaseMap
&>
(
responseHeader
);
StrCaseMap
&
httpHeader
=
const_cast
<
StrCaseMap
&>
(
responseHeader
);
std
::
shared_ptr
<
FILE
>
fp
(
fopen
(
filePath
.
data
(),
"rb"
),
[](
FILE
*
fp
)
{
auto
fileBody
=
std
::
make_shared
<
HttpFileBody
>
(
filePath
,
use_mmap
);
if
(
fp
)
{
if
(
fileBody
->
remainSize
()
<
0
)
{
fclose
(
fp
);
}
});
if
(
!
fp
)
{
//打开文件失败
//打开文件失败
GET_CONFIG
(
string
,
notFound
,
Http
::
kNotFound
);
GET_CONFIG
(
string
,
notFound
,
Http
::
kNotFound
);
GET_CONFIG
(
string
,
charSet
,
Http
::
kCharSet
);
GET_CONFIG
(
string
,
charSet
,
Http
::
kCharSet
);
...
@@ -600,29 +595,23 @@ void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
...
@@ -600,29 +595,23 @@ void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
}
}
auto
&
strRange
=
const_cast
<
StrCaseMap
&>
(
requestHeader
)[
"Range"
];
auto
&
strRange
=
const_cast
<
StrCaseMap
&>
(
requestHeader
)[
"Range"
];
size_t
iRangeStart
=
0
;
int
code
=
200
;
size_t
iRangeEnd
=
0
;
if
(
!
strRange
.
empty
())
{
size_t
fileSize
=
File
::
fileSize
(
fp
.
get
());
int
code
;
if
(
strRange
.
size
()
==
0
)
{
//全部下载
code
=
200
;
iRangeEnd
=
fileSize
-
1
;
}
else
{
//分节下载
//分节下载
code
=
206
;
code
=
206
;
iRangeStart
=
atoll
(
FindField
(
strRange
.
data
(),
"bytes="
,
"-"
).
data
());
auto
iRangeStart
=
atoll
(
FindField
(
strRange
.
data
(),
"bytes="
,
"-"
).
data
());
iRangeEnd
=
atoll
(
FindField
(
strRange
.
data
(),
"-"
,
nullptr
).
data
());
auto
iRangeEnd
=
atoll
(
FindField
(
strRange
.
data
(),
"-"
,
nullptr
).
data
());
auto
fileSize
=
fileBody
->
remainSize
();
if
(
iRangeEnd
==
0
)
{
if
(
iRangeEnd
==
0
)
{
iRangeEnd
=
fileSize
-
1
;
iRangeEnd
=
fileSize
-
1
;
}
}
//设置文件范围
fileBody
->
setRange
(
iRangeStart
,
iRangeEnd
-
iRangeStart
+
1
);
//分节下载返回Content-Range头
//分节下载返回Content-Range头
httpHeader
.
emplace
(
"Content-Range"
,
StrPrinter
<<
"bytes "
<<
iRangeStart
<<
"-"
<<
iRangeEnd
<<
"/"
<<
fileSize
<<
endl
);
httpHeader
.
emplace
(
"Content-Range"
,
StrPrinter
<<
"bytes "
<<
iRangeStart
<<
"-"
<<
iRangeEnd
<<
"/"
<<
fileSize
<<
endl
);
}
}
//回复文件
//回复文件
HttpBody
::
Ptr
fileBody
=
std
::
make_shared
<
HttpFileBody
>
(
fp
,
iRangeStart
,
iRangeEnd
-
iRangeStart
+
1
,
use_mmap
);
(
*
this
)(
code
,
httpHeader
,
fileBody
);
(
*
this
)(
code
,
httpHeader
,
fileBody
);
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论