Skip to content

Commit a8b4c8a

Browse files
committed
factors: Refactor OutboundHttpFactor SelfRequestOrigin
Signed-off-by: Lann Martin <[email protected]>
1 parent 4237a81 commit a8b4c8a

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl Factor for OutboundHttpFactor {
6969
wasi_http_ctx: WasiHttpCtx::new(),
7070
allowed_hosts,
7171
component_tls_configs,
72+
self_request_origin: None,
7273
request_interceptor: None,
7374
})
7475
}
@@ -78,10 +79,19 @@ pub struct InstanceState {
7879
wasi_http_ctx: WasiHttpCtx,
7980
allowed_hosts: OutboundAllowedHosts,
8081
component_tls_configs: ComponentTlsConfigs,
82+
self_request_origin: Option<SelfRequestOrigin>,
8183
request_interceptor: Option<Box<dyn OutboundHttpInterceptor>>,
8284
}
8385

8486
impl InstanceState {
87+
/// Sets the [`SelfRequestOrigin`] for this instance.
88+
///
89+
/// This is used to handle outbound requests to relative URLs. If unset,
90+
/// those requests will fail.
91+
pub fn set_self_request_origin(&mut self, origin: SelfRequestOrigin) {
92+
self.self_request_origin = Some(origin);
93+
}
94+
8595
/// Sets a [`OutboundHttpInterceptor`] for this instance.
8696
///
8797
/// Returns an error if it has already been called for this instance.

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl<'a> WasiHttpView for WasiHttpImplInner<'a> {
106106
request,
107107
config,
108108
self.state.allowed_hosts.clone(),
109+
self.state.self_request_origin.clone(),
109110
tls_client_config,
110111
)
111112
.in_current_span(),
@@ -118,6 +119,7 @@ async fn send_request_impl(
118119
mut request: Request<wasmtime_wasi_http::body::HyperOutgoingBody>,
119120
mut config: wasmtime_wasi_http::types::OutgoingRequestConfig,
120121
outbound_allowed_hosts: OutboundAllowedHosts,
122+
self_request_origin: Option<SelfRequestOrigin>,
121123
tls_client_config: Arc<ClientConfig>,
122124
) -> anyhow::Result<Result<IncomingResponse, ErrorCode>> {
123125
if request.uri().authority().is_some() {
@@ -137,10 +139,7 @@ async fn send_request_impl(
137139
return Ok(Err(ErrorCode::HttpRequestDenied));
138140
}
139141

140-
let origin = request
141-
.extensions()
142-
.get::<SelfRequestOrigin>()
143-
.cloned()
142+
let origin = self_request_origin
144143
.context("cannot send relative outbound request; no 'origin' set by host")?;
145144

146145
config.use_tls = origin.use_tls();

crates/factor-outbound-http/tests/factor_test.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ async fn allowed_host_is_allowed() -> anyhow::Result<()> {
4040
#[tokio::test]
4141
async fn self_request_smoke_test() -> anyhow::Result<()> {
4242
let mut state = test_instance_state("http://self").await?;
43-
let mut wasi_http = OutboundHttpFactor::get_wasi_http_impl(&mut state).unwrap();
43+
let origin = SelfRequestOrigin::from_uri(&Uri::from_static("http://[100::1]"))?;
44+
state.http.set_self_request_origin(origin);
4445

45-
let mut req = Request::get("/self-request").body(Default::default())?;
46-
let origin = Uri::from_static("http://[100::1]");
47-
req.extensions_mut()
48-
.insert(SelfRequestOrigin::from_uri(&origin).unwrap());
46+
let mut wasi_http = OutboundHttpFactor::get_wasi_http_impl(&mut state).unwrap();
47+
let req = Request::get("/self-request").body(Default::default())?;
4948
let mut future_resp = wasi_http.send_request(req, test_request_config())?;
5049
future_resp.ready().await;
5150

crates/trigger-http2/src/outbound_http.rs

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

66
use http::uri::Scheme;
77
use spin_factor_outbound_http::{
8-
HostFutureIncomingResponse, InterceptOutcome, OutgoingRequestConfig, Request, SelfRequestOrigin,
8+
HostFutureIncomingResponse, InterceptOutcome, OutgoingRequestConfig, Request,
99
};
1010
use spin_http::routes::RouteMatch;
1111
use spin_outbound_networking::parse_service_chaining_target;
@@ -16,12 +16,11 @@ use crate::HttpServer;
1616
/// An outbound HTTP interceptor that handles service chaining requests.
1717
pub struct OutboundHttpInterceptor {
1818
server: Arc<HttpServer>,
19-
origin: SelfRequestOrigin,
2019
}
2120

2221
impl OutboundHttpInterceptor {
23-
pub fn new(server: Arc<HttpServer>, origin: SelfRequestOrigin) -> Self {
24-
Self { server, origin }
22+
pub fn new(server: Arc<HttpServer>) -> Self {
23+
Self { server }
2524
}
2625
}
2726

@@ -58,7 +57,6 @@ impl spin_factor_outbound_http::OutboundHttpInterceptor for OutboundHttpIntercep
5857
let resp = HostFutureIncomingResponse::pending(wasmtime_wasi::runtime::spawn(resp_fut));
5958
InterceptOutcome::Complete(Ok(resp))
6059
} else {
61-
request.extensions_mut().insert(self.origin.clone());
6260
InterceptOutcome::Continue
6361
}
6462
}

crates/trigger-http2/src/server.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,10 @@ impl HttpServer {
188188
let mut instance_builder = self.trigger_app.prepare(component_id)?;
189189

190190
// Set up outbound HTTP request origin and service chaining
191+
let outbound_http = instance_builder.factor_builders().outbound_http();
191192
let origin = SelfRequestOrigin::create(server_scheme, &self.listen_addr)?;
192-
instance_builder
193-
.factor_builders()
194-
.outbound_http()
195-
.set_request_interceptor(OutboundHttpInterceptor::new(self.clone(), origin))?;
193+
outbound_http.set_self_request_origin(origin);
194+
outbound_http.set_request_interceptor(OutboundHttpInterceptor::new(self.clone()))?;
196195

197196
// Prepare HTTP executor
198197
let trigger_config = self.component_trigger_configs.get(component_id).unwrap();

0 commit comments

Comments
 (0)