From fdcc7e7c96b6ba50c6988d4bfd47666192e641cc Mon Sep 17 00:00:00 2001 From: Lann Martin Date: Mon, 9 Sep 2024 08:46:27 -0400 Subject: [PATCH] Simplify OutboundHttpInterceptor Signed-off-by: Lann Martin --- crates/factor-outbound-http/src/lib.rs | 17 +++++++---------- crates/factor-outbound-http/src/wasi.rs | 11 +++++++++-- crates/trigger-http/src/outbound_http.rs | 16 ++++------------ 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/crates/factor-outbound-http/src/lib.rs b/crates/factor-outbound-http/src/lib.rs index 127dc7fdf..0d576f044 100644 --- a/crates/factor-outbound-http/src/lib.rs +++ b/crates/factor-outbound-http/src/lib.rs @@ -17,7 +17,7 @@ use spin_factors::{ anyhow, ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder, }; use spin_world::async_trait; -use wasmtime_wasi_http::{types::IncomingResponse, WasiHttpCtx}; +use wasmtime_wasi_http::WasiHttpCtx; pub use wasmtime_wasi_http::{ body::HyperOutgoingBody, @@ -124,6 +124,7 @@ impl InstanceState { impl SelfInstanceBuilder for InstanceState {} pub type Request = http::Request; +pub type Response = http::Response; /// SelfRequestOrigin indicates the base URI to use for "self" requests. /// @@ -183,17 +184,13 @@ pub trait OutboundHttpInterceptor: Send + Sync { /// Intercept an outgoing HTTP request. /// /// If this method returns [`InterceptedResponse::Continue`], the (possibly - /// updated) request and config will be passed on to the default outgoing - /// request handler. + /// updated) request will be passed on to the default outgoing request + /// handler. /// /// If this method returns [`InterceptedResponse::Intercepted`], the inner /// result will be returned as the result of the request, bypassing the /// default handler. The `request` will also be dropped immediately. - async fn intercept( - &self, - request: &mut Request, - config: &mut OutgoingRequestConfig, - ) -> HttpResult; + async fn intercept(&self, request: &mut Request) -> HttpResult; } /// The type returned by an [`OutboundHttpInterceptor`]. @@ -201,7 +198,7 @@ pub enum InterceptOutcome { /// The intercepted request will be passed on to the default outgoing /// request handler. Continue, - /// The given result will be returned as the result of the intercepted + /// The given response will be returned as the result of the intercepted /// request, bypassing the default handler. - Complete(IncomingResponse), + Complete(Response), } diff --git a/crates/factor-outbound-http/src/wasi.rs b/crates/factor-outbound-http/src/wasi.rs index 0d32e52a8..67fabc4a1 100644 --- a/crates/factor-outbound-http/src/wasi.rs +++ b/crates/factor-outbound-http/src/wasi.rs @@ -133,9 +133,16 @@ async fn send_request_impl( spin_telemetry::inject_trace_context(&mut request); if let Some(interceptor) = request_interceptor { - match interceptor.intercept(&mut request, &mut config).await? { + match interceptor.intercept(&mut request).await? { InterceptOutcome::Continue => (), - InterceptOutcome::Complete(resp) => return Ok(Ok(resp)), + InterceptOutcome::Complete(resp) => { + let resp = IncomingResponse { + resp, + worker: None, + between_bytes_timeout: config.between_bytes_timeout, + }; + return Ok(Ok(resp)); + } } } diff --git a/crates/trigger-http/src/outbound_http.rs b/crates/trigger-http/src/outbound_http.rs index 9db66ce04..f5cc556d2 100644 --- a/crates/trigger-http/src/outbound_http.rs +++ b/crates/trigger-http/src/outbound_http.rs @@ -5,11 +5,11 @@ use std::{ use http::uri::Scheme; use spin_core::async_trait; -use spin_factor_outbound_http::{InterceptOutcome, OutgoingRequestConfig, Request}; +use spin_factor_outbound_http::{InterceptOutcome, Request}; use spin_factor_outbound_networking::parse_service_chaining_target; use spin_factors::RuntimeFactors; use spin_http::routes::RouteMatch; -use wasmtime_wasi_http::{types::IncomingResponse, HttpError, HttpResult}; +use wasmtime_wasi_http::{HttpError, HttpResult}; use crate::HttpServer; @@ -30,11 +30,7 @@ const CHAINED_CLIENT_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new impl spin_factor_outbound_http::OutboundHttpInterceptor for OutboundHttpInterceptor { - async fn intercept( - &self, - request: &mut Request, - config: &mut OutgoingRequestConfig, - ) -> HttpResult { + async fn intercept(&self, request: &mut Request) -> HttpResult { // Handle service chaining requests if let Some(component_id) = parse_service_chaining_target(request.uri()) { let req = std::mem::take(request); @@ -44,11 +40,7 @@ impl spin_factor_outbound_http::OutboundHttpInterceptor .handle_trigger_route(req, route_match, Scheme::HTTP, CHAINED_CLIENT_ADDR) .await .map_err(HttpError::trap)?; - Ok(InterceptOutcome::Complete(IncomingResponse { - resp, - worker: None, - between_bytes_timeout: config.between_bytes_timeout, - })) + Ok(InterceptOutcome::Complete(resp)) } else { Ok(InterceptOutcome::Continue) }