@@ -23,6 +23,8 @@ use log::{debug, trace};
2323use crate :: util:: KeyValueVecKeyPrinter ;
2424
2525const MAXIMUM_REQUEST_BODY_SIZE : usize = 1024 * 1024 * 1024 ;
26+ const PROTOCOL_VERSION_HEADER : & str = "vss-protocol-version" ;
27+ const PROTOCOL_VERSION : Version = Version :: V1 ;
2628
2729#[ derive( Clone , Copy ) ]
2830pub ( crate ) struct VssServiceConfig {
@@ -81,12 +83,8 @@ impl Service<Request<Incoming>> for VssService {
8183
8284 match prefix_stripped_path {
8385 "/version" => {
84- let response = VersionResponse { version : Version :: V1 . into ( ) } ;
85- let response = Response :: builder ( )
86- . body ( Full :: new ( Bytes :: from ( response. encode_to_vec ( ) ) ) )
87- // unwrap safety: body only errors when previous chained calls failed.
88- . unwrap ( ) ;
89- Ok ( response)
86+ let response = VersionResponse { version : PROTOCOL_VERSION . into ( ) } ;
87+ Ok ( build_response ( StatusCode :: OK , Bytes :: from ( response. encode_to_vec ( ) ) ) )
9088 } ,
9189 "/getObject" => {
9290 handle_request (
@@ -128,13 +126,10 @@ impl Service<Request<Incoming>> for VssService {
128126 )
129127 . await
130128 } ,
131- _ => {
132- let error_msg = "Invalid request path." . as_bytes ( ) ;
133- Ok ( Response :: builder ( )
134- . status ( StatusCode :: BAD_REQUEST )
135- . body ( Full :: new ( Bytes :: from ( error_msg) ) )
136- . unwrap ( ) )
137- } ,
129+ _ => Ok ( build_response (
130+ StatusCode :: BAD_REQUEST ,
131+ Bytes :: from_static ( b"Invalid request path." ) ,
132+ ) ) ,
138133 }
139134 } )
140135 }
@@ -226,29 +221,35 @@ async fn handle_request<
226221 let bytes = match limited_body. collect ( ) . await {
227222 Ok ( body) => body. to_bytes ( ) ,
228223 Err ( _) => {
229- return Ok ( Response :: builder ( )
230- . status ( StatusCode :: PAYLOAD_TOO_LARGE )
231- . body ( Full :: new ( Bytes :: from ( "Request body too large" ) ) )
232- // unwrap safety: body only errors when previous chained calls failed.
233- . unwrap ( ) ) ;
224+ return Ok ( build_response (
225+ StatusCode :: PAYLOAD_TOO_LARGE ,
226+ Bytes :: from_static ( b"Request body too large" ) ,
227+ ) ) ;
234228 } ,
235229 } ;
236230 match T :: decode ( bytes) {
237231 Ok ( request) => match handler ( store. clone ( ) , user_token, request) . await {
238- Ok ( response) => Ok ( Response :: builder ( )
239- . body ( Full :: new ( Bytes :: from ( response. encode_to_vec ( ) ) ) )
240- // unwrap safety: body only errors when previous chained calls failed.
241- . unwrap ( ) ) ,
232+ Ok ( response) => {
233+ Ok ( build_response ( StatusCode :: OK , Bytes :: from ( response. encode_to_vec ( ) ) ) )
234+ } ,
242235 Err ( e) => Ok ( build_error_response ( e) ) ,
243236 } ,
244- Err ( _) => Ok ( Response :: builder ( )
245- . status ( StatusCode :: BAD_REQUEST )
246- . body ( Full :: new ( Bytes :: from ( b"Error parsing request" . to_vec ( ) ) ) )
247- // unwrap safety: body only errors when previous chained calls failed.
248- . unwrap ( ) ) ,
237+ Err ( _) => Ok ( build_response (
238+ StatusCode :: BAD_REQUEST ,
239+ Bytes :: from_static ( b"Error parsing request" ) ,
240+ ) ) ,
249241 }
250242}
251243
244+ fn build_response ( status_code : StatusCode , body : Bytes ) -> Response < Full < Bytes > > {
245+ Response :: builder ( )
246+ . status ( status_code)
247+ . header ( PROTOCOL_VERSION_HEADER , PROTOCOL_VERSION . as_str_name ( ) )
248+ . body ( Full :: new ( body) )
249+ // unwrap safety: body only errors when previous chained calls failed.
250+ . unwrap ( )
251+ }
252+
252253fn build_error_response ( e : VssError ) -> Response < Full < Bytes > > {
253254 let ( status_code, error_response) = match e {
254255 VssError :: NoSuchKeyError ( msg) => {
@@ -292,9 +293,36 @@ fn build_error_response(e: VssError) -> Response<Full<Bytes>> {
292293 ( status, error)
293294 } ,
294295 } ;
295- Response :: builder ( )
296- . status ( status_code)
297- . body ( Full :: new ( Bytes :: from ( error_response. encode_to_vec ( ) ) ) )
298- // unwrap safety: body only errors when previous chained calls failed.
299- . unwrap ( )
296+ build_response ( status_code, Bytes :: from ( error_response. encode_to_vec ( ) ) )
297+ }
298+
299+ #[ cfg( test) ]
300+ mod tests {
301+ use super :: * ;
302+
303+ #[ test]
304+ fn build_response_adds_protocol_version_header ( ) {
305+ let response = build_response ( StatusCode :: OK , Bytes :: new ( ) ) ;
306+
307+ assert_eq ! (
308+ Version :: from_str_name(
309+ response. headers( ) . get( PROTOCOL_VERSION_HEADER ) . unwrap( ) . to_str( ) . unwrap( )
310+ )
311+ . unwrap( ) ,
312+ PROTOCOL_VERSION ,
313+ ) ;
314+ }
315+
316+ #[ test]
317+ fn build_error_response_adds_protocol_version_header ( ) {
318+ let response = build_error_response ( VssError :: InvalidRequestError ( "bad request" . into ( ) ) ) ;
319+
320+ assert_eq ! (
321+ Version :: from_str_name(
322+ response. headers( ) . get( PROTOCOL_VERSION_HEADER ) . unwrap( ) . to_str( ) . unwrap( )
323+ )
324+ . unwrap( ) ,
325+ PROTOCOL_VERSION ,
326+ ) ;
327+ }
300328}
0 commit comments