diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-03-03 18:10:34 +0000 | 
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-03-03 18:10:34 +0000 | 
| commit | 5549efdf74449f6ce13592210dc19c1eddbb2898 (patch) | |
| tree | c8ac9cde93424eb31a904cb5a17b79e60abbe599 | |
| parent | Dedupe mapping of vars (diff) | |
| download | icespider-5549efdf74449f6ce13592210dc19c1eddbb2898.tar.bz2 icespider-5549efdf74449f6ce13592210dc19c1eddbb2898.tar.xz icespider-5549efdf74449f6ce13592210dc19c1eddbb2898.zip | |
Use a variadic template map lookup that's copy-free
| -rw-r--r-- | icespider/fcgi/cgiRequestBase.cpp | 25 | 
1 files 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<typename Ex, typename Map> +	const std::string_view & +	findFirstOrElse(const Map &, const std::string & msg) +	{ +		throw Ex(msg); +	} + +	template<typename Ex, typename Map, typename ... Ks> +	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<Ex>(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<std::runtime_error>(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);  		} | 
