@@ -123,37 +123,6 @@ void HTTPRpcRequest::ProcessRequest(
123123 }
124124 }
125125
126- std::string json_str;
127- if (_type == POST )
128- {
129- json_str = _req_body->ToString ();
130- }
131- else
132- {
133- json_str = _query_params[" request" ];
134- }
135- if (json_str.empty ())
136- {
137- // if null json str, set as null object
138- json_str = " {}" ;
139- }
140-
141- std::string err;
142- _req_json = ParseJson (json_str.c_str (), err);
143- if (_req_json == NULL )
144- {
145- #if defined( LOG )
146- LOG (ERROR ) << " ProcessRequest(): " << RpcEndpointToString (_remote_endpoint)
147- << " : {" << SequenceId () << " }: parse json failed: " << err;
148- #else
149- SLOG (ERROR , " ProcessRequest(): %s: {%lu}: parse json failed: %s" ,
150- RpcEndpointToString (_remote_endpoint).c_str (), SequenceId (), err.c_str ());
151- #endif
152- SendFailedResponse (server_stream,
153- RPC_ERROR_PARSE_REQUEST_MESSAGE , " parse json failed: " + err);
154- return ;
155- }
156-
157126 MethodBoard* method_board = FindMethodBoard (service_pool, service_name, method_name);
158127 if (method_board == NULL )
159128 {
@@ -173,18 +142,57 @@ void HTTPRpcRequest::ProcessRequest(
173142 const google::protobuf::MethodDescriptor* method_desc = method_board->Descriptor ();
174143
175144 google::protobuf::Message* request = service->GetRequestPrototype (method_desc).New ();
176- if (jsonobject2pb (_req_json, request, err) < 0 )
145+ if (_type == POST_PB )
177146 {
147+ bool parse_request_return = request->ParseFromZeroCopyStream (_req_body.get ());
148+ if (!parse_request_return)
149+ {
178150#if defined( LOG )
179- LOG (ERROR ) << " ProcessRequest(): " << RpcEndpointToString (_remote_endpoint)
180- << " : {" << SequenceId () << " }: parse json to pb failed: " << err ;
151+ LOG (ERROR ) << " ProcessRequest(): " << RpcEndpointToString (_remote_endpoint)
152+ << " : {" << SequenceId () << " }: parse pb body failed " ;
181153#else
182- SLOG (ERROR , " ProcessRequest(): %s: {%lu}: parse json to pb failed: %s " ,
183- RpcEndpointToString (_remote_endpoint).c_str (), SequenceId (), err. c_str ());
154+ SLOG (ERROR , " ProcessRequest(): %s: {%lu}: parse pb body failed " ,
155+ RpcEndpointToString (_remote_endpoint).c_str (), SequenceId ());
184156#endif
185- SendFailedResponse (server_stream,
186- RPC_ERROR_PARSE_REQUEST_MESSAGE , " parse json to pb failed: " + err);
187- return ;
157+ SendFailedResponse (server_stream,
158+ RPC_ERROR_PARSE_REQUEST_MESSAGE , " parse pb body failed" );
159+ delete request;
160+ return ;
161+ }
162+ }
163+ else
164+ {
165+ std::string json_str;
166+ if (_type == POST )
167+ {
168+ json_str = _req_body->ToString ();
169+ }
170+ else
171+ {
172+ json_str = _query_params[" request" ];
173+ }
174+ if (json_str.empty ())
175+ {
176+ // if null json str, set as null object
177+ json_str = " {}" ;
178+ }
179+
180+ std::string err;
181+ _req_json = ParseJson (json_str.c_str (), err);
182+ if (_req_json == NULL || jsonobject2pb (_req_json, request, err) < 0 )
183+ {
184+ #if defined( LOG )
185+ LOG (ERROR ) << " ProcessRequest(): " << RpcEndpointToString (_remote_endpoint)
186+ << " : {" << SequenceId () << " }: parse json failed: " << err;
187+ #else
188+ SLOG (ERROR , " ProcessRequest(): %s: {%lu}: parse json failed: %s" ,
189+ RpcEndpointToString (_remote_endpoint).c_str (), SequenceId (), err.c_str ());
190+ #endif
191+ SendFailedResponse (server_stream,
192+ RPC_ERROR_PARSE_REQUEST_MESSAGE , " parse json failed: " + err);
193+ delete request;
194+ return ;
195+ }
188196 }
189197
190198 google::protobuf::Message* response = service->GetResponsePrototype (method_desc).New ();
@@ -208,14 +216,24 @@ ReadBufferPtr HTTPRpcRequest::AssembleSucceedResponse(
208216 const google::protobuf::Message* response,
209217 std::string& err)
210218{
211- std::string json_str;
212- pb2json (response, json_str);
213-
214219 WriteBuffer write_buffer;
215- if (! RenderJsonResponse (&write_buffer, json_str) )
220+ if (_type == POST_PB )
216221 {
217- err = " render json response failed" ;
218- return ReadBufferPtr ();
222+ if (!RenderResponse (&write_buffer, PROTOBUF , response->SerializeAsString ()))
223+ {
224+ err = " render protobuf response failed" ;
225+ return ReadBufferPtr ();
226+ }
227+ }
228+ else
229+ {
230+ std::string json_str;
231+ pb2json (response, json_str);
232+ if (!RenderResponse (&write_buffer, JSON , json_str))
233+ {
234+ err = " render json response failed" ;
235+ return ReadBufferPtr ();
236+ }
219237 }
220238
221239 ReadBufferPtr read_buffer (new ReadBuffer ());
@@ -233,7 +251,7 @@ ReadBufferPtr HTTPRpcRequest::AssembleFailedResponse(
233251 << StringUtils::replace_all (reason, " \" " , " \\\" " ) << " \" " ;
234252
235253 WriteBuffer write_buffer;
236- if (!RenderJsonResponse (&write_buffer, oss.str ()))
254+ if (!RenderResponse (&write_buffer, JSON , oss.str ()))
237255 {
238256 err = " render json response failed" ;
239257 return ReadBufferPtr ();
@@ -385,7 +403,7 @@ void HTTPRpcRequest::SendPage(
385403 const std::string& page)
386404{
387405 WriteBuffer write_buffer;
388- if (!RenderHtmlResponse (&write_buffer, page))
406+ if (!RenderResponse (&write_buffer, HTML , page))
389407 {
390408#if defined( LOG )
391409 LOG (ERROR ) << " SendPage(): " << RpcEndpointToString (_remote_endpoint)
@@ -415,35 +433,33 @@ void HTTPRpcRequest::SendError(
415433 SendPage (server_stream, oss.str ());
416434}
417435
418- bool HTTPRpcRequest::RenderJsonResponse (
436+ bool HTTPRpcRequest::RenderResponse (
419437 google::protobuf::io::ZeroCopyOutputStream* output,
420- const std::string& json)
438+ const RenderType type,
439+ const std::string& body)
421440{
422441 std::ostringstream oss;
423- oss << json .size ();
442+ oss << body .size ();
424443 google::protobuf::io::Printer printer (output, ' $' );
425444 printer.Print (" HTTP/1.1 200 OK\r\n " );
426- printer.Print (" Content-Type: application/json\r\n " );
427- printer.Print (" Access-Control-Allow-Origin: *\r\n " );
428- printer.Print (" Content-Length: $LENGTH$\r\n " , " LENGTH" , oss.str ());
429- printer.Print (" \r\n " );
430- printer.PrintRaw (json);
431- return !printer.failed ();
432- }
433-
434- bool HTTPRpcRequest::RenderHtmlResponse (
435- google::protobuf::io::ZeroCopyOutputStream* output,
436- const std::string& html)
437- {
438- std::ostringstream oss;
439- oss << html.size ();
440- google::protobuf::io::Printer printer (output, ' $' );
441- printer.Print (" HTTP/1.1 200 OK\r\n " );
442- printer.Print (" Content-Type: text/html; charset=UTF-8\r\n " );
445+ switch (type)
446+ {
447+ case JSON :
448+ printer.Print (" Content-Type: application/json\r\n " );
449+ break ;
450+ case PROTOBUF :
451+ printer.Print (" Content-Type: application/protobuf\r\n " );
452+ break ;
453+ case HTML :
454+ printer.Print (" Content-Type: text/html; charset=UTF-8\r\n " );
455+ break ;
456+ default :
457+ break ;
458+ }
443459 printer.Print (" Access-Control-Allow-Origin: *\r\n " );
444460 printer.Print (" Content-Length: $LENGTH$\r\n " , " LENGTH" , oss.str ());
445461 printer.Print (" \r\n " );
446- printer.PrintRaw (html );
462+ printer.PrintRaw (body );
447463 return !printer.failed ();
448464}
449465
0 commit comments