@@ -70,7 +70,7 @@ impl Encoder {
7070
7171impl Encoder {
7272 // 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 > > {
7474 self . state = EncoderState :: Head ;
7575
7676 let reason = self . res . status ( ) . canonical_reason ( ) ;
@@ -104,11 +104,11 @@ impl Encoder {
104104 }
105105
106106 std:: io:: Write :: write_fmt ( & mut self . head , format_args ! ( "\r \n " ) ) ?;
107- Ok ( ( ) )
107+ self . encode_head ( cx , buf )
108108 }
109109
110110 /// 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 > > {
112112 // Read from the serialized headers, url and methods.
113113 let head_len = self . head . len ( ) ;
114114 let len = std:: cmp:: min ( head_len - self . head_bytes_read , buf. len ( ) ) ;
@@ -122,18 +122,21 @@ impl Encoder {
122122 if self . head_bytes_read == head_len {
123123 // The response length lets us know if we are encoding
124124 // our body in chunks or not
125- self . state = match self . res . len ( ) {
125+ match self . res . len ( ) {
126126 Some ( body_len) => {
127127 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) ;
129134 }
130- None => EncoderState :: UncomputedChunked ,
131135 } ;
132- true
133136 } else {
134137 // If we haven't read the entire header it means `buf` isn't
135138 // big enough. Break out of loop and return from `poll_read`
136- false
139+ return Poll :: Ready ( Ok ( self . bytes_read ) ) ;
137140 }
138141 }
139142
@@ -190,6 +193,7 @@ impl Encoder {
190193 }
191194 }
192195
196+ /// Compute a "chunk", which is the value from the stream between CRLFs.
193197 fn encode_uncomputed_chunked (
194198 & mut self ,
195199 cx : & mut Context < ' _ > ,
@@ -286,6 +290,7 @@ impl Encoder {
286290 }
287291 }
288292
293+ /// We already have a chunk stored in memory; write it back out.
289294 fn encode_computed_chunked (
290295 & mut self ,
291296 cx : & mut Context < ' _ > ,
@@ -323,19 +328,13 @@ impl Read for Encoder {
323328 // we keep track how many bytes of the head and body we've read
324329 // in this call of `poll_read`
325330 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 ) ) ,
339338 }
340339 }
341340}
0 commit comments