@@ -8,13 +8,13 @@ use std::task::{Context, Poll};
8
8
use crate :: mime:: { self , Mime } ;
9
9
use crate :: { Headers , StatusCode } ;
10
10
11
- type Body = dyn BufRead + Unpin + Send + ' static ;
11
+ type BodyReader = dyn BufRead + Unpin + Send + ' static ;
12
12
13
13
pin_project_lite:: pin_project! {
14
14
/// An HTTP response.
15
15
pub struct Response {
16
16
#[ pin]
17
- body : Box <Body >,
17
+ body_reader : Box <BodyReader >,
18
18
status: StatusCode ,
19
19
headers: Headers ,
20
20
length: Option <usize >,
@@ -27,8 +27,8 @@ impl Response {
27
27
Self {
28
28
status,
29
29
headers : Headers :: new ( ) ,
30
- body : Box :: new ( io:: empty ( ) ) ,
31
- length : None ,
30
+ body_reader : Box :: new ( io:: empty ( ) ) ,
31
+ length : Some ( 0 ) ,
32
32
}
33
33
}
34
34
@@ -37,9 +37,12 @@ impl Response {
37
37
& self . status
38
38
}
39
39
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 ;
43
46
self
44
47
}
45
48
@@ -51,7 +54,7 @@ impl Response {
51
54
pub fn set_body_string ( mut self , string : String ) -> io:: Result < Self > {
52
55
self . length = Some ( string. len ( ) ) ;
53
56
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 )
55
58
}
56
59
57
60
/// Pass bytes as the request body.
@@ -63,7 +66,7 @@ impl Response {
63
66
let bytes = bytes. as_ref ( ) . to_owned ( ) ;
64
67
self . length = Some ( bytes. len ( ) ) ;
65
68
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 )
67
70
}
68
71
69
72
/// Get HTTP headers.
@@ -121,18 +124,18 @@ impl Read for Response {
121
124
cx : & mut Context < ' _ > ,
122
125
buf : & mut [ u8 ] ,
123
126
) -> 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)
125
128
}
126
129
}
127
130
128
131
impl BufRead for Response {
129
132
#[ allow( missing_doc_code_examples) ]
130
133
fn poll_fill_buf ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < & ' _ [ u8 ] > > {
131
134
let this = self . project ( ) ;
132
- this. body . poll_fill_buf ( cx)
135
+ this. body_reader . poll_fill_buf ( cx)
133
136
}
134
137
135
138
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)
137
140
}
138
141
}
0 commit comments