Implement br_static (#359)

This commit is contained in:
An Tao 2020-02-23 17:39:03 +08:00 committed by GitHub
parent cc3149dc58
commit ee77800821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 1 deletions

View File

@ -193,6 +193,10 @@
//file with the extension ".gz" in the same path and send the compressed file to the client. //file with the extension ".gz" in the same path and send the compressed file to the client.
//The default value of gzip_static is true. //The default value of gzip_static is true.
"gzip_static": true, "gzip_static": true,
//br_static: If it is set to true, when the client requests a static file, drogon first finds the compressed
//file with the extension ".br" in the same path and send the compressed file to the client.
//The default value of br_static is true.
"br_static": true,
//client_max_body_size: Set the maximum body size of HTTP requests received by drogon. The default value is "1M". //client_max_body_size: Set the maximum body size of HTTP requests received by drogon. The default value is "1M".
//One can set it to "1024", "1k", "10M", "1G", etc. Setting it to "" means no limit. //One can set it to "1024", "1k", "10M", "1G", etc. Setting it to "" means no limit.
"client_max_body_size": "1M", "client_max_body_size": "1M",

View File

@ -193,6 +193,10 @@
//file with the extension ".gz" in the same path and send the compressed file to the client. //file with the extension ".gz" in the same path and send the compressed file to the client.
//The default value of gzip_static is true. //The default value of gzip_static is true.
"gzip_static": true, "gzip_static": true,
//br_static: If it is set to true, when the client requests a static file, drogon first finds the compressed
//file with the extension ".br" in the same path and send the compressed file to the client.
//The default value of br_static is true.
"br_static": true,
//client_max_body_size: Set the maximum body size of HTTP requests received by drogon. The default value is "1M". //client_max_body_size: Set the maximum body size of HTTP requests received by drogon. The default value is "1M".
//One can set it to "1024", "1k", "10M", "1G", etc. Setting it to "" means no limit. //One can set it to "1024", "1k", "10M", "1G", etc. Setting it to "" means no limit.
"client_max_body_size": "1M", "client_max_body_size": "1M",

View File

@ -923,6 +923,17 @@ class HttpAppFramework : public trantor::NonCopyable
*/ */
virtual HttpAppFramework &setGzipStatic(bool useGzipStatic) = 0; virtual HttpAppFramework &setGzipStatic(bool useGzipStatic) = 0;
/// Set the br_static option.
/**
* If it is set to true, when the client requests a static file, drogon
* first finds the compressed file with the extension ".br" in the same path
* and send the compressed file to the client. The default value is true.
*
* @note
* This operation can be performed by an option in the configuration file.
*/
virtual HttpAppFramework &setBrStatic(bool useGzipStatic) = 0;
/// Set the max body size of the requests received by drogon. /// Set the max body size of the requests received by drogon.
/** /**
* The default value is 1M. * The default value is 1M.

View File

@ -369,6 +369,8 @@ static void loadApp(const Json::Value &app)
drogon::app().setPipeliningRequestsNumber(pipeliningReqs); drogon::app().setPipeliningRequestsNumber(pipeliningReqs);
auto useGzipStatic = app.get("gzip_static", true).asBool(); auto useGzipStatic = app.get("gzip_static", true).asBool();
drogon::app().setGzipStatic(useGzipStatic); drogon::app().setGzipStatic(useGzipStatic);
auto useBrStatic = app.get("br_static", true).asBool();
drogon::app().setBrStatic(useBrStatic);
auto maxBodySize = app.get("client_max_body_size", "1M").asString(); auto maxBodySize = app.get("client_max_body_size", "1M").asString();
size_t size; size_t size;
if (bytesSize(maxBodySize, size)) if (bytesSize(maxBodySize, size))

View File

@ -164,6 +164,11 @@ HttpAppFramework &HttpAppFrameworkImpl::setGzipStatic(bool useGzipStatic)
staticFileRouterPtr_->setGzipStatic(useGzipStatic); staticFileRouterPtr_->setGzipStatic(useGzipStatic);
return *this; return *this;
} }
HttpAppFramework &HttpAppFrameworkImpl::setBrStatic(bool useGzipStatic)
{
staticFileRouterPtr_->setBrStatic(useGzipStatic);
return *this;
}
#ifndef _WIN32 #ifndef _WIN32
HttpAppFramework &HttpAppFrameworkImpl::enableDynamicViewsLoading( HttpAppFramework &HttpAppFrameworkImpl::enableDynamicViewsLoading(
const std::vector<std::string> &libPaths, const std::vector<std::string> &libPaths,

View File

@ -274,6 +274,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
return *this; return *this;
} }
virtual HttpAppFramework &setGzipStatic(bool useGzipStatic) override; virtual HttpAppFramework &setGzipStatic(bool useGzipStatic) override;
virtual HttpAppFramework &setBrStatic(bool useGzipStatic) override;
virtual HttpAppFramework &setClientMaxBodySize(size_t maxSize) override virtual HttpAppFramework &setClientMaxBodySize(size_t maxSize) override
{ {
clientMaxBodySize_ = maxSize; clientMaxBodySize_ = maxSize;

View File

@ -248,7 +248,22 @@ void StaticFileRouter::sendStaticFileResponse(
return; return;
} }
HttpResponsePtr resp; HttpResponsePtr resp;
if (gzipStaticFlag_ && if (brStaticFlag_ &&
req->getHeaderBy("accept-encoding").find("br") != std::string::npos)
{
// Find compressed file first.
auto gzipFileName = filePath + ".br";
std::ifstream infile(gzipFileName, std::ifstream::binary);
if (infile)
{
resp =
HttpResponse::newFileResponse(gzipFileName,
"",
drogon::getContentType(filePath));
resp->addHeader("Content-Encoding", "br");
}
}
if (!resp && gzipStaticFlag_ &&
req->getHeaderBy("accept-encoding").find("gzip") != std::string::npos) req->getHeaderBy("accept-encoding").find("gzip") != std::string::npos)
{ {
// Find compressed file first. // Find compressed file first.
@ -263,6 +278,7 @@ void StaticFileRouter::sendStaticFileResponse(
resp->addHeader("Content-Encoding", "gzip"); resp->addHeader("Content-Encoding", "gzip");
} }
} }
if (!resp) if (!resp)
resp = HttpResponse::newFileResponse(filePath); resp = HttpResponse::newFileResponse(filePath);
if (resp->statusCode() != k404NotFound) if (resp->statusCode() != k404NotFound)

View File

@ -42,6 +42,10 @@ class StaticFileRouter
{ {
gzipStaticFlag_ = useGzipStatic; gzipStaticFlag_ = useGzipStatic;
} }
void setBrStatic(bool useBrStatic)
{
brStaticFlag_ = useBrStatic;
}
void init(const std::vector<trantor::EventLoop *> &ioloops); void init(const std::vector<trantor::EventLoop *> &ioloops);
void sendStaticFileResponse( void sendStaticFileResponse(
@ -95,6 +99,7 @@ class StaticFileRouter
int staticFilesCacheTime_{5}; int staticFilesCacheTime_{5};
bool enableLastModify_{true}; bool enableLastModify_{true};
bool gzipStaticFlag_{true}; bool gzipStaticFlag_{true};
bool brStaticFlag_{true};
std::unique_ptr< std::unique_ptr<
IOThreadStorage<std::unique_ptr<CacheMap<std::string, char>>>> IOThreadStorage<std::unique_ptr<CacheMap<std::string, char>>>>
staticFilesCacheMap_; staticFilesCacheMap_;