@@ -70,7 +70,7 @@ impl Encoder {
70
70
71
71
impl Encoder {
72
72
// Encode the headers to a buffer, the first time we poll.
73
- fn encode_start ( & mut self ) -> io:: Result < ( ) > {
73
+ fn encode_start ( & mut self , cx : & mut Context < ' _ > , buf : & mut [ u8 ] ) -> Poll < io:: Result < usize > > {
74
74
self . state = EncoderState :: Head ;
75
75
76
76
let reason = self . res . status ( ) . canonical_reason ( ) ;
@@ -104,11 +104,11 @@ impl Encoder {
104
104
}
105
105
106
106
std:: io:: Write :: write_fmt ( & mut self . head , format_args ! ( "\r \n " ) ) ?;
107
- Ok ( ( ) )
107
+ self . encode_head ( cx , buf )
108
108
}
109
109
110
110
/// Encode the status code + headers.
111
- fn encode_head ( & mut self , buf : & mut [ u8 ] ) -> bool {
111
+ fn encode_head ( & mut self , cx : & mut Context < ' _ > , buf : & mut [ u8 ] ) -> Poll < io :: Result < usize > > {
112
112
// Read from the serialized headers, url and methods.
113
113
let head_len = self . head . len ( ) ;
114
114
let len = std:: cmp:: min ( head_len - self . head_bytes_read , buf. len ( ) ) ;
@@ -122,18 +122,21 @@ impl Encoder {
122
122
if self . head_bytes_read == head_len {
123
123
// The response length lets us know if we are encoding
124
124
// our body in chunks or not
125
- self . state = match self . res . len ( ) {
125
+ match self . res . len ( ) {
126
126
Some ( body_len) => {
127
127
self . body_len = body_len;
128
- EncoderState :: Body
128
+ self . state = EncoderState :: Body ;
129
+ return self . encode_body ( cx, buf) ;
130
+ }
131
+ None => {
132
+ self . state = EncoderState :: UncomputedChunked ;
133
+ return self . encode_uncomputed_chunked ( cx, buf) ;
129
134
}
130
- None => EncoderState :: UncomputedChunked ,
131
135
} ;
132
- true
133
136
} else {
134
137
// If we haven't read the entire header it means `buf` isn't
135
138
// big enough. Break out of loop and return from `poll_read`
136
- false
139
+ return Poll :: Ready ( Ok ( self . bytes_read ) ) ;
137
140
}
138
141
}
139
142
@@ -190,6 +193,7 @@ impl Encoder {
190
193
}
191
194
}
192
195
196
+ /// Compute a "chunk", which is the value from the stream between CRLFs.
193
197
fn encode_uncomputed_chunked (
194
198
& mut self ,
195
199
cx : & mut Context < ' _ > ,
@@ -286,6 +290,7 @@ impl Encoder {
286
290
}
287
291
}
288
292
293
+ /// We already have a chunk stored in memory; write it back out.
289
294
fn encode_computed_chunked (
290
295
& mut self ,
291
296
cx : & mut Context < ' _ > ,
@@ -323,19 +328,13 @@ impl Read for Encoder {
323
328
// we keep track how many bytes of the head and body we've read
324
329
// in this call of `poll_read`
325
330
self . bytes_read = 0 ;
326
- loop {
327
- match self . state {
328
- EncoderState :: Start => self . encode_start ( ) ?,
329
- EncoderState :: Head => {
330
- if !self . encode_head ( buf) {
331
- return Poll :: Ready ( Ok ( self . bytes_read ) ) ;
332
- }
333
- }
334
- EncoderState :: Body => return self . encode_body ( cx, buf) ,
335
- EncoderState :: UncomputedChunked => return self . encode_uncomputed_chunked ( cx, buf) ,
336
- EncoderState :: ComputedChunked => return self . encode_computed_chunked ( cx, buf) ,
337
- EncoderState :: Done => return Poll :: Ready ( Ok ( self . bytes_read ) ) ,
338
- }
331
+ match self . state {
332
+ EncoderState :: Start => self . encode_start ( cx, buf) ,
333
+ EncoderState :: Head => self . encode_head ( cx, buf) ,
334
+ EncoderState :: Body => self . encode_body ( cx, buf) ,
335
+ EncoderState :: UncomputedChunked => self . encode_uncomputed_chunked ( cx, buf) ,
336
+ EncoderState :: ComputedChunked => self . encode_computed_chunked ( cx, buf) ,
337
+ EncoderState :: Done => Poll :: Ready ( Ok ( self . bytes_read ) ) ,
339
338
}
340
339
}
341
340
}
0 commit comments