mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-12-02 00:12:33 +08:00
添加下载文件http api范例
This commit is contained in:
parent
52a663033d
commit
fd10ef1187
@ -181,19 +181,35 @@ static inline void addHttpListener(){
|
|||||||
if(api_debug){
|
if(api_debug){
|
||||||
auto newInvoker = [invoker,parser,allArgs](const string &codeOut,
|
auto newInvoker = [invoker,parser,allArgs](const string &codeOut,
|
||||||
const HttpSession::KeyValue &headerOut,
|
const HttpSession::KeyValue &headerOut,
|
||||||
const string &contentOut){
|
const HttpBody::Ptr &body){
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
for(auto &pr : allArgs ){
|
for(auto &pr : allArgs ){
|
||||||
ss << pr.first << " : " << pr.second << "\r\n";
|
ss << pr.first << " : " << pr.second << "\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
DebugL << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n"
|
//body默认为空
|
||||||
<< "# content:\r\n" << parser.Content() << "\r\n"
|
int64_t size = 0;
|
||||||
<< "# args:\r\n" << ss.str()
|
if (body && body->remainSize()) {
|
||||||
<< "# response:\r\n"
|
//有body,获取body大小
|
||||||
<< contentOut << "\r\n";
|
size = body->remainSize();
|
||||||
|
}
|
||||||
|
|
||||||
invoker(codeOut,headerOut,contentOut);
|
if(size < 4 * 1024){
|
||||||
|
string contentOut = body->readData(size)->toString();
|
||||||
|
DebugL << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n"
|
||||||
|
<< "# content:\r\n" << parser.Content() << "\r\n"
|
||||||
|
<< "# args:\r\n" << ss.str()
|
||||||
|
<< "# response:\r\n"
|
||||||
|
<< contentOut << "\r\n";
|
||||||
|
invoker(codeOut,headerOut,contentOut);
|
||||||
|
} else{
|
||||||
|
DebugL << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n"
|
||||||
|
<< "# content:\r\n" << parser.Content() << "\r\n"
|
||||||
|
<< "# args:\r\n" << ss.str()
|
||||||
|
<< "# response size:"
|
||||||
|
<< size <<"\r\n";
|
||||||
|
invoker(codeOut,headerOut,body);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
((HttpSession::HttpResponseInvoker &)invoker) = newInvoker;
|
((HttpSession::HttpResponseInvoker &)invoker) = newInvoker;
|
||||||
}
|
}
|
||||||
@ -596,6 +612,18 @@ void installWebApi() {
|
|||||||
});
|
});
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
//新增http api下载可执行程序文件接口
|
||||||
|
//测试url http://127.0.0.1/index/api/downloadBin
|
||||||
|
API_REGIST_INVOKER(api,downloadBin,{
|
||||||
|
CHECK_SECRET();
|
||||||
|
auto body = std::make_shared<HttpFileBody>(exePath());
|
||||||
|
if(!body->remainSize()){
|
||||||
|
invoker("404 Not Found", HttpSession::KeyValue(), "");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
invoker("200 OK", HttpSession::KeyValue(), body);
|
||||||
|
});
|
||||||
|
|
||||||
////////////以下是注册的Hook API////////////
|
////////////以下是注册的Hook API////////////
|
||||||
API_REGIST(hook,on_publish,{
|
API_REGIST(hook,on_publish,{
|
||||||
//开始推流事件
|
//开始推流事件
|
||||||
|
@ -58,12 +58,32 @@ Buffer::Ptr HttpStringBody::readData(uint32_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////
|
||||||
|
HttpFileBody::HttpFileBody(const string &filePath){
|
||||||
|
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
|
||||||
|
if(fp){
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if(!fp){
|
||||||
|
init(fp,0,0);
|
||||||
|
}else{
|
||||||
|
init(fp,0,HttpMultiFormBody::fileSize(fp.get()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
HttpFileBody::HttpFileBody(const std::shared_ptr<FILE> &fp, uint64_t offset, uint64_t max_size) {
|
HttpFileBody::HttpFileBody(const std::shared_ptr<FILE> &fp, uint64_t offset, uint64_t max_size) {
|
||||||
|
init(fp,offset,max_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HttpFileBody::init(const std::shared_ptr<FILE> &fp,uint64_t offset,uint64_t max_size){
|
||||||
_fp = fp;
|
_fp = fp;
|
||||||
_max_size = max_size;
|
_max_size = max_size;
|
||||||
#ifdef ENABLE_MMAP
|
#ifdef ENABLE_MMAP
|
||||||
do {
|
do {
|
||||||
|
if(!_fp){
|
||||||
|
//文件不存在
|
||||||
|
break;
|
||||||
|
}
|
||||||
int fd = fileno(fp.get());
|
int fd = fileno(fp.get());
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
WarnL << "fileno failed:" << get_uv_errmsg(false);
|
WarnL << "fileno failed:" << get_uv_errmsg(false);
|
||||||
@ -79,7 +99,7 @@ HttpFileBody::HttpFileBody(const std::shared_ptr<FILE> &fp, uint64_t offset, uin
|
|||||||
});
|
});
|
||||||
} while (false);
|
} while (false);
|
||||||
#endif
|
#endif
|
||||||
if(!_map_addr && offset){
|
if(!_map_addr && offset && fp.get()){
|
||||||
//未映射,那么fseek设置偏移量
|
//未映射,那么fseek设置偏移量
|
||||||
fseek(fp.get(), offset, SEEK_SET);
|
fseek(fp.get(), offset, SEEK_SET);
|
||||||
}
|
}
|
||||||
|
@ -93,10 +93,13 @@ public:
|
|||||||
* @param max_size 最大读取字节数,未判断是否大于文件真实大小
|
* @param max_size 最大读取字节数,未判断是否大于文件真实大小
|
||||||
*/
|
*/
|
||||||
HttpFileBody(const std::shared_ptr<FILE> &fp,uint64_t offset,uint64_t max_size);
|
HttpFileBody(const std::shared_ptr<FILE> &fp,uint64_t offset,uint64_t max_size);
|
||||||
|
HttpFileBody(const string &file_path);
|
||||||
~HttpFileBody(){};
|
~HttpFileBody(){};
|
||||||
|
|
||||||
uint64_t remainSize() override ;
|
uint64_t remainSize() override ;
|
||||||
Buffer::Ptr readData(uint32_t size) override;
|
Buffer::Ptr readData(uint32_t size) override;
|
||||||
|
private:
|
||||||
|
void init(const std::shared_ptr<FILE> &fp,uint64_t offset,uint64_t max_size);
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<FILE> _fp;
|
std::shared_ptr<FILE> _fp;
|
||||||
uint64_t _max_size;
|
uint64_t _max_size;
|
||||||
|
Loading…
Reference in New Issue
Block a user