添加下载文件http api范例

This commit is contained in:
xiongziliang
2019-10-28 17:23:16 +08:00
parent 52a663033d
commit fd10ef1187
3 changed files with 59 additions and 8 deletions

View File

@@ -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<HttpFileBody>(exePath());
if(!body->remainSize()){
invoker("404 Not Found", HttpSession::KeyValue(), "");
return;
}
invoker("200 OK", HttpSession::KeyValue(), body);
});
////////////以下是注册的Hook API////////////
API_REGIST(hook,on_publish,{
//开始推流事件