bmcweb: Fix issues with /dump endpoint
Technically, if multiple dump files existed, we could get a double end()
called loading each file. Avoid that by returning.
Tested By:
Downloading a file from dump, using the /dump url. File downloads
correctly.
Change-Id: Icd71a2fbbe365557daa1a7e24c5563f3749a748d
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp
index fe85d65..4c68fac 100644
--- a/include/openbmc_dbus_rest.hpp
+++ b/include/openbmc_dbus_rest.hpp
@@ -1509,30 +1509,32 @@
BMCWEB_ROUTE(app, "/download/dump/<str>/")
.methods("GET"_method)([](const crow::Request &req, crow::Response &res,
const std::string &dumpId) {
- std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]+)$");
+ std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]*)$");
if (!std::regex_match(dumpId, validFilename))
{
- res.result(boost::beast::http::status::not_found);
+ res.result(boost::beast::http::status::bad_request);
res.end();
return;
}
std::experimental::filesystem::path loc(
"/var/lib/phosphor-debug-collector/dumps");
- loc += dumpId;
+ loc /= dumpId;
if (!std::experimental::filesystem::exists(loc) ||
!std::experimental::filesystem::is_directory(loc))
{
+ BMCWEB_LOG_ERROR << loc << "Not found";
res.result(boost::beast::http::status::not_found);
res.end();
return;
}
std::experimental::filesystem::directory_iterator files(loc);
+
for (auto &file : files)
{
std::ifstream readFile(file.path());
- if (readFile.good())
+ if (!readFile.good())
{
continue;
}
@@ -1540,6 +1542,7 @@
res.body() = {std::istreambuf_iterator<char>(readFile),
std::istreambuf_iterator<char>()};
res.end();
+ return;
}
res.result(boost::beast::http::status::not_found);
res.end();