| 69 | ViewServiceImpl() {} |
| 70 | virtual ~ViewServiceImpl() {} |
| 71 | virtual void default_method(google::protobuf::RpcController* cntl_base, |
| 72 | const HttpRequest*, |
| 73 | HttpResponse*, |
| 74 | google::protobuf::Closure* done) { |
| 75 | brpc::ClosureGuard done_guard(done); |
| 76 | brpc::Controller* server_cntl = |
| 77 | static_cast<brpc::Controller*>(cntl_base); |
| 78 | |
| 79 | // Get or set target. Notice that we don't access FLAGS_target directly |
| 80 | // which is thread-unsafe (for string flags). |
| 81 | std::string target; |
| 82 | const std::string* newtarget = |
| 83 | server_cntl->http_request().uri().GetQuery("changetarget"); |
| 84 | if (newtarget) { |
| 85 | if (GFLAGS_NAMESPACE::SetCommandLineOption("target", newtarget->c_str()).empty()) { |
| 86 | server_cntl->SetFailed("Fail to change value of -target"); |
| 87 | return; |
| 88 | } |
| 89 | target = *newtarget; |
| 90 | } else { |
| 91 | if (!GFLAGS_NAMESPACE::GetCommandLineOption("target", &target)) { |
| 92 | server_cntl->SetFailed("Fail to get value of -target"); |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Create the http channel on-the-fly. Notice that we've set |
| 98 | // `defer_close_second' in main() so that dtor of channels do not |
| 99 | // close connections immediately and ad-hoc creation of channels |
| 100 | // often reuses the not-yet-closed connections. |
| 101 | brpc::Channel http_chan; |
| 102 | brpc::ChannelOptions http_chan_opt; |
| 103 | http_chan_opt.protocol = brpc::PROTOCOL_HTTP; |
| 104 | if (http_chan.Init(target.c_str(), &http_chan_opt) != 0) { |
| 105 | server_cntl->SetFailed(brpc::EINTERNAL, |
| 106 | "Fail to connect to %s", target.c_str()); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Remove "Accept-Encoding". We need to insert additional texts |
| 111 | // before </body>, preventing the server from compressing the content |
| 112 | // simplifies our code. The additional bandwidth consumption shouldn't |
| 113 | // be an issue for infrequent checking out of builtin services pages. |
| 114 | server_cntl->http_request().RemoveHeader("Accept-Encoding"); |
| 115 | |
| 116 | brpc::Controller* client_cntl = new brpc::Controller; |
| 117 | client_cntl->http_request() = server_cntl->http_request(); |
| 118 | // Remove "Host" so that RPC will laterly serialize the (correct) |
| 119 | // target server in. |
| 120 | client_cntl->http_request().RemoveHeader("host"); |
| 121 | |
| 122 | // Setup the URI. |
| 123 | const brpc::URI& server_uri = server_cntl->http_request().uri(); |
| 124 | std::string uri = server_uri.path(); |
| 125 | if (!server_uri.query().empty()) { |
| 126 | uri.push_back('?'); |
| 127 | uri.append(server_uri.query()); |
| 128 | } |
nothing calls this directly
no test coverage detected