diff options
author | Ed Tanous <ed.tanous@intel.com> | 2019-03-26 19:17:55 +0300 |
---|---|---|
committer | Ed Tanous <ed.tanous@intel.com> | 2019-04-01 20:01:55 +0300 |
commit | de5c9f3cf9637729d20318839f3c7d3ac51139fd (patch) | |
tree | 95656f2a1b11ba146c0d2acc7e972ac78dc6330b | |
parent | f00032db72e7ee88946bb4839b5d5311a3e092b3 (diff) | |
download | bmcweb-de5c9f3cf9637729d20318839f3c7d3ac51139fd.tar.xz |
bmcweb: avoid move in error conditions
Moving a response container is a bit non-sensical, and a little wasteful
when we're handling errors. This commit moves to simply handling it
explicitly using the existing structures.
Change-Id: Idacf633741363945b57194730bd7c3427a23b38d
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
-rw-r--r-- | crow/include/crow/http_connection.h | 2 | ||||
-rw-r--r-- | crow/include/crow/http_response.h | 17 | ||||
-rw-r--r-- | crow/include/crow/routing.h | 36 |
3 files changed, 19 insertions, 36 deletions
diff --git a/crow/include/crow/http_connection.h b/crow/include/crow/http_connection.h index ba2bcdd98c..6f44186962 100644 --- a/crow/include/crow/http_connection.h +++ b/crow/include/crow/http_connection.h @@ -323,7 +323,7 @@ class Connection if (req->getHeaderValue(boost::beast::http::field::host).empty()) { isInvalidRequest = true; - res = Response(boost::beast::http::status::bad_request); + res.result(boost::beast::http::status::bad_request); } } diff --git a/crow/include/crow/http_response.h b/crow/include/crow/http_response.h index 093bd90423..640197f1e7 100644 --- a/crow/include/crow/http_response.h +++ b/crow/include/crow/http_response.h @@ -38,23 +38,6 @@ struct Response { } - explicit Response(boost::beast::http::status code) : - stringResponse(response_type{}) - { - } - - explicit Response(std::string_view body_) : stringResponse(response_type{}) - { - stringResponse->body() = std::string(body_); - } - - Response(boost::beast::http::status code, std::string_view s) : - stringResponse(response_type{}) - { - stringResponse->result(code); - stringResponse->body() = std::string(s); - } - Response(Response&& r) { BMCWEB_LOG_DEBUG << "Moving response containers"; diff --git a/crow/include/crow/routing.h b/crow/include/crow/routing.h index e657e2ef1b..7d7a1b6ea5 100644 --- a/crow/include/crow/routing.h +++ b/crow/include/crow/routing.h @@ -50,7 +50,7 @@ class BaseRule virtual void handleUpgrade(const Request&, Response& res, boost::asio::ip::tcp::socket&&) { - res = Response(boost::beast::http::status::not_found); + res.result(boost::beast::http::status::not_found); res.end(); } #ifdef BMCWEB_ENABLE_SSL @@ -58,7 +58,7 @@ class BaseRule handleUpgrade(const Request&, Response& res, boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&&) { - res = Response(boost::beast::http::status::not_found); + res.result(boost::beast::http::status::not_found); res.end(); } #endif @@ -206,7 +206,7 @@ template <typename Func, typename... ArgsWrapped> struct Wrapped { handler = [f = std::move(f)](const Request&, Response& res, Args... args) { - res = Response(f(args...)); + res.result(f(args...)); res.end(); }; } @@ -219,7 +219,7 @@ template <typename Func, typename... ArgsWrapped> struct Wrapped void operator()(const Request& req, Response& res, Args... args) { - res = Response(f(req, args...)); + res.result(f(req, args...)); res.end(); } @@ -242,7 +242,7 @@ template <typename Func, typename... ArgsWrapped> struct Wrapped /*handler = ( [f = std::move(f)] (const Request& req, Response& res, Args... args){ - res = Response(f(req, args...)); + res.result(f(req, args...)); res.end(); });*/ } @@ -319,7 +319,7 @@ class WebSocketRule : public BaseRule void handle(const Request&, Response& res, const RoutingParams&) override { - res = Response(boost::beast::http::status::not_found); + res.result(boost::beast::http::status::not_found); res.end(); } @@ -533,7 +533,7 @@ class TaggedRule : public BaseRule, handler = [f = std::move(f)](const Request&, Response& res, Args... args) { - res = Response(f(args...)); + res.result(f(args...)); res.end(); }; } @@ -560,7 +560,7 @@ class TaggedRule : public BaseRule, handler = [f = std::move(f)](const crow::Request& req, crow::Response& res, Args... args) { - res = Response(f(req, args...)); + res.result(f(req, args...)); res.end(); }; } @@ -1085,7 +1085,7 @@ class Router if (!ruleIndex) { BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url; - res = Response(boost::beast::http::status::not_found); + res.result(boost::beast::http::status::not_found); res.end(); return; } @@ -1097,7 +1097,7 @@ class Router { BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url; - res = Response(boost::beast::http::status::moved_permanently); + res.result(boost::beast::http::status::moved_permanently); // TODO absolute url building if (req.getHeaderValue("Host").empty()) @@ -1124,7 +1124,7 @@ class Router << " with " << req.methodString() << "(" << (uint32_t)req.method() << ") / " << rules[ruleIndex]->getMethods(); - res = Response(boost::beast::http::status::not_found); + res.result(boost::beast::http::status::not_found); res.end(); return; } @@ -1141,7 +1141,7 @@ class Router catch (std::exception& e) { BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what(); - res = Response(boost::beast::http::status::internal_server_error); + res.result(boost::beast::http::status::internal_server_error); res.end(); return; } @@ -1150,7 +1150,7 @@ class Router BMCWEB_LOG_ERROR << "An uncaught exception occurred. The type was unknown " "so no information was available."; - res = Response(boost::beast::http::status::internal_server_error); + res.result(boost::beast::http::status::internal_server_error); res.end(); return; } @@ -1183,7 +1183,7 @@ class Router { BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url; - res = Response(boost::beast::http::status::moved_permanently); + res.result(boost::beast::http::status::moved_permanently); // TODO absolute url building if (req.getHeaderValue("Host").empty()) @@ -1208,7 +1208,7 @@ class Router << " with " << req.methodString() << "(" << (uint32_t)req.method() << ") / " << rules[ruleIndex]->getMethods(); - res = Response(boost::beast::http::status::not_found); + res.result(boost::beast::http::status::method_not_allowed); res.end(); return; } @@ -1225,7 +1225,7 @@ class Router if (!rules[ruleIndex]->checkPrivileges(userPrivileges)) { - res.result(boost::beast::http::status::method_not_allowed); + res.result(boost::beast::http::status::unauthorized); res.end(); return; } @@ -1238,7 +1238,7 @@ class Router catch (std::exception& e) { BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what(); - res = Response(boost::beast::http::status::internal_server_error); + res.result(boost::beast::http::status::internal_server_error); res.end(); return; } @@ -1247,7 +1247,7 @@ class Router BMCWEB_LOG_ERROR << "An uncaught exception occurred. The type was unknown " "so no information was available."; - res = Response(boost::beast::http::status::internal_server_error); + res.result(boost::beast::http::status::internal_server_error); res.end(); return; } |