add file support

This commit is contained in:
Антон 2024-03-26 20:03:56 +03:00
parent f15b77ac61
commit 7ab65a520f
1 changed files with 16 additions and 5 deletions

View File

@ -5,6 +5,7 @@
#include <string>
namespace fs = std::filesystem;
namespace beast = boost::beast;
using namespace std;
namespace http_server
@ -13,13 +14,23 @@ FileResponse GetStaticAssetHandler(const StringRequest& req)
{
string rel_path_str {req.target().begin(), req.target().end()};
fs::path base_path("./public/"s);
fs::path rel_path(rel_path_str);
fs::path rel_path("."s + rel_path_str);
fs::path abs_path = fs::weakly_canonical(base_path / rel_path);
FileResponse res(http::status::ok, 1);
res.set(http::field::content_type, content_type::k_JSON);
res.content_length(0);
res.keep_alive(true);
http::file_body::value_type file;
FileResponse res;
res.version(11);
if (sys::error_code ec; file.open(abs_path.string().c_str(), beast::file_mode::read, ec), ec)
{
res.result(http::status::not_found);
}
else
{
res.body() = std::move(file);
res.prepare_payload();
}
return res;
}