@@ -121,9 +121,6 @@ impl Encoder {
121
121
std:: io:: Write :: write_fmt ( & mut head, format_args ! ( "Content-Length: {}\r \n " , len) ) ?;
122
122
} else {
123
123
std:: io:: Write :: write_fmt ( & mut head, format_args ! ( "Transfer-Encoding: chunked\r \n " ) ) ?;
124
- panic ! ( "chunked encoding is not implemented yet" ) ;
125
- // See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding
126
- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer
127
124
}
128
125
129
126
for ( header, value) in self . res . headers ( ) . iter ( ) {
@@ -150,7 +147,8 @@ impl Read for Encoder {
150
147
151
148
// Send the headers. As long as the headers aren't fully sent yet we
152
149
// keep sending more of the headers.
153
- let mut bytes_read = 0 ;
150
+ let mut head_bytes_read = 0 ;
151
+ let mut body_bytes_read = 0 ;
154
152
155
153
// Read from the serialized headers, url and methods.
156
154
if !self . head_done {
@@ -163,19 +161,39 @@ impl Read for Encoder {
163
161
if self . cursor == head_len {
164
162
self . head_done = true ;
165
163
}
166
- bytes_read += len;
164
+ head_bytes_read += len;
167
165
}
168
166
169
167
// Read from the AsyncRead impl on the inner Response struct.
170
168
if !self . body_done {
171
- let n = ready ! ( Pin :: new( & mut self . res) . poll_read( cx, & mut buf[ bytes_read..] ) ) ?;
172
- bytes_read += n;
169
+ // figure out how many bytes we can read. If a len was set, we need
170
+ // to make sure we don't read more than that.
171
+ let upper_bound = match self . res . len ( ) {
172
+ Some ( len) => ( head_bytes_read + len - self . body_bytes_read ) . min ( buf. len ( ) ) ,
173
+ None => buf. len ( ) ,
174
+ } ;
175
+
176
+ // Read bytes, and update internal tracking stuff.
177
+ let n = ready ! ( Pin :: new( & mut self . res) . poll_read( cx, & mut buf[ head_bytes_read..upper_bound] ) ) ?;
178
+ body_bytes_read += n;
173
179
self . body_bytes_read += n;
174
- if bytes_read == 0 {
180
+
181
+ // If our stream no longer gives bytes, end.
182
+ if body_bytes_read == 0 {
175
183
self . body_done = true ;
176
184
}
185
+
186
+ // If we know we've read all bytes, end.
187
+ if let Some ( len) = self . res . len ( ) {
188
+ if len == self . body_bytes_read {
189
+ self . body_done = true ;
190
+ }
191
+ debug_assert ! ( self . body_bytes_read <= len, "Too many bytes read. Expected: {}, read: {}" , len, self . body_bytes_read) ;
192
+ }
177
193
}
178
194
195
+ // Return the total amount of bytes read.
196
+ let bytes_read = head_bytes_read + body_bytes_read;
179
197
Poll :: Ready ( Ok ( bytes_read as usize ) )
180
198
}
181
199
}
@@ -211,7 +229,6 @@ where
211
229
// Convert our header buf into an httparse instance, and validate.
212
230
let status = httparse_req. parse ( & buf) ?;
213
231
if status. is_partial ( ) {
214
- dbg ! ( String :: from_utf8( buf) . unwrap( ) ) ;
215
232
return Err ( "Malformed HTTP head" . into ( ) ) ;
216
233
}
217
234
0 commit comments