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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_FILEREADER_H
|
|
|
|
|
#define ZLMEDIAKIT_FILEREADER_H
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include "Network/Buffer.h"
|
|
|
|
|
#include "Util/ResourcePool.h"
|
|
|
|
|
#include "Util/logger.h"
|
2019-12-28 13:11:41 +08:00
|
|
|
|
#include "Thread/WorkThreadPool.h"
|
2019-10-27 02:04:51 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
|
#define MIN(a,b) ((a) < (b) ? (a) : (b) )
|
|
|
|
|
#endif //MIN
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* http content部分基类定义
|
|
|
|
|
*/
|
2019-12-28 13:11:41 +08:00
|
|
|
|
class HttpBody : public std::enable_shared_from_this<HttpBody>{
|
2019-10-27 02:04:51 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<HttpBody> Ptr;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
HttpBody(){}
|
|
|
|
|
|
2019-10-27 02:04:51 +08:00
|
|
|
|
virtual ~HttpBody(){}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* 剩余数据大小,如果返回-1, 那么就不设置content-length
|
2019-10-27 02:04:51 +08:00
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
virtual size_t remainSize() { return 0;};
|
2019-10-27 02:04:51 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 读取一定字节数,返回大小可能小于size
|
|
|
|
|
* @param size 请求大小
|
2019-12-24 10:25:28 +08:00
|
|
|
|
* @return 字节对象,如果读完了,那么请返回nullptr
|
2019-10-27 02:04:51 +08:00
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
virtual Buffer::Ptr readData(size_t size) { return nullptr;};
|
2019-12-28 13:11:41 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 异步请求读取一定字节数,返回大小可能小于size
|
|
|
|
|
* @param size 请求大小
|
|
|
|
|
* @param cb 回调函数
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
virtual void readDataAsync(size_t size,const function<void(const Buffer::Ptr &buf)> &cb){
|
2019-12-29 15:55:20 +08:00
|
|
|
|
//由于unix和linux是通过mmap的方式读取文件,所以把读文件操作放在后台线程并不能提高性能
|
|
|
|
|
//反而会由于频繁的线程切换导致性能降低以及延时增加,所以我们默认同步获取文件内容
|
|
|
|
|
//(其实并没有读,拷贝文件数据时在内核态完成文件读)
|
|
|
|
|
cb(readData(size));
|
2019-12-28 13:11:41 +08:00
|
|
|
|
}
|
2019-10-27 02:04:51 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* string类型的content
|
|
|
|
|
*/
|
|
|
|
|
class HttpStringBody : public HttpBody{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<HttpStringBody> Ptr;
|
|
|
|
|
HttpStringBody(const string &str);
|
|
|
|
|
virtual ~HttpStringBody(){}
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t remainSize() override;
|
|
|
|
|
Buffer::Ptr readData(size_t size) override ;
|
|
|
|
|
|
2019-10-27 02:04:51 +08:00
|
|
|
|
private:
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _offset = 0;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
mutable string _str;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 文件类型的content
|
|
|
|
|
*/
|
|
|
|
|
class HttpFileBody : public HttpBody{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<HttpFileBody> Ptr;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @param fp 文件句柄,文件的偏移量必须为0
|
|
|
|
|
* @param offset 相对文件头的偏移量
|
|
|
|
|
* @param max_size 最大读取字节数,未判断是否大于文件真实大小
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
HttpFileBody(const std::shared_ptr<FILE> &fp,size_t offset,size_t max_size);
|
2019-10-28 17:23:16 +08:00
|
|
|
|
HttpFileBody(const string &file_path);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
~HttpFileBody(){};
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t remainSize() override ;
|
|
|
|
|
Buffer::Ptr readData(size_t size) override;
|
|
|
|
|
|
2019-10-28 17:23:16 +08:00
|
|
|
|
private:
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void init(const std::shared_ptr<FILE> &fp,size_t offset,size_t max_size);
|
|
|
|
|
|
2019-10-27 02:04:51 +08:00
|
|
|
|
private:
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _max_size;
|
|
|
|
|
size_t _offset = 0;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
std::shared_ptr<FILE> _fp;
|
|
|
|
|
std::shared_ptr<char> _map_addr;
|
|
|
|
|
ResourcePool<BufferRaw> _pool;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class HttpArgs;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* http MultiForm 方式提交的http content
|
|
|
|
|
*/
|
|
|
|
|
class HttpMultiFormBody : public HttpBody {
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<HttpMultiFormBody> Ptr;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @param args http提交参数列表
|
|
|
|
|
* @param filePath 文件路径
|
|
|
|
|
* @param boundary boundary字符串
|
|
|
|
|
*/
|
|
|
|
|
HttpMultiFormBody(const HttpArgs &args,const string &filePath,const string &boundary = "0xKhTmLbOuNdArY");
|
|
|
|
|
virtual ~HttpMultiFormBody(){}
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t remainSize() override ;
|
|
|
|
|
Buffer::Ptr readData(size_t size) override;
|
|
|
|
|
|
2019-10-27 02:04:51 +08:00
|
|
|
|
public:
|
|
|
|
|
static string multiFormBodyPrefix(const HttpArgs &args,const string &boundary,const string &fileName);
|
|
|
|
|
static string multiFormBodySuffix(const string &boundary);
|
2021-01-17 18:31:50 +08:00
|
|
|
|
static size_t fileSize(FILE *fp);
|
2019-10-27 02:04:51 +08:00
|
|
|
|
static string multiFormContentType(const string &boundary);
|
2021-01-17 18:31:50 +08:00
|
|
|
|
|
2019-10-27 02:04:51 +08:00
|
|
|
|
private:
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _offset = 0;
|
|
|
|
|
size_t _totalSize;
|
2019-10-27 02:04:51 +08:00
|
|
|
|
string _bodyPrefix;
|
|
|
|
|
string _bodySuffix;
|
|
|
|
|
HttpFileBody::Ptr _fileBody;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_FILEREADER_H
|