11use http_body_util:: { BodyExt , Full } ;
22use hyper:: body:: { Bytes , Incoming } ;
33use hyper:: service:: Service ;
4- use hyper:: { Error , Request , Response , StatusCode } ;
4+ use hyper:: { Request , Response , StatusCode } ;
55use std:: collections:: HashMap ;
66
77use prost:: Message ;
@@ -94,7 +94,6 @@ async fn handle_request<
9494 store : Arc < dyn KvStore > , authorizer : Arc < dyn Authorizer > , request : Request < Incoming > ,
9595 handler : F ,
9696) -> Result < <VssService as Service < Request < Incoming > > >:: Response , hyper:: Error > {
97- // TODO: we should bound the amount of data we read to avoid allocating too much memory.
9897 let ( parts, body) = request. into_parts ( ) ;
9998 let headers_map = parts
10099 . headers
@@ -104,17 +103,17 @@ async fn handle_request<
104103
105104 let user_token = match authorizer. verify ( & headers_map) . await {
106105 Ok ( auth_response) => auth_response. user_token ,
107- Err ( e) => return build_error_response ( e) ,
106+ Err ( e) => return Ok ( build_error_response ( e) ) ,
108107 } ;
109-
108+ // TODO: we should bound the amount of data we read to avoid allocating too much memory.
110109 let bytes = body. collect ( ) . await ?. to_bytes ( ) ;
111110 match T :: decode ( bytes) {
112111 Ok ( request) => match handler ( store. clone ( ) , user_token, request) . await {
113112 Ok ( response) => Ok ( Response :: builder ( )
114113 . body ( Full :: new ( Bytes :: from ( response. encode_to_vec ( ) ) ) )
115114 // unwrap safety: body only errors when previous chained calls failed.
116115 . unwrap ( ) ) ,
117- Err ( e) => build_error_response ( e) ,
116+ Err ( e) => Ok ( build_error_response ( e) ) ,
118117 } ,
119118 Err ( _) => Ok ( Response :: builder ( )
120119 . status ( StatusCode :: BAD_REQUEST )
@@ -124,7 +123,7 @@ async fn handle_request<
124123 }
125124}
126125
127- fn build_error_response ( e : VssError ) -> Result < Response < Full < Bytes > > , Error > {
126+ fn build_error_response ( e : VssError ) -> Response < Full < Bytes > > {
128127 let error_response = match e {
129128 VssError :: NoSuchKeyError ( msg) => ErrorResponse {
130129 error_code : ErrorCode :: NoSuchKeyException . into ( ) ,
@@ -146,9 +145,9 @@ fn build_error_response(e: VssError) -> Result<Response<Full<Bytes>>, Error> {
146145 message : "Unknown Server Error occurred." . to_string ( ) ,
147146 } ,
148147 } ;
149- Ok ( Response :: builder ( )
148+ Response :: builder ( )
150149 . status ( StatusCode :: INTERNAL_SERVER_ERROR )
151150 . body ( Full :: new ( Bytes :: from ( error_response. encode_to_vec ( ) ) ) )
152151 // unwrap safety: body only errors when previous chained calls failed.
153- . unwrap ( ) )
152+ . unwrap ( )
154153}
0 commit comments