Skip to content

Commit 0772633

Browse files
committed
Move HandlerType to spin-http
Signed-off-by: Ryan Levick <[email protected]>
1 parent a22119c commit 0772633

File tree

5 files changed

+68
-64
lines changed

5 files changed

+68
-64
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/http/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ serde = { workspace = true }
1616
spin-app = { path = "../app", optional = true }
1717
spin-locked-app = { path = "../locked-app" }
1818
tracing = { workspace = true }
19+
wasmtime = { workspace = true }
1920
wasmtime-wasi-http = { workspace = true, optional = true }
2021

2122
[dev-dependencies]

crates/http/src/trigger.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use serde::{Deserialize, Serialize};
2+
use wasmtime::component::Component;
23

34
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
45
#[serde(deny_unknown_fields)]
@@ -11,3 +12,65 @@ pub struct Metadata {
1112
pub fn default_base() -> String {
1213
"/".into()
1314
}
15+
16+
/// The type of http handler export used by a component.
17+
#[derive(Clone, Copy)]
18+
pub enum HandlerType {
19+
Spin,
20+
Wagi,
21+
Wasi0_2,
22+
Wasi2023_11_10,
23+
Wasi2023_10_18,
24+
}
25+
26+
/// The `incoming-handler` export for `wasi:http` version rc-2023-10-18
27+
pub const WASI_HTTP_EXPORT_2023_10_18: &str = "wasi:http/[email protected]";
28+
/// The `incoming-handler` export for `wasi:http` version rc-2023-11-10
29+
pub const WASI_HTTP_EXPORT_2023_11_10: &str = "wasi:http/[email protected]";
30+
/// The `incoming-handler` export for `wasi:http` version 0.2.0
31+
pub const WASI_HTTP_EXPORT_0_2_0: &str = "wasi:http/[email protected]";
32+
/// The `incoming-handler` export for `wasi:http` version 0.2.1
33+
pub const WASI_HTTP_EXPORT_0_2_1: &str = "wasi:http/[email protected]";
34+
35+
impl HandlerType {
36+
/// Determine the handler type from the exports of a component
37+
pub fn from_component(
38+
engine: impl AsRef<wasmtime::Engine>,
39+
component: &Component,
40+
) -> anyhow::Result<HandlerType> {
41+
let mut handler_ty = None;
42+
43+
let mut set = |ty: HandlerType| {
44+
if handler_ty.is_none() {
45+
handler_ty = Some(ty);
46+
Ok(())
47+
} else {
48+
Err(anyhow::anyhow!(
49+
"component exports multiple different handlers but \
50+
it's expected to export only one"
51+
))
52+
}
53+
};
54+
let ty = component.component_type();
55+
for (name, _) in ty.exports(engine.as_ref()) {
56+
match name {
57+
WASI_HTTP_EXPORT_2023_10_18 => set(HandlerType::Wasi2023_10_18)?,
58+
WASI_HTTP_EXPORT_2023_11_10 => set(HandlerType::Wasi2023_11_10)?,
59+
WASI_HTTP_EXPORT_0_2_0 | WASI_HTTP_EXPORT_0_2_1 => set(HandlerType::Wasi0_2)?,
60+
"fermyon:spin/inbound-http" => set(HandlerType::Spin)?,
61+
_ => {}
62+
}
63+
}
64+
65+
handler_ty.ok_or_else(|| {
66+
anyhow::anyhow!(
67+
"Expected component to export one of \
68+
`{WASI_HTTP_EXPORT_2023_10_18}`, \
69+
`{WASI_HTTP_EXPORT_2023_11_10}`, \
70+
`{WASI_HTTP_EXPORT_0_2_0}`, \
71+
`{WASI_HTTP_EXPORT_0_2_1}`, \
72+
or `fermyon:spin/inbound-http` but it exported none of those"
73+
)
74+
})
75+
}
76+
}

crates/trigger-http/src/server.rs

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ use spin_http::{
2020
body,
2121
config::{HttpExecutorType, HttpTriggerConfig},
2222
routes::{RouteMatch, Router},
23+
trigger::HandlerType,
2324
};
2425
use tokio::{
2526
io::{AsyncRead, AsyncWrite},
2627
net::TcpListener,
2728
task,
2829
};
2930
use tracing::Instrument;
30-
use wasmtime::component::Component;
3131
use wasmtime_wasi_http::body::HyperOutgoingBody;
3232

3333
use crate::{
@@ -464,61 +464,3 @@ pub(crate) trait HttpExecutor: Clone + Send + Sync + 'static {
464464
client_addr: SocketAddr,
465465
) -> impl Future<Output = anyhow::Result<Response<Body>>>;
466466
}
467-
468-
/// Whether this handler uses the custom Spin http handler interface for wasi-http
469-
#[derive(Clone, Copy)]
470-
pub enum HandlerType {
471-
Spin,
472-
Wagi,
473-
Wasi0_2,
474-
Wasi2023_11_10,
475-
Wasi2023_10_18,
476-
}
477-
478-
pub const WASI_HTTP_EXPORT_2023_10_18: &str = "wasi:http/[email protected]";
479-
pub const WASI_HTTP_EXPORT_2023_11_10: &str = "wasi:http/[email protected]";
480-
pub const WASI_HTTP_EXPORT_0_2_0: &str = "wasi:http/[email protected]";
481-
pub const WASI_HTTP_EXPORT_0_2_1: &str = "wasi:http/[email protected]";
482-
483-
impl HandlerType {
484-
/// Determine the handler type from the exports of a component
485-
pub fn from_component(
486-
engine: impl AsRef<wasmtime::Engine>,
487-
component: &Component,
488-
) -> anyhow::Result<HandlerType> {
489-
let mut handler_ty = None;
490-
491-
let mut set = |ty: HandlerType| {
492-
if handler_ty.is_none() {
493-
handler_ty = Some(ty);
494-
Ok(())
495-
} else {
496-
Err(anyhow::anyhow!(
497-
"component exports multiple different handlers but \
498-
it's expected to export only one"
499-
))
500-
}
501-
};
502-
let ty = component.component_type();
503-
for (name, _) in ty.exports(engine.as_ref()) {
504-
match name {
505-
WASI_HTTP_EXPORT_2023_10_18 => set(HandlerType::Wasi2023_10_18)?,
506-
WASI_HTTP_EXPORT_2023_11_10 => set(HandlerType::Wasi2023_11_10)?,
507-
WASI_HTTP_EXPORT_0_2_0 | WASI_HTTP_EXPORT_0_2_1 => set(HandlerType::Wasi0_2)?,
508-
"fermyon:spin/inbound-http" => set(HandlerType::Spin)?,
509-
_ => {}
510-
}
511-
}
512-
513-
handler_ty.ok_or_else(|| {
514-
anyhow::anyhow!(
515-
"Expected component to export one of \
516-
`{WASI_HTTP_EXPORT_2023_10_18}`, \
517-
`{WASI_HTTP_EXPORT_2023_11_10}`, \
518-
`{WASI_HTTP_EXPORT_0_2_0}`, \
519-
`{WASI_HTTP_EXPORT_0_2_1}`, \
520-
or `fermyon:spin/inbound-http` but it exported none of those"
521-
)
522-
})
523-
}
524-
}

crates/trigger-http/src/wasi.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ use spin_factor_outbound_http::wasi_2023_10_18::exports::wasi::http::incoming_ha
88
use spin_factor_outbound_http::wasi_2023_11_10::exports::wasi::http::incoming_handler as incoming_handler2023_11_10;
99
use spin_factors::RuntimeFactors;
1010
use spin_http::routes::RouteMatch;
11+
use spin_http::trigger::HandlerType;
1112
use tokio::{sync::oneshot, task};
1213
use tracing::{instrument, Instrument, Level};
1314
use wasmtime_wasi_http::bindings::http::types::Scheme;
1415
use wasmtime_wasi_http::{bindings::Proxy, body::HyperIncomingBody as Body, WasiHttpView};
1516

16-
use crate::{
17-
headers::prepare_request_headers,
18-
server::{HandlerType, HttpExecutor},
19-
TriggerInstanceBuilder,
20-
};
17+
use crate::{headers::prepare_request_headers, server::HttpExecutor, TriggerInstanceBuilder};
2118

2219
/// An [`HttpExecutor`] that uses the `wasi:http/incoming-handler` interface.
2320
#[derive(Clone)]

0 commit comments

Comments
 (0)