blob: 958b9d9f8c17d02bb749cb6dd7c35f20cfe02526 [file] [log] [blame]
#include "smart_router.h"
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include "absl/container/flat_hash_set.h"
#include "absl/synchronization/mutex.h"
#include "tlbmc/hal/shared_mem/server.h"
#include "app_interface.h"
#include "dbus_utility.hpp" // NOLINT
#include "async_resp.hpp" // NOLINT
#include "http_request.hpp" // NOLINT
#include "routing.hpp"
// DO NOT MODIFY THIS FILE IN GERRIT. THIS FILE SHOULD ONLY BE MODIFIED IN G3
namespace crow {
using ::milotic_tlbmc::SharedMemoryServer;
void SmartRouter::Handle(crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& async_resp) {
boost::urls::url url(req.target());
// For now only forward GETs to tlbmc.
if (req.method() != boost::beast::http::verb::get) {
gbmcweb_router_.handle(req, async_resp);
return;
}
// If the request is not owned by TLBMC, then we will forward it to gBMCWeb.
if (tlbmc_app_ == nullptr || !IsTlbmcOwnedUrl(url.encoded_path())) {
gbmcweb_router_.handle(req, async_resp);
if (tlbmc_app_ != nullptr) {
SharedMemoryServer::GetInstance().UpdateMetricsRequestCount(false);
}
return;
}
// Otherwise we prepare for tlbmc to handle it.
SharedMemoryServer::GetInstance().UpdateMetricsRequestCount(true);
tlbmc_app_->HandleFromCrowRequest(req, async_resp);
}
std::vector<const std::string*> SmartRouter::GetRoutes(
const std::string& parent) {
return gbmcweb_router_.getRoutes(parent);
}
DynamicRule& SmartRouter::NewRuleDynamic(std::string&& rule) {
return gbmcweb_router_.newRuleDynamic(rule);
}
void SmartRouter::DebugPrint() {
gbmcweb_router_.debugPrint();
if (tlbmc_app_ != nullptr) {
tlbmc_app_->DebugPrint();
}
}
void SmartRouter::Validate() { gbmcweb_router_.validate(); }
absl::flat_hash_set<std::string> SmartRouter::GetTlbmcOwnedUrls() {
if (tlbmc_app_ == nullptr) {
return {};
}
return tlbmc_app_->GetOwnedUrls();
}
bool SmartRouter::IsTlbmcOwnedUrl(std::string_view url) {
absl::MutexLock lock(&tlbmc_owned_urls_mutex_);
return tlbmc_owned_urls_.contains(url);
}
void SmartRouter::UpdateTlbmcOwnedUrls() {
absl::MutexLock lock(&tlbmc_owned_urls_mutex_);
tlbmc_owned_urls_ = GetTlbmcOwnedUrls();
}
} // namespace crow