Skip to content

Commit

Permalink
compact RenderResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
cyshi committed Nov 9, 2015
1 parent b1671ef commit aea623e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 51 deletions.
64 changes: 23 additions & 41 deletions src/sofa/pbrpc/http_rpc_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ ReadBufferPtr HTTPRpcRequest::AssembleSucceedResponse(
WriteBuffer write_buffer;
if (_type == POST_PB)
{
if (!RenderProtobufResponse(&write_buffer, response))
if (!RenderResponse(&write_buffer, PROTOBUF, response->SerializeAsString()))
{
err = "render protobuf response failed";
return ReadBufferPtr();
Expand All @@ -229,7 +229,7 @@ ReadBufferPtr HTTPRpcRequest::AssembleSucceedResponse(
{
std::string json_str;
pb2json(response, json_str);
if (!RenderJsonResponse(&write_buffer, json_str))
if (!RenderResponse(&write_buffer, JSON, json_str))
{
err = "render json response failed";
return ReadBufferPtr();
Expand All @@ -251,7 +251,7 @@ ReadBufferPtr HTTPRpcRequest::AssembleFailedResponse(
<< StringUtils::replace_all(reason, "\"", "\\\"") << "\"";

WriteBuffer write_buffer;
if (!RenderJsonResponse(&write_buffer, oss.str()))
if (!RenderResponse(&write_buffer, JSON, oss.str()))
{
err = "render json response failed";
return ReadBufferPtr();
Expand Down Expand Up @@ -403,7 +403,7 @@ void HTTPRpcRequest::SendPage(
const std::string& page)
{
WriteBuffer write_buffer;
if (!RenderHtmlResponse(&write_buffer, page))
if (!RenderResponse(&write_buffer, HTML, page))
{
#if defined( LOG )
LOG(ERROR) << "SendPage(): " << RpcEndpointToString(_remote_endpoint)
Expand Down Expand Up @@ -433,51 +433,33 @@ void HTTPRpcRequest::SendError(
SendPage(server_stream, oss.str());
}

bool HTTPRpcRequest::RenderJsonResponse(
bool HTTPRpcRequest::RenderResponse(
google::protobuf::io::ZeroCopyOutputStream* output,
const std::string& json)
const RenderType type,
const std::string& body)
{
std::ostringstream oss;
oss << json.size();
oss << body.size();
google::protobuf::io::Printer printer(output, '$');
printer.Print("HTTP/1.1 200 OK\r\n");
printer.Print("Content-Type: application/json\r\n");
printer.Print("Access-Control-Allow-Origin: *\r\n");
printer.Print("Content-Length: $LENGTH$\r\n", "LENGTH", oss.str());
printer.Print("\r\n");
printer.PrintRaw(json);
return !printer.failed();
}

bool HTTPRpcRequest::RenderHtmlResponse(
google::protobuf::io::ZeroCopyOutputStream* output,
const std::string& html)
{
std::ostringstream oss;
oss << html.size();
google::protobuf::io::Printer printer(output, '$');
printer.Print("HTTP/1.1 200 OK\r\n");
printer.Print("Content-Type: text/html; charset=UTF-8\r\n");
printer.Print("Access-Control-Allow-Origin: *\r\n");
printer.Print("Content-Length: $LENGTH$\r\n", "LENGTH", oss.str());
printer.Print("\r\n");
printer.PrintRaw(html);
return !printer.failed();
}

bool HTTPRpcRequest::RenderProtobufResponse(
google::protobuf::io::ZeroCopyOutputStream* output,
const google::protobuf::Message* response)
{
std::ostringstream oss;
oss << response->ByteSize();
google::protobuf::io::Printer printer(output, '$');
printer.Print("HTTP/1.1 200 OK\r\n");
printer.Print("Content-Type: application/protobuf\r\n");
switch (type)
{
case JSON:
printer.Print("Content-Type: application/json\r\n");
break;
case PROTOBUF:
printer.Print("Content-Type: application/protobuf\r\n");
break;
case HTML:
printer.Print("Content-Type: text/html; charset=UTF-8\r\n");
break;
default:
break;
}
printer.Print("Access-Control-Allow-Origin: *\r\n");
printer.Print("Content-Length: $LENGTH$\r\n", "LENGTH", oss.str());
printer.Print("\r\n");
printer.PrintRaw(response->SerializeAsString());
printer.PrintRaw(body);
return !printer.failed();
}

Expand Down
21 changes: 11 additions & 10 deletions src/sofa/pbrpc/http_rpc_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class HTTPRpcRequest : public RpcRequest
const std::string& reason,
std::string& err);

private:
enum RenderType
{
JSON = 1,
PROTOBUF = 2,
HTML = 3
};

private:
// Parse http path.
// @return false if parse failed.
Expand All @@ -64,17 +72,10 @@ class HTTPRpcRequest : public RpcRequest
const RpcServerStreamWPtr& server_stream,
const std::string& error);

static bool RenderJsonResponse(
google::protobuf::io::ZeroCopyOutputStream* output,
const std::string& json);

static bool RenderHtmlResponse(
google::protobuf::io::ZeroCopyOutputStream* output,
const std::string& html);

static bool RenderProtobufResponse(
static bool RenderResponse(
google::protobuf::io::ZeroCopyOutputStream* output,
const google::protobuf::Message* response);
const RenderType type,
const std::string& body);

static rapidjson::Document* ParseJson(
const char* str,
Expand Down

0 comments on commit aea623e

Please sign in to comment.