summaryrefslogtreecommitdiffhomepage
path: root/response.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-10 16:58:49 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-10 16:58:49 +0200
commit0f55f61ee745f38c312a53009b883feb5aa86b49 (patch)
tree46b8b9f0847e0dcbb1ea8e25fff69de8c5337850 /response.cpp
parentc0ccf16c69d43a89674640c61d13ec2c02b128d6 (diff)
Simplify http and https units
Diffstat (limited to 'response.cpp')
-rw-r--r--response.cpp57
1 files changed, 53 insertions, 4 deletions
diff --git a/response.cpp b/response.cpp
index 6b7ae6a..70c93b1 100644
--- a/response.cpp
+++ b/response.cpp
@@ -7,6 +7,8 @@
using namespace std::placeholders;
+namespace {
+
std::string extend_index_html(std::string path)
{
if (path.size() && path.back() == '/')
@@ -14,8 +16,6 @@ std::string extend_index_html(std::string path)
return path;
}
-namespace {
-
std::string GetServerParam(const std::string& key, Server& server)
{
// following are the supported fields:
@@ -73,10 +73,51 @@ void SetResponseHeader(const std::string& key, const std::string& value, respons
throw std::runtime_error("Unsupported response field: "s + key);
}
+// Return a reasonable mime type based on the extension of a file.
+beast::string_view
+mime_type(beast::string_view path)
+{
+ using beast::iequals;
+ auto const ext = [&path]
+ {
+ auto const pos = path.rfind(".");
+ if (pos == beast::string_view::npos)
+ return beast::string_view{};
+ return path.substr(pos);
+ }();
+ if(iequals(ext, ".htm")) return "text/html";
+ if(iequals(ext, ".html")) return "text/html";
+ if(iequals(ext, ".php")) return "text/html";
+ if(iequals(ext, ".css")) return "text/css";
+ if(iequals(ext, ".txt")) return "text/plain";
+ if(iequals(ext, ".js")) return "application/javascript";
+ if(iequals(ext, ".json")) return "application/json";
+ if(iequals(ext, ".xml")) return "application/xml";
+ if(iequals(ext, ".swf")) return "application/x-shockwave-flash";
+ if(iequals(ext, ".flv")) return "video/x-flv";
+ if(iequals(ext, ".png")) return "image/png";
+ if(iequals(ext, ".jpe")) return "image/jpeg";
+ if(iequals(ext, ".jpeg")) return "image/jpeg";
+ if(iequals(ext, ".jpg")) return "image/jpeg";
+ if(iequals(ext, ".gif")) return "image/gif";
+ if(iequals(ext, ".bmp")) return "image/bmp";
+ if(iequals(ext, ".ico")) return "image/vnd.microsoft.icon";
+ if(iequals(ext, ".tiff")) return "image/tiff";
+ if(iequals(ext, ".tif")) return "image/tiff";
+ if(iequals(ext, ".svg")) return "image/svg+xml";
+ if(iequals(ext, ".svgz")) return "image/svg+xml";
+ return "application/text";
+}
+
} // anonymous namespace
-std::string generate_response(request_type& req, response_type& res, Server& server)
+response_type generate_response(request_type& req, Server& server)
{
+ response_type res{http::status::ok, req.version()};
+ res.set(http::field::server, VersionString);
+ res.set(http::field::content_type, mime_type(extend_index_html(std::string(req.target()))));
+ res.keep_alive(req.keep_alive());
+
std::string host{req["host"]};
std::string target{req.target()};
std::string plugin_name { server.GetConfig().GetPlugin(server.GetSocket(), host, target)};
@@ -86,6 +127,14 @@ std::string generate_response(request_type& req, response_type& res, Server& ser
auto GetRequestParamFunction {std::function<std::string(const std::string& key)>(std::bind(GetRequestParam, _1, std::ref(req), std::ref(server)))};
auto SetResponseHeaderFunction{std::function<void(const std::string& key, const std::string& value)>(std::bind(SetResponseHeader, _1, _2, std::ref(res)))};
- return plugin->generate_page(GetServerParamFunction, GetRequestParamFunction, SetResponseHeaderFunction);
+ std::string res_data { plugin->generate_page(GetServerParamFunction, GetRequestParamFunction, SetResponseHeaderFunction)};
+ if (req.method() == http::verb::head) {
+ res.content_length(res_data.size());
+ } else {
+ res.body() = res_data;
+ res.prepare_payload();
+ }
+
+ return res;
}