From 5549efdf74449f6ce13592210dc19c1eddbb2898 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 3 Mar 2018 18:10:34 +0000 Subject: Use a variadic template map lookup that's copy-free --- icespider/fcgi/cgiRequestBase.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/icespider/fcgi/cgiRequestBase.cpp b/icespider/fcgi/cgiRequestBase.cpp index da1bd9f..6a22426 100644 --- a/icespider/fcgi/cgiRequestBase.cpp +++ b/icespider/fcgi/cgiRequestBase.cpp @@ -56,14 +56,31 @@ namespace IceSpider { } } + template + const std::string_view & + findFirstOrElse(const Map &, const std::string & msg) + { + throw Ex(msg); + } + + template + const std::string_view & + findFirstOrElse(const Map & map, const std::string & msg, const std::string_view & k, const Ks & ... ks) + { + auto i = map.find(k); + if (i == map.end()) { + return findFirstOrElse(map, msg, ks...); + } + return i->second; + } + void CgiRequestBase::initialize() { namespace ba = boost::algorithm; - auto path = (optionalLookup(REDIRECT_URL, envmap) / - [this]() { return optionalLookup(SCRIPT_NAME, envmap); } / - [this]() -> std::string { throw std::runtime_error("Couldn't determine request path"); }) - .substr(1); + auto path = findFirstOrElse(envmap, "Couldn't determine request path"s, + REDIRECT_URL, + SCRIPT_NAME).substr(1); if (!path.empty()) { ba::split(pathElements, path, ba::is_any_of("/"), ba::token_compress_off); } -- cgit v1.2.3