Skip to content

Commit fdcc7e7

Browse files
committed
Simplify OutboundHttpInterceptor
Signed-off-by: Lann Martin <[email protected]>
1 parent b3ab371 commit fdcc7e7

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

crates/factor-outbound-http/src/lib.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use spin_factors::{
1717
anyhow, ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
1818
};
1919
use spin_world::async_trait;
20-
use wasmtime_wasi_http::{types::IncomingResponse, WasiHttpCtx};
20+
use wasmtime_wasi_http::WasiHttpCtx;
2121

2222
pub use wasmtime_wasi_http::{
2323
body::HyperOutgoingBody,
@@ -124,6 +124,7 @@ impl InstanceState {
124124
impl SelfInstanceBuilder for InstanceState {}
125125

126126
pub type Request = http::Request<wasmtime_wasi_http::body::HyperOutgoingBody>;
127+
pub type Response = http::Response<wasmtime_wasi_http::body::HyperIncomingBody>;
127128

128129
/// SelfRequestOrigin indicates the base URI to use for "self" requests.
129130
///
@@ -183,25 +184,21 @@ pub trait OutboundHttpInterceptor: Send + Sync {
183184
/// Intercept an outgoing HTTP request.
184185
///
185186
/// If this method returns [`InterceptedResponse::Continue`], the (possibly
186-
/// updated) request and config will be passed on to the default outgoing
187-
/// request handler.
187+
/// updated) request will be passed on to the default outgoing request
188+
/// handler.
188189
///
189190
/// If this method returns [`InterceptedResponse::Intercepted`], the inner
190191
/// result will be returned as the result of the request, bypassing the
191192
/// default handler. The `request` will also be dropped immediately.
192-
async fn intercept(
193-
&self,
194-
request: &mut Request,
195-
config: &mut OutgoingRequestConfig,
196-
) -> HttpResult<InterceptOutcome>;
193+
async fn intercept(&self, request: &mut Request) -> HttpResult<InterceptOutcome>;
197194
}
198195

199196
/// The type returned by an [`OutboundHttpInterceptor`].
200197
pub enum InterceptOutcome {
201198
/// The intercepted request will be passed on to the default outgoing
202199
/// request handler.
203200
Continue,
204-
/// The given result will be returned as the result of the intercepted
201+
/// The given response will be returned as the result of the intercepted
205202
/// request, bypassing the default handler.
206-
Complete(IncomingResponse),
203+
Complete(Response),
207204
}

crates/factor-outbound-http/src/wasi.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,16 @@ async fn send_request_impl(
133133
spin_telemetry::inject_trace_context(&mut request);
134134

135135
if let Some(interceptor) = request_interceptor {
136-
match interceptor.intercept(&mut request, &mut config).await? {
136+
match interceptor.intercept(&mut request).await? {
137137
InterceptOutcome::Continue => (),
138-
InterceptOutcome::Complete(resp) => return Ok(Ok(resp)),
138+
InterceptOutcome::Complete(resp) => {
139+
let resp = IncomingResponse {
140+
resp,
141+
worker: None,
142+
between_bytes_timeout: config.between_bytes_timeout,
143+
};
144+
return Ok(Ok(resp));
145+
}
139146
}
140147
}
141148

crates/trigger-http/src/outbound_http.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use std::{
55

66
use http::uri::Scheme;
77
use spin_core::async_trait;
8-
use spin_factor_outbound_http::{InterceptOutcome, OutgoingRequestConfig, Request};
8+
use spin_factor_outbound_http::{InterceptOutcome, Request};
99
use spin_factor_outbound_networking::parse_service_chaining_target;
1010
use spin_factors::RuntimeFactors;
1111
use spin_http::routes::RouteMatch;
12-
use wasmtime_wasi_http::{types::IncomingResponse, HttpError, HttpResult};
12+
use wasmtime_wasi_http::{HttpError, HttpResult};
1313

1414
use crate::HttpServer;
1515

@@ -30,11 +30,7 @@ const CHAINED_CLIENT_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new
3030
impl<F: RuntimeFactors> spin_factor_outbound_http::OutboundHttpInterceptor
3131
for OutboundHttpInterceptor<F>
3232
{
33-
async fn intercept(
34-
&self,
35-
request: &mut Request,
36-
config: &mut OutgoingRequestConfig,
37-
) -> HttpResult<InterceptOutcome> {
33+
async fn intercept(&self, request: &mut Request) -> HttpResult<InterceptOutcome> {
3834
// Handle service chaining requests
3935
if let Some(component_id) = parse_service_chaining_target(request.uri()) {
4036
let req = std::mem::take(request);
@@ -44,11 +40,7 @@ impl<F: RuntimeFactors> spin_factor_outbound_http::OutboundHttpInterceptor
4440
.handle_trigger_route(req, route_match, Scheme::HTTP, CHAINED_CLIENT_ADDR)
4541
.await
4642
.map_err(HttpError::trap)?;
47-
Ok(InterceptOutcome::Complete(IncomingResponse {
48-
resp,
49-
worker: None,
50-
between_bytes_timeout: config.between_bytes_timeout,
51-
}))
43+
Ok(InterceptOutcome::Complete(resp))
5244
} else {
5345
Ok(InterceptOutcome::Continue)
5446
}

0 commit comments

Comments
 (0)