Skip to content

Commit 3d80fbc

Browse files
committed
add headers to requests
and propogate some errors out instead of unwrapping
1 parent d28e4ca commit 3d80fbc

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/http/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Client {
1818

1919
/// Send an HTTP request.
2020
pub async fn send<B: Body>(&self, req: Request<B>) -> Result<Response<IncomingBody>> {
21-
let (wasi_req, body) = req.into_outgoing();
21+
let (wasi_req, body) = req.into_outgoing()?;
2222
let wasi_body = wasi_req.body().unwrap();
2323
let body_stream = wasi_body.write().unwrap();
2424

src/http/error.rs

Lines changed: 6 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::Other(e) => write!(f, "{e}"),
2223
}
2324
}
2425
}
@@ -30,13 +31,17 @@ impl fmt::Display for Error {
3031
ErrorVariant::WasiHeader(e) => write!(f, "wasi header error: {e}"),
3132
ErrorVariant::HeaderName(e) => write!(f, "header name error: {e}"),
3233
ErrorVariant::HeaderValue(e) => write!(f, "header value error: {e}"),
34+
ErrorVariant::Other(e) => write!(f, "{e}"),
3335
}
3436
}
3537
}
3638

3739
impl std::error::Error for Error {}
3840

3941
impl Error {
42+
pub(crate) fn other(s: impl Into<String>) -> Self {
43+
ErrorVariant::Other(s.into()).into()
44+
}
4045
pub(crate) fn context(self, s: impl Into<String>) -> Self {
4146
let mut context = self.context;
4247
context.push(s.into());
@@ -86,4 +91,5 @@ pub enum ErrorVariant {
8691
WasiHeader(wasi::http::types::HeaderError),
8792
HeaderName(http::header::InvalidHeaderName),
8893
HeaderValue(http::header::InvalidHeaderValue),
94+
Other(String),
8995
}

src/http/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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;
15+
pub(crate) use fields::{header_map_from_wasi, header_map_to_wasi};
1616

1717
pub mod body;
1818

src/http/request.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use crate::io::{empty, Empty};
22

3-
use super::{Body, IntoBody, Method};
3+
use super::{header_map_to_wasi, Body, Error, HeaderMap, IntoBody, Method, Result};
44
use http::uri::Uri;
55
use wasi::http::outgoing_handler::OutgoingRequest;
6-
use wasi::http::types::{Headers as WasiHeaders, Scheme};
6+
use wasi::http::types::Scheme;
77

88
/// An HTTP request
99
#[derive(Debug)]
1010
pub struct Request<B: Body> {
1111
method: Method,
1212
uri: Uri,
13-
headers: WasiHeaders,
13+
headers: HeaderMap,
1414
body: B,
1515
}
1616

@@ -21,12 +21,22 @@ impl Request<Empty> {
2121
body: empty(),
2222
method,
2323
uri,
24-
headers: WasiHeaders::new(),
24+
headers: HeaderMap::new(),
2525
}
2626
}
2727
}
2828

2929
impl<B: Body> Request<B> {
30+
/// Get the HTTP headers from the impl
31+
pub fn headers(&self) -> &HeaderMap {
32+
&self.headers
33+
}
34+
35+
/// Mutably get the HTTP headers from the impl
36+
pub fn headers_mut(&mut self) -> &mut HeaderMap {
37+
&mut self.headers
38+
}
39+
3040
/// Set an HTTP body.
3141
pub fn set_body<C: IntoBody>(self, body: C) -> Request<C::IntoBody> {
3242
let Self {
@@ -43,13 +53,14 @@ impl<B: Body> Request<B> {
4353
}
4454
}
4555

46-
pub(crate) fn into_outgoing(self) -> (OutgoingRequest, B) {
47-
let wasi_req = OutgoingRequest::new(self.headers);
56+
pub(crate) fn into_outgoing(self) -> Result<(OutgoingRequest, B)> {
57+
let wasi_req = OutgoingRequest::new(header_map_to_wasi(&self.headers)?);
4858

4959
// Set the HTTP method
60+
let method = self.method.into();
5061
wasi_req
51-
.set_method(&self.method.into())
52-
.expect("method accepted by wasi-http implementation");
62+
.set_method(&method)
63+
.map_err(|()| Error::other(format!("method rejected by wasi-http: {method:?}",)))?;
5364

5465
// Set the url scheme
5566
let scheme = match self.uri.scheme().map(|s| s.as_str()) {
@@ -59,21 +70,24 @@ impl<B: Body> Request<B> {
5970
};
6071
wasi_req
6172
.set_scheme(Some(&scheme))
62-
.expect("scheme accepted by wasi-http implementation");
73+
.map_err(|()| Error::other(format!("scheme rejected by wasi-http: {scheme:?}")))?;
6374

6475
// Set authority
76+
let authority = self.uri.authority().map(|a| a.as_str());
6577
wasi_req
66-
.set_authority(self.uri.authority().map(|a| a.as_str()))
67-
.expect("authority accepted by wasi-http implementation");
78+
.set_authority(authority)
79+
.map_err(|()| Error::other(format!("authority rejected by wasi-http {authority:?}")))?;
6880

6981
// Set the url path + query string
7082
if let Some(p_and_q) = self.uri.path_and_query() {
7183
wasi_req
7284
.set_path_with_query(Some(&p_and_q.to_string()))
73-
.expect("path with query accepted by wasi-http implementation")
85+
.map_err(|()| {
86+
Error::other(format!("path and query rejected by wasi-http {p_and_q:?}"))
87+
})?;
7488
}
7589

7690
// All done; request is ready for send-off
77-
(wasi_req, self.body)
91+
Ok((wasi_req, self.body))
7892
}
7993
}

0 commit comments

Comments
 (0)