11use body_plz:: variants:: Body ;
22use bytes:: BytesMut ;
3- use header_plz:: body_headers:: encoding_info:: EncodingInfo ;
3+ use header_plz:: body_headers:: {
4+ content_encoding:: ContentEncoding , encoding_info:: EncodingInfo ,
5+ } ;
46
57use crate :: {
68 decode_struct:: DecodeStruct ,
@@ -31,69 +33,77 @@ where
3133
3234 pub fn try_next ( self ) -> Result < Self , MultiDecompressErrorReason > {
3335 match self {
34- DecodeState :: Start ( mut decode_struct ) => {
35- let next_state = if decode_struct . transfer_encoding_is_some ( ) {
36- let encodings = decode_struct . transfer_encoding ( ) ;
37- Self :: TransferEncoding ( decode_struct , encodings)
38- } else if decode_struct . content_encoding_is_some ( ) {
39- let encodings = decode_struct . content_encoding ( ) ;
40- Self :: ContentEncoding ( decode_struct , encodings)
41- } else if decode_struct . extra_body_is_some ( ) {
42- Self :: UpdateContentLength ( decode_struct )
36+ DecodeState :: Start ( mut ds ) => {
37+ let next_state = if ds . transfer_encoding_is_some ( ) {
38+ let encodings = ds . get_transfer_encoding ( ) ;
39+ Self :: TransferEncoding ( ds , encodings)
40+ } else if ds . content_encoding_is_some ( ) {
41+ let encodings = ds . get_content_encoding ( ) ;
42+ Self :: ContentEncoding ( ds , encodings)
43+ } else if ds . extra_body_is_some ( ) {
44+ Self :: UpdateContentLength ( ds )
4345 } else {
44- let body = decode_struct . take_main_body ( ) ;
45- decode_struct . message . set_body ( Body :: Raw ( body) ) ;
46+ let body = ds . take_main_body ( ) ;
47+ ds . message . set_body ( Body :: Raw ( body) ) ;
4648 Self :: End
4749 } ;
4850 Ok ( next_state)
4951 }
50- DecodeState :: TransferEncoding (
51- mut decode_struct,
52- mut encoding_infos,
53- ) => {
52+ DecodeState :: TransferEncoding ( mut ds, mut encoding_infos) => {
5453 // Convert chunked to raw
5554 // http/1 only
56- // TODO: check if only te is chunked
57- if decode_struct. is_chunked_te ( ) {
58- decode_struct. chunked_to_raw ( ) ;
55+ // remove chunked te from encoding_infos
56+ if ds. is_chunked_te ( ) {
57+ ds. chunked_to_raw ( ) ;
58+ // remove chunked TE
59+ encoding_infos
60+ . last_mut ( )
61+ . unwrap ( )
62+ . encodings_as_mut ( )
63+ . pop ( ) ;
64+ //if encoding_infos.last().unwrap().encodings().is_empty() {
65+ // encoding_infos.pop();
66+ //}
5967 }
60- let next_state = match apply_encoding (
61- & mut decode_struct,
62- & mut encoding_infos,
63- ) {
64- Ok ( ( ) ) if decode_struct. content_encoding_is_some ( ) => {
65- let encodings = decode_struct. content_encoding ( ) ;
66- Self :: ContentEncoding ( decode_struct, encodings)
68+
69+ let mut next_state = if is_chunked_te_only ( & encoding_infos) {
70+ ds. message . header_map_as_mut ( ) . remove_header_on_position (
71+ encoding_infos[ 0 ] . header_index ,
72+ ) ;
73+ if ds. content_encoding_is_some ( ) {
74+ let encodings = ds. get_content_encoding ( ) ;
75+ Self :: ContentEncoding ( ds, encodings)
76+ } else {
77+ Self :: UpdateContentLength ( ds)
6778 }
68- Ok ( ( ) ) => Self :: UpdateContentLength ( decode_struct) ,
69- Err ( e) => {
70- // TODO: maybe remove chunked TE
71- Self :: UpdateContentLengthAndErr ( decode_struct, e)
79+ } else {
80+ match apply_encoding ( & mut ds, & mut encoding_infos) {
81+ Ok ( ( ) ) if ds. content_encoding_is_some ( ) => {
82+ let encodings = ds. get_content_encoding ( ) ;
83+ Self :: ContentEncoding ( ds, encodings)
84+ }
85+ Ok ( ( ) ) => Self :: UpdateContentLength ( ds) ,
86+ Err ( e) => Self :: UpdateContentLengthAndErr ( ds, e) ,
7287 }
7388 } ;
89+ next_state. set_transfer_encoding ( encoding_infos) ;
7490 Ok ( next_state)
7591 }
76- DecodeState :: ContentEncoding (
77- mut decode_struct,
78- mut encoding_infos,
79- ) => {
80- let next_state = match apply_encoding (
81- & mut decode_struct,
82- & mut encoding_infos,
83- ) {
84- Err ( e) => {
85- Self :: UpdateContentLengthAndErr ( decode_struct, e)
86- }
87- Ok ( _) => Self :: UpdateContentLength ( decode_struct) ,
88- } ;
92+ DecodeState :: ContentEncoding ( mut ds, mut encoding_infos) => {
93+ let mut next_state =
94+ match apply_encoding ( & mut ds, & mut encoding_infos) {
95+ Err ( e) => Self :: UpdateContentLengthAndErr ( ds, e) ,
96+ Ok ( _) => Self :: UpdateContentLength ( ds) ,
97+ } ;
98+ next_state. set_content_encoding ( encoding_infos) ;
8999 Ok ( next_state)
90100 }
91- DecodeState :: UpdateContentLength ( mut decode_struct ) => {
92- decode_struct . add_body_and_update_cl ( ) ;
101+ DecodeState :: UpdateContentLength ( mut ds ) => {
102+ ds . add_body_and_update_cl ( ) ;
93103 Ok ( Self :: End )
94104 }
95- DecodeState :: UpdateContentLengthAndErr ( mut decode_struct , e) => {
96- decode_struct . add_body_and_update_cl ( ) ;
105+ DecodeState :: UpdateContentLengthAndErr ( mut ds , e) => {
106+ ds . add_body_and_update_cl ( ) ;
97107 Err ( e)
98108 }
99109 DecodeState :: End => Ok ( DecodeState :: End ) ,
@@ -103,6 +113,27 @@ where
103113 pub fn is_ended ( & self ) -> bool {
104114 matches ! ( self , DecodeState :: End )
105115 }
116+
117+ pub fn decode_struct_as_mut ( & mut self ) -> & mut DecodeStruct < ' a , T > {
118+ match self {
119+ DecodeState :: Start ( ds) => ds,
120+ DecodeState :: TransferEncoding ( ds, _) => ds,
121+ DecodeState :: ContentEncoding ( ds, _) => ds,
122+ DecodeState :: UpdateContentLength ( ds) => ds,
123+ DecodeState :: UpdateContentLengthAndErr ( ds, _) => ds,
124+ _ => unreachable ! ( ) ,
125+ }
126+ }
127+
128+ pub fn set_transfer_encoding ( & mut self , te : Vec < EncodingInfo > ) {
129+ let ds_mut = self . decode_struct_as_mut ( ) ;
130+ ds_mut. set_transfer_encoding ( te) ;
131+ }
132+
133+ pub fn set_content_encoding ( & mut self , ce : Vec < EncodingInfo > ) {
134+ let ds_mut = self . decode_struct_as_mut ( ) ;
135+ ds_mut. set_content_encoding ( ce) ;
136+ }
106137}
107138
108139fn apply_encoding < T > (
@@ -175,3 +206,7 @@ where
175206 }
176207 }
177208}
209+
210+ fn is_chunked_te_only ( einfo_vec : & [ EncodingInfo ] ) -> bool {
211+ einfo_vec. len ( ) == 1 && einfo_vec[ 0 ] . encodings ( ) . is_empty ( )
212+ }
0 commit comments