From 519398c1597f50c8b8c1e17516425b089bbff6cd Mon Sep 17 00:00:00 2001 From: Muhammad <73486659+Mis1eader-dev@users.noreply.github.com> Date: Mon, 22 Apr 2024 12:52:36 +0300 Subject: [PATCH] Avoid string copy and lowercasing on every request (#2008) --- lib/src/RealIpResolver.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/src/RealIpResolver.cc b/lib/src/RealIpResolver.cc index 2de2d6c7..dd72a1e2 100644 --- a/lib/src/RealIpResolver.cc +++ b/lib/src/RealIpResolver.cc @@ -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_) {