handle HTTP response of accessing builtin services of the target server.
| 28 | |
| 29 | // handle HTTP response of accessing builtin services of the target server. |
| 30 | static void handle_response(brpc::Controller* client_cntl, |
| 31 | std::string target, |
| 32 | brpc::Controller* server_cntl, |
| 33 | google::protobuf::Closure* server_done) { |
| 34 | // Copy all headers. The "Content-Length" will be overwriteen. |
| 35 | server_cntl->http_response() = client_cntl->http_response(); |
| 36 | // Copy content. |
| 37 | server_cntl->response_attachment() = client_cntl->response_attachment(); |
| 38 | // Insert "rpc_view: <target>" before </body> so that users are always |
| 39 | // visually notified with target server w/o confusions. |
| 40 | butil::IOBuf& content = server_cntl->response_attachment(); |
| 41 | butil::IOBuf before_body; |
| 42 | if (content.cut_until(&before_body, "</body>") == 0) { |
| 43 | before_body.append( |
| 44 | "<style type=\"text/css\">\n" |
| 45 | ".rpcviewlogo {position: fixed; bottom: 0px; right: 0px;" |
| 46 | " color: #ffffff; background-color: #000000; }\n" |
| 47 | " </style>\n" |
| 48 | "<span class='rpcviewlogo'> rpc_view: "); |
| 49 | before_body.append(target); |
| 50 | before_body.append(" </span></body>"); |
| 51 | before_body.append(content); |
| 52 | content = before_body; |
| 53 | } |
| 54 | // Notice that we don't set RPC to failed on http errors because we |
| 55 | // want to pass unchanged content to the users otherwise RPC replaces |
| 56 | // the content with ErrorText. |
| 57 | if (client_cntl->Failed() && |
| 58 | client_cntl->ErrorCode() != brpc::EHTTP) { |
| 59 | server_cntl->SetFailed(client_cntl->ErrorCode(), |
| 60 | "%s", client_cntl->ErrorText().c_str()); |
| 61 | } |
| 62 | delete client_cntl; |
| 63 | server_done->Run(); |
| 64 | } |
| 65 | |
| 66 | // A http_master_service. |
| 67 | class ViewServiceImpl : public ViewService { |