@@ -11,6 +11,9 @@ use super::{
1111} ;
1212use crate :: { body:: BodySize , error:: ParseError , ConnectionType , Request , Response , ServiceConfig } ;
1313
14+ #[ cfg( feature = "proxy-protocol" ) ]
15+ use crate :: { http_message:: HttpMessage , proxy_protocol:: ProxyProtocol } ;
16+
1417bitflags ! {
1518 #[ derive( Debug , Clone , Copy ) ]
1619 struct Flags : u8 {
@@ -110,6 +113,7 @@ impl Decoder for Codec {
110113 type Error = ParseError ;
111114
112115 fn decode ( & mut self , src : & mut BytesMut ) -> Result < Option < Self :: Item > , Self :: Error > {
116+ #[ allow( clippy:: collapsible_else_if) ]
113117 if let Some ( ref mut payload) = self . payload {
114118 Ok ( match payload. decode ( src) ? {
115119 Some ( PayloadItem :: Chunk ( chunk) ) => Some ( Message :: Chunk ( Some ( chunk) ) ) ,
@@ -119,29 +123,49 @@ impl Decoder for Codec {
119123 }
120124 None => None ,
121125 } )
122- } else if let Some ( ( req, payload) ) = self . decoder . decode ( src) ? {
123- let head = req. head ( ) ;
124- self . flags . set ( Flags :: HEAD , head. method == Method :: HEAD ) ;
125- self . version = head. version ;
126- self . conn_type = head. connection_type ( ) ;
127-
128- if self . conn_type == ConnectionType :: KeepAlive
129- && !self . flags . contains ( Flags :: KEEP_ALIVE_ENABLED )
130- {
131- self . conn_type = ConnectionType :: Close
132- }
126+ } else {
127+ #[ cfg( feature = "proxy-protocol" ) ]
128+ let proxy_protocol = if self . config . proxy_protocol ( ) {
129+ let p = ProxyProtocol :: decode ( src) ?;
130+ if p. is_none ( ) {
131+ return Ok ( None ) ;
132+ }
133+ p
134+ } else {
135+ None
136+ } ;
137+
138+ if let Some ( ( req, payload) ) = self . decoder . decode ( src) ? {
139+ let head = req. head ( ) ;
140+ self . flags . set ( Flags :: HEAD , head. method == Method :: HEAD ) ;
141+ self . version = head. version ;
142+ self . conn_type = head. connection_type ( ) ;
143+
144+ if self . conn_type == ConnectionType :: KeepAlive
145+ && !self . flags . contains ( Flags :: KEEP_ALIVE_ENABLED )
146+ {
147+ self . conn_type = ConnectionType :: Close
148+ }
133149
134- match payload {
135- PayloadType :: None => self . payload = None ,
136- PayloadType :: Payload ( pl) => self . payload = Some ( pl) ,
137- PayloadType :: Stream ( pl) => {
138- self . payload = Some ( pl) ;
139- self . flags . insert ( Flags :: STREAM ) ;
150+ #[ cfg( feature = "proxy-protocol" ) ]
151+ // set proxy protocol
152+ if let Some ( proxy_protocol) = proxy_protocol {
153+ let mut extensions = req. extensions_mut ( ) ;
154+ extensions. insert ( proxy_protocol) ;
140155 }
156+
157+ match payload {
158+ PayloadType :: None => self . payload = None ,
159+ PayloadType :: Payload ( pl) => self . payload = Some ( pl) ,
160+ PayloadType :: Stream ( pl) => {
161+ self . payload = Some ( pl) ;
162+ self . flags . insert ( Flags :: STREAM ) ;
163+ }
164+ }
165+ Ok ( Some ( Message :: Item ( req) ) )
166+ } else {
167+ Ok ( None )
141168 }
142- Ok ( Some ( Message :: Item ( req) ) )
143- } else {
144- Ok ( None )
145169 }
146170 }
147171}
0 commit comments