Use sendfile() system-call

This commit is contained in:
antao 2018-09-25 18:07:29 +08:00
parent 735d423fa5
commit 5238da4df7
10 changed files with 83 additions and 33 deletions

View File

@ -62,6 +62,9 @@
//run_as_daemon:false by default
"run_as_daemon":false,
//relaunch_on_error:false by default,if true,the program will be restart by parent after exit;
"relaunch_on_error":false
"relaunch_on_error":false,
//use_sendfile:true by default,if ture,the program will
//use sendfile() system-call to send static file to client;
"use_sendfile":true
}
}

View File

@ -83,7 +83,7 @@ int main()
std::cout<<banner<<std::endl;
drogon::HttpAppFramework::instance().addListener("0.0.0.0",12345);
// drogon::HttpAppFramework::instance().addListener("0.0.0.0",12345);
drogon::HttpAppFramework::instance().addListener("0.0.0.0",8080);
//#ifdef USE_OPENSSL
// //https

View File

@ -75,9 +75,6 @@ namespace drogon
virtual void registerHttpSimpleController(const std::string &pathName,
const std::string &crtlName,
const std::vector<std::string> &filters=std::vector<std::string>())=0;
virtual void registerHttpApiController(const std::string &pathPattern,
const HttpApiBinderBasePtr &binder,
const std::vector<std::string> &filters=std::vector<std::string>())=0;
template <typename FUNCTION>
static void registerHttpApiMethod(const std::string &pathPattern,
FUNCTION && function,
@ -109,5 +106,12 @@ namespace drogon
virtual void setLogPath(const std::string &logPath,
const std::string &logfileBaseName="",
size_t logSize=100000000)=0;
virtual void enableSendfile(bool sendFile)=0;
private:
virtual void registerHttpApiController(const std::string &pathPattern,
const HttpApiBinderBasePtr &binder,
const std::vector<std::string> &filters=std::vector<std::string>())=0;
};
}

View File

@ -131,6 +131,8 @@ static void loadApp(const Json::Value &app)
{
HttpAppFramework::instance().enableRelaunchOnError();
}
auto useSendfile=app.get("use_sendfile",true).asBool();
HttpAppFramework::instance().enableSendfile(useSendfile);
}
static void loadListeners(const Json::Value &listeners)
{
@ -157,7 +159,7 @@ static void loadSSL(const Json::Value &sslFiles)
HttpAppFramework::instance().setSSLFiles(cert,key);
}
void ConfigLoader::load() {
std::cout<<_configJsonRoot<<std::endl;
//std::cout<<_configJsonRoot<<std::endl;
loadApp(_configJsonRoot["app"]);
loadSSL(_configJsonRoot["ssl"]);
loadListeners(_configJsonRoot["listeners"]);

View File

@ -892,18 +892,26 @@ void HttpAppFrameworkImpl::readSendFile(const std::string& filePath,const HttpRe
}
}
std::streambuf * pbuf = infile.rdbuf();
std::streamsize size = pbuf->pubseekoff(0,infile.end);
pbuf->pubseekoff(0,infile.beg); // rewind
std::string str;
str.resize(size);
pbuf->sgetn (&str[0],size);
infile.close();
if(_useSendfile&&
(req->getHeader("Accept-Encoding").find("gzip")==std::string::npos||
resp->getContentTypeCode()>=CT_APPLICATION_OCTET_STREAM))
{
//binary file or no gzip supported by client
std::dynamic_pointer_cast<HttpResponseImpl>(resp)->setSendfile(filePath);
}
else
{
std::streambuf * pbuf = infile.rdbuf();
std::streamsize size = pbuf->pubseekoff(0,infile.end);
pbuf->pubseekoff(0,infile.beg); // rewind
std::string str;
str.resize(size);
pbuf->sgetn (&str[0],size);
LOG_INFO << "file len:" << str.length();
resp->setBody(std::move(str));
}
resp->setStatusCode(HttpResponse::k200OK);
LOG_INFO << "file len:" << str.length();
resp->setBody(std::move(str));
}
trantor::EventLoop *HttpAppFrameworkImpl::loop()

View File

