Skip to content

Commit

Permalink
Simplify OutboundHttpInterceptor
Browse files Browse the repository at this point in the history
Signed-off-by: Lann Martin <[email protected]>
  • Loading branch information
lann committed Sep 9, 2024
1 parent b3ab371 commit fdcc7e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
17 changes: 7 additions & 10 deletions crates/factor-outbound-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -124,6 +124,7 @@ impl InstanceState {
impl SelfInstanceBuilder for InstanceState {}

pub type Request = http::Request<wasmtime_wasi_http::body::HyperOutgoingBody>;
pub type Response = http::Response<wasmtime_wasi_http::body::HyperIncomingBody>;

/// SelfRequestOrigin indicates the base URI to use for "self" requests.
///
Expand Down Expand Up @@ -183,25 +184,21 @@ 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<InterceptOutcome>;
async fn intercept(&self, request: &mut Request) -> HttpResult<InterceptOutcome>;
}

/// The type returned by an [`OutboundHttpInterceptor`].
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),
}
11 changes: 9 additions & 2 deletions crates/factor-outbound-http/src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}

Expand Down
16 changes: 4 additions & 12 deletions crates/trigger-http/src/outbound_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,11 +30,7 @@ const CHAINED_CLIENT_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new
impl<F: RuntimeFactors> spin_factor_outbound_http::OutboundHttpInterceptor
for OutboundHttpInterceptor<F>
{
async fn intercept(
&self,
request: &mut Request,
config: &mut OutgoingRequestConfig,
) -> HttpResult<InterceptOutcome> {
async fn intercept(&self, request: &mut Request) -> HttpResult<InterceptOutcome> {
// Handle service chaining requests
if let Some(component_id) = parse_service_chaining_target(request.uri()) {
let req = std::mem::take(request);
Expand All @@ -44,11 +40,7 @@ impl<F: RuntimeFactors> 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)
}
Expand Down

0 comments on commit fdcc7e7

Please sign in to comment.