summaryrefslogtreecommitdiffhomepage
path: root/http.cpp
diff options
context:
space:
mode:
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));
}
//------------------------------------------------------------------------------