summaryrefslogtreecommitdiffhomepage
path: root/http.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'http.cpp')
-rw-r--r--http.cpp55
1 files changed, 4 insertions, 51 deletions
diff --git a/http.cpp b/http.cpp
index 18a8397..edf935b 100644
--- a/http.cpp
+++ b/http.cpp
@@ -26,65 +26,18 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace {
-// 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";
-}
-
// This function produces an HTTP response for the given
// request. The type of the response object depends on the
// contents of the request, so the interface requires the
// caller to pass a generic lambda for receiving the response.
-template<
- class Body, class Allocator,
- class Send>
+template<class Send>
void
handle_request(
::Server& server,
- http::request<Body, http::basic_fields<Allocator>>&& req,
+ request_type&& req,
Send&& send)
{
- http::response<http::string_body> 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 res_data = generate_response(req, res, server);
- if (req.method() != http::verb::head) {
- res.body() = res_data;
- res.prepare_payload();
- }
- return send(std::move(res));
+ return send(std::move(generate_response(req, server)));
}
//------------------------------------------------------------------------------
@@ -200,7 +153,7 @@ public:
return fail(ec, "read");
// Send the response
- handle_request(m_server , std::move(req_), lambda_);
+ handle_request(m_server, std::move(req_), lambda_);
}
void