Use enum overload for field setting
There are two overloads of addHeader, one that takes a string, and one
that takes a boost enum. For most common headers, boost contains a
string table with all of those entries anyway, so there's no point in
duplicating the strings, and ensures that we don't make trivial
mistakes, like capitalization or - versus underscore that aren't caught
at compile time.
Tested:
This saves a trivial amount (572 bytes) of compressed binary size.
curl --insecure -vvv --user root:0penBmc https://192.168.7.2/redfish/v1
returns < Content-Type: application/json
curl --insecure -vvv -H "Accept: text/html" --user root:0penBmc https://192.168.7.2/redfish/v1
Returns
< Content-Type: text/html;charset=UTF-8
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I34c198b4f9e219247fcfe719f9b3616d35aea3dc
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index d061973..6494586 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -35,7 +35,8 @@
{
json_html_util::dumpHtml(res.body(), res.jsonValue);
- res.addHeader("Content-Type", "text/html;charset=UTF-8");
+ res.addHeader(boost::beast::http::field::content_type,
+ "text/html;charset=UTF-8");
}
static int connectionCount = 0;
diff --git a/include/forward_unauthorized.hpp b/include/forward_unauthorized.hpp
index ddf3e3b..a48b775 100644
--- a/include/forward_unauthorized.hpp
+++ b/include/forward_unauthorized.hpp
@@ -20,7 +20,7 @@
if (hasWebuiRoute)
{
res.result(boost::beast::http::status::temporary_redirect);
- res.addHeader("Location",
+ res.addHeader(boost::beast::http::field::location,
"/#/login?next=" + http_helpers::urlEncode(url));
return;
}
@@ -46,6 +46,6 @@
{
return;
}
- res.addHeader("WWW-Authenticate", "Basic");
+ res.addHeader(boost::beast::http::field::www_authenticate, "Basic");
}
} // namespace forward_unauthorized
diff --git a/include/ibm/management_console_rest.hpp b/include/ibm/management_console_rest.hpp
index 0d8e287..7396ce6 100644
--- a/include/ibm/management_console_rest.hpp
+++ b/include/ibm/management_console_rest.hpp
@@ -321,7 +321,8 @@
std::string contentDispositionParam =
"attachment; filename=\"" + fileID + "\"";
- asyncResp->res.addHeader("Content-Disposition", contentDispositionParam);
+ asyncResp->res.addHeader(boost::beast::http::field::content_disposition,
+ contentDispositionParam);
std::string fileData;
fileData = {std::istreambuf_iterator<char>(readfile),
std::istreambuf_iterator<char>()};
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp
index 9c8d9f5..f458fac 100644
--- a/include/openbmc_dbus_rest.hpp
+++ b/include/openbmc_dbus_rest.hpp
@@ -2547,7 +2547,7 @@
continue;
}
- asyncResp->res.addHeader("Content-Type",
+ asyncResp->res.addHeader(boost::beast::http::field::content_type,
"application/octet-stream");
// Assuming only one dump file will be present in the dump
@@ -2567,8 +2567,9 @@
std::string contentDispositionParam =
"attachment; filename=\"" + dumpFileName + "\"";
- asyncResp->res.addHeader("Content-Disposition",
- contentDispositionParam);
+ asyncResp->res.addHeader(
+ boost::beast::http::field::content_disposition,
+ contentDispositionParam);
asyncResp->res.body() = {std::istreambuf_iterator<char>(readFile),
std::istreambuf_iterator<char>()};
diff --git a/include/webassets.hpp b/include/webassets.hpp
index 3d25296..cd57eb3 100644
--- a/include/webassets.hpp
+++ b/include/webassets.hpp
@@ -152,13 +152,15 @@
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
if (contentType != nullptr)
{
- asyncResp->res.addHeader("Content-Type", contentType);
+ asyncResp->res.addHeader(
+ boost::beast::http::field::content_type, contentType);
}
if (contentEncoding != nullptr)
{
- asyncResp->res.addHeader("Content-Encoding",
- contentEncoding);
+ asyncResp->res.addHeader(
+ boost::beast::http::field::content_encoding,
+ contentEncoding);
}
// res.set_header("Cache-Control", "public, max-age=86400");
diff --git a/redfish-core/lib/certificate_service.hpp b/redfish-core/lib/certificate_service.hpp
index 9e958cb..fc347dc 100644
--- a/redfish-core/lib/certificate_service.hpp
+++ b/redfish-core/lib/certificate_service.hpp
@@ -1,8 +1,10 @@
#pragma once
#include <app.hpp>
+#include <async_resp.hpp>
#include <boost/system/linux_error.hpp>
#include <dbus_utility.hpp>
+#include <http_response.hpp>
#include <query.hpp>
#include <registries/privilege_registry.hpp>
@@ -702,7 +704,8 @@
}
}
asyncResp->res.addHeader(
- "Location", std::string_view(certURL.data(), certURL.size()));
+ boost::beast::http::field::location,
+ std::string_view(certURL.data(), certURL.size()));
},
service, objectPath, certs::dbusPropIntf, "GetAll",
certs::certPropIntf);
diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index 7740014..34e9f73 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -1747,9 +1747,10 @@
std::string_view strData(data.data(), data.size());
std::string output = crow::utility::base64encode(strData);
- asyncResp->res.addHeader("Content-Type",
+ asyncResp->res.addHeader(boost::beast::http::field::content_type,
"application/octet-stream");
- asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
+ asyncResp->res.addHeader(
+ boost::beast::http::field::content_transfer_encoding, "Base64");
asyncResp->res.body() = std::move(output);
},
"xyz.openbmc_project.Logging",
@@ -2971,7 +2972,8 @@
// Configure this to be a file download when accessed
// from a browser
- asyncResp->res.addHeader("Content-Disposition", "attachment");
+ asyncResp->res.addHeader(
+ boost::beast::http::field::content_disposition, "attachment");
};
crow::connections::systemBus->async_method_call(
std::move(getStoredLogCallback), crashdumpObject,
@@ -3620,9 +3622,10 @@
const char* d = reinterpret_cast<const char*>(c.data());
std::string_view strData(d, c.size());
- asyncResp->res.addHeader("Content-Type",
+ asyncResp->res.addHeader(boost::beast::http::field::content_type,
"application/octet-stream");
- asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
+ asyncResp->res.addHeader(
+ boost::beast::http::field::content_transfer_encoding, "Base64");
asyncResp->res.body() = crow::utility::base64encode(strData);
},
"xyz.openbmc_project.State.Boot.PostCode0",
diff --git a/redfish-core/src/error_messages.cpp b/redfish-core/src/error_messages.cpp
index c688fdc..b3cd257 100644
--- a/redfish-core/src/error_messages.cpp
+++ b/redfish-core/src/error_messages.cpp
@@ -359,7 +359,7 @@
void serviceTemporarilyUnavailable(crow::Response& res, std::string_view arg1)
{
- res.addHeader("Retry-After", arg1);
+ res.addHeader(boost::beast::http::field::retry_after, arg1);
res.result(boost::beast::http::status::service_unavailable);
addMessageToErrorJson(res.jsonValue, serviceTemporarilyUnavailable(arg1));
}