add file support
This commit is contained in:
parent
f15b77ac61
commit
7ab65a520f
|
@ -5,6 +5,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
namespace beast = boost::beast;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace http_server
|
namespace http_server
|
||||||
|
@ -13,13 +14,23 @@ FileResponse GetStaticAssetHandler(const StringRequest& req)
|
||||||
{
|
{
|
||||||
string rel_path_str {req.target().begin(), req.target().end()};
|
string rel_path_str {req.target().begin(), req.target().end()};
|
||||||
fs::path base_path("./public/"s);
|
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);
|
fs::path abs_path = fs::weakly_canonical(base_path / rel_path);
|
||||||
|
|
||||||
FileResponse res(http::status::ok, 1);
|
http::file_body::value_type file;
|
||||||
res.set(http::field::content_type, content_type::k_JSON);
|
|
||||||
res.content_length(0);
|
FileResponse res;
|
||||||
res.keep_alive(true);
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue