summaryrefslogtreecommitdiffhomepage
path: root/http.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-10 15:36:59 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-10 15:36:59 +0200
commitc0ccf16c69d43a89674640c61d13ec2c02b128d6 (patch)
treeae840bc16f0ddb430bdd68aacef4d7cb2af970d9 /http.cpp
parent0d157fb407a35f8afe6d6f0f4c2cc5cd5d5a1933 (diff)
First working plugin: static-files
Diffstat (limited to 'http.cpp')
-rw-r--r--http.cpp68
1 files changed, 10 insertions, 58 deletions
diff --git a/http.cpp b/http.cpp
index 714bcc4..18a8397 100644
--- a/http.cpp
+++ b/http.cpp
@@ -71,68 +71,20 @@ template<
class Send>
void
handle_request(
- Server& server,
+ ::Server& server,
http::request<Body, http::basic_fields<Allocator>>&& req,
Send&& send)
{
- // Returns a bad request response
- auto const bad_request =
- [&req](beast::string_view why)
- {
- http::response<http::string_body> res{http::status::bad_request, req.version()};
- res.set(http::field::server, VersionString);
- res.set(http::field::content_type, "text/html");
- res.keep_alive(req.keep_alive());
- res.body() = std::string(why);
- res.prepare_payload();
- return res;
- };
-
- // Returns a not found response
- auto const not_found =
- [&req](beast::string_view target)
- {
- http::response<http::string_body> res{http::status::not_found, req.version()};
- res.set(http::field::server, VersionString);
- res.set(http::field::content_type, "text/html");
- res.keep_alive(req.keep_alive());
- res.body() = "The resource '" + std::string(target) + "' was not found.";
- res.prepare_payload();
- return res;
- };
-
- // Returns a server error response
- auto const server_error =
- [&req](beast::string_view what)
- {
- http::response<http::string_body> res{http::status::internal_server_error, req.version()};
- res.set(http::field::server, VersionString);
- res.set(http::field::content_type, "text/html");
- res.keep_alive(req.keep_alive());
- res.body() = "An error occurred: '" + std::string(what) + "'";
- res.prepare_payload();
- return res;
- };
-
- try {
- 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.content_length(res_data.size());
- }
- return send(std::move(res));
- } catch(const bad_request_exception& ex) {
- return send(bad_request(ex.what()));
- } catch(const not_found_exception& ex) {
- return send(not_found(ex.what()));
- } catch(const server_error_exception& ex) {
- return send(server_error(ex.what()));
+ 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));
}
//------------------------------------------------------------------------------