#include "index_mon_page.h" #include #include using namespace NMonitoring; void TIndexMonPage::OutputIndexPage(IMonHttpRequest& request) { request.Output() << HTTPOKHTML; request.Output() << "\n"; OutputHead(request.Output()); OutputBody(request); request.Output() << "\n"; } void TIndexMonPage::Output(IMonHttpRequest& request) { TStringBuf pathInfo = request.GetPathInfo(); if (pathInfo.empty() || pathInfo == TStringBuf("/")) { OutputIndexPage(request); return; } Y_ABORT_UNLESS(pathInfo.StartsWith('/')); TMonPagePtr found; // analogous to CGI PATH_INFO { TGuard g(Mtx); TStringBuf pathTmp = request.GetPathInfo(); for (;;) { if (TPagesByPath::iterator i = PagesByPath.find(pathTmp); i != PagesByPath.end()) { found = *i->second; pathInfo = request.GetPathInfo().substr(pathTmp.size()); Y_ABORT_UNLESS(pathInfo.empty() || pathInfo.StartsWith('/')); break; } size_t slash = pathTmp.find_last_of('/'); Y_ABORT_UNLESS(slash != TString::npos); pathTmp = pathTmp.substr(0, slash); if (!pathTmp) { break; } } } if (found) { THolder child(request.MakeChild(found.Get(), TString{pathInfo})); found->Output(*child); } else { request.Output() << HTTPNOTFOUND; } } void TIndexMonPage::OutputIndex(IOutputStream& out, bool pathEndsWithSlash) { TGuard g(Mtx); for (auto& Page : Pages) { IMonPage* page = Page.Get(); if (page->IsInIndex()) { TString pathToDir = ""; if (!pathEndsWithSlash) { pathToDir = this->GetPath() + "/"; } out << "" << page->GetTitle() << "
\n"; } } } void TIndexMonPage::Register(TMonPagePtr page) { TGuard g(Mtx); if (auto [it, inserted] = PagesByPath.try_emplace("/" + page->GetPath()); inserted) { // new unique page just inserted, insert it to the end it->second = Pages.insert(Pages.end(), page); } else { // a page with the given path is already present, replace it with the new page *it->second = page; } page->Parent = this; } TIndexMonPage* TIndexMonPage::RegisterIndexPage(const TString& path, const TString& title) { TGuard g(Mtx); TIndexMonPage* page = VerifyDynamicCast(FindPage(path)); if (page) { return page; } page = new TIndexMonPage(path, title); Register(page); return VerifyDynamicCast(page); } IMonPage* TIndexMonPage::FindPage(const TString& relativePath) { TGuard g(Mtx); TPagesByPath::iterator i = PagesByPath.find("/" + relativePath); if (i == PagesByPath.end()) { return nullptr; } else { return i->second->Get(); } } IMonPage* TIndexMonPage::FindPageByAbsolutePath(const TString& absolutePath) { TIndexMonPage* page = this; TString path = absolutePath; while (!path.empty()) { while (path.StartsWith('/')) { path.erase(0, 1); } TString tryPath = path; while (!tryPath.empty()) { IMonPage* found = page->FindPage(tryPath); if (found == nullptr) { size_t slash = tryPath.find_last_of('/'); if (slash == TString::npos) { return nullptr; } tryPath.resize(slash); } else { if (tryPath.size() == path.size()) { return found; } if (found->IsIndex()) { path = path.substr(tryPath.size() + 1); page = static_cast(found); break; } else { return found; } } } } return nullptr; } TIndexMonPage* TIndexMonPage::FindIndexPage(const TString& relativePath) { return VerifyDynamicCast(FindPage(relativePath)); } void TIndexMonPage::OutputCommonJsCss(IOutputStream& out) { out << "\n"; out << "\n"; out << "\n"; } void TIndexMonPage::OutputHead(IOutputStream& out) { out << "\n"; OutputCommonJsCss(out); out << "" << Title << "\n"; out << "\n"; } void TIndexMonPage::OutputBody(IMonHttpRequest& req) { auto& out = req.Output(); out << "\n"; // part of common navbar OutputNavBar(out); out << "
\n" << "

" << Title << "

\n"; OutputIndex(out, req.GetPathInfo().EndsWith('/')); out << "
\n" << "\n"; } void TIndexMonPage::SortPages() { TGuard g(Mtx); Pages.sort([](const TMonPagePtr& a, const TMonPagePtr& b) { return AsciiCompareIgnoreCase(a->GetTitle(), b->GetTitle()) < 0; }); } void TIndexMonPage::ClearPages() { TGuard g(Mtx); Pages.clear(); PagesByPath.clear(); }