2019-10-27 02:04:51 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-10-27 02:04:51 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-10-27 02:04:51 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2019-10-27 02:04:51 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2022-02-05 20:39:24 +08:00
|
|
|
|
#include <csignal>
|
2022-02-11 13:40:41 +08:00
|
|
|
|
#include <tuple>
|
2022-02-10 20:23:37 +08:00
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(__linux__) || defined(__linux)
|
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-10-20 18:09:37 +08:00
|
|
|
|
#include "Util/File.h"
|
2019-10-27 02:04:51 +08:00
|
|
|
|
#include "Util/logger.h"
|
2022-02-05 20:39:24 +08:00
|
|
|
|
#include "Util/onceToken.h"
|
2022-02-10 20:23:37 +08:00
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/uv_errno.h"
|
|
|
|
|
|
|
|
|
|
#include "HttpBody.h"
|
2019-10-27 02:04:51 +08:00
|
|
|
|
#include "HttpClient.h"
|
2022-02-10 20:23:37 +08:00
|
|
|
|
#include "Common/macros.h"
|
2019-10-27 02:04:51 +08:00
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2019-10-27 03:42:56 +08:00
|
|
|
|
#define ENABLE_MMAP
|
2019-10-27 02:04:51 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2019-10-27 02:04:51 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
HttpStringBody::HttpStringBody(string str) {
|
2021-09-30 16:10:09 +08:00
|
|
|
|
_str = std::move(str);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
2021-01-17 18:31:50 +08:00
|
|
|
|
|
2022-02-10 21:06:51 +08:00
|
|
|
|
int64_t HttpStringBody::remainSize() {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
return _str.size() - _offset;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
Buffer::Ptr HttpStringBody::readData(size_t size) {
|
2021-01-19 16:05:38 +08:00
|
|
|
|
size = MIN((size_t)remainSize(), size);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
if (!size) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
//没有剩余字节了
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-02-10 20:23:37 +08:00
|
|
|
|
auto ret = std::make_shared<BufferString>(_str, _offset, size);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
_offset += size;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
#ifdef ENABLE_MMAP
|
|
|
|
|
|
2022-02-11 13:10:56 +08:00
|
|
|
|
static mutex s_mtx;
|
2022-02-11 13:40:41 +08:00
|
|
|
|
static unordered_map<string /*file_path*/, std::tuple<char */*ptr*/, int64_t /*size*/, weak_ptr<char> /*mmap*/ > > s_shared_mmap;
|
2022-02-11 13:10:56 +08:00
|
|
|
|
|
|
|
|
|
//删除mmap记录
|
|
|
|
|
static void delSharedMmap(const string &file_path, char *ptr) {
|
|
|
|
|
lock_guard<mutex> lck(s_mtx);
|
|
|
|
|
auto it = s_shared_mmap.find(file_path);
|
2022-02-11 13:40:41 +08:00
|
|
|
|
if (it != s_shared_mmap.end() && std::get<0>(it->second) == ptr) {
|
2022-02-11 13:10:56 +08:00
|
|
|
|
s_shared_mmap.erase(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::shared_ptr<char> getSharedMmap(const string &file_path, int64_t &file_size) {
|
2022-02-10 20:23:37 +08:00
|
|
|
|
{
|
|
|
|
|
lock_guard<mutex> lck(s_mtx);
|
|
|
|
|
auto it = s_shared_mmap.find(file_path);
|
|
|
|
|
if (it != s_shared_mmap.end()) {
|
2022-02-11 13:40:41 +08:00
|
|
|
|
auto ret = std::get<2>(it->second).lock();
|
2022-02-10 20:23:37 +08:00
|
|
|
|
if (ret) {
|
|
|
|
|
//命中mmap缓存
|
2022-02-11 13:40:41 +08:00
|
|
|
|
file_size = std::get<1>(it->second);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 13:10:56 +08:00
|
|
|
|
//打开文件
|
|
|
|
|
std::shared_ptr<FILE> fp(fopen(file_path.data(), "rb"), [](FILE *fp) {
|
|
|
|
|
if (fp) {
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!fp) {
|
|
|
|
|
//文件不存在
|
|
|
|
|
file_size = -1;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
//获取文件大小
|
|
|
|
|
file_size = File::fileSize(fp.get());
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
int fd = fileno(fp.get());
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
WarnL << "fileno failed:" << get_uv_errmsg(false);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-02-11 13:10:56 +08:00
|
|
|
|
auto ptr = (char *)mmap(NULL, file_size, PROT_READ, MAP_SHARED, fd, 0);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
if (ptr == MAP_FAILED) {
|
|
|
|
|
WarnL << "mmap " << file_path << " failed:" << get_uv_errmsg(false);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-02-11 13:10:56 +08:00
|
|
|
|
std::shared_ptr<char> ret(ptr, [file_size, fp, file_path](char *ptr) {
|
|
|
|
|
munmap(ptr, file_size);
|
|
|
|
|
delSharedMmap(file_path, ptr);
|
2022-02-10 21:23:50 +08:00
|
|
|
|
});
|
2022-02-11 13:10:56 +08:00
|
|
|
|
|
2022-02-11 14:02:55 +08:00
|
|
|
|
#if 0
|
2022-02-11 13:10:56 +08:00
|
|
|
|
if (file_size < 10 * 1024 * 1024 && file_path.rfind(".ts") != string::npos) {
|
|
|
|
|
//如果是小ts文件,那么尝试先加载到内存
|
|
|
|
|
auto buf = BufferRaw::create();
|
|
|
|
|
buf->assign(ret.get(), file_size);
|
|
|
|
|
ret.reset(buf->data(), [buf, file_path](char *ptr) {
|
|
|
|
|
delSharedMmap(file_path, ptr);
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-02-11 14:02:55 +08:00
|
|
|
|
#endif
|
2022-02-10 20:23:37 +08:00
|
|
|
|
{
|
|
|
|
|
lock_guard<mutex> lck(s_mtx);
|
2022-02-11 13:40:41 +08:00
|
|
|
|
s_shared_mmap[file_path] = std::make_tuple(ret.get(), file_size, ret);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
HttpFileBody::HttpFileBody(const string &file_path, bool use_mmap) {
|
|
|
|
|
#ifdef ENABLE_MMAP
|
2022-02-11 13:10:56 +08:00
|
|
|
|
if (use_mmap ) {
|
|
|
|
|
_map_addr = getSharedMmap(file_path, _read_to);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2022-02-11 13:40:41 +08:00
|
|
|
|
if (!_map_addr && _read_to != -1) {
|
2022-02-11 13:10:56 +08:00
|
|
|
|
_fp.reset(fopen(file_path.data(), "rb"), [](FILE *fp) {
|
|
|
|
|
if (fp) {
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!_fp) {
|
|
|
|
|
//文件不存在
|
|
|
|
|
_read_to = -1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_read_to = File::fileSize(_fp.get());
|
|
|
|
|
}
|
2019-10-28 17:23:16 +08:00
|
|
|
|
}
|
2019-10-27 02:04:51 +08:00
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
void HttpFileBody::setRange(uint64_t offset, uint64_t max_size) {
|
2022-02-11 11:13:43 +08:00
|
|
|
|
CHECK((int64_t)offset <= _read_to && (int64_t)(max_size + offset) <= _read_to);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
_read_to = max_size + offset;
|
|
|
|
|
_file_offset = offset;
|
|
|
|
|
if (_fp && !_map_addr) {
|
|
|
|
|
fseek64(_fp.get(), _file_offset, SEEK_SET);
|
|
|
|
|
}
|
2019-10-28 17:23:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 23:02:19 +08:00
|
|
|
|
int HttpFileBody::sendFile(int fd) {
|
2022-02-05 20:55:01 +08:00
|
|
|
|
#if defined(__linux__) || defined(__linux)
|
2022-02-11 13:10:56 +08:00
|
|
|
|
if (!_fp) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-02-10 20:23:37 +08:00
|
|
|
|
static onceToken s_token([]() { signal(SIGPIPE, SIG_IGN); });
|
2022-02-04 23:02:19 +08:00
|
|
|
|
off_t off = _file_offset;
|
2022-02-10 20:23:37 +08:00
|
|
|
|
return sendfile(fd, fileno(_fp.get()), &off, _read_to - _file_offset);
|
2022-02-04 23:02:19 +08:00
|
|
|
|
#else
|
|
|
|
|
return -1;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
class BufferMmap : public Buffer {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<BufferMmap> Ptr;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
BufferMmap(const std::shared_ptr<char> &map_addr, size_t offset, size_t size) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
_map_addr = map_addr;
|
|
|
|
|
_data = map_addr.get() + offset;
|
|
|
|
|
_size = size;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
}
|
2022-02-10 20:23:37 +08:00
|
|
|
|
~BufferMmap() override {};
|
2019-10-27 02:04:51 +08:00
|
|
|
|
//返回数据长度
|
2022-02-10 20:23:37 +08:00
|
|
|
|
char *data() const override { return _data; }
|
|
|
|
|
size_t size() const override { return _size; }
|
|
|
|
|
|
2019-10-27 02:04:51 +08:00
|
|
|
|
private:
|
|
|
|
|
char *_data;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _size;
|
2022-02-10 20:23:37 +08:00
|
|
|
|
std::shared_ptr<char> _map_addr;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-10 21:06:51 +08:00
|
|
|
|
int64_t HttpFileBody::remainSize() {
|
2022-02-10 20:23:37 +08:00
|
|
|
|
return _read_to - _file_offset;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
Buffer::Ptr HttpFileBody::readData(size_t size) {
|
2022-02-10 20:23:37 +08:00
|
|
|
|
size = MIN((size_t)remainSize(), size);
|
|
|
|
|
if (!size) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
//没有剩余字节了
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-02-10 20:23:37 +08:00
|
|
|
|
if (!_map_addr) {
|
|
|
|
|
// fread模式
|
2021-01-19 16:05:38 +08:00
|
|
|
|
ssize_t iRead;
|
2022-01-06 14:30:44 +08:00
|
|
|
|
auto ret = _pool.obtain2();
|
2019-10-27 02:04:51 +08:00
|
|
|
|
ret->setCapacity(size + 1);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
do {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
iRead = fread(ret->data(), 1, size, _fp.get());
|
2022-02-10 20:23:37 +08:00
|
|
|
|
} while (-1 == iRead && UV_EINTR == get_uv_error(false));
|
2019-10-27 02:04:51 +08:00
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
if (iRead > 0) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
//读到数据了
|
|
|
|
|
ret->setSize(iRead);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
_file_offset += iRead;
|
2021-01-17 20:15:08 +08:00
|
|
|
|
return std::move(ret);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
//读取文件异常,文件真实长度小于声明长度
|
2022-02-10 20:23:37 +08:00
|
|
|
|
_file_offset = _read_to;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
WarnL << "read file err:" << get_uv_errmsg();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
// mmap模式
|
|
|
|
|
auto ret = std::make_shared<BufferMmap>(_map_addr, _file_offset, size);
|
|
|
|
|
_file_offset += size;
|
2020-09-21 14:32:56 +08:00
|
|
|
|
return ret;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2022-02-10 20:23:37 +08:00
|
|
|
|
|
|
|
|
|
HttpMultiFormBody::HttpMultiFormBody(const HttpArgs &args, const string &filePath, const string &boundary) {
|
|
|
|
|
_fileBody = std::make_shared<HttpFileBody>(filePath);
|
|
|
|
|
if (_fileBody->remainSize() < 0) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
throw std::invalid_argument(StrPrinter << "open file failed:" << filePath << " " << get_uv_errmsg());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto fileName = filePath;
|
|
|
|
|
auto pos = filePath.rfind('/');
|
2022-02-10 20:23:37 +08:00
|
|
|
|
if (pos != string::npos) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
fileName = filePath.substr(pos + 1);
|
|
|
|
|
}
|
2022-02-10 20:23:37 +08:00
|
|
|
|
_bodyPrefix = multiFormBodyPrefix(args, boundary, fileName);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
_bodySuffix = multiFormBodySuffix(boundary);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
_totalSize = _bodyPrefix.size() + _bodySuffix.size() + _fileBody->remainSize();
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 21:06:51 +08:00
|
|
|
|
int64_t HttpMultiFormBody::remainSize() {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
return _totalSize - _offset;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
Buffer::Ptr HttpMultiFormBody::readData(size_t size) {
|
|
|
|
|
if (_bodyPrefix.size()) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
auto ret = std::make_shared<BufferString>(_bodyPrefix);
|
|
|
|
|
_offset += _bodyPrefix.size();
|
|
|
|
|
_bodyPrefix.clear();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
if (_fileBody->remainSize()) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
auto ret = _fileBody->readData(size);
|
2022-02-10 20:23:37 +08:00
|
|
|
|
if (!ret) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
//读取文件出现异常,提前中断
|
|
|
|
|
_offset = _totalSize;
|
2022-02-10 20:23:37 +08:00
|
|
|
|
} else {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
_offset += ret->size();
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
if (_bodySuffix.size()) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
auto ret = std::make_shared<BufferString>(_bodySuffix);
|
|
|
|
|
_offset = _totalSize;
|
|
|
|
|
_bodySuffix.clear();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
string HttpMultiFormBody::multiFormBodySuffix(const string &boundary) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
string MPboundary = string("--") + boundary;
|
|
|
|
|
string endMPboundary = MPboundary + "--";
|
|
|
|
|
_StrPrinter body;
|
|
|
|
|
body << "\r\n" << endMPboundary;
|
2021-01-17 20:15:08 +08:00
|
|
|
|
return std::move(body);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
string HttpMultiFormBody::multiFormContentType(const string &boundary) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
return StrPrinter << "multipart/form-data; boundary=" << boundary;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
string HttpMultiFormBody::multiFormBodyPrefix(const HttpArgs &args, const string &boundary, const string &fileName) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
string MPboundary = string("--") + boundary;
|
|
|
|
|
_StrPrinter body;
|
2022-02-10 20:23:37 +08:00
|
|
|
|
for (auto &pr : args) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
body << MPboundary << "\r\n";
|
|
|
|
|
body << "Content-Disposition: form-data; name=\"" << pr.first << "\"\r\n\r\n";
|
|
|
|
|
body << pr.second << "\r\n";
|
|
|
|
|
}
|
|
|
|
|
body << MPboundary << "\r\n";
|
2022-02-10 20:23:37 +08:00
|
|
|
|
body << "Content-Disposition: form-data; name=\""
|
|
|
|
|
<< "file"
|
|
|
|
|
<< "\";filename=\"" << fileName << "\"\r\n";
|
|
|
|
|
body << "Content-Type: application/octet-stream\r\n\r\n";
|
2021-01-17 20:15:08 +08:00
|
|
|
|
return std::move(body);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 21:02:07 +08:00
|
|
|
|
HttpBufferBody::HttpBufferBody(Buffer::Ptr buffer) {
|
|
|
|
|
_buffer = std::move(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 21:06:51 +08:00
|
|
|
|
int64_t HttpBufferBody::remainSize() {
|
2021-08-12 21:02:07 +08:00
|
|
|
|
return _buffer ? _buffer->size() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Buffer::Ptr HttpBufferBody::readData(size_t size) {
|
|
|
|
|
return Buffer::Ptr(std::move(_buffer));
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 20:23:37 +08:00
|
|
|
|
} // namespace mediakit
|