2019-10-27 02:04:51 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright ( c ) 2016 - present The ZLMediaKit project authors . All Rights Reserved .
2019-10-27 02:04:51 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit ( https : //github.com/ZLMediaKit/ZLMediaKit).
2019-10-27 02:04:51 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT - like license that can be found in the
2020-04-04 20:30:09 +08:00
* 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
# ifndef MIN
# define MIN(a,b) ((a) < (b) ? (a) : (b) )
# endif //MIN
namespace mediakit {
/**
* http content部分基类定义
2024-09-19 14:53:50 +08:00
* Base class definition for http content part
* [ AUTO - TRANSLATED : 1 eee419a ]
2019-10-27 02:04:51 +08:00
*/
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 :
2022-12-02 14:43:06 +08:00
using Ptr = std : : shared_ptr < HttpBody > ;
2023-04-22 21:59:26 +08:00
virtual ~ HttpBody ( ) = default ;
2019-10-27 02:04:51 +08:00
/**
2021-01-17 18:31:50 +08:00
* 剩 余 数 据 大 小 , 如 果 返 回 - 1 , 那 么 就 不 设 置 content - length
2024-09-19 14:53:50 +08:00
* Remaining data size , if - 1 is returned , then content - length is not set
* [ AUTO - TRANSLATED : 75375 ce7 ]
2019-10-27 02:04:51 +08:00
*/
2022-02-10 21:06:51 +08:00
virtual int64_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
2024-09-19 14:53:50 +08:00
* Read a certain number of bytes , the returned size may be less than size
* @ param size Request size
* @ return Byte object , if it is read , please return nullptr
* [ AUTO - TRANSLATED : 6f d85f91 ]
2019-10-27 02:04:51 +08:00
*/
2022-02-02 20:34:50 +08:00
virtual toolkit : : Buffer : : Ptr readData ( size_t size ) { return nullptr ; } ;
2019-12-28 13:11:41 +08:00
/**
* 异 步 请 求 读 取 一 定 字 节 数 , 返 回 大 小 可 能 小 于 size
* @ param size 请 求 大 小
* @ param cb 回 调 函 数
2024-09-19 14:53:50 +08:00
* Asynchronously request to read a certain number of bytes , the returned size may be less than size
* @ param size Request size
* @ param cb Callback function
* [ AUTO - TRANSLATED : a5304046 ]
2019-12-28 13:11:41 +08:00
*/
2022-02-02 20:34:50 +08:00
virtual void readDataAsync ( size_t size , const std : : function < void ( const toolkit : : Buffer : : Ptr & buf ) > & cb ) {
2024-09-19 14:53:50 +08:00
// 由于unix和linux是通过mmap的方式读取文件, 所以把读文件操作放在后台线程并不能提高性能 [AUTO-TRANSLATED:59ef443d]
// Since unix and linux read files through mmap, putting file reading operations in the background thread does not improve performance
// 反而会由于频繁的线程切换导致性能降低以及延时增加,所以我们默认同步获取文件内容 [AUTO-TRANSLATED:93d2a0b5]
// On the contrary, frequent thread switching will lead to performance degradation and increased latency, so we get the file content synchronously by default
// (其实并没有读,拷贝文件数据时在内核态完成文件读) [AUTO-TRANSLATED:6eb98a5d]
// (Actually, there is no reading, the file data is copied in the kernel state when copying)
2019-12-29 15:55:20 +08:00
cb ( readData ( size ) ) ;
2019-12-28 13:11:41 +08:00
}
2022-02-04 23:02:19 +08:00
/**
* 使 用 sendfile优化文件发送
* @ param fd socket fd
* @ return 0 成 功 , 其 他 为 错 误 代 码
2024-09-19 14:53:50 +08:00
* Use sendfile to optimize file sending
* @ param fd socket fd
* @ return 0 success , other error codes
* [ AUTO - TRANSLATED : eacc5f98 ]
2022-02-04 23:02:19 +08:00
*/
virtual int sendFile ( int fd ) {
return - 1 ;
}
2019-10-27 02:04:51 +08:00
} ;
/**
2022-02-02 20:34:50 +08:00
* std : : string类型的content
2024-09-19 14:53:50 +08:00
* std : : string type content
* [ AUTO - TRANSLATED : 59f c3e5b ]
2019-10-27 02:04:51 +08:00
*/
class HttpStringBody : public HttpBody {
public :
2022-12-02 14:43:06 +08:00
using Ptr = std : : shared_ptr < HttpStringBody > ;
2022-02-02 20:34:50 +08:00
HttpStringBody ( std : : string str ) ;
2021-08-12 21:02:07 +08:00
2022-02-10 21:06:51 +08:00
int64_t remainSize ( ) override ;
2022-02-02 20:34:50 +08:00
toolkit : : Buffer : : Ptr readData ( size_t size ) override ;
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 ;
2022-02-02 20:34:50 +08:00
mutable std : : string _str ;
2019-10-27 02:04:51 +08:00
} ;
2021-08-12 21:02:07 +08:00
/**
* Buffer类型的content
2024-09-19 14:53:50 +08:00
* Buffer type content
* [ AUTO - TRANSLATED : 350 b9513 ]
2021-08-12 21:02:07 +08:00
*/
class HttpBufferBody : public HttpBody {
public :
2022-12-02 14:43:06 +08:00
using Ptr = std : : shared_ptr < HttpBufferBody > ;
2022-02-02 20:34:50 +08:00
HttpBufferBody ( toolkit : : Buffer : : Ptr buffer ) ;
2021-08-12 21:02:07 +08:00
2022-02-10 21:06:51 +08:00
int64_t remainSize ( ) override ;
2022-02-02 20:34:50 +08:00
toolkit : : Buffer : : Ptr readData ( size_t size ) override ;
2021-08-12 21:02:07 +08:00
private :
2022-02-02 20:34:50 +08:00
toolkit : : Buffer : : Ptr _buffer ;
2021-08-12 21:02:07 +08:00
} ;
2019-10-27 02:04:51 +08:00
/**
* 文 件 类 型 的 content
2024-09-19 14:53:50 +08:00
* File type content
* [ AUTO - TRANSLATED : baf9c0f3 ]
2019-10-27 02:04:51 +08:00
*/
2022-02-10 20:23:37 +08:00
class HttpFileBody : public HttpBody {
2019-10-27 02:04:51 +08:00
public :
2022-12-02 14:43:06 +08:00
using Ptr = std : : shared_ptr < HttpFileBody > ;
2019-10-27 02:04:51 +08:00
/**
* 构 造 函 数
2022-02-10 20:23:37 +08:00
* @ param file_path 文 件 路 径
2021-12-22 15:42:03 +08:00
* @ param use_mmap 是 否 使 用 mmap方式访问文件
2024-09-19 14:53:50 +08:00
* Constructor
* @ param file_path File path
* @ param use_mmap Whether to use mmap to access the file
* [ AUTO - TRANSLATED : 40 c85c53 ]
2019-10-27 02:04:51 +08:00
*/
2022-02-02 20:34:50 +08:00
HttpFileBody ( const std : : string & file_path , bool use_mmap = true ) ;
2019-10-27 02:04:51 +08:00
2022-02-10 20:23:37 +08:00
/**
* 设 置 读 取 范 围
* @ param offset 相 对 文 件 头 的 偏 移 量
* @ param max_size 最 大 读 取 字 节 数
2024-09-19 14:53:50 +08:00
* Set the reading range
* @ param offset Offset relative to the file header
* @ param max_size Maximum number of bytes to read
* [ AUTO - TRANSLATED : 30532 a4e ]
2022-02-10 20:23:37 +08:00
*/
void setRange ( uint64_t offset , uint64_t max_size ) ;
2022-02-10 21:06:51 +08:00
int64_t remainSize ( ) override ;
2022-02-02 20:34:50 +08:00
toolkit : : Buffer : : Ptr readData ( size_t size ) override ;
2022-02-04 23:02:19 +08:00
int sendFile ( int fd ) override ;
2021-01-17 18:31:50 +08:00
2019-10-28 17:23:16 +08:00
private :
2022-02-10 20:23:37 +08:00
int64_t _read_to = 0 ;
uint64_t _file_offset = 0 ;
2019-10-27 02:04:51 +08:00
std : : shared_ptr < FILE > _fp ;
std : : shared_ptr < char > _map_addr ;
2022-02-02 20:34:50 +08:00
toolkit : : ResourcePool < toolkit : : BufferRaw > _pool ;
2019-10-27 02:04:51 +08:00
} ;
class HttpArgs ;
/**
* http MultiForm 方 式 提 交 的 http content
2024-09-19 14:53:50 +08:00
* http MultiForm way to submit http content
* [ AUTO - TRANSLATED : 211 a2d8e ]
2019-10-27 02:04:51 +08:00
*/
class HttpMultiFormBody : public HttpBody {
public :
2022-12-02 14:43:06 +08:00
using Ptr = std : : shared_ptr < HttpMultiFormBody > ;
2019-10-27 02:04:51 +08:00
/**
* 构 造 函 数
* @ param args http提交参数列表
* @ param filePath 文 件 路 径
* @ param boundary boundary字符串
2024-09-19 14:53:50 +08:00
* Constructor
* @ param args http submission parameter list
* @ param filePath File path
* @ param boundary Boundary string
* [ AUTO - TRANSLATED : d093cfa7 ]
2019-10-27 02:04:51 +08:00
*/
2022-02-02 20:34:50 +08:00
HttpMultiFormBody ( const HttpArgs & args , const std : : string & filePath , const std : : string & boundary = " 0xKhTmLbOuNdArY " ) ;
2022-02-10 21:06:51 +08:00
int64_t remainSize ( ) override ;
2022-02-02 20:34:50 +08:00
toolkit : : Buffer : : Ptr readData ( size_t size ) override ;
2021-01-17 18:31:50 +08:00
2019-10-27 02:04:51 +08:00
public :
2022-02-02 20:34:50 +08:00
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 ) ;
2021-01-17 18:31:50 +08:00
2019-10-27 02:04:51 +08:00
private :
2022-02-10 21:06:51 +08:00
uint64_t _offset = 0 ;
int64_t _totalSize ;
2022-02-02 20:34:50 +08:00
std : : string _bodyPrefix ;
std : : string _bodySuffix ;
2019-10-27 02:04:51 +08:00
HttpFileBody : : Ptr _fileBody ;
} ;
} //namespace mediakit
# endif //ZLMEDIAKIT_FILEREADER_H