@@ -21,6 +21,8 @@ pub(crate) struct Encoder {
21
21
res : Response ,
22
22
/// The state of the encoding process
23
23
state : EncoderState ,
24
+ /// Track bytes read in a call to poll_read.
25
+ bytes_read : usize ,
24
26
}
25
27
26
28
#[ derive( Debug ) ]
@@ -48,6 +50,7 @@ impl Encoder {
48
50
Self {
49
51
res,
50
52
state : EncoderState :: Start ,
53
+ bytes_read : 0 ,
51
54
}
52
55
}
53
56
@@ -92,7 +95,7 @@ impl Read for Encoder {
92
95
) -> Poll < io:: Result < usize > > {
93
96
// we must keep track how many bytes of the head and body we've read
94
97
// in this call of `poll_read`
95
- let mut bytes_read = 0 ;
98
+ self . bytes_read = 0 ;
96
99
loop {
97
100
match self . state {
98
101
EncoderState :: Start => {
@@ -112,7 +115,7 @@ impl Read for Encoder {
112
115
let len = std:: cmp:: min ( head_len - head_bytes_read, buf. len ( ) ) ;
113
116
let range = head_bytes_read..head_bytes_read + len;
114
117
buf[ 0 ..len] . copy_from_slice ( & data[ range] ) ;
115
- bytes_read += len;
118
+ self . bytes_read += len;
116
119
head_bytes_read += len;
117
120
118
121
// If we've read the total length of the head we're done
@@ -139,31 +142,31 @@ impl Read for Encoder {
139
142
} => {
140
143
// Double check that we didn't somehow read more bytes than
141
144
// can fit in our buffer
142
- debug_assert ! ( bytes_read <= buf. len( ) ) ;
145
+ debug_assert ! ( self . bytes_read <= buf. len( ) ) ;
143
146
144
147
// ensure we have at least room for 1 more byte in our buffer
145
- if bytes_read == buf. len ( ) {
148
+ if self . bytes_read == buf. len ( ) {
146
149
break ;
147
150
}
148
151
149
152
// Figure out how many bytes we can read.
150
- let upper_bound = ( bytes_read + body_len - body_bytes_read) . min ( buf. len ( ) ) ;
153
+ let upper_bound = ( self . bytes_read + body_len - body_bytes_read) . min ( buf. len ( ) ) ;
151
154
// Read bytes from body
152
- let inner_poll_result =
153
- Pin :: new ( & mut self . res ) . poll_read ( cx, & mut buf[ bytes_read..upper_bound ] ) ;
155
+ let range = self . bytes_read ..upper_bound ;
156
+ let inner_poll_result = Pin :: new ( & mut self . res ) . poll_read ( cx, & mut buf[ range ] ) ;
154
157
let new_body_bytes_read = match inner_poll_result {
155
158
Poll :: Ready ( Ok ( n) ) => n,
156
159
Poll :: Ready ( Err ( e) ) => return Poll :: Ready ( Err ( e) ) ,
157
160
Poll :: Pending => {
158
- if bytes_read == 0 {
161
+ if self . bytes_read == 0 {
159
162
return Poll :: Pending ;
160
163
} else {
161
164
break ;
162
165
}
163
166
}
164
167
} ;
165
168
body_bytes_read += new_body_bytes_read;
166
- bytes_read += new_body_bytes_read;
169
+ self . bytes_read += new_body_bytes_read;
167
170
168
171
// Double check we did not read more body bytes than the total
169
172
// length of the body
@@ -192,7 +195,7 @@ impl Read for Encoder {
192
195
EncoderState :: UncomputedChunked => {
193
196
// We can read a maximum of the buffer's total size
194
197
// minus what we've already filled the buffer with
195
- let buffer_remaining = buf. len ( ) - bytes_read;
198
+ let buffer_remaining = buf. len ( ) - self . bytes_read ;
196
199
197
200
// ensure we have at least room for 1 byte in our buffer
198
201
if buffer_remaining == 0 {
@@ -208,7 +211,7 @@ impl Read for Encoder {
208
211
Poll :: Ready ( Ok ( n) ) => n,
209
212
Poll :: Ready ( Err ( e) ) => return Poll :: Ready ( Err ( e) ) ,
210
213
Poll :: Pending => {
211
- if bytes_read == 0 {
214
+ if self . bytes_read == 0 {
212
215
return Poll :: Pending ;
213
216
} else {
214
217
break ;
@@ -224,7 +227,7 @@ impl Read for Encoder {
224
227
225
228
// calculate the total size of the chunk including serialized
226
229
// length and the CRLF padding
227
- let total_chunk_size = bytes_read
230
+ let total_chunk_size = self . bytes_read
228
231
+ chunk_length_bytes_len
229
232
+ CRLF_LENGTH
230
233
+ chunk_length
@@ -233,24 +236,24 @@ impl Read for Encoder {
233
236
// See if we can write the chunk out in one go
234
237
if total_chunk_size < buffer_remaining {
235
238
// Write the chunk length into the buffer
236
- buf[ bytes_read..bytes_read + chunk_length_bytes_len]
239
+ buf[ self . bytes_read ..( self . bytes_read + chunk_length_bytes_len) ]
237
240
. copy_from_slice ( chunk_length_bytes) ;
238
- bytes_read += chunk_length_bytes_len;
241
+ self . bytes_read += chunk_length_bytes_len;
239
242
240
243
// follow chunk length with CRLF
241
- buf[ bytes_read] = CR ;
242
- buf[ bytes_read + 1 ] = LF ;
243
- bytes_read += 2 ;
244
+ buf[ self . bytes_read ] = CR ;
245
+ buf[ self . bytes_read + 1 ] = LF ;
246
+ self . bytes_read += 2 ;
244
247
245
248
// copy chunk into buf
246
- buf[ bytes_read..bytes_read + chunk_length]
249
+ buf[ self . bytes_read ..( self . bytes_read + chunk_length) ]
247
250
. copy_from_slice ( & chunk_buf[ ..chunk_length] ) ;
248
- bytes_read += chunk_length;
251
+ self . bytes_read += chunk_length;
249
252
250
253
// follow chunk with CRLF
251
- buf[ bytes_read] = CR ;
252
- buf[ bytes_read + 1 ] = LF ;
253
- bytes_read += 2 ;
254
+ buf[ self . bytes_read ] = CR ;
255
+ buf[ self . bytes_read + 1 ] = LF ;
256
+ self . bytes_read += 2 ;
254
257
255
258
if chunk_length == 0 {
256
259
self . state = EncoderState :: Done ;
@@ -276,7 +279,7 @@ impl Read for Encoder {
276
279
// follow chunk with CRLF
277
280
chunk[ bytes_written] = CR ;
278
281
chunk[ bytes_written + 1 ] = LF ;
279
- bytes_read += 2 ;
282
+ self . bytes_read += 2 ;
280
283
self . state = EncoderState :: ComputedChunked {
281
284
chunk : io:: Cursor :: new ( chunk) ,
282
285
is_last : chunk_length == 0 ,
@@ -288,18 +291,18 @@ impl Read for Encoder {
288
291
is_last,
289
292
} => {
290
293
let inner_poll_result = Pin :: new ( chunk) . poll_read ( cx, & mut buf) ;
291
- bytes_read += match inner_poll_result {
294
+ self . bytes_read += match inner_poll_result {
292
295
Poll :: Ready ( Ok ( n) ) => n,
293
296
Poll :: Ready ( Err ( e) ) => return Poll :: Ready ( Err ( e) ) ,
294
297
Poll :: Pending => {
295
- if bytes_read == 0 {
298
+ if self . bytes_read == 0 {
296
299
return Poll :: Pending ;
297
300
} else {
298
301
break ;
299
302
}
300
303
}
301
304
} ;
302
- if bytes_read == 0 {
305
+ if self . bytes_read == 0 {
303
306
self . state = match is_last {
304
307
true => EncoderState :: Done ,
305
308
false => EncoderState :: UncomputedChunked ,
@@ -311,6 +314,6 @@ impl Read for Encoder {
311
314
}
312
315
}
313
316
314
- Poll :: Ready ( Ok ( bytes_read as usize ) )
317
+ Poll :: Ready ( Ok ( self . bytes_read as usize ) )
315
318
}
316
319
}
0 commit comments