5 #include "util/http/http_common.h" 7 #include "absl/strings/match.h" 8 #include "absl/strings/numbers.h" 9 #include "absl/strings/str_split.h" 10 #include "base/flags.h" 11 #include "base/logging.h" 12 #include "strings/stringpiece.h" 17 using namespace boost;
19 namespace h2 = beast::http;
23 void HandleVModule(StringPiece str) {
24 vector<StringPiece> parts = absl::StrSplit(str,
",", absl::SkipEmpty());
25 for (StringPiece p : parts) {
26 size_t sep = p.find(
'=');
28 if (sep != StringPiece::npos &&
29 absl::SimpleAtoi(p.substr(sep + 1), &level)) {
30 string module_expr = strings::AsString(p.substr(0, sep));
31 int prev = google::SetVLOGLevel(module_expr.c_str(), level);
32 LOG(INFO) <<
"Setting module " << module_expr <<
" to loglevel " << level
33 <<
", prev: " << prev;
40 const char kHtmlMime[] =
"text/html";
41 const char kJsonMime[] =
"application/json";
42 const char kSvgMime[] =
"image/svg+xml";
43 const char kTextMime[] =
"text/plain";
44 const char kXmlMime[] =
"application/xml";
45 const char kBinMime[] =
"application/octet-stream";;
47 QueryParam ParseQuery(StringPiece str) {
48 std::pair<StringPiece, StringPiece> res;
49 size_t pos = str.find(
'?');
50 res.first = str.substr(0, pos);
51 if (pos != StringPiece::npos) {
52 res.second = str.substr(pos + 1);
57 QueryArgs SplitQuery(StringPiece query) {
58 vector<StringPiece> args = absl::StrSplit(query,
'&');
59 vector<std::pair<StringPiece, StringPiece>> res(args.size());
60 for (
size_t i = 0; i < args.size(); ++i) {
61 size_t pos = args[i].find(
'=');
62 res[i].first = args[i].substr(0, pos);
64 (pos == StringPiece::npos) ? StringPiece() : args[i].substr(pos + 1);
69 h2::response<h2::string_body> ParseFlagz(
const QueryArgs& args) {
70 h2::response<h2::string_body> response(h2::status::ok, 11);
72 StringPiece flag_name;
74 for (
const auto& k_v : args) {
75 if (k_v.first ==
"flag") {
76 flag_name = k_v.second;
77 }
else if (k_v.first ==
"value") {
81 if (!flag_name.empty()) {
82 google::CommandLineFlagInfo flag_info;
83 string fname = strings::AsString(flag_name);
84 if (!google::GetCommandLineFlagInfo(fname.c_str(), &flag_info)) {
85 response.body() =
"Flag not found \n";
87 SetMime(kHtmlMime, &response);
89 .append(
"<p>Current value ")
90 .append(flag_info.current_value)
92 string new_val = strings::AsString(value);
93 string res = google::SetCommandLineOption(fname.c_str(), new_val.c_str());
94 response.body().append(
"Flag ").append(res);
96 if (flag_name ==
"vmodule") {
100 }
else if (args.size() == 1) {
101 LOG(INFO) <<
"Printing all flags";
102 std::vector<google::CommandLineFlagInfo> flags;
103 google::GetAllFlags(&flags);
104 for (
const auto& v : flags) {
109 .append(v.current_value)
111 SetMime(kTextMime, &response);
117 ::boost::system::error_code LoadFileResponse(absl::string_view fname,
118 FileResponse* resp) {
119 FileResponse::body_type::value_type body;
120 system::error_code ec;
121 body.open(fname.data(), boost::beast::file_mode::scan, ec);
126 size_t sz = body.size();
128 FileResponse{std::piecewise_construct, std::make_tuple(std::move(body)),
129 std::make_tuple(h2::status::ok, 11)};
131 const char* mime = kHtmlMime;
132 if (absl::EndsWith(fname,
".svg")) {
134 }
else if (absl::EndsWith(fname,
".html")) {
140 resp->content_length(sz);