From fd10ef1187f7f821849e6b763400cbd85384a6e3 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 28 Oct 2019 17:23:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8B=E8=BD=BD=E6=96=87?= =?UTF-8?q?=E4=BB=B6http=20api=E8=8C=83=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 42 +++++++++++++++++++++++++++++++++++------- src/Http/HttpBody.cpp | 22 +++++++++++++++++++++- src/Http/HttpBody.h | 3 +++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 6a040011..036c1977 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -181,19 +181,35 @@ static inline void addHttpListener(){ if(api_debug){ auto newInvoker = [invoker,parser,allArgs](const string &codeOut, const HttpSession::KeyValue &headerOut, - const string &contentOut){ + const HttpBody::Ptr &body){ stringstream ss; for(auto &pr : allArgs ){ ss << pr.first << " : " << pr.second << "\r\n"; } - 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"; + //body默认为空 + int64_t size = 0; + if (body && body->remainSize()) { + //有body,获取body大小 + 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; } @@ -596,6 +612,18 @@ void installWebApi() { }); #endif + //新增http api下载可执行程序文件接口 + //测试url http://127.0.0.1/index/api/downloadBin + API_REGIST_INVOKER(api,downloadBin,{ + CHECK_SECRET(); + auto body = std::make_shared(exePath()); + if(!body->remainSize()){ + invoker("404 Not Found", HttpSession::KeyValue(), ""); + return; + } + invoker("200 OK", HttpSession::KeyValue(), body); + }); + ////////////以下是注册的Hook API//////////// API_REGIST(hook,on_publish,{ //开始推流事件 diff --git a/src/Http/HttpBody.cpp b/src/Http/HttpBody.cpp index cc4aced4..9e6a0592 100644 --- a/src/Http/HttpBody.cpp +++ b/src/Http/HttpBody.cpp @@ -58,12 +58,32 @@ Buffer::Ptr HttpStringBody::readData(uint32_t size) { } ////////////////////////////////////////////////////////////////// +HttpFileBody::HttpFileBody(const string &filePath){ + std::shared_ptr 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 &fp, uint64_t offset, uint64_t max_size) { + init(fp,offset,max_size); +} + +void HttpFileBody::init(const std::shared_ptr &fp,uint64_t offset,uint64_t max_size){ _fp = fp; _max_size = max_size; #ifdef ENABLE_MMAP do { + if(!_fp){ + //文件不存在 + break; + } int fd = fileno(fp.get()); if (fd < 0) { WarnL << "fileno failed:" << get_uv_errmsg(false); @@ -79,7 +99,7 @@ HttpFileBody::HttpFileBody(const std::shared_ptr &fp, uint64_t offset, uin }); } while (false); #endif - if(!_map_addr && offset){ + if(!_map_addr && offset && fp.get()){ //未映射,那么fseek设置偏移量 fseek(fp.get(), offset, SEEK_SET); } diff --git a/src/Http/HttpBody.h b/src/Http/HttpBody.h index bf9e5cfe..d119cbc5 100644 --- a/src/Http/HttpBody.h +++ b/src/Http/HttpBody.h @@ -93,10 +93,13 @@ public: * @param max_size 最大读取字节数,未判断是否大于文件真实大小 */ HttpFileBody(const std::shared_ptr &fp,uint64_t offset,uint64_t max_size); + HttpFileBody(const string &file_path); ~HttpFileBody(){}; uint64_t remainSize() override ; Buffer::Ptr readData(uint32_t size) override; +private: + void init(const std::shared_ptr &fp,uint64_t offset,uint64_t max_size); private: std::shared_ptr _fp; uint64_t _max_size;