@@ -4,7 +4,7 @@ use buffer_plz::Cursor;
44use bytes:: BytesMut ;
55use thiserror:: Error ;
66
7- use crate :: variants:: chunked:: ChunkedBody ;
7+ use crate :: variants:: chunked:: ChunkType ;
88
99use super :: content_length_reader:: read_content_length;
1010use header_plz:: abnf:: CRLF ;
@@ -26,7 +26,7 @@ pub enum ChunkReaderError {
2626// Enum to represent chunked reader state
2727#[ cfg_attr( any( debug_assertions, test) , derive( Eq , PartialEq ) ) ]
2828#[ derive( Debug ) ]
29- pub enum ChunkReader {
29+ pub enum ChunkReaderState {
3030 ReadSize ,
3131 ReadChunk ( usize ) ,
3232 LastChunk ,
@@ -75,24 +75,24 @@ pub enum ChunkReader {
7575 * ChunkReaderError::LastChunkPoll [3]
7676 */
7777
78- impl ChunkReader {
79- pub fn next ( & mut self , buf : & mut Cursor ) -> Option < ChunkedBody > {
78+ impl ChunkReaderState {
79+ pub fn next ( & mut self , buf : & mut Cursor ) -> Option < ChunkType > {
8080 match self {
8181 // 1. Read Size
8282 Self :: ReadSize => {
8383 // 1.a. call mark_size_chunk()
84- if ChunkReader :: mark_size_chunk ( buf) {
84+ if ChunkReaderState :: mark_size_chunk ( buf) {
8585 match Self :: get_size ( buf) {
8686 Ok ( size) => {
8787 // 1.b.1. If size == 0, then LastChunk
8888 if size == 0 {
8989 * self = Self :: LastChunk ;
90- return Some ( ChunkedBody :: LastChunk ( buf. split_at_current_pos ( ) ) ) ;
90+ return Some ( ChunkType :: LastChunk ( buf. split_at_current_pos ( ) ) ) ;
9191 // 1.b.2. else, ReadChunk(size + 2)
9292 } else {
9393 * self = Self :: ReadChunk ( size + 2 ) ;
9494 }
95- return Some ( ChunkedBody :: Size ( buf. split_at_current_pos ( ) ) ) ;
95+ return Some ( ChunkType :: Size ( buf. split_at_current_pos ( ) ) ) ;
9696 }
9797 // 1.c. If get_size() returns error, Failed State
9898 Err ( e) => {
@@ -107,27 +107,27 @@ impl ChunkReader {
107107 & mut Self :: ReadChunk ( ref mut size) => {
108108 if read_content_length ( buf, size) {
109109 * self = Self :: ReadSize ;
110- return Some ( ChunkedBody :: Chunk ( buf. split_at_current_pos ( ) ) ) ;
110+ return Some ( ChunkType :: Chunk ( buf. split_at_current_pos ( ) ) ) ;
111111 }
112112 None
113113 }
114114 Self :: LastChunk => {
115115 * self = Self :: Failed ( ChunkReaderError :: LastChunkPoll ) ;
116116 // Temproary return Should not reach this state
117- Some ( ChunkedBody :: LastChunk ( BytesMut :: new ( ) ) )
117+ Some ( ChunkType :: LastChunk ( BytesMut :: new ( ) ) )
118118 }
119119 Self :: ReadTrailers => {
120120 // 4.a. If Empty Header
121121 if buf. remaining ( ) == CRLF . as_bytes ( ) {
122122 buf. set_position ( buf. position ( ) + 2 ) ;
123123 * self = Self :: End ;
124- return Some ( ChunkedBody :: EndCRLF ( buf. split_at_current_pos ( ) ) ) ;
124+ return Some ( ChunkType :: EndCRLF ( buf. split_at_current_pos ( ) ) ) ;
125125 }
126126 // 4.b. Actual Headers
127127 if find_message_head_end ( buf) {
128128 * self = Self :: End ;
129129 let header_map = HeaderMap :: new ( buf. split_at_current_pos ( ) ) ;
130- Some ( ChunkedBody :: Trailers ( header_map) )
130+ Some ( ChunkType :: Trailers ( header_map) )
131131 } else {
132132 None
133133 }
@@ -140,7 +140,7 @@ impl ChunkReader {
140140 {
141141 buf. set_position ( index + 2 ) ;
142142 * self = Self :: End ;
143- Some ( ChunkedBody :: EndCRLF ( buf. split_at_current_pos ( ) ) )
143+ Some ( ChunkType :: EndCRLF ( buf. split_at_current_pos ( ) ) )
144144 } else {
145145 None
146146 }
@@ -211,11 +211,11 @@ pub(crate) mod tests {
211211 let mut buf = BytesMut :: from ( data. as_bytes ( ) ) ;
212212 let mut cbuf = Cursor :: new ( & mut buf) ;
213213 let verify = cbuf. as_ref ( ) . into ( ) ;
214- let mut state = ChunkReader :: ReadSize ;
214+ let mut state = ChunkReaderState :: ReadSize ;
215215 let result = state. next ( & mut cbuf) ;
216- assert_eq ! ( result. unwrap( ) , ChunkedBody :: Size ( verify) ) ;
216+ assert_eq ! ( result. unwrap( ) , ChunkType :: Size ( verify) ) ;
217217 assert_eq ! ( cbuf. position( ) , cbuf. len( ) ) ;
218- assert_eq ! ( state, ChunkReader :: ReadChunk ( 6 ) ) ;
218+ assert_eq ! ( state, ChunkReaderState :: ReadChunk ( 6 ) ) ;
219219 }
220220
221221 #[ test]
@@ -225,11 +225,11 @@ pub(crate) mod tests {
225225 let mut cbuf = Cursor :: new ( & mut buf) ;
226226
227227 let verify = "mozilla\r \n " ;
228- let mut state = ChunkReader :: ReadChunk ( 9 ) ;
228+ let mut state = ChunkReaderState :: ReadChunk ( 9 ) ;
229229 let result = state. next ( & mut cbuf) ;
230- assert_eq ! ( result. unwrap( ) , ChunkedBody :: Chunk ( verify. into( ) ) ) ;
230+ assert_eq ! ( result. unwrap( ) , ChunkType :: Chunk ( verify. into( ) ) ) ;
231231 assert_eq ! ( cbuf. position( ) , 0 ) ;
232- assert_eq ! ( ChunkReader :: ReadSize , state) ;
232+ assert_eq ! ( ChunkReaderState :: ReadSize , state) ;
233233 }
234234
235235 #[ test]
@@ -238,53 +238,53 @@ pub(crate) mod tests {
238238 let mut buf = BytesMut :: from ( data. as_bytes ( ) ) ;
239239 let mut cbuf = Cursor :: new ( & mut buf) ;
240240 let verify = cbuf. as_ref ( ) . into ( ) ;
241- let mut state = ChunkReader :: ReadSize ;
241+ let mut state = ChunkReaderState :: ReadSize ;
242242 let result = state. next ( & mut cbuf) ;
243- assert_eq ! ( result. unwrap( ) , ChunkedBody :: LastChunk ( verify) ) ;
243+ assert_eq ! ( result. unwrap( ) , ChunkType :: LastChunk ( verify) ) ;
244244 assert_eq ! ( cbuf. position( ) , cbuf. len( ) ) ;
245- assert_eq ! ( ChunkReader :: LastChunk , state) ;
245+ assert_eq ! ( ChunkReaderState :: LastChunk , state) ;
246246 }
247247
248248 #[ test]
249249 fn test_chunked_reader_trailer ( ) {
250250 let data = "key: value\r \n \r \n " ;
251- let mut state = ChunkReader :: ReadTrailers ;
251+ let mut state = ChunkReaderState :: ReadTrailers ;
252252 let mut buf = BytesMut :: from ( data. as_bytes ( ) ) ;
253253 let mut cbuf = Cursor :: new ( & mut buf) ;
254254 let result = state. next ( & mut cbuf) ;
255255 assert_eq ! ( cbuf. position( ) , cbuf. len( ) ) ;
256- assert_eq ! ( ChunkReader :: End , state) ;
257- assert ! ( matches!( result. unwrap( ) , ChunkedBody :: Trailers ( _) ) ) ;
256+ assert_eq ! ( ChunkReaderState :: End , state) ;
257+ assert ! ( matches!( result. unwrap( ) , ChunkType :: Trailers ( _) ) ) ;
258258 }
259259
260260 #[ test]
261261 fn test_chunked_reader_trailer_incremental ( ) {
262262 let data = "key: value" ;
263263 let mut buf = BytesMut :: from ( data. as_bytes ( ) ) ;
264264 let mut cbuf = Cursor :: new ( & mut buf) ;
265- let mut state = ChunkReader :: ReadTrailers ;
265+ let mut state = ChunkReaderState :: ReadTrailers ;
266266 let result = state. next ( & mut cbuf) ;
267267 assert_eq ! ( result, None ) ;
268268 cbuf. as_mut ( ) . put_slice ( b"\r \n " ) ;
269269 let result = state. next ( & mut cbuf) ;
270270 assert_eq ! ( result, None ) ;
271271 cbuf. as_mut ( ) . put_slice ( b"\r \n " ) ;
272272 let result = state. next ( & mut cbuf) ;
273- assert ! ( matches!( result. unwrap( ) , ChunkedBody :: Trailers ( _) ) ) ;
273+ assert ! ( matches!( result. unwrap( ) , ChunkType :: Trailers ( _) ) ) ;
274274 assert_eq ! ( cbuf. position( ) , cbuf. len( ) ) ;
275- assert_eq ! ( ChunkReader :: End , state) ;
275+ assert_eq ! ( ChunkReaderState :: End , state) ;
276276 }
277277
278278 #[ test]
279279 fn test_chunked_reader_no_trailer ( ) {
280280 let data = "\r \n " ;
281281 let mut buf = BytesMut :: from ( data. as_bytes ( ) ) ;
282282 let mut cbuf = Cursor :: new ( & mut buf) ;
283- let mut state = ChunkReader :: ReadTrailers ;
283+ let mut state = ChunkReaderState :: ReadTrailers ;
284284 let result = state. next ( & mut cbuf) ;
285- assert ! ( matches!( result. unwrap( ) , ChunkedBody :: EndCRLF ( _) ) ) ;
285+ assert ! ( matches!( result. unwrap( ) , ChunkType :: EndCRLF ( _) ) ) ;
286286 assert_eq ! ( cbuf. position( ) , cbuf. len( ) ) ;
287- assert_eq ! ( ChunkReader :: End , state) ;
287+ assert_eq ! ( ChunkReaderState :: End , state) ;
288288 }
289289
290290 #[ test]
@@ -301,12 +301,12 @@ pub(crate) mod tests {
301301 \r \n ";
302302 let mut buf = BytesMut :: from ( data. as_bytes ( ) ) ;
303303 let mut cbuf = Cursor :: new ( & mut buf) ;
304- let mut state = ChunkReader :: ReadSize ;
304+ let mut state = ChunkReaderState :: ReadSize ;
305305 // Poll 1 Read Chunk
306306 match state. next ( & mut cbuf) {
307- Some ( ChunkedBody :: Size ( data) ) => {
307+ Some ( ChunkType :: Size ( data) ) => {
308308 assert_eq ! ( 0 , cbuf. position( ) ) ;
309- assert_eq ! ( ChunkReader :: ReadChunk ( 9 ) , state) ;
309+ assert_eq ! ( ChunkReaderState :: ReadChunk ( 9 ) , state) ;
310310 let verify = BytesMut :: from ( "7; hola amigo\r \n " ) ;
311311 assert_eq ! ( data, verify) ;
312312 }
@@ -317,9 +317,9 @@ pub(crate) mod tests {
317317 //dbg!(String::from_utf8_lossy(&cbuf.remaining()));
318318 // Poll 2 Read Size
319319 match state. next ( & mut cbuf) {
320- Some ( ChunkedBody :: Chunk ( data) ) => {
320+ Some ( ChunkType :: Chunk ( data) ) => {
321321 assert_eq ! ( 0 , cbuf. position( ) ) ;
322- assert_eq ! ( ChunkReader :: ReadSize , state) ;
322+ assert_eq ! ( ChunkReaderState :: ReadSize , state) ;
323323 let verify = BytesMut :: from ( "Mozilla\r \n " ) ;
324324 assert_eq ! ( data, verify) ;
325325 }
@@ -330,9 +330,9 @@ pub(crate) mod tests {
330330 //dbg!(String::from_utf8_lossy(&cbuf.remaining()));
331331 // Poll 3 Read Chunk
332332 match state. next ( & mut cbuf) {
333- Some ( ChunkedBody :: Size ( data) ) => {
333+ Some ( ChunkType :: Size ( data) ) => {
334334 assert_eq ! ( 0 , cbuf. position( ) ) ;
335- assert_eq ! ( ChunkReader :: ReadChunk ( 11 ) , state) ;
335+ assert_eq ! ( ChunkReaderState :: ReadChunk ( 11 ) , state) ;
336336 let verify = BytesMut :: from ( "9\r \n " ) ;
337337 assert_eq ! ( data, verify) ;
338338 }
@@ -343,9 +343,9 @@ pub(crate) mod tests {
343343 //dbg!(String::from_utf8_lossy(&cbuf.remaining()));
344344 // Poll 4
345345 match state. next ( & mut cbuf) {
346- Some ( ChunkedBody :: Chunk ( data) ) => {
346+ Some ( ChunkType :: Chunk ( data) ) => {
347347 assert_eq ! ( 0 , cbuf. position( ) ) ;
348- assert_eq ! ( ChunkReader :: ReadSize , state) ;
348+ assert_eq ! ( ChunkReaderState :: ReadSize , state) ;
349349 let verify = BytesMut :: from ( "Developer\r \n " ) ;
350350 assert_eq ! ( data, verify) ;
351351 }
@@ -355,9 +355,9 @@ pub(crate) mod tests {
355355 }
356356 // Poll 5 Read Size
357357 match state. next ( & mut cbuf) {
358- Some ( ChunkedBody :: Size ( data) ) => {
358+ Some ( ChunkType :: Size ( data) ) => {
359359 assert_eq ! ( 0 , cbuf. position( ) ) ;
360- assert_eq ! ( ChunkReader :: ReadChunk ( 9 ) , state) ;
360+ assert_eq ! ( ChunkReaderState :: ReadChunk ( 9 ) , state) ;
361361 let verify = BytesMut :: from ( "7\r \n " ) ;
362362 assert_eq ! ( data, verify) ;
363363 }
@@ -367,9 +367,9 @@ pub(crate) mod tests {
367367 }
368368 // Poll 6
369369 match state. next ( & mut cbuf) {
370- Some ( ChunkedBody :: Chunk ( data) ) => {
370+ Some ( ChunkType :: Chunk ( data) ) => {
371371 assert_eq ! ( 0 , cbuf. position( ) ) ;
372- assert_eq ! ( ChunkReader :: ReadSize , state) ;
372+ assert_eq ! ( ChunkReaderState :: ReadSize , state) ;
373373 let verify = BytesMut :: from ( "Network\r \n " ) ;
374374 assert_eq ! ( data, verify) ;
375375 }
@@ -379,9 +379,9 @@ pub(crate) mod tests {
379379 }
380380 // Poll 7
381381 match state. next ( & mut cbuf) {
382- Some ( ChunkedBody :: LastChunk ( data) ) => {
382+ Some ( ChunkType :: LastChunk ( data) ) => {
383383 assert_eq ! ( 0 , cbuf. position( ) ) ;
384- assert_eq ! ( ChunkReader :: LastChunk , state) ;
384+ assert_eq ! ( ChunkReaderState :: LastChunk , state) ;
385385 let verify = BytesMut :: from ( "0\r \n " ) ;
386386 assert_eq ! ( data, verify) ;
387387 }
@@ -391,9 +391,12 @@ pub(crate) mod tests {
391391 }
392392 // Poll 8 - If polled in Last Chunk State Error
393393 match state. next ( & mut cbuf) {
394- Some ( ChunkedBody :: LastChunk ( data) ) => {
394+ Some ( ChunkType :: LastChunk ( data) ) => {
395395 assert_eq ! ( 0 , cbuf. position( ) ) ;
396- assert_eq ! ( ChunkReader :: Failed ( ChunkReaderError :: LastChunkPoll ) , state) ;
396+ assert_eq ! (
397+ ChunkReaderState :: Failed ( ChunkReaderError :: LastChunkPoll ) ,
398+ state
399+ ) ;
397400 let verify = BytesMut :: from ( "" ) ;
398401 assert_eq ! ( data, verify) ;
399402 }
@@ -402,11 +405,11 @@ pub(crate) mod tests {
402405 }
403406 }
404407 // Poll 9
405- state = ChunkReader :: ReadTrailers ;
408+ state = ChunkReaderState :: ReadTrailers ;
406409 match state. next ( & mut cbuf) {
407- Some ( ChunkedBody :: Trailers ( data) ) => {
410+ Some ( ChunkType :: Trailers ( data) ) => {
408411 assert_eq ! ( 0 , cbuf. position( ) ) ;
409- assert_eq ! ( ChunkReader :: End , state) ;
412+ assert_eq ! ( ChunkReaderState :: End , state) ;
410413 let buf = "a: b\r \n \
411414 c: d\r \n \r \n ";
412415 let verify = HeaderMap :: new ( BytesMut :: from ( buf) ) ;
@@ -433,25 +436,25 @@ pub(crate) mod tests {
433436 \r \n ";
434437 let mut buf = BytesMut :: from ( data) ;
435438 let mut cbuf = Cursor :: new ( & mut buf) ;
436- let mut state = ChunkReader :: ReadSize ;
439+ let mut state = ChunkReaderState :: ReadSize ;
437440 loop {
438441 match state. next ( & mut cbuf) {
439442 Some ( _) => match state {
440- ChunkReader :: LastChunk => break ,
443+ ChunkReaderState :: LastChunk => break ,
441444 _ => continue ,
442445 } ,
443446 None => {
444- assert_eq ! ( state, ChunkReader :: ReadChunk ( 14 ) ) ;
447+ assert_eq ! ( state, ChunkReaderState :: ReadChunk ( 14 ) ) ;
445448 cbuf. as_mut ( ) . put_slice ( remain. as_bytes ( ) ) ;
446449 continue ;
447450 }
448451 }
449452 }
450453
451- assert_eq ! ( state, ChunkReader :: LastChunk ) ;
452- state = ChunkReader :: EndCRLF ;
454+ assert_eq ! ( state, ChunkReaderState :: LastChunk ) ;
455+ state = ChunkReaderState :: EndCRLF ;
453456 state. next ( & mut cbuf) ;
454- assert_eq ! ( state, ChunkReader :: End ) ;
457+ assert_eq ! ( state, ChunkReaderState :: End ) ;
455458 assert_eq ! ( cbuf. position( ) , cbuf. len( ) ) ;
456459 }
457460
@@ -469,31 +472,31 @@ pub(crate) mod tests {
469472 \r \n ";
470473 let mut buf = BytesMut :: from ( data) ;
471474 let mut cbuf = Cursor :: new ( & mut buf) ;
472- let mut state = ChunkReader :: ReadSize ;
475+ let mut state = ChunkReaderState :: ReadSize ;
473476 loop {
474477 match state. next ( & mut cbuf) {
475478 Some ( _) => match state {
476- ChunkReader :: LastChunk => state = ChunkReader :: ReadTrailers ,
477- ChunkReader :: End => {
479+ ChunkReaderState :: LastChunk => state = ChunkReaderState :: ReadTrailers ,
480+ ChunkReaderState :: End => {
478481 break ;
479482 }
480483 _ => continue ,
481484 } ,
482485 None => panic ! ( ) ,
483486 }
484487 }
488+
485489 assert_eq ! ( cbuf. remaining( ) . len( ) , 0 ) ;
486- assert_eq ! ( state, ChunkReader :: End ) ;
487490 }
488491
489492 #[ test]
490493 fn test_get_size ( ) {
491494 let data = "7\r \n " ;
492495 let mut buf = BytesMut :: from ( data) ;
493496 let mut cbuf = Cursor :: new ( & mut buf) ;
494- let result = ChunkReader :: mark_size_chunk ( & mut cbuf) ;
497+ let result = ChunkReaderState :: mark_size_chunk ( & mut cbuf) ;
495498 assert ! ( result) ;
496- let size = ChunkReader :: get_size ( & mut cbuf) . unwrap ( ) ;
499+ let size = ChunkReaderState :: get_size ( & mut cbuf) . unwrap ( ) ;
497500 assert_eq ! ( size, 7 ) ;
498501 }
499502
@@ -503,10 +506,10 @@ pub(crate) mod tests {
503506 extra data";
504507 let mut buf = BytesMut :: from ( data) ;
505508 let mut cbuf = Cursor :: new ( & mut buf) ;
506- let mut state = ChunkReader :: EndCRLF ;
509+ let mut state = ChunkReaderState :: EndCRLF ;
507510 let chunk = state. next ( & mut cbuf) . unwrap ( ) ;
508- assert ! ( matches!( chunk, ChunkedBody :: EndCRLF ( _) ) ) ;
509- assert_eq ! ( state, ChunkReader :: End ) ;
511+ assert ! ( matches!( chunk, ChunkType :: EndCRLF ( _) ) ) ;
512+ assert_eq ! ( state, ChunkReaderState :: End ) ;
510513 assert_eq ! ( cbuf. remaining( ) , & b"extra data" [ ..] ) ;
511514 }
512515}
0 commit comments