Skip to content

Commit 0d2c9c7

Browse files
authored
Merge pull request #16 from yoshuawuyts/body-fix
Rename body to body_reader in response and default to 0 length body
2 parents e92cceb + 97a4047 commit 0d2c9c7

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Request {
3030
url,
3131
headers: Headers::new(),
3232
body_reader: Box::new(io::empty()),
33-
length: None,
33+
length: Some(0),
3434
}
3535
}
3636

src/response.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use std::task::{Context, Poll};
88
use crate::mime::{self, Mime};
99
use crate::{Headers, StatusCode};
1010

11-
type Body = dyn BufRead + Unpin + Send + 'static;
11+
type BodyReader = dyn BufRead + Unpin + Send + 'static;
1212

1313
pin_project_lite::pin_project! {
1414
/// An HTTP response.
1515
pub struct Response {
1616
#[pin]
17-
body: Box<Body>,
17+
body_reader: Box<BodyReader>,
1818
status: StatusCode,
1919
headers: Headers,
2020
length: Option<usize>,
@@ -27,8 +27,8 @@ impl Response {
2727
Self {
2828
status,
2929
headers: Headers::new(),
30-
body: Box::new(io::empty()),
31-
length: None,
30+
body_reader: Box::new(io::empty()),
31+
length: Some(0),
3232
}
3333
}
3434

@@ -37,9 +37,12 @@ impl Response {
3737
&self.status
3838
}
3939

40-
/// Set the body.
41-
pub fn set_body(mut self, body: impl BufRead + Unpin + Send + 'static) -> Self {
42-
self.body = Box::new(body);
40+
/// Set the body reader to `body` and unset the length.
41+
///
42+
/// This will make the body a chunked stream.
43+
pub fn set_body_reader(mut self, body: impl BufRead + Unpin + Send + 'static) -> Self {
44+
self.body_reader = Box::new(body);
45+
self.length = None;
4346
self
4447
}
4548

@@ -51,7 +54,7 @@ impl Response {
5154
pub fn set_body_string(mut self, string: String) -> io::Result<Self> {
5255
self.length = Some(string.len());
5356
let reader = io::Cursor::new(string.into_bytes());
54-
self.set_body(reader).set_mime(mime::PLAIN)
57+
self.set_body_reader(reader).set_mime(mime::PLAIN)
5558
}
5659

5760
/// Pass bytes as the request body.
@@ -63,7 +66,7 @@ impl Response {
6366
let bytes = bytes.as_ref().to_owned();
6467
self.length = Some(bytes.len());
6568
let reader = io::Cursor::new(bytes);
66-
self.set_body(reader).set_mime(mime::BYTE_STREAM)
69+
self.set_body_reader(reader).set_mime(mime::BYTE_STREAM)
6770
}
6871

6972
/// Get HTTP headers.
@@ -121,18 +124,18 @@ impl Read for Response {
121124
cx: &mut Context<'_>,
122125
buf: &mut [u8],
123126
) -> Poll<io::Result<usize>> {
124-
Pin::new(&mut self.body).poll_read(cx, buf)
127+
Pin::new(&mut self.body_reader).poll_read(cx, buf)
125128
}
126129
}
127130

128131
impl BufRead for Response {
129132
#[allow(missing_doc_code_examples)]
130133
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
131134
let this = self.project();
132-
this.body.poll_fill_buf(cx)
135+
this.body_reader.poll_fill_buf(cx)
133136
}
134137

135138
fn consume(mut self: Pin<&mut Self>, amt: usize) {
136-
Pin::new(&mut self.body).consume(amt)
139+
Pin::new(&mut self.body_reader).consume(amt)
137140
}
138141
}

0 commit comments

Comments
 (0)