index_mon_page.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "index_mon_page.h"
  2. #include <util/generic/cast.h>
  3. #include <util/string/ascii.h>
  4. using namespace NMonitoring;
  5. void TIndexMonPage::OutputIndexPage(IMonHttpRequest& request) {
  6. request.Output() << HTTPOKHTML;
  7. request.Output() << "<html>\n";
  8. OutputHead(request.Output());
  9. OutputBody(request);
  10. request.Output() << "</html>\n";
  11. }
  12. void TIndexMonPage::Output(IMonHttpRequest& request) {
  13. TStringBuf pathInfo = request.GetPathInfo();
  14. if (pathInfo.empty() || pathInfo == TStringBuf("/")) {
  15. OutputIndexPage(request);
  16. return;
  17. }
  18. Y_ABORT_UNLESS(pathInfo.StartsWith('/'));
  19. TMonPagePtr found;
  20. // analogous to CGI PATH_INFO
  21. {
  22. TGuard<TMutex> g(Mtx);
  23. TStringBuf pathTmp = request.GetPathInfo();
  24. for (;;) {
  25. if (TPagesByPath::iterator i = PagesByPath.find(pathTmp); i != PagesByPath.end()) {
  26. found = *i->second;
  27. pathInfo = request.GetPathInfo().substr(pathTmp.size());
  28. Y_ABORT_UNLESS(pathInfo.empty() || pathInfo.StartsWith('/'));
  29. break;
  30. }
  31. size_t slash = pathTmp.find_last_of('/');
  32. Y_ABORT_UNLESS(slash != TString::npos);
  33. pathTmp = pathTmp.substr(0, slash);
  34. if (!pathTmp) {
  35. break;
  36. }
  37. }
  38. }
  39. if (found) {
  40. THolder<IMonHttpRequest> child(request.MakeChild(found.Get(), TString{pathInfo}));
  41. found->Output(*child);
  42. } else {
  43. request.Output() << HTTPNOTFOUND;
  44. }
  45. }
  46. void TIndexMonPage::OutputIndex(IOutputStream& out, bool pathEndsWithSlash) {
  47. TGuard<TMutex> g(Mtx);
  48. for (auto& Page : Pages) {
  49. IMonPage* page = Page.Get();
  50. if (page->IsInIndex()) {
  51. TString pathToDir = "";
  52. if (!pathEndsWithSlash) {
  53. pathToDir = this->GetPath() + "/";
  54. }
  55. out << "<a href='" << pathToDir << page->GetPath() << "'>" << page->GetTitle() << "</a><br/>\n";
  56. }
  57. }
  58. }
  59. void TIndexMonPage::Register(TMonPagePtr page) {
  60. TGuard<TMutex> g(Mtx);
  61. if (auto [it, inserted] = PagesByPath.try_emplace("/" + page->GetPath()); inserted) {
  62. // new unique page just inserted, insert it to the end
  63. it->second = Pages.insert(Pages.end(), page);
  64. } else {
  65. // a page with the given path is already present, replace it with the new page
  66. *it->second = page;
  67. }
  68. page->Parent = this;
  69. }
  70. TIndexMonPage* TIndexMonPage::RegisterIndexPage(const TString& path, const TString& title) {
  71. TGuard<TMutex> g(Mtx);
  72. TIndexMonPage* page = VerifyDynamicCast<TIndexMonPage*>(FindPage(path));
  73. if (page) {
  74. return page;
  75. }
  76. page = new TIndexMonPage(path, title);
  77. Register(page);
  78. return VerifyDynamicCast<TIndexMonPage*>(page);
  79. }
  80. IMonPage* TIndexMonPage::FindPage(const TString& relativePath) {
  81. TGuard<TMutex> g(Mtx);
  82. TPagesByPath::iterator i = PagesByPath.find("/" + relativePath);
  83. if (i == PagesByPath.end()) {
  84. return nullptr;
  85. } else {
  86. return i->second->Get();
  87. }
  88. }
  89. IMonPage* TIndexMonPage::FindPageByAbsolutePath(const TString& absolutePath) {
  90. TIndexMonPage* page = this;
  91. TString path = absolutePath;
  92. while (!path.empty()) {
  93. while (path.StartsWith('/')) {
  94. path.erase(0, 1);
  95. }
  96. TString tryPath = path;
  97. while (!tryPath.empty()) {
  98. IMonPage* found = page->FindPage(tryPath);
  99. if (found == nullptr) {
  100. size_t slash = tryPath.find_last_of('/');
  101. if (slash == TString::npos) {
  102. return nullptr;
  103. }
  104. tryPath.resize(slash);
  105. } else {
  106. if (tryPath.size() == path.size()) {
  107. return found;
  108. }
  109. if (found->IsIndex()) {
  110. path = path.substr(tryPath.size() + 1);
  111. page = static_cast<TIndexMonPage*>(found);
  112. break;
  113. } else {
  114. return found;
  115. }
  116. }
  117. }
  118. }
  119. return nullptr;
  120. }
  121. TIndexMonPage* TIndexMonPage::FindIndexPage(const TString& relativePath) {
  122. return VerifyDynamicCast<TIndexMonPage*>(FindPage(relativePath));
  123. }
  124. void TIndexMonPage::OutputCommonJsCss(IOutputStream& out) {
  125. out << "<link rel='stylesheet' href='/static/css/bootstrap.min.css'>\n";
  126. out << "<script language='javascript' type='text/javascript' src='/static/js/jquery.min.js'></script>\n";
  127. out << "<script language='javascript' type='text/javascript' src='/static/js/bootstrap.min.js'></script>\n";
  128. }
  129. void TIndexMonPage::OutputHead(IOutputStream& out) {
  130. out << "<head>\n";
  131. OutputCommonJsCss(out);
  132. out << "<title>" << Title << "</title>\n";
  133. out << "</head>\n";
  134. }
  135. void TIndexMonPage::OutputBody(IMonHttpRequest& req) {
  136. auto& out = req.Output();
  137. out << "<body>\n";
  138. // part of common navbar
  139. OutputNavBar(out);
  140. out << "<div class='container'>\n"
  141. << "<h2>" << Title << "</h2>\n";
  142. OutputIndex(out, req.GetPathInfo().EndsWith('/'));
  143. out << "</div>\n"
  144. << "</body>\n";
  145. }
  146. void TIndexMonPage::SortPages() {
  147. TGuard<TMutex> g(Mtx);
  148. Pages.sort([](const TMonPagePtr& a, const TMonPagePtr& b) {
  149. return AsciiCompareIgnoreCase(a->GetTitle(), b->GetTitle()) < 0;
  150. });
  151. }
  152. void TIndexMonPage::ClearPages() {
  153. TGuard<TMutex> g(Mtx);
  154. Pages.clear();
  155. PagesByPath.clear();
  156. }