@ -33,7 +33,7 @@ namespace drogon
{
public:
HttpAppFrameworkImpl():
_connectionNum(0)
_connectionNum(0)
{
}
virtual void addListener(const std::string &ip,
@ -53,9 +53,6 @@ namespace drogon
const std::string &crtlName,
const std::vector<std::string> &filters=
std::vector<std::string>())override ;
virtual void registerHttpApiController(const std::string &pathPattern,
const HttpApiBinderBasePtr &binder,
const std::vector<std::string> &filters=std::vector<std::string>()) override ;
virtual void enableSession(const size_t timeout=0) override { _useSession=true;_sessionTimeout=timeout;}
virtual void disableSession() override { _useSession=false;}
virtual const std::string & getDocumentRoot() const override {return _rootPath;}
@ -69,14 +66,19 @@ namespace drogon
virtual void setLogPath(const std::string &logPath,
const std::string &logfileBaseName="",
size_t logfileSize=100000000) override;
virtual void enableSendfile(bool sendFile) override {_useSendfile=sendFile;}
~HttpAppFrameworkImpl(){
//Destroy the following objects before _loop destruction
_sharedLibManagerPtr.reset();
_sessionMapPtr.reset();
}
//Destroy the following objects before _loop destruction
_sharedLibManagerPtr.reset();
_sessionMapPtr.reset();
}
trantor::EventLoop *loop();
private:
virtual void registerHttpApiController(const std::string &pathPattern,
const HttpApiBinderBasePtr &binder,
const std::vector<std::string> &filters=std::vector<std::string>()) override ;
std::vector<std::tuple<std::string,uint16_t,bool,std::string,std::string>> _listeners;
void onAsyncRequest(const HttpRequestPtr& req,const std::function<void (const HttpResponsePtr &)> & callback);
void onNewWebsockRequest(const HttpRequestPtr& req,
@ -104,11 +106,11 @@ namespace drogon
const std::string &session_id,
const std::function<void ()> &missCallback);
void doFilterChain(const std::shared_ptr<std::queue<std::shared_ptr<HttpFilterBase>>> &chain,
const HttpRequestPtr& req,
const std::function<void (const HttpResponsePtr &)> & callback,
bool needSetJsessionid,
const std::string &session_id,
const std::function<void ()> &missCallback);
const HttpRequestPtr& req,
const std::function<void (const HttpResponsePtr &)> & callback,
bool needSetJsessionid,
const std::string &session_id,
const std::function<void ()> &missCallback);
//
struct ControllerAndFiltersName
{
@ -169,5 +171,6 @@ namespace drogon
std::string _logPath="";
std::string _logfileBaseName="";
size_t _logfileSize=100000000;
bool _useSendfile=true;
};
}

View File

@ -27,6 +27,7 @@
#include <drogon/HttpAppFramework.h>
#include <trantor/utils/Logger.h>
#include <stdio.h>
#include <sys/stat.h>
using namespace trantor;
using namespace drogon;
@ -313,12 +314,26 @@ std::string HttpResponseImpl::web_response_code_to_string(int code)
}
void HttpResponseImpl::appendToBuffer(MsgBuffer* output) const
{
char buf[32];
char buf[64];
snprintf(buf, sizeof buf, "HTTP/1.1 %d ", _statusCode);
output->append(buf);
output->append(_statusMessage);
output->append("\r\n");
snprintf(buf, sizeof buf, "Content-Length: %lu\r\n", _body.size());
if(_sendfileName.empty())
{
snprintf(buf, sizeof buf, "Content-Length: %lu\r\n", _body.size());
}
else
{
struct stat filestat;
if(stat(_sendfileName.c_str(), &filestat)<0)
{
LOG_SYSERR<<_sendfileName<<" stat error";
return;
}
snprintf(buf, sizeof buf, "Content-Length: %lld\r\n",filestat.st_size);
}
output->append(buf);
if(_headers.find("Connection")==_headers.end())
{

View File

@ -268,6 +268,12 @@ namespace drogon
}
return _jsonPtr;
}
const std::string &sendfileName() const {
return _sendfileName;
}
void setSendfile(const std::string &filename){
_sendfileName=filename;
}
protected:
static std::string web_content_type_to_string(uint8_t contenttype);
static const std::string web_content_type_and_charset_to_string(uint8_t contenttype,
@ -288,6 +294,7 @@ namespace drogon
size_t _current_chunk_length;
uint8_t _contentType=CT_TEXT_HTML;
std::string _sendfileName;
mutable std::shared_ptr<Json::Value> _jsonPtr;
//trantor::Date receiveTime_;

View File

@ -177,7 +177,10 @@ void HttpServer::onRequest(const TcpConnectionPtr& conn, const HttpRequestPtr& r
//if the request method is HEAD,remove the body of response(rfc2616-9.4)
if(_isHeadMethod)
response->setBody(std::string());
if(response->getContentTypeCode()<CT_APPLICATION_OCTET_STREAM&&
auto & sendfileName=std::dynamic_pointer_cast<HttpResponseImpl>(response)->sendfileName();
if(sendfileName.empty()&&
response->getContentTypeCode()<CT_APPLICATION_OCTET_STREAM&&
response->getBody().length()>1024&&
req->getHeader("Accept-Encoding").find("gzip")!=std::string::npos)
{
@ -240,6 +243,11 @@ void HttpServer::sendResponse(const TcpConnectionPtr& conn,
MsgBuffer buf;
std::dynamic_pointer_cast<HttpResponseImpl>(response)->appendToBuffer(&buf);
conn->send(std::move(buf));
auto & sendfileName=std::dynamic_pointer_cast<HttpResponseImpl>(response)->sendfileName();
if(!sendfileName.empty())
{
conn->sendFile(sendfileName.c_str());
}
if (response->closeConnection()) {
conn->shutdown();
}

@ -1 +1 @@
Subproject commit 87aa3947909b8b8eeb67066eb1491d7a26f83a9e
Subproject commit 19693734fa9b8e48887a255368b289542adb43f7