Files
App/src/util/http_util.cpp
2026-06-13 13:35:00 +07:00

42 lines
1.2 KiB
C++

#include "util/http_util.hpp"
#include "util/string_util.hpp"
namespace lm {
std::string HttpUtil::contentTypeForPath(const std::filesystem::path& p)
{
const auto ext = StringUtil::toLower(p.extension().string());
if (ext == ".html")
return "text/html; charset=utf-8";
if (ext == ".css")
return "text/css; charset=utf-8";
if (ext == ".js")
return "application/javascript; charset=utf-8";
if (ext == ".json")
return "application/json; charset=utf-8";
if (ext == ".png")
return "image/png";
if (ext == ".svg")
return "image/svg+xml";
if (ext == ".ico")
return "image/x-icon";
return "application/octet-stream";
}
void HttpUtil::jsonError(httplib::Response& res, int status, const std::string& msg)
{
res.status = status;
res.set_header("Content-Type", "application/json; charset=utf-8");
res.body = nlohmann::json({{"error", msg}}).dump();
}
void HttpUtil::addCors(httplib::Response& res)
{
res.set_header("Access-Control-Allow-Origin", "*");
res.set_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.set_header("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept-Language");
}
} // namespace lm