diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-04-10 15:36:59 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-04-10 15:36:59 +0200 |
commit | c0ccf16c69d43a89674640c61d13ec2c02b128d6 (patch) | |
tree | ae840bc16f0ddb430bdd68aacef4d7cb2af970d9 /http.cpp | |
parent | 0d157fb407a35f8afe6d6f0f4c2cc5cd5d5a1933 (diff) |
First working plugin: static-files
Diffstat (limited to 'http.cpp')
-rw-r--r-- | http.cpp | 68 |
1 files changed, 10 insertions, 58 deletions
@@ -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)); } //------------------------------------------------------------------------------ |