Add an interface to set the 'Server' header field

This commit is contained in:
antao 2019-03-02 09:27:45 +08:00
parent 0f51ac56c0
commit e0147fac8b
6 changed files with 26 additions and 9 deletions

View File

@ -135,6 +135,9 @@
//of the connection without read or write
"idle_connection_timeout": 60,
//enable_fast_db_client: Defaults to false
"enable_fast_db_client": false
"enable_fast_db_client": false,
//server_header_field: Set the 'server' header field in each response sent by drogon,
//empty string by default with which the 'server' header field will be "Server: drogon/version string\r\n"
"server_header_field": ""
}
}

View File

@ -135,6 +135,9 @@
//of the connection without read or write
"idle_connection_timeout": 60,
//enable_fast_db_client: Defaults to false
"enable_fast_db_client": false
"enable_fast_db_client": false,
//server_header_field: Set the 'server' header field in each response sent by drogon,
//empty string by default with which the 'server' header field will be "Server: drogon/version string\r\n"
"server_header_field": ""
}
}

View File

@ -184,6 +184,7 @@ class HttpAppFramework : public trantor::NonCopyable
virtual void setStaticFilesCacheTime(int cacheTime) = 0;
virtual int staticFilesCacheTime() const = 0;
virtual void setIdleConnectionTimeout(size_t timeout) = 0;
virtual void setServerHeaderField(const std::string &server) = 0;
#if USE_ORM
virtual orm::DbClientPtr getDbClient(const std::string &name = "default") = 0;
#if USE_FAST_CLIENT

View File

@ -236,6 +236,9 @@ static void loadApp(const Json::Value &app)
drogon::app().enableFastDbClient();
#endif
#endif
auto server = app.get("server_header_field", "").asString();
if(!server.empty())
drogon::app().setServerHeaderField(server);
}
static void loadDbClients(const Json::Value &dbClients)
{

View File

@ -106,6 +106,17 @@ class HttpAppFrameworkImpl : public HttpAppFramework
if (loop()->isRunning())
loop()->quit();
}
virtual void setServerHeaderField(const std::string &server) override
{
assert(!_running);
assert(server.find("\r\n") == std::string::npos);
_serverHeader = "Server: " + server + "\r\n";
}
const std::string &getServerHeaderString() const
{
return _serverHeader;
}
#if USE_ORM
virtual orm::DbClientPtr getDbClient(const std::string &name = "default") override;
#if USE_FAST_CLIENT
@ -156,6 +167,8 @@ class HttpAppFrameworkImpl : public HttpAppFramework
size_t _idleConnectionTimeout = 60;
bool _useSession = false;
std::vector<std::tuple<std::string, uint16_t, bool, std::string, std::string>> _listeners;
std::string _serverHeader = "Server: drogon/" + drogon::getVersion() + "\r\n";
typedef std::shared_ptr<Session> SessionPtr;
std::unique_ptr<CacheMap<std::string, SessionPtr>> _sessionMapPtr;
std::unique_ptr<CacheMap<std::string, HttpResponsePtr>> _responseCachingMap;

View File

@ -26,12 +26,6 @@
using namespace trantor;
using namespace drogon;
static const std::string &getServerString()
{
static const std::string server = "Server: drogon/" + drogon::getVersion() + "\r\n";
return server;
}
HttpResponsePtr HttpResponse::newHttpResponse()
{
auto res = std::make_shared<HttpResponseImpl>(k200OK, CT_TEXT_HTML);
@ -334,7 +328,7 @@ void HttpResponseImpl::makeHeaderString(const std::shared_ptr<std::string> &head
headerStringPtr->append(it->second);
headerStringPtr->append("\r\n");
}
headerStringPtr->append(getServerString());
headerStringPtr->append(HttpAppFrameworkImpl::instance().getServerHeaderString());
}
std::shared_ptr<std::string> HttpResponseImpl::renderToString() const