Skip to content

Commit b5ad808

Browse files
committed
http: convert Request to use AsyncOutputStream
* eliminate its internal OutputStream wrapper. * fix io::copy to bubble up body copy error rather than panic. * add http error variant for io errors on body.
1 parent 32f16ac commit b5ad808

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

src/http/client.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{response::IncomingBody, Body, Error, Request, Response, Result};
2-
use crate::io::{self, AsyncWrite};
2+
use crate::io::{self, AsyncOutputStream};
33
use crate::runtime::Reactor;
44
use crate::time::Duration;
55
use wasi::http::types::{OutgoingBody, RequestOptions as WasiRequestOptions};
@@ -27,9 +27,7 @@ impl Client {
2727
let res = wasi::http::outgoing_handler::handle(wasi_req, self.wasi_options()?).unwrap();
2828

2929
// 2. Start sending the request body
30-
io::copy(body, OutputStream::new(body_stream))
31-
.await
32-
.expect("io::copy broke oh no");
30+
io::copy(body, AsyncOutputStream::new(body_stream)).await?;
3331

3432
// 3. Finish sending the request body
3533
let trailers = None;
@@ -74,33 +72,6 @@ impl Client {
7472
}
7573
}
7674

77-
struct OutputStream {
78-
stream: wasi::http::types::OutputStream,
79-
}
80-
81-
impl OutputStream {
82-
fn new(stream: wasi::http::types::OutputStream) -> Self {
83-
Self { stream }
84-
}
85-
}
86-
87-
impl AsyncWrite for OutputStream {
88-
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
89-
let max = self.stream.check_write().unwrap() as usize;
90-
let max = max.min(buf.len());
91-
let buf = &buf[0..max];
92-
self.stream.write(buf).unwrap();
93-
Reactor::current().wait_for(self.stream.subscribe()).await;
94-
Ok(max)
95-
}
96-
97-
async fn flush(&mut self) -> io::Result<()> {
98-
self.stream.flush().unwrap();
99-
Reactor::current().wait_for(self.stream.subscribe()).await;
100-
Ok(())
101-
}
102-
}
103-
10475
#[derive(Default, Debug)]
10576
struct RequestOptions {
10677
connect_timeout: Option<Duration>,

src/http/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl fmt::Debug for Error {
2424
ErrorVariant::HeaderName(e) => write!(f, "header name error: {e:?}"),
2525
ErrorVariant::HeaderValue(e) => write!(f, "header value error: {e:?}"),
2626
ErrorVariant::Method(e) => write!(f, "method error: {e:?}"),
27+
ErrorVariant::BodyIo(e) => write!(f, "body error: {e:?}"),
2728
ErrorVariant::Other(e) => write!(f, "{e}"),
2829
}
2930
}
@@ -37,6 +38,7 @@ impl fmt::Display for Error {
3738
ErrorVariant::HeaderName(e) => write!(f, "header name error: {e}"),
3839
ErrorVariant::HeaderValue(e) => write!(f, "header value error: {e}"),
3940
ErrorVariant::Method(e) => write!(f, "method error: {e}"),
41+
ErrorVariant::BodyIo(e) => write!(f, "body error: {e}"),
4042
ErrorVariant::Other(e) => write!(f, "{e}"),
4143
}
4244
}
@@ -100,12 +102,19 @@ impl From<InvalidMethod> for Error {
100102
}
101103
}
102104

105+
impl From<std::io::Error> for Error {
106+
fn from(e: std::io::Error) -> Error {
107+
ErrorVariant::BodyIo(e).into()
108+
}
109+
}
110+
103111
#[derive(Debug)]
104112
pub enum ErrorVariant {
105113
WasiHttp(WasiHttpErrorCode),
106114
WasiHeader(WasiHttpHeaderError),
107115
HeaderName(InvalidHeaderName),
108116
HeaderValue(InvalidHeaderValue),
109117
Method(InvalidMethod),
118+
BodyIo(std::io::Error),
110119
Other(String),
111120
}

0 commit comments

Comments
 (0)