Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasi): add wasi crate support #720

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ jobs:
- name: Check
run: cargo check --target wasm32-unknown-unknown

- name: Test WASI
run: cargo test --features wasi

minimal-versions:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ std = []
bytes = "1"
fnv = "1.0.5"
itoa = "1"
wasi = { version = "0.13", optional = true }

[dev-dependencies]
quickcheck = "1"
Expand Down
35 changes: 35 additions & 0 deletions src/header/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3872,6 +3872,41 @@ mod as_header_name {
impl<'a> AsHeaderName for &'a String {}
}

#[cfg(feature = "wasi")]
impl TryFrom<wasi::http::types::Fields> for HeaderMap {
type Error = Error;

fn try_from(fields: wasi::http::types::Fields) -> Result<Self, Self::Error> {
let mut headers = HeaderMap::new();
for (name, value) in fields.entries() {
let name = HeaderName::try_from(name)?;
let value = HeaderValue::try_from(value)?;
match headers.entry(name) {
Entry::Vacant(entry) => {
entry.insert(value);
}
Entry::Occupied(mut entry) => {
entry.append(value);
}
};
}
Ok(headers)
}
}

#[cfg(feature = "wasi")]
impl TryFrom<HeaderMap> for wasi::http::types::Fields {
type Error = wasi::http::types::HeaderError;

fn try_from(headers: HeaderMap) -> Result<Self, Self::Error> {
let fields = wasi::http::types::Fields::new();
for (name, value) in &headers {
fields.append(&name.to_string(), &value.as_bytes().to_vec())?;
}
Ok(fields)
}
}

#[test]
fn test_bounds() {
fn check_bounds<T: Send + Send>() {}
Expand Down
53 changes: 53 additions & 0 deletions src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,45 @@ mod extension {
}
}

#[cfg(feature = "wasi")]
impl From<Method> for wasi::http::types::Method {
fn from(method: Method) -> Self {
match method {
Method(Get) => Self::Get,
Method(Head) => Self::Head,
Method(Post) => Self::Post,
Method(Put) => Self::Put,
Method(Delete) => Self::Delete,
Method(Connect) => Self::Connect,
Method(Options) => Self::Options,
Method(Trace) => Self::Trace,
Method(Patch) => Self::Patch,
Method(ExtensionInline(inline)) => Self::Other(inline.as_str().into()),
Method(ExtensionAllocated(allocated)) => Self::Other(allocated.as_str().into()),
}
}
}

#[cfg(feature = "wasi")]
impl TryFrom<wasi::http::types::Method> for Method {
type Error = InvalidMethod;

fn try_from(method: wasi::http::types::Method) -> Result<Self, Self::Error> {
match method {
wasi::http::types::Method::Get => Ok(Self::GET),
wasi::http::types::Method::Head => Ok(Self::HEAD),
wasi::http::types::Method::Post => Ok(Self::POST),
wasi::http::types::Method::Put => Ok(Self::PUT),
wasi::http::types::Method::Delete => Ok(Self::DELETE),
wasi::http::types::Method::Connect => Ok(Self::CONNECT),
wasi::http::types::Method::Options => Ok(Self::OPTIONS),
wasi::http::types::Method::Trace => Ok(Self::TRACE),
wasi::http::types::Method::Patch => Ok(Self::PATCH),
wasi::http::types::Method::Other(method) => method.parse(),
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -482,4 +521,18 @@ mod test {
);
}
}

#[cfg(feature = "wasi")]
#[test]
fn test_method_wasi_conv() {
use std::convert::TryInto;

let m: Method = wasi::http::types::Method::Get
.try_into()
.expect("failed to convert WASI method");
assert_eq!(m, Method::GET);

let m: wasi::http::types::Method = Method::GET.into();
assert!(matches!(m, wasi::http::types::Method::Get));
}
}
42 changes: 42 additions & 0 deletions src/uri/scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,34 @@ impl From<Scheme2> for Scheme {
}
}

#[cfg(feature = "wasi")]
impl From<Scheme> for wasi::http::types::Scheme {
fn from(scheme: Scheme) -> Self {
use self::Protocol::*;
use self::Scheme2::*;

match scheme.inner {
Standard(Http) => Self::Http,
Standard(Https) => Self::Https,
Other(v) => Self::Other(v.to_string()),
None => unreachable!(),
}
}
}

#[cfg(feature = "wasi")]
impl TryFrom<wasi::http::types::Scheme> for Scheme {
type Error = InvalidUri;

fn try_from(scheme: wasi::http::types::Scheme) -> Result<Self, Self::Error> {
match scheme {
wasi::http::types::Scheme::Http => Ok(Self::HTTP),
wasi::http::types::Scheme::Https => Ok(Self::HTTPS),
wasi::http::types::Scheme::Other(scheme) => scheme.parse(),
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -358,4 +386,18 @@ mod test {
fn scheme(s: &str) -> Scheme {
s.parse().expect(&format!("Invalid scheme: {}", s))
}

#[cfg(feature = "wasi")]
#[test]
fn wasi_conv() {
use std::convert::TryInto;

let s: Scheme = wasi::http::types::Scheme::Http
.try_into()
.expect("failed to convert WASI scheme");
assert_eq!(s, Scheme::HTTP);

let s: wasi::http::types::Scheme = Scheme::HTTP.into();
assert!(matches!(s, wasi::http::types::Scheme::Http));
}
}