diff options
| author | Ed Tanous <ed.tanous@intel.com> | 2018-04-27 10:25:29 -0700 |
|---|---|---|
| committer | Ed Tanous <ed.tanous@intel.com> | 2018-06-29 18:18:45 +0000 |
| commit | a4e18f2a2b1652b378bf2e182a13595e05e66881 (patch) | |
| tree | 7f8b8185a8d0b561a9377e200b157978d6cc9ac6 /include | |
| parent | 9bd21fc16817ab1a47e9cd4ac6baf8f5b1d4ba63 (diff) | |
| download | bmcweb-a4e18f2a2b1652b378bf2e182a13595e05e66881.tar.gz bmcweb-a4e18f2a2b1652b378bf2e182a13595e05e66881.zip | |
Fix issue with dbus interfaces and compatibility
Change-Id: I3fb40defd978e87aad8c7294d5aab1aa72561fa2
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
Diffstat (limited to 'include')
| -rw-r--r-- | include/openbmc_dbus_rest.hpp | 85 |
1 files changed, 53 insertions, 32 deletions
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp index e6052f1..52b751b 100644 --- a/include/openbmc_dbus_rest.hpp +++ b/include/openbmc_dbus_rest.hpp @@ -85,9 +85,10 @@ void get_managed_objects_for_enumerate( for (auto &object_path : objects) { CROW_LOG_DEBUG << "Reading object " - << static_cast<const std::string&>(object_path.first); + << static_cast<const std::string &>( + object_path.first); nlohmann::json &object_json = - data_json[static_cast<const std::string&>(object_path.first)]; + data_json[static_cast<const std::string &>(object_path.first)]; if (object_json.is_null()) { object_json = nlohmann::json::object(); } @@ -220,36 +221,43 @@ void request_routes(Crow<Middlewares...> &app) { std::make_shared<nlohmann::json>(nlohmann::json::object()); using GetObjectType = std::vector<std::pair<std::string, std::vector<std::string>>>; - std::string object_path; + std::string object_path = "/xyz/" + path; + std::string dest_property; std::string property_set_value; - size_t attr_position = path.find("/attr/"); - if (attr_position == path.npos) { - object_path = "/xyz/" + path; - } else { + constexpr char *attr_seperator = "/attr/"; + size_t attr_position = path.find(attr_seperator); + if (attr_position != path.npos) { object_path = "/xyz/" + path.substr(0, attr_position); - dest_property = - path.substr((attr_position + strlen("/attr/")), path.length()); - auto request_dbus_data = - nlohmann::json::parse(req.body, nullptr, false); - if (request_dbus_data.is_discarded()) { - res.result(boost::beast::http::status::unauthorized); - res.end(); - return; - } + dest_property = path.substr((attr_position + strlen(attr_seperator)), + path.length()); + // If this is attempting to write a property, parse the json in the + // request + if (req.method() == "PUT"_method) { + auto request_dbus_data = + nlohmann::json::parse(req.body, nullptr, false); + + if (request_dbus_data.is_discarded()) { + res.result(boost::beast::http::status::bad_request); + res.end(); + return; + } - auto property_value_it = request_dbus_data.find("data"); - if (property_value_it == request_dbus_data.end()) { - res.result(boost::beast::http::status::unauthorized); - res.end(); - return; - } + auto property_value_it = request_dbus_data.find("data"); + if (property_value_it == request_dbus_data.end()) { + res.result(boost::beast::http::status::bad_request); + res.end(); + return; + } - property_set_value = property_value_it->get<const std::string>(); - if (property_set_value.empty()) { - res.result(boost::beast::http::status::unauthorized); - res.end(); - return; + const std::string *ptr = + property_value_it->get_ptr<const std::string *>(); + if (ptr == nullptr) { + res.result(boost::beast::http::status::bad_request); + res.end(); + return; + } + property_set_value = *ptr; } } @@ -279,15 +287,28 @@ void request_routes(Crow<Middlewares...> &app) { if (req.method() == "GET"_method) { for (auto &interface : object_names[0].second) { crow::connections::system_bus->async_method_call( - [&res, transaction]( - const boost::system::error_code ec, - const std::vector<std::pair< - std::string, DbusRestVariantType>> &properties) { + [ + &res, transaction, + dest_property{std::move(dest_property)} + ](const boost::system::error_code ec, + const std::vector<std::pair< + std::string, DbusRestVariantType>> &properties) { if (ec) { CROW_LOG_ERROR << "Bad dbus request error: " << ec; } else { for (auto &property : properties) { - nlohmann::json &it = (*transaction)[property.first]; + nlohmann::json &it = (*transaction)["data"]; + // if we don't have a specific property, assume we + // are getting all properties + if (dest_property.empty()) { + nlohmann::json &it = it[property.first]; + } else { + // if the property doesn't match our single + // property, nothing to do here + if (dest_property != property.first) { + continue; + } + } mapbox::util::apply_visitor( [&it](auto &&val) { it = val; }, property.second); |

