From c0ccf16c69d43a89674640c61d13ec2c02b128d6 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 10 Apr 2020 15:36:59 +0200 Subject: First working plugin: static-files --- https.cpp | 65 +++++++++------------------------------------------------------ 1 file changed, 9 insertions(+), 56 deletions(-) (limited to 'https.cpp') diff --git a/https.cpp b/https.cpp index f312295..d410bb3 100644 --- a/https.cpp +++ b/https.cpp @@ -88,63 +88,16 @@ handle_request( http::request>&& req, Send&& send) { - // Returns a bad request response - auto const bad_request = - [&req](beast::string_view why) - { - http::response 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 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 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 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 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)); } //------------------------------------------------------------------------------ -- cgit v1.2.3