| 133 | } |
| 134 | |
| 135 | void FlagsService::default_method(::google::protobuf::RpcController* cntl_base, |
| 136 | const ::brpc::FlagsRequest*, |
| 137 | ::brpc::FlagsResponse*, |
| 138 | ::google::protobuf::Closure* done) { |
| 139 | ClosureGuard done_guard(done); |
| 140 | Controller *cntl = static_cast<Controller*>(cntl_base); |
| 141 | const std::string* value_str = |
| 142 | cntl->http_request().uri().GetQuery(SETVALUE_STR); |
| 143 | const std::string& constraint = cntl->http_request().unresolved_path(); |
| 144 | |
| 145 | const bool use_html = UseHTML(cntl->http_request()); |
| 146 | cntl->http_response().set_content_type( |
| 147 | use_html ? "text/html" : "text/plain"); |
| 148 | |
| 149 | if (value_str != NULL) { |
| 150 | // reload value if ?setvalue=VALUE is present. |
| 151 | if (constraint.empty()) { |
| 152 | cntl->SetFailed(ENOMETHOD, "Require gflag name"); |
| 153 | return; |
| 154 | } |
| 155 | if (use_html && cntl->http_request().uri().GetQuery("withform")) { |
| 156 | return set_value_page(cntl, done_guard.release()); |
| 157 | } |
| 158 | GFLAGS_NAMESPACE::CommandLineFlagInfo info; |
| 159 | if (!GFLAGS_NAMESPACE::GetCommandLineFlagInfo(constraint.c_str(), &info)) { |
| 160 | cntl->SetFailed(ENOMETHOD, "No such gflag"); |
| 161 | return; |
| 162 | } |
| 163 | if (!info.has_validator_fn) { |
| 164 | cntl->SetFailed(EPERM, "A reloadable gflag must have validator"); |
| 165 | return; |
| 166 | } |
| 167 | if (FLAGS_immutable_flags) { |
| 168 | cntl->SetFailed(EPERM, "Cannot modify `%s' because -immutable_flags is on", |
| 169 | constraint.c_str()); |
| 170 | return; |
| 171 | } |
| 172 | if (GFLAGS_NAMESPACE::SetCommandLineOption(constraint.c_str(), |
| 173 | value_str->c_str()).empty()) { |
| 174 | cntl->SetFailed(EPERM, "Fail to set `%s' to %s", |
| 175 | constraint.c_str(), |
| 176 | (value_str->empty() ? "empty string" : value_str->c_str())); |
| 177 | return; |
| 178 | } |
| 179 | butil::IOBufBuilder os; |
| 180 | os << "Set `" << constraint << "' to " << *value_str; |
| 181 | if (use_html) { |
| 182 | os << "<br><a href='/flags'>[back to flags]</a>"; |
| 183 | } |
| 184 | os.move_to(cntl->response_attachment()); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | // Parse query-string which is comma-separated flagnames/wildcards. |
| 189 | std::vector<std::string> wildcards; |
| 190 | std::set<std::string> exact; |
| 191 | if (!constraint.empty()) { |
| 192 | for (butil::StringMultiSplitter sp(constraint.c_str(), ",;"); sp != NULL; ++sp) { |
nothing calls this directly
no test coverage detected