ZLMediaKit/src/Http/HttpBody.h

174 lines
4.9 KiB
C++
Raw Normal View History

/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
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.
*/
#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"
#include "Thread/WorkThreadPool.h"
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b) )
#endif //MIN
namespace mediakit {
/**
* http content部分基类定义
*/
class HttpBody : public std::enable_shared_from_this<HttpBody>{
public:
typedef std::shared_ptr<HttpBody> Ptr;
HttpBody(){}
virtual ~HttpBody(){}
/**
* -1, content-length
*/
2021-01-19 16:05:38 +08:00
virtual ssize_t remainSize() { return 0;};
/**
* size
* @param size
* @return ,nullptr
*/
virtual toolkit::Buffer::Ptr readData(size_t size) { return nullptr;};
/**
* size
* @param size
* @param cb
*/
virtual void readDataAsync(size_t size,const std::function<void(const toolkit::Buffer::Ptr &buf)> &cb){
2019-12-29 15:55:20 +08:00
//由于unix和linux是通过mmap的方式读取文件所以把读文件操作放在后台线程并不能提高性能
//反而会由于频繁的线程切换导致性能降低以及延时增加,所以我们默认同步获取文件内容
//(其实并没有读,拷贝文件数据时在内核态完成文件读)
cb(readData(size));
}
/**
* 使sendfile优化文件发送
* @param fd socket fd
* @return 0
*/
virtual int sendFile(int fd) {
return -1;
}
};
/**
* std::string类型的content
*/
class HttpStringBody : public HttpBody{
public:
typedef std::shared_ptr<HttpStringBody> Ptr;
HttpStringBody(std::string str);
2021-08-12 21:02:07 +08:00
~HttpStringBody() override = default;
2021-01-19 16:05:38 +08:00
ssize_t remainSize() override;
toolkit::Buffer::Ptr readData(size_t size) override ;
private:
size_t _offset = 0;
mutable std::string _str;
};
2021-08-12 21:02:07 +08:00
/**
* Buffer类型的content
*/
class HttpBufferBody : public HttpBody{
public:
typedef std::shared_ptr<HttpBufferBody> Ptr;
HttpBufferBody(toolkit::Buffer::Ptr buffer);
2021-08-12 21:02:07 +08:00
~HttpBufferBody() override = default;
ssize_t remainSize() override;
toolkit::Buffer::Ptr readData(size_t size) override;
2021-08-12 21:02:07 +08:00
private:
toolkit::Buffer::Ptr _buffer;
2021-08-12 21:02:07 +08:00
};
/**
* content
*/
class HttpFileBody : public HttpBody{
public:
typedef std::shared_ptr<HttpFileBody> Ptr;
/**
*
* @param fp 0
* @param offset
* @param max_size
* @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);
2021-08-12 21:02:07 +08:00
~HttpFileBody() override = default;
2021-01-19 16:05:38 +08:00
ssize_t remainSize() override ;
toolkit::Buffer::Ptr readData(size_t size) override;
int sendFile(int fd) override;
2019-10-28 17:23:16 +08:00
private:
void init(const std::shared_ptr<FILE> &fp,size_t offset,size_t max_size, bool use_mmap);
private:
size_t _max_size;
size_t _offset = 0;
size_t _file_offset = 0;
std::shared_ptr<FILE> _fp;
std::shared_ptr<char> _map_addr;
toolkit::ResourcePool<toolkit::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 std::string &filePath,const std::string &boundary = "0xKhTmLbOuNdArY");
virtual ~HttpMultiFormBody(){}
2021-01-19 16:05:38 +08:00
ssize_t remainSize() override ;
toolkit::Buffer::Ptr readData(size_t size) override;
public:
static std::string multiFormBodyPrefix(const HttpArgs &args,const std::string &boundary,const std::string &fileName);
static std::string multiFormBodySuffix(const std::string &boundary);
static std::string multiFormContentType(const std::string &boundary);
private:
size_t _offset = 0;
size_t _totalSize;
std::string _bodyPrefix;
std::string _bodySuffix;
HttpFileBody::Ptr _fileBody;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_FILEREADER_H