Skip to content

Commit e51745f

Browse files
committed
also swap out local definition of Method for http's
1 parent 3d80fbc commit e51745f

File tree

7 files changed

+45
-71
lines changed

7 files changed

+45
-71
lines changed

examples/http_get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use wstd::io::AsyncRead;
44

55
#[wstd::main]
66
async fn main() -> Result<(), Box<dyn Error>> {
7-
let request = Request::new(Method::Get, "https://postman-echo.com/get".parse()?);
7+
let request = Request::new(Method::GET, "https://postman-echo.com/get".parse()?);
88
let mut response = Client::new().send(request).await?;
99

1010
let content_type = response

src/http/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ impl fmt::Debug for Error {
1919
ErrorVariant::WasiHeader(e) => write!(f, "wasi header error: {e:?}"),
2020
ErrorVariant::HeaderName(e) => write!(f, "header name error: {e:?}"),
2121
ErrorVariant::HeaderValue(e) => write!(f, "header value error: {e:?}"),
22+
ErrorVariant::Method(e) => write!(f, "method error: {e:?}"),
2223
ErrorVariant::Other(e) => write!(f, "{e}"),
2324
}
2425
}
@@ -31,6 +32,7 @@ impl fmt::Display for Error {
3132
ErrorVariant::WasiHeader(e) => write!(f, "wasi header error: {e}"),
3233
ErrorVariant::HeaderName(e) => write!(f, "header name error: {e}"),
3334
ErrorVariant::HeaderValue(e) => write!(f, "header value error: {e}"),
35+
ErrorVariant::Method(e) => write!(f, "method error: {e}"),
3436
ErrorVariant::Other(e) => write!(f, "{e}"),
3537
}
3638
}
@@ -85,11 +87,18 @@ impl From<http::header::InvalidHeaderName> for Error {
8587
}
8688
}
8789

90+
impl From<http::method::InvalidMethod> for Error {
91+
fn from(e: http::method::InvalidMethod) -> Error {
92+
ErrorVariant::Method(e).into()
93+
}
94+
}
95+
8896
#[derive(Debug)]
8997
pub enum ErrorVariant {
9098
WasiHttp(wasi::http::types::ErrorCode),
9199
WasiHeader(wasi::http::types::HeaderError),
92100
HeaderName(http::header::InvalidHeaderName),
93101
HeaderValue(http::header::InvalidHeaderValue),
102+
Method(http::method::InvalidMethod),
94103
Other(String),
95104
}

src/http/fields.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub(crate) fn header_map_from_wasi(wasi_fields: Fields) -> Result<HeaderMap> {
1515
Ok(output)
1616
}
1717

