-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2863 from fermyon/more-http
Move `HandlerType` to `spin-http`
- Loading branch information
Showing
8 changed files
with
87 additions
and
70 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use wasmtime::component::Component; | ||
|
||
#[derive(Clone, Debug, Default, Deserialize, Serialize)] | ||
#[serde(deny_unknown_fields)] | ||
|
@@ -11,3 +12,67 @@ pub struct Metadata { | |
pub fn default_base() -> String { | ||
"/".into() | ||
} | ||
|
||
/// The type of http handler export used by a component. | ||
#[derive(Clone, Copy)] | ||
pub enum HandlerType { | ||
Spin, | ||
Wagi, | ||
Wasi0_2, | ||
Wasi2023_11_10, | ||
Wasi2023_10_18, | ||
} | ||
|
||
/// The `incoming-handler` export for `wasi:http` version rc-2023-10-18 | ||
pub const WASI_HTTP_EXPORT_2023_10_18: &str = "wasi:http/[email protected]"; | ||
/// The `incoming-handler` export for `wasi:http` version rc-2023-11-10 | ||
pub const WASI_HTTP_EXPORT_2023_11_10: &str = "wasi:http/[email protected]"; | ||
/// The `incoming-handler` export for `wasi:http` version 0.2.0 | ||
pub const WASI_HTTP_EXPORT_0_2_0: &str = "wasi:http/[email protected]"; | ||
/// The `incoming-handler` export for `wasi:http` version 0.2.1 | ||
pub const WASI_HTTP_EXPORT_0_2_1: &str = "wasi:http/[email protected]"; | ||
/// The `inbound-http` export for `fermyon:spin` | ||
pub const SPIN_HTTP_EXPORT: &str = "fermyon:spin/inbound-http"; | ||
|
||
impl HandlerType { | ||
/// Determine the handler type from the exports of a component. | ||
pub fn from_component( | ||
engine: &wasmtime::Engine, | ||
component: &Component, | ||
) -> anyhow::Result<HandlerType> { | ||
let mut handler_ty = None; | ||
|
||
let mut set = |ty: HandlerType| { | ||
if handler_ty.is_none() { | ||
handler_ty = Some(ty); | ||
Ok(()) | ||
} else { | ||
Err(anyhow::anyhow!( | ||
"component exports multiple different handlers but \ | ||
it's expected to export only one" | ||
)) | ||
} | ||
}; | ||
let ty = component.component_type(); | ||
for (name, _) in ty.exports(engine) { | ||
match name { | ||
WASI_HTTP_EXPORT_2023_10_18 => set(HandlerType::Wasi2023_10_18)?, | ||
WASI_HTTP_EXPORT_2023_11_10 => set(HandlerType::Wasi2023_11_10)?, | ||
WASI_HTTP_EXPORT_0_2_0 | WASI_HTTP_EXPORT_0_2_1 => set(HandlerType::Wasi0_2)?, | ||
SPIN_HTTP_EXPORT => set(HandlerType::Spin)?, | ||
_ => {} | ||
} | ||
} | ||
|
||
handler_ty.ok_or_else(|| { | ||
anyhow::anyhow!( | ||
"Expected component to export one of \ | ||
`{WASI_HTTP_EXPORT_2023_10_18}`, \ | ||
`{WASI_HTTP_EXPORT_2023_11_10}`, \ | ||
`{WASI_HTTP_EXPORT_0_2_0}`, \ | ||
`{WASI_HTTP_EXPORT_0_2_1}`, \ | ||
or `{SPIN_HTTP_EXPORT}` but it exported none of those" | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,14 +20,14 @@ use spin_http::{ | |
body, | ||
config::{HttpExecutorType, HttpTriggerConfig}, | ||
routes::{RouteMatch, Router}, | ||
trigger::HandlerType, | ||
}; | ||
use tokio::{ | ||
io::{AsyncRead, AsyncWrite}, | ||
net::TcpListener, | ||
task, | ||
}; | ||
use tracing::Instrument; | ||
use wasmtime::component::Component; | ||
use wasmtime_wasi_http::body::HyperOutgoingBody; | ||
|
||
use crate::{ | ||
|
@@ -104,7 +104,7 @@ impl<F: RuntimeFactors> HttpServer<F> { | |
let handler_type = match &trigger_config.executor { | ||
None | Some(HttpExecutorType::Http) => { | ||
let component = trigger_app.get_component(component_id)?; | ||
HandlerType::from_component(trigger_app.engine(), component)? | ||
HandlerType::from_component(trigger_app.engine().as_ref(), component)? | ||
} | ||
Some(HttpExecutorType::Wagi(wagi_config)) => { | ||
anyhow::ensure!( | ||
|
@@ -464,61 +464,3 @@ pub(crate) trait HttpExecutor: Clone + Send + Sync + 'static { | |
client_addr: SocketAddr, | ||
) -> impl Future<Output = anyhow::Result<Response<Body>>>; | ||
} | ||
|
||
/// Whether this handler uses the custom Spin http handler interface for wasi-http | ||
#[derive(Clone, Copy)] | ||
pub enum HandlerType { | ||
Spin, | ||
Wagi, | ||
Wasi0_2, | ||
Wasi2023_11_10, | ||
Wasi2023_10_18, | ||
} | ||
|
||
pub const WASI_HTTP_EXPORT_2023_10_18: &str = "wasi:http/[email protected]"; | ||
pub const WASI_HTTP_EXPORT_2023_11_10: &str = "wasi:http/[email protected]"; | ||
pub const WASI_HTTP_EXPORT_0_2_0: &str = "wasi:http/[email protected]"; | ||
pub const WASI_HTTP_EXPORT_0_2_1: &str = "wasi:http/[email protected]"; | ||
|
||
impl HandlerType { | ||
/// Determine the handler type from the exports of a component | ||
pub fn from_component( | ||
engine: impl AsRef<wasmtime::Engine>, | ||
component: &Component, | ||
) -> anyhow::Result<HandlerType> { | ||
let mut handler_ty = None; | ||
|
||
let mut set = |ty: HandlerType| { | ||
if handler_ty.is_none() { | ||
handler_ty = Some(ty); | ||
Ok(()) | ||
} else { | ||
Err(anyhow::anyhow!( | ||
"component exports multiple different handlers but \ | ||
it's expected to export only one" | ||
)) | ||
} | ||
}; | ||
let ty = component.component_type(); | ||
for (name, _) in ty.exports(engine.as_ref()) { | ||
match name { | ||
WASI_HTTP_EXPORT_2023_10_18 => set(HandlerType::Wasi2023_10_18)?, | ||
WASI_HTTP_EXPORT_2023_11_10 => set(HandlerType::Wasi2023_11_10)?, | ||
WASI_HTTP_EXPORT_0_2_0 | WASI_HTTP_EXPORT_0_2_1 => set(HandlerType::Wasi0_2)?, | ||
"fermyon:spin/inbound-http" => set(HandlerType::Spin)?, | ||
_ => {} | ||
} | ||
} | ||
|
||
handler_ty.ok_or_else(|| { | ||
anyhow::anyhow!( | ||
"Expected component to export one of \ | ||
`{WASI_HTTP_EXPORT_2023_10_18}`, \ | ||
`{WASI_HTTP_EXPORT_2023_11_10}`, \ | ||
`{WASI_HTTP_EXPORT_0_2_0}`, \ | ||
`{WASI_HTTP_EXPORT_0_2_1}`, \ | ||
or `fermyon:spin/inbound-http` but it exported none of those" | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters