diff --git a/librepomgr/webapi/routes_buildaction.cpp b/librepomgr/webapi/routes_buildaction.cpp index 5af8a52..c12e1b2 100644 --- a/librepomgr/webapi/routes_buildaction.cpp +++ b/librepomgr/webapi/routes_buildaction.cpp @@ -122,12 +122,17 @@ static std::string readNameParam(const Params ¶ms, ResponseHandler &&handler return nameParams.front(); } -static void getBuildActionFile(const Params ¶ms, std::vector BuildAction::*fileList, ResponseHandler &&handler) +static void getBuildActionFile( + const Params ¶ms, std::vector 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 ¶ms, std::vector 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 ¶ms, std::vector Bu void getBuildActionLogFile(const Params ¶ms, ResponseHandler &&handler) { - getBuildActionFile(params, &BuildAction::logfiles, std::move(handler)); + getBuildActionFile(params, &BuildAction::logfiles, std::move(handler), false); } void getBuildActionArtefact(const Params ¶ms, ResponseHandler &&handler) { - getBuildActionFile(params, &BuildAction::artefacts, std::move(handler)); + getBuildActionFile(params, &BuildAction::artefacts, std::move(handler), true); } void postBuildAction(const Params ¶ms, ResponseHandler &&handler) diff --git a/librepomgr/webapi/session.cpp b/librepomgr/webapi/session.cpp index fba0dac..843a181 100644 --- a/librepomgr/webapi/session.cpp +++ b/librepomgr/webapi/session.cpp @@ -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 diff --git a/librepomgr/webapi/session.h b/librepomgr/webapi/session.h index 1685653..27dbcdb 100644 --- a/librepomgr/webapi/session.h +++ b/librepomgr/webapi/session.h @@ -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;