18-
#[allow(dead_code)]
1918
pub(crate) fn header_map_to_wasi(header_map: &HeaderMap) -> Result<Fields> {
2019
let wasi_fields = Fields::new();
2120
for (key, value) in header_map {

src/http/method.rs

Lines changed: 29 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,36 @@
11
use wasi::http::types::Method as WasiMethod;
22

3-
/// The method for the HTTP request
4-
#[derive(Debug)]
5-
#[non_exhaustive]
6-
pub enum Method {
7-
/// The GET method requests transfer of a current selected representation
8-
/// for the target resource.
9-
Get,
10-
/// The HEAD method is identical to GET except that the server MUST NOT send a message body in
11-
/// the response.
12-
Head,
13-
/// The POST method requests that the target resource process the representation enclosed in
14-
/// the request according to the resource's own specific semantics.
15-
Post,
16-
/// The PUT method requests that the state of the target resource be created or replaced with
17-
/// the state defined by the representation enclosed in the request message payload.
18-
Put,
19-
/// The DELETE method requests that the origin server remove the association between the target
20-
/// resource and its current functionality.
21-
Delete,
22-
/// The CONNECT method requests that the recipient establish a tunnel to the destination origin
23-
/// server identified by the request-target and, if successful, thereafter restrict its
24-
/// behavior to blind forwarding of packets, in both directions, until the tunnel is closed.
25-
Connect,
26-
/// The OPTIONS method requests information about the communication options available for the
27-
/// target resource, at either the origin server or an intervening intermediary.
28-
Options,
29-
/// The TRACE method requests a remote, application-level loop-back of the request message.
30-
Trace,
31-
/// The PATCH method requests that a set of changes described in the request entity be applied
32-
/// to the resource identified by the Request- URI.
33-
///
34-
Patch,
35-
/// Send a method not covered by this list.
36-
Other(String),
37-
}
3+
use super::Result;
4+
pub use http::Method;
385

39-
impl From<Method> for WasiMethod {
40-
fn from(value: Method) -> Self {
41-
match value {
42-
Method::Get => WasiMethod::Get,
43-
Method::Head => WasiMethod::Head,
44-
Method::Post => WasiMethod::Post,
45-
Method::Put => WasiMethod::Put,
46-
Method::Delete => WasiMethod::Delete,
47-
Method::Connect => WasiMethod::Connect,
48-
Method::Options => WasiMethod::Options,
49-
Method::Trace => WasiMethod::Trace,
50-
Method::Patch => WasiMethod::Patch,
51-
Method::Other(s) => WasiMethod::Other(s),
52-
}
6+
pub(crate) fn to_wasi_method(value: Method) -> WasiMethod {
7+
match value {
8+
Method::GET => WasiMethod::Get,
9+
Method::HEAD => WasiMethod::Head,
10+
Method::POST => WasiMethod::Post,
11+
Method::PUT => WasiMethod::Put,
12+
Method::DELETE => WasiMethod::Delete,
13+
Method::CONNECT => WasiMethod::Connect,
14+
Method::OPTIONS => WasiMethod::Options,
15+
Method::TRACE => WasiMethod::Trace,
16+
Method::PATCH => WasiMethod::Patch,
17+
other => WasiMethod::Other(other.as_str().to_owned()),
5318
}
5419
}
5520

56-
impl From<WasiMethod> for Method {
57-
fn from(value: WasiMethod) -> Self {
58-
match value {
59-
WasiMethod::Get => Method::Get,
60-
WasiMethod::Head => Method::Head,
61-
WasiMethod::Post => Method::Post,
62-
WasiMethod::Put => Method::Put,
63-
WasiMethod::Delete => Method::Delete,
64-
WasiMethod::Connect => Method::Connect,
65-
WasiMethod::Options => Method::Options,
66-
WasiMethod::Trace => Method::Trace,
67-
WasiMethod::Patch => Method::Patch,
68-
WasiMethod::Other(s) => Method::Other(s),
69-
}
70-
}
21+
// This will become useful once we support IncomingRequest
22+
#[allow(dead_code)]
23+
pub(crate) fn from_wasi_method(value: WasiMethod) -> Result<Method> {
24+
Ok(match value {
25+
WasiMethod::Get => Method::GET,
26+
WasiMethod::Head => Method::HEAD,
27+
WasiMethod::Post => Method::POST,
28+
WasiMethod::Put => Method::PUT,
29+
WasiMethod::Delete => Method::DELETE,
30+
WasiMethod::Connect => Method::CONNECT,
31+
WasiMethod::Options => Method::OPTIONS,
32+
WasiMethod::Trace => Method::TRACE,
33+
WasiMethod::Patch => Method::PATCH,
34+
WasiMethod::Other(s) => Method::from_bytes(s.as_bytes())?,
35+
})
7136
}

src/http/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ pub use request::Request;
1212
pub use response::Response;
1313
pub use status_code::StatusCode;
1414

15-
pub(crate) use fields::{header_map_from_wasi, header_map_to_wasi};
16-
1715
pub mod body;
1816

1917
mod client;

src/http/request.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::io::{empty, Empty};
22

3-
use super::{header_map_to_wasi, Body, Error, HeaderMap, IntoBody, Method, Result};
3+
use super::{
4+
fields::header_map_to_wasi, method::to_wasi_method, Body, Error, HeaderMap, IntoBody, Method,
5+
Result,
6+
};
47
use http::uri::Uri;
58
use wasi::http::outgoing_handler::OutgoingRequest;
69
use wasi::http::types::Scheme;
@@ -57,7 +60,7 @@ impl<B: Body> Request<B> {
5760
let wasi_req = OutgoingRequest::new(header_map_to_wasi(&self.headers)?);
5861

5962
// Set the HTTP method
60-
let method = self.method.into();
63+
let method = to_wasi_method(self.method);
6164
wasi_req
6265
.set_method(&method)
6366
.map_err(|()| Error::other(format!("method rejected by wasi-http: {method:?}",)))?;

src/http/response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use wasi::http::types::{IncomingBody as WasiIncomingBody, IncomingResponse};
22
use wasi::io::streams::{InputStream, StreamError};
33

4-
use super::{header_map_from_wasi, Body, HeaderMap, StatusCode};
4+
use super::{fields::header_map_from_wasi, Body, HeaderMap, StatusCode};
55
use crate::io::AsyncRead;
66
use crate::runtime::Reactor;
77

0 commit comments

Comments
 (0)