@@ -20,17 +20,17 @@ pub(crate) struct Encoder {
20
20
/// The state of the encoding process
21
21
state : State ,
22
22
/// Track bytes read in a call to poll_read.
23
- bytes_read : usize ,
23
+ bytes_written : usize ,
24
24
/// The data we're writing as part of the head section.
25
25
head : Vec < u8 > ,
26
26
/// The amount of bytes read from the head section.
27
- head_bytes_read : usize ,
27
+ head_bytes_written : usize ,
28
28
/// The total length of the body.
29
29
/// This is only used in the known-length body encoder.
30
30
body_len : usize ,
31
31
/// The amount of bytes read from the body.
32
32
/// This is only used in the known-length body encoder.
33
- body_bytes_read : usize ,
33
+ body_bytes_written : usize ,
34
34
/// An encoder for chunked encoding.
35
35
chunked : ChunkedEncoder ,
36
36
}
@@ -51,11 +51,11 @@ impl Encoder {
51
51
Self {
52
52
res,
53
53
state : State :: Init ,
54
- bytes_read : 0 ,
54
+ bytes_written : 0 ,
55
55
head : vec ! [ ] ,
56
- head_bytes_read : 0 ,
56
+ head_bytes_written : 0 ,
57
57
body_len : 0 ,
58
- body_bytes_read : 0 ,
58
+ body_bytes_written : 0 ,
59
59
chunked : ChunkedEncoder :: new ( ) ,
60
60
}
61
61
}
@@ -65,16 +65,16 @@ impl Encoder {
65
65
cx : & mut Context < ' _ > ,
66
66
buf : & mut [ u8 ] ,
67
67
) -> Poll < io:: Result < usize > > {
68
- self . bytes_read = 0 ;
68
+ self . bytes_written = 0 ;
69
69
let res = match self . state {
70
70
State :: Init => self . init ( cx, buf) ,
71
71
State :: ComputeHead => self . compute_head ( cx, buf) ,
72
72
State :: EncodeHead => self . encode_head ( cx, buf) ,
73
73
State :: FixedBody => self . encode_fixed_body ( cx, buf) ,
74
74
State :: ChunkedBody => self . encode_chunked_body ( cx, buf) ,
75
- State :: End => Poll :: Ready ( Ok ( self . bytes_read ) ) ,
75
+ State :: End => Poll :: Ready ( Ok ( self . bytes_written ) ) ,
76
76
} ;
77
- log:: trace!( "ServerEncoder {} bytes written" , self . bytes_read ) ;
77
+ log:: trace!( "ServerEncoder {} bytes written" , self . bytes_written ) ;
78
78
res
79
79
}
80
80
@@ -144,15 +144,15 @@ impl Encoder {
144
144
fn encode_head ( & mut self , cx : & mut Context < ' _ > , buf : & mut [ u8 ] ) -> Poll < io:: Result < usize > > {
145
145
// Read from the serialized headers, url and methods.
146
146
let head_len = self . head . len ( ) ;
147
- let len = std:: cmp:: min ( head_len - self . head_bytes_read , buf. len ( ) ) ;
148
- let range = self . head_bytes_read ..self . head_bytes_read + len;
147
+ let len = std:: cmp:: min ( head_len - self . head_bytes_written , buf. len ( ) ) ;
148
+ let range = self . head_bytes_written ..self . head_bytes_written + len;
149
149
buf[ 0 ..len] . copy_from_slice ( & self . head [ range] ) ;
150
- self . bytes_read += len;
151
- self . head_bytes_read += len;
150
+ self . bytes_written += len;
151
+ self . head_bytes_written += len;
152
152
153
153
// If we've read the total length of the head we're done
154
154
// reading the head and can transition to reading the body
155
- if self . head_bytes_read == head_len {
155
+ if self . head_bytes_written == head_len {
156
156
// The response length lets us know if we are encoding
157
157
// our body in chunks or not
158
158
match self . res . len ( ) {
@@ -171,7 +171,7 @@ impl Encoder {
171
171
} else {
172
172
// If we haven't read the entire header it means `buf` isn't
173
173
// big enough. Break out of loop and return from `poll_read`
174
- return Poll :: Ready ( Ok ( self . bytes_read ) ) ;
174
+ return Poll :: Ready ( Ok ( self . bytes_written ) ) ;
175
175
}
176
176
}
177
177
@@ -183,47 +183,48 @@ impl Encoder {
183
183
) -> Poll < io:: Result < usize > > {
184
184
// Double check that we didn't somehow read more bytes than
185
185
// can fit in our buffer
186
- debug_assert ! ( self . bytes_read <= buf. len( ) ) ;
186
+ debug_assert ! ( self . bytes_written <= buf. len( ) ) ;
187
187
188
188
// ensure we have at least room for 1 more byte in our buffer
189
- if self . bytes_read == buf. len ( ) {
190
- return Poll :: Ready ( Ok ( self . bytes_read ) ) ;
189
+ if self . bytes_written == buf. len ( ) {
190
+ return Poll :: Ready ( Ok ( self . bytes_written ) ) ;
191
191
}
192
192
193
193
// Figure out how many bytes we can read.
194
- let upper_bound = ( self . bytes_read + self . body_len - self . body_bytes_read ) . min ( buf. len ( ) ) ;
194
+ let upper_bound =
195
+ ( self . bytes_written + self . body_len - self . body_bytes_written ) . min ( buf. len ( ) ) ;
195
196
// Read bytes from body
196
- let range = self . bytes_read ..upper_bound;
197
+ let range = self . bytes_written ..upper_bound;
197
198
let inner_poll_result = Pin :: new ( & mut self . res ) . poll_read ( cx, & mut buf[ range] ) ;
198
- let new_body_bytes_read = match inner_poll_result {
199
+ let new_body_bytes_written = match inner_poll_result {
199
200
Poll :: Ready ( Ok ( n) ) => n,
200
201
Poll :: Ready ( Err ( e) ) => return Poll :: Ready ( Err ( e) ) ,
201
- Poll :: Pending => match self . bytes_read {
202
+ Poll :: Pending => match self . bytes_written {
202
203
0 => return Poll :: Pending ,
203
204
n => return Poll :: Ready ( Ok ( n) ) ,
204
205
} ,
205
206
} ;
206
- self . body_bytes_read += new_body_bytes_read ;
207
- self . bytes_read += new_body_bytes_read ;
207
+ self . body_bytes_written += new_body_bytes_written ;
208
+ self . bytes_written += new_body_bytes_written ;
208
209
209
210
// Double check we did not read more body bytes than the total
210
211
// length of the body
211
212
debug_assert ! (
212
- self . body_bytes_read <= self . body_len,
213
+ self . body_bytes_written <= self . body_len,
213
214
"Too many bytes read. Expected: {}, read: {}" ,
214
215
self . body_len,
215
- self . body_bytes_read
216
+ self . body_bytes_written
216
217
) ;
217
218
218
- if self . body_len == self . body_bytes_read {
219
+ if self . body_len == self . body_bytes_written {
219
220
// If we've read the `len` number of bytes, end
220
221
self . set_state ( State :: End ) ;
221
- return Poll :: Ready ( Ok ( self . bytes_read ) ) ;
222
- } else if new_body_bytes_read == 0 {
222
+ return Poll :: Ready ( Ok ( self . bytes_written ) ) ;
223
+ } else if new_body_bytes_written == 0 {
223
224
// If we've reached unexpected EOF, end anyway
224
225
// TODO: do something?
225
226
self . set_state ( State :: End ) ;
226
- return Poll :: Ready ( Ok ( self . bytes_read ) ) ;
227
+ return Poll :: Ready ( Ok ( self . bytes_written ) ) ;
227
228
} else {
228
229
self . encode_fixed_body ( cx, buf)
229
230
}
@@ -236,19 +237,19 @@ impl Encoder {
236
237
cx : & mut Context < ' _ > ,
237
238
buf : & mut [ u8 ] ,
238
239
) -> Poll < io:: Result < usize > > {
239
- let buf = & mut buf[ self . bytes_read ..] ;
240
+ let buf = & mut buf[ self . bytes_written ..] ;
240
241
match self . chunked . encode ( & mut self . res , cx, buf) {
241
242
Poll :: Ready ( Ok ( read) ) => {
242
- self . bytes_read += read;
243
- if self . bytes_read == 0 {
243
+ self . bytes_written += read;
244
+ if self . bytes_written == 0 {
244
245
self . set_state ( State :: End ) ;
245
246
}
246
- Poll :: Ready ( Ok ( self . bytes_read ) )
247
+ Poll :: Ready ( Ok ( self . bytes_written ) )
247
248
}
248
249
Poll :: Ready ( Err ( err) ) => Poll :: Ready ( Err ( err) ) ,
249
250
Poll :: Pending => {
250
- if self . bytes_read > 0 {
251
- return Poll :: Ready ( Ok ( self . bytes_read ) ) ;
251
+ if self . bytes_written > 0 {
252
+ return Poll :: Ready ( Ok ( self . bytes_written ) ) ;
252
253
}
253
254
Poll :: Pending
254
255
}
0 commit comments