mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-12-02 11:47:56 +08:00
Use sendfile() system-call
This commit is contained in:
parent
735d423fa5
commit
5238da4df7
@ -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
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -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"]);
|
||||
|
@ -892,18 +892,26 @@ void HttpAppFrameworkImpl::readSendFile(const std::string& filePath,const HttpRe
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
infile.close();
|
||||
|
||||
resp->setStatusCode(HttpResponse::k200OK);
|
||||
LOG_INFO << "file len:" << str.length();
|
||||
resp->setBody(std::move(str));
|
||||
}
|
||||
|
||||
resp->setStatusCode(HttpResponse::k200OK);
|
||||
}
|
||||
|
||||
trantor::EventLoop *HttpAppFrameworkImpl::loop()
|
||||
|
@ -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,6 +66,7 @@ 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();
|
||||
@ -77,6 +75,10 @@ namespace drogon
|
||||
|
||||
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,
|
||||
@ -169,5 +171,6 @@ namespace drogon
|
||||
std::string _logPath="";
|
||||
std::string _logfileBaseName="";
|
||||
size_t _logfileSize=100000000;
|
||||
bool _useSendfile=true;
|
||||
};
|
||||
}
|
||||
|
@ -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");
|
||||
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())
|
||||
{
|
||||
|
@ -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_;
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
2
trantor
2
trantor
@ -1 +1 @@
|
||||
Subproject commit 87aa3947909b8b8eeb67066eb1491d7a26f83a9e
|
||||
Subproject commit 19693734fa9b8e48887a255368b289542adb43f7
|
Loading…
Reference in New Issue
Block a user