Skip to content

Commit

Permalink
Avoid string copy and lowercasing on every request (drogonframework#2008
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Mis1eader-dev authored Apr 22, 2024
1 parent 96919df commit 519398c
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/src/RealIpResolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
{
Expand Down

0 comments on commit 519398c

Please sign in to comment.