Improve MIME-type when serving artefacts

This commit is contained in:
Martchus 2022-03-15 00:21:08 +01:00
parent 81440bdd4d
commit b0234ff02e
3 changed files with 32 additions and 17 deletions

View File

@ -122,12 +122,17 @@ static std::string readNameParam(const Params &params, ResponseHandler &&handler
return nameParams.front();
}
static void getBuildActionFile(const Params &params, std::vector<std::string> BuildAction::*fileList, ResponseHandler &&handler)
static void getBuildActionFile(
const Params &params, std::vector<std::string> BuildAction::*fileList, ResponseHandler &&handler, bool determineMimeType)
{
const auto name = readNameParam(params, std::move(handler));
if (name.empty()) {
return;
}
auto mimeType = std::string_view("application/octet-stream");
if (determineMimeType) {
mimeType = Session::determineMimeType(name, mimeType);
}
auto buildActionsSearchResult = findBuildActions(params, std::move(handler), false, 1);
if (!buildActionsSearchResult.ok) {
return;
@ -136,7 +141,7 @@ static void getBuildActionFile(const Params &params, std::vector<std::string> Bu
auto &buildAction = buildActionsSearchResult.actions.front();
for (const auto &logFile : (*buildAction).*fileList) {
if (name == logFile) {
buildAction->streamFile(params, name, "application/octet-stream");
buildAction->streamFile(params, name, mimeType);
return;
}
}
@ -145,12 +150,12 @@ static void getBuildActionFile(const Params &params, std::vector<std::string> Bu
void getBuildActionLogFile(const Params &params, ResponseHandler &&handler)
{
getBuildActionFile(params, &BuildAction::logfiles, std::move(handler));
getBuildActionFile(params, &BuildAction::logfiles, std::move(handler), false);
}
void getBuildActionArtefact(const Params &params, ResponseHandler &&handler)
{
getBuildActionFile(params, &BuildAction::artefacts, std::move(handler));
getBuildActionFile(params, &BuildAction::artefacts, std::move(handler), true);
}
void postBuildAction(const Params &params, ResponseHandler &&handler)

View File

@ -102,19 +102,7 @@ void Session::received(boost::system::error_code ec, size_t bytesTransferred)
// handle requests to static files (intended for development only; use NGINX in production)
if (!m_setup.webServer.staticFilesPath.empty() && (path.find("../") == string::npos || path.find("..\\") == string::npos)) {
const auto filePath = argsToString(m_setup.webServer.staticFilesPath, params.target.path);
const auto *mimeType = "text/plain";
if (endsWith(params.target.path, ".html")) {
mimeType = "text/html";
} else if (endsWith(params.target.path, ".json")) {
mimeType = "application/json";
} else if (endsWith(params.target.path, ".js")) {
mimeType = "text/javascript";
} else if (endsWith(params.target.path, ".css")) {
mimeType = "text/css";
} else if (endsWith(params.target.path, ".svg")) {
mimeType = "image/svg+xml";
}
respond(filePath.data(), mimeType, params.target.path);
respond(filePath.data(), determineMimeType(params.target.path).data(), params.target.path);
return;
}
@ -179,6 +167,27 @@ void Session::responded(boost::system::error_code ec, std::size_t bytesTransferr
receive();
}
std::string_view Session::determineMimeType(std::string_view path, std::string_view fallback)
{
if (path.ends_with(".html")) {
return "text/html";
} else if (path.ends_with(".json")) {
return "application/json";
} else if (path.ends_with(".js")) {
return "text/javascript";
} else if (path.ends_with(".css")) {
return "text/css";
} else if (path.ends_with(".svg")) {
return "image/svg+xml";
} else if (path.ends_with(".txt") || path.ends_with(".log") || path.ends_with("PKGBUILD")) {
return "text/plain";
} else if (path.ends_with(".md")) {
return "text/plain"; // using text/markdown leads to download prompt in Firefox
} else {
return fallback;
}
}
void Session::close()
{
// send a TCP shutdown

View File

@ -29,6 +29,7 @@ public:
boost::asio::ip::tcp::socket &socket();
void received(boost::system::error_code ec, std::size_t bytesTransferred);
void responded(boost::system::error_code ec, std::size_t bytesTransferred, bool shouldClose);
static std::string_view determineMimeType(std::string_view path, std::string_view fallback = "text/plain");
private:
boost::asio::ip::tcp::socket m_socket;