Avoid string copy and lowercasing on every request (#2008)

This commit is contained in:
Muhammad 2024-04-22 12:52:36 +03:00 committed by GitHub
parent 96919df488
commit 519398c159
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 2 deletions

View File

@ -107,15 +107,17 @@ void RealIpResolver::initAndStart(const Json::Value &config)
}
drogon::app().registerPreRoutingAdvice([this](const HttpRequestPtr &req) {
const std::string &ipHeader = req->getHeader(fromHeader_);
const auto &headers = req->headers();
auto ipHeaderFind = headers.find(fromHeader_);
const trantor::InetAddress &peerAddr = req->getPeerAddr();
if (ipHeader.empty() || !matchCidr(peerAddr))
if (ipHeaderFind == headers.end() || !matchCidr(peerAddr))
{
// Target header is empty, or
// direct peer is already a non-proxy
req->attributes()->insert(attributeKey_, peerAddr);
return;
}
const std::string &ipHeader = ipHeaderFind->second;
// Use a header field which contains a single ip
if (!useXForwardedFor_)
{