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(); 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)); const auto name = readNameParam(params, std::move(handler));
if (name.empty()) { if (name.empty()) {
return; 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); auto buildActionsSearchResult = findBuildActions(params, std::move(handler), false, 1);
if (!buildActionsSearchResult.ok) { if (!buildActionsSearchResult.ok) {
return; return;
@ -136,7 +141,7 @@ static void getBuildActionFile(const Params &params, std::vector<std::string> Bu
auto &buildAction = buildActionsSearchResult.actions.front(); auto &buildAction = buildActionsSearchResult.actions.front();
for (const auto &logFile : (*buildAction).*fileList) { for (const auto &logFile : (*buildAction).*fileList) {
if (name == logFile) { if (name == logFile) {
buildAction->streamFile(params, name, "application/octet-stream"); buildAction->streamFile(params, name, mimeType);
return; return;
} }
} }
@ -145,12 +150,12 @@ static void getBuildActionFile(const Params &params, std::vector<std::string> Bu
void getBuildActionLogFile(const Params &params, ResponseHandler &&handler) 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) 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) 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) // 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)) { 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 filePath = argsToString(m_setup.webServer.staticFilesPath, params.target.path);
const auto *mimeType = "text/plain"; respond(filePath.data(), determineMimeType(params.target.path).data(), params.target.path);
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);
return; return;
} }
@ -179,6 +167,27 @@ void Session::responded(boost::system::error_code ec, std::size_t bytesTransferr
receive(); 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() void Session::close()
{ {
// send a TCP shutdown // send a TCP shutdown

View File

@ -29,6 +29,7 @@ public:
boost::asio::ip::tcp::socket &socket(); boost::asio::ip::tcp::socket &socket();
void received(boost::system::error_code ec, std::size_t bytesTransferred); void received(boost::system::error_code ec, std::size_t bytesTransferred);
void responded(boost::system::error_code ec, std::size_t bytesTransferred, bool shouldClose); 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: private:
boost::asio::ip::tcp::socket m_socket; boost::asio::ip::tcp::socket m_socket;