Skip to content

Commit

Permalink
Weave scheme through in the definition of self requests
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Jul 26, 2024
1 parent ee44d26 commit d1b1a85
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
14 changes: 9 additions & 5 deletions crates/trigger-http/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{net::SocketAddr, str, str::FromStr};

use crate::{Body, ChainedRequestHandler, HttpExecutor, HttpInstance, HttpTrigger, Store};
use crate::{
Body, ChainedRequestHandler, HttpExecutor, HttpInstance, HttpTrigger, SelfRequestDefinition,
Store,
};
use anyhow::{anyhow, Context, Result};
use futures::TryFutureExt;
use http::{HeaderName, HeaderValue};
Expand Down Expand Up @@ -33,7 +36,7 @@ impl HttpExecutor for HttpHandlerExecutor {
route_match: &RouteMatch,
req: Request<Body>,
client_addr: SocketAddr,
self_authority: &str,
self_definition: SelfRequestDefinition,
) -> Result<Response<Body>> {
let component_id = route_match.component_id();

Expand All @@ -47,7 +50,7 @@ impl HttpExecutor for HttpHandlerExecutor {
unreachable!()
};

set_http_origin(&mut store, engine.clone(), self, &self_authority);
set_http_origin(&mut store, engine.clone(), self, self_definition);

// set the client tls options for the current component_id.
// The OutboundWasiHttpHandler in this file is only used
Expand Down Expand Up @@ -395,9 +398,9 @@ fn set_http_origin(
store: &mut Store,
engine: Arc<TriggerAppEngine<HttpTrigger>>,
handler: &HttpHandlerExecutor,
self_authority: &str,
self_definition: SelfRequestDefinition,
) {
let origin = format!("http://{self_authority}");
let origin = format!("{}://{}", self_definition.scheme, self_definition.authority);
if let Some(outbound_http_handle) = engine
.engine
.find_host_component_handle::<Arc<OutboundHttpComponent>>()
Expand All @@ -413,6 +416,7 @@ fn set_http_origin(
let chained_request_handler = ChainedRequestHandler {
engine: engine.clone(),
executor: handler.clone(),
self_definition,
};
store.as_mut().data_mut().as_mut().origin = Some(origin);
store.as_mut().data_mut().as_mut().chained_handler = Some(chained_request_handler);
Expand Down
25 changes: 19 additions & 6 deletions crates/trigger-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl HttpTrigger {
server_addr: SocketAddr,
client_addr: SocketAddr,
) -> Result<Response<Body>> {
set_req_uri(&mut req, scheme)?;
set_req_uri(&mut req, scheme.clone())?;
strip_forbidden_headers(&mut req);

spin_telemetry::extract_trace_context(&req);
Expand Down Expand Up @@ -278,6 +278,12 @@ impl HttpTrigger {
let trigger = self.component_trigger_configs.get(component_id).unwrap();

let executor = trigger.executor.as_ref().unwrap_or(&HttpExecutorType::Http);
// Set the definition of outbound requests to `self` to be equal to
// the incoming request's scheme and the bound listening address.
let self_definition = SelfRequestDefinition {
scheme,
authority: server_addr.to_string(),
};

let res = match executor {
HttpExecutorType::Http => {
Expand All @@ -288,7 +294,7 @@ impl HttpTrigger {
&route_match,
req,
client_addr,
server_addr.to_string().as_str(),
self_definition,
)
.await
}
Expand All @@ -303,7 +309,7 @@ impl HttpTrigger {
&route_match,
req,
client_addr,
server_addr.to_string().as_str(),
self_definition,
)
.await
}
Expand Down Expand Up @@ -594,14 +600,22 @@ pub(crate) trait HttpExecutor: Clone + Send + Sync + 'static {
route_match: &RouteMatch,
req: Request<Body>,
client_addr: SocketAddr,
self_authority: &str,
self_definition: SelfRequestDefinition,
) -> Result<Response<Body>>;
}

/// The definition of the `self` host for outbound requests.
#[derive(Clone)]
pub struct SelfRequestDefinition {
scheme: Scheme,
authority: String,
}

#[derive(Clone)]
struct ChainedRequestHandler {
engine: Arc<TriggerAppEngine<HttpTrigger>>,
executor: HttpHandlerExecutor,
self_definition: SelfRequestDefinition,
}

#[derive(Default)]
Expand Down Expand Up @@ -642,7 +656,6 @@ impl HttpRuntimeData {

let between_bytes_timeout = config.between_bytes_timeout;

let self_authority = this.origin.clone().expect("origin must be set");
let resp_fut = async move {
match handler
.execute(
Expand All @@ -651,7 +664,7 @@ impl HttpRuntimeData {
&route_match,
request,
client_addr,
&self_authority,
chained_handler.self_definition,
)
.await
{
Expand Down
4 changes: 2 additions & 2 deletions crates/trigger-http/src/wagi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{io::Cursor, net::SocketAddr, sync::Arc};

use crate::HttpInstance;
use crate::{HttpInstance, SelfRequestDefinition};
use anyhow::{anyhow, ensure, Context, Result};
use async_trait::async_trait;
use http_body_util::BodyExt;
Expand Down Expand Up @@ -28,7 +28,7 @@ impl HttpExecutor for WagiHttpExecutor {
route_match: &RouteMatch,
req: Request<Body>,
client_addr: SocketAddr,
_self_authority: &str,
_self_authority: SelfRequestDefinition,
) -> Result<Response<Body>> {
let component = route_match.component_id();

Expand Down
2 changes: 1 addition & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ Caused by:
}

#[test]
fn outbound_http_works() -> anyhow::Result<()> {
fn outbound_http_to_same_app_works() -> anyhow::Result<()> {
run_test(
"outbound-http-to-same-app",
SpinConfig {
Expand Down

0 comments on commit d1b1a85

Please sign in to comment.