#include "response.h" #include "file.h" #include #include #include using namespace std::placeholders; std::string extend_index_html(std::string path) { if (path.size() && path.back() == '/') path.append("index.html"); return path; } namespace { std::string GetServerParam(const std::string& key, Server& server) { // following are the supported fields: // ... throw std::runtime_error("Unsupported server param: "s + key); } std::unordered_map> GetRequestParamFunctions{ // following are the supported fields: {"target", [](request_type& req, Server& server){return std::string{req.target()};}}, {"rel_target", [](request_type& req, Server& server){ std::string host{req["host"]}; std::string target{req.target()}; return server.GetConfig().GetRelativePath(server.GetSocket(), host, target); }}, {"doc_root", [](request_type& req, Server& server) { std::string host{req["host"]}; std::string target{req.target()}; return server.GetConfig().DocRoot(server.GetSocket(), host, target); }}, {"method", [](request_type& req, Server& server){ if (req.method() == http::verb::get) return "GET"; else if (req.method() == http::verb::post) return "POST"; else if (req.method() == http::verb::head) return "HEAD"; else return ""; }}, }; std::string GetRequestParam(const std::string& key, request_type& req, Server& server) { auto it = GetRequestParamFunctions.find(key); if (it != GetRequestParamFunctions.end()) return it->second(req, server); throw std::runtime_error("Unsupported request param: "s + key); } void SetResponseHeader(const std::string& key, const std::string& value, response_type& res) { // following are the supported fields: if (key == "status") { // HTTP Status, e.g. "200" (OK) res.result(unsigned(stoul(value))); } else if (key == "server") { // Server name/version string res.set(http::field::server, value); } else if (key == "content_type") { // e.g. text/html res.set(http::field::content_type, value); } else throw std::runtime_error("Unsupported response field: "s + key); } } // anonymous namespace std::string generate_response(request_type& req, response_type& res, Server& server) { std::string host{req["host"]}; std::string target{req.target()}; std::string plugin_name { server.GetConfig().GetPlugin(server.GetSocket(), host, target)}; plugin_type plugin{server.GetPlugin(plugin_name)}; auto GetServerParamFunction {std::function(std::bind(GetServerParam, _1, std::ref(server)))}; auto GetRequestParamFunction {std::function(std::bind(GetRequestParam, _1, std::ref(req), std::ref(server)))}; auto SetResponseHeaderFunction{std::function(std::bind(SetResponseHeader, _1, _2, std::ref(res)))}; return plugin->generate_page(GetServerParamFunction, GetRequestParamFunction, SetResponseHeaderFunction); }