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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "HttpBody.h"
|
|
|
|
|
#include "Util/util.h"
|
2021-10-20 18:09:37 +08:00
|
|
|
|
#include "Util/File.h"
|
2019-10-27 02:04:51 +08:00
|
|
|
|
#include "Util/uv_errno.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "HttpClient.h"
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2019-10-27 03:42:56 +08:00
|
|
|
|
#define ENABLE_MMAP
|
2019-10-27 02:04:51 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2021-09-30 16:10:09 +08:00
|
|
|
|
HttpStringBody::HttpStringBody(string str){
|
|
|
|
|
_str = std::move(str);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
2021-01-17 18:31:50 +08:00
|
|
|
|
|
2021-01-19 16:05:38 +08:00
|
|
|
|
ssize_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);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
if(!size){
|
|
|
|
|
//没有剩余字节了
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
auto ret = std::make_shared<BufferString>(_str,_offset,size);
|
|
|
|
|
_offset += size;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2021-09-30 16:10:09 +08:00
|
|
|
|
|
2019-10-28 17:23:16 +08:00
|
|
|
|
HttpFileBody::HttpFileBody(const string &filePath){
|
|
|
|
|
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
|
2021-10-20 18:09:37 +08:00
|
|
|
|
if (fp) {
|
2019-10-28 17:23:16 +08:00
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-10-20 18:09:37 +08:00
|
|
|
|
if (!fp) {
|
|
|
|
|
init(fp, 0, 0);
|
|
|
|
|
} else {
|
|
|
|
|
init(fp, 0, File::fileSize(fp.get()));
|
2019-10-28 17:23:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-27 02:04:51 +08:00
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
HttpFileBody::HttpFileBody(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size) {
|
2019-10-28 17:23:16 +08:00
|
|
|
|
init(fp,offset,max_size);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 18:09:37 +08:00
|
|
|
|
void HttpFileBody::init(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
_fp = fp;
|
|
|
|
|
_max_size = max_size;
|
|
|
|
|
#ifdef ENABLE_MMAP
|
|
|
|
|
do {
|
2019-10-28 17:23:16 +08:00
|
|
|
|
if(!_fp){
|
|
|
|
|
//文件不存在
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-10-27 02:04:51 +08:00
|
|
|
|
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);
|
2019-10-27 03:00:19 +08:00
|
|
|
|
if (ptr == MAP_FAILED) {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
WarnL << "mmap failed:" << get_uv_errmsg(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-10-27 03:42:56 +08:00
|
|
|
|
_map_addr.reset(ptr,[max_size,fp](char *ptr){
|
2019-10-27 02:04:51 +08:00
|
|
|
|
munmap(ptr,max_size);
|
|
|
|
|
});
|
|
|
|
|
} while (false);
|
|
|
|
|
#endif
|
2019-10-28 17:23:16 +08:00
|
|
|
|
if(!_map_addr && offset && fp.get()){
|
2019-10-27 02:04:51 +08:00
|
|
|
|
//未映射,那么fseek设置偏移量
|
2021-01-17 18:31:50 +08:00
|
|
|
|
fseek64(fp.get(), offset, SEEK_SET);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BufferMmap : public Buffer{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
~BufferMmap() override{};
|
2019-10-27 02:04:51 +08:00
|
|
|
|
//返回数据长度
|
|
|
|
|
char *data() const override {
|
|
|
|
|
return _data;
|
|
|
|
|
}
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t size() const override{
|
2019-10-27 02:04:51 +08:00
|
|
|
|
return _size;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
std::shared_ptr<char> _map_addr;
|
|
|
|
|
char *_data;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _size;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-01-19 16:05:38 +08:00
|
|
|
|
ssize_t HttpFileBody::remainSize() {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
return _max_size - _offset;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
Buffer::Ptr HttpFileBody::readData(size_t size) {
|
2021-01-19 16:05:38 +08:00
|
|
|
|
size = MIN((size_t)remainSize(),size);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
if(!size){
|
|
|
|
|
//没有剩余字节了
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if(!_map_addr){
|
|
|
|
|
//fread模式
|
2021-01-19 16:05:38 +08:00
|
|
|
|
ssize_t iRead;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
auto ret = _pool.obtain();
|
|
|
|
|
ret->setCapacity(size + 1);
|
|
|
|
|
do{
|
|
|
|
|
iRead = fread(ret->data(), 1, size, _fp.get());
|
|
|
|
|
}while(-1 == iRead && UV_EINTR == get_uv_error(false));
|
|
|
|
|
|
|
|
|
|
if(iRead > 0){
|
|
|
|
|
//读到数据了
|
|
|
|
|
ret->setSize(iRead);
|
|
|
|
|
_offset += iRead;
|
2021-01-17 20:15:08 +08:00
|
|
|
|
return std::move(ret);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
//读取文件异常,文件真实长度小于声明长度
|
|
|
|
|
_offset = _max_size;
|
|
|
|
|
WarnL << "read file err:" << get_uv_errmsg();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//mmap模式
|
|
|
|
|
auto ret = std::make_shared<BufferMmap>(_map_addr,_offset,size);
|
|
|
|
|
_offset += size;
|
2020-09-21 14:32:56 +08:00
|
|
|
|
return ret;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
HttpMultiFormBody::HttpMultiFormBody(const HttpArgs &args,const string &filePath,const string &boundary){
|
|
|
|
|
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
|
|
|
|
|
if(fp){
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if(!fp){
|
|
|
|
|
throw std::invalid_argument(StrPrinter << "open file failed:" << filePath << " " << get_uv_errmsg());
|
|
|
|
|
}
|
2021-10-20 18:09:37 +08:00
|
|
|
|
_fileBody = std::make_shared<HttpFileBody>(fp, 0, File::fileSize(fp.get()));
|
2019-10-27 02:04:51 +08:00
|
|
|
|
|
|
|
|
|
auto fileName = filePath;
|
|
|
|
|
auto pos = filePath.rfind('/');
|
|
|
|
|
if(pos != string::npos){
|
|
|
|
|
fileName = filePath.substr(pos + 1);
|
|
|
|
|
}
|
|
|
|
|
_bodyPrefix = multiFormBodyPrefix(args,boundary,fileName);
|
|
|
|
|
_bodySuffix = multiFormBodySuffix(boundary);
|
|
|
|
|
_totalSize = _bodyPrefix.size() + _bodySuffix.size() + _fileBody->remainSize();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 16:05:38 +08:00
|
|
|
|
ssize_t HttpMultiFormBody::remainSize() {
|
2019-10-27 02:04:51 +08:00
|
|
|
|
return _totalSize - _offset;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
Buffer::Ptr HttpMultiFormBody::readData(size_t size){
|
2019-10-27 02:04:51 +08:00
|
|
|
|
if(_bodyPrefix.size()){
|
|
|
|
|
auto ret = std::make_shared<BufferString>(_bodyPrefix);
|
|
|
|
|
_offset += _bodyPrefix.size();
|
|
|
|
|
_bodyPrefix.clear();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(_fileBody->remainSize()){
|
|
|
|
|
auto ret = _fileBody->readData(size);
|
|
|
|
|
if(!ret){
|
|
|
|
|
//读取文件出现异常,提前中断
|
|
|
|
|
_offset = _totalSize;
|
|
|
|
|
}else{
|
|
|
|
|
_offset += ret->size();
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(_bodySuffix.size()){
|
|
|
|
|
auto ret = std::make_shared<BufferString>(_bodySuffix);
|
|
|
|
|
_offset = _totalSize;
|
|
|
|
|
_bodySuffix.clear();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string HttpMultiFormBody::multiFormBodySuffix(const string &boundary){
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string HttpMultiFormBody::multiFormContentType(const string &boundary){
|
|
|
|
|
return StrPrinter << "multipart/form-data; boundary=" << boundary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string HttpMultiFormBody::multiFormBodyPrefix(const HttpArgs &args,const string &boundary,const string &fileName){
|
|
|
|
|
string MPboundary = string("--") + boundary;
|
|
|
|
|
_StrPrinter body;
|
|
|
|
|
for(auto &pr : args){
|
|
|
|
|
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";
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssize_t HttpBufferBody::remainSize() {
|
|
|
|
|
return _buffer ? _buffer->size() : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Buffer::Ptr HttpBufferBody::readData(size_t size) {
|
|
|
|
|
return Buffer::Ptr(std::move(_buffer));
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 02:04:51 +08:00
|
|
|
|
}//namespace mediakit
|