Older/Server/ServiceLogic.cpp

33 lines
1.2 KiB
C++
Raw Normal View History

2023-07-21 16:17:01 +08:00
#include "ServiceLogic.h"
#include <sstream>
namespace ServiceLogic {
2023-12-31 01:13:26 +08:00
using namespace boost::beast;
2023-07-21 16:17:01 +08:00
boost::beast::http::response<boost::beast::http::string_body>
2023-12-31 01:13:26 +08:00
serverError(const boost::beast::http::request<boost::beast::http::string_body> &request,
std::string_view errorMessage) {
2023-07-21 16:17:01 +08:00
using namespace boost::beast;
2023-12-31 01:13:26 +08:00
http::response<http::string_body> res{http::status::internal_server_error, request.version()};
2023-07-21 16:17:01 +08:00
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(request.keep_alive());
std::ostringstream oss;
2023-12-31 01:13:26 +08:00
oss << "An error occurred: '" << errorMessage << "'";
2023-07-21 16:17:01 +08:00
res.body() = oss.str();
res.prepare_payload();
return res;
}
2023-12-31 01:13:26 +08:00
http::response<http::string_body> badRequest(const http::request<http::string_body> &request, std::string_view why) {
http::response<http::string_body> res{http::status::bad_request, request.version()};
2023-07-21 16:17:01 +08:00
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(request.keep_alive());
2023-12-31 01:13:26 +08:00
res.body() = std::string(why);
2023-07-21 16:17:01 +08:00
res.prepare_payload();
return res;
}
2023-12-31 01:13:26 +08:00
2023-07-21 16:17:01 +08:00
} // namespace ServiceLogic