diff --git a/rpc-toolkit/src/server/http.rs b/rpc-toolkit/src/server/http.rs index 8eba483..c5da3f1 100644 --- a/rpc-toolkit/src/server/http.rs +++ b/rpc-toolkit/src/server/http.rs @@ -54,15 +54,16 @@ pub trait Middleware: Clone + Send + Sync + 'static { #[allow(unused_variables)] async fn process_rpc_request( &mut self, + context: &Context, metadata: Self::Metadata, request: &mut RpcRequest, ) -> Result<(), RpcResponse> { Ok(()) } #[allow(unused_variables)] - async fn process_rpc_response(&mut self, response: &mut RpcResponse) {} + async fn process_rpc_response(&mut self, context: &Context, response: &mut RpcResponse) {} #[allow(unused_variables)] - async fn process_http_response(&mut self, response: &mut Response) {} + async fn process_http_response(&mut self, context: &Context, response: &mut Response) {} } #[allow(private_bounds)] @@ -75,12 +76,19 @@ trait _Middleware: Send + Sync { ) -> BoxFuture<'a, Result<(), Response>>; fn process_rpc_request<'a>( &'a mut self, + context: &'a Context, metadata: Value, request: &'a mut RpcRequest, ) -> BoxFuture<'a, Result<(), RpcResponse>>; - fn process_rpc_response<'a>(&'a mut self, response: &'a mut RpcResponse) -> BoxFuture<'a, ()>; + fn process_rpc_response<'a>( + &'a mut self, + + context: &'a Context, + response: &'a mut RpcResponse, + ) -> BoxFuture<'a, ()>; fn process_http_response<'a>( &'a mut self, + context: &'a Context, response: &'a mut Response, ) -> BoxFuture<'a, ()>; } @@ -97,11 +105,13 @@ impl + Send + Sync> _Middleware< } fn process_rpc_request<'a>( &'a mut self, + context: &'a Context, metadata: Value, request: &'a mut RpcRequest, ) -> BoxFuture<'a, Result<(), RpcResponse>> { >::process_rpc_request( self, + context, match imbl_value::from_value(metadata) { Ok(a) => a, Err(e) => return async { Err(internal_error(e).into()) }.boxed(), @@ -109,14 +119,19 @@ impl + Send + Sync> _Middleware< request, ) } - fn process_rpc_response<'a>(&'a mut self, response: &'a mut RpcResponse) -> BoxFuture<'a, ()> { - >::process_rpc_response(self, response) + fn process_rpc_response<'a>( + &'a mut self, + context: &'a Context, + response: &'a mut RpcResponse, + ) -> BoxFuture<'a, ()> { + >::process_rpc_response(self, context, response) } fn process_http_response<'a>( &'a mut self, + context: &'a Context, response: &'a mut Response, ) -> BoxFuture<'a, ()> { - >::process_http_response(self, response) + >::process_http_response(self, context, response) } } @@ -171,10 +186,11 @@ impl HttpServer { .map_err(parse_error)? { SingleOrBatchRpcRequest::Single(rpc_req) => { - let mut res = - json_http_response(&self.process_rpc_request(&mut mid, rpc_req).await); + let mut res = json_http_response( + &self.process_rpc_request(&ctx, &mut mid, rpc_req).await, + ); for middleware in mid.iter_mut() { - middleware.0.process_http_response(&mut res).await; + middleware.0.process_http_response(&ctx, &mut res).await; } Ok(res) } @@ -182,7 +198,7 @@ impl HttpServer { let (mids, rpc_res): (Vec<_>, Vec<_>) = join_all(rpc_reqs.into_iter().map(|rpc_req| async { let mut mid = mid.clone(); - let res = self.process_rpc_request(&mut mid, rpc_req).await; + let res = self.process_rpc_request(&ctx, &mut mid, rpc_req).await; (mid, res) })) .await @@ -199,7 +215,7 @@ impl HttpServer { }, ) { for middleware in mid.iter_mut() { - middleware.0.process_http_response(&mut res).await; + middleware.0.process_http_response(&ctx, &mut res).await; } } Ok(res) @@ -217,6 +233,7 @@ impl HttpServer { } async fn process_rpc_request( &self, + ctx: &Context, mid: &mut Vec>, mut req: RpcRequest, ) -> RpcResponse { @@ -247,7 +264,7 @@ impl HttpServer { for middleware in mid.iter_mut().rev() { if let Err(res) = middleware .0 - .process_rpc_request(metadata.clone(), &mut req) + .process_rpc_request(ctx, metadata.clone(), &mut req) .await { return res; @@ -257,7 +274,7 @@ impl HttpServer { } .await; for middleware in mid.iter_mut() { - middleware.0.process_rpc_response(&mut res).await; + middleware.0.process_rpc_response(ctx, &mut res).await; } res }