generated from Sithas/conan_template
TASK00 - Авторизация и регистрация
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/beast.hpp>
|
||||
|
||||
namespace uad {
|
||||
#include "./../helpers/helpers.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
template <class Body, class Allocator, class Send>
|
||||
void HandleRequest(boost::beast::string_view doc_root,
|
||||
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>> &&req,
|
||||
Send &&send) {
|
||||
auto const bad_request = [&req](beast::string_view why) {
|
||||
boost::beast::http::response<boost::beast::http::string_body> res{boost::beast::http::status::bad_request,
|
||||
req.version()};
|
||||
void HandleRequest(
|
||||
boost::beast::string_view doc_root,
|
||||
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req, Send&& send)
|
||||
{
|
||||
auto const bad_request = [&req](boost::beast::string_view why)
|
||||
{
|
||||
boost::beast::http::response<boost::beast::http::string_body> res{
|
||||
boost::beast::http::status::bad_request, req.version()};
|
||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(boost::beast::http::field::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
@@ -16,9 +23,10 @@ void HandleRequest(boost::beast::string_view doc_root,
|
||||
return res;
|
||||
};
|
||||
|
||||
auto const not_found = [&req](beast::string_view target) {
|
||||
boost::beast::http::response<boost::beast::http::string_body> res{boost::beast::http::status::not_found,
|
||||
req.version()};
|
||||
auto const not_found = [&req](boost::beast::string_view target)
|
||||
{
|
||||
boost::beast::http::response<boost::beast::http::string_body> res{
|
||||
boost::beast::http::status::not_found, req.version()};
|
||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(boost::beast::http::field::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
@@ -27,9 +35,10 @@ void HandleRequest(boost::beast::string_view doc_root,
|
||||
return res;
|
||||
};
|
||||
|
||||
auto const server_error = [&req](beast::string_view what) {
|
||||
boost::beast::http::response<boost::beast::http::string_body> res{boost::beast::http::status::internal_server_error,
|
||||
req.version()};
|
||||
auto const server_error = [&req](boost::beast::string_view what)
|
||||
{
|
||||
boost::beast::http::response<boost::beast::http::string_body> res{
|
||||
boost::beast::http::status::internal_server_error, req.version()};
|
||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(boost::beast::http::field::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
@@ -38,22 +47,23 @@ void HandleRequest(boost::beast::string_view doc_root,
|
||||
return res;
|
||||
};
|
||||
|
||||
if (req.method() != boost::beast::http::verb::get && req.method() != boost::beast::http::verb::head)
|
||||
if (req.method() != boost::beast::http::verb::get &&
|
||||
req.method() != boost::beast::http::verb::head)
|
||||
return send(bad_request("Unknown boost::beast::HTTP-method"));
|
||||
|
||||
if (req.target().empty() || req.target()[0] != '/' ||
|
||||
req.target().find("..") != beast::string_view::npos)
|
||||
req.target().find("..") != boost::beast::string_view::npos)
|
||||
return send(bad_request("Illegal request-target"));
|
||||
|
||||
std::string path = PathCat(doc_root, req.target());
|
||||
if (req.target().back() == '/')
|
||||
path.append("index.html");
|
||||
|
||||
beast::error_code ec;
|
||||
boost::beast::error_code ec;
|
||||
boost::beast::http::file_body::value_type body;
|
||||
body.open(path.c_str(), beast::file_mode::scan, ec);
|
||||
body.open(path.c_str(), boost::beast::file_mode::scan, ec);
|
||||
|
||||
if (ec == beast::errc::no_such_file_or_directory)
|
||||
if (ec == boost::beast::errc::no_such_file_or_directory)
|
||||
return send(not_found(req.target()));
|
||||
|
||||
if (ec)
|
||||
@@ -61,8 +71,10 @@ void HandleRequest(boost::beast::string_view doc_root,
|
||||
|
||||
auto const size = body.size();
|
||||
|
||||
if (req.method() == boost::beast::http::verb::head) {
|
||||
boost::beast::http::response<boost::beast::http::empty_body> res{boost::beast::http::status::ok, req.version()};
|
||||
if (req.method() == boost::beast::http::verb::head)
|
||||
{
|
||||
boost::beast::http::response<boost::beast::http::empty_body> res{boost::beast::http::status::ok,
|
||||
req.version()};
|
||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(boost::beast::http::field::content_type, MimeType(path));
|
||||
res.content_length(size);
|
||||
@@ -71,12 +83,12 @@ void HandleRequest(boost::beast::string_view doc_root,
|
||||
}
|
||||
|
||||
boost::beast::http::response<boost::beast::http::file_body> res{
|
||||
std::piecewise_construct, std::make_tuple(std::move(body)),
|
||||
std::make_tuple(boost::beast::http::status::ok, req.version())};
|
||||
std::piecewise_construct, std::make_tuple(std::move(body)),
|
||||
std::make_tuple(boost::beast::http::status::ok, req.version())};
|
||||
res.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(boost::beast::http::field::content_type, MimeType(path));
|
||||
res.content_length(size);
|
||||
res.keep_alive(req.keep_alive());
|
||||
return send(std::move(res));
|
||||
}
|
||||
}
|
||||
} // namespace uad
|
||||
|
||||
Reference in New Issue
Block a user