@@ -10,6 +10,8 @@ use super::{
1010 encoder, Message , MessageType ,
1111} ;
1212use crate :: { body:: BodySize , error:: ParseError , ConnectionType , Request , Response , ServiceConfig } ;
13+ #[ cfg( feature = "proxy-protocol" ) ]
14+ use crate :: { http_message:: HttpMessage , proxy_protocol:: ProxyProtocol } ;
1315
1416bitflags ! {
1517 #[ derive( Debug , Clone , Copy ) ]
@@ -110,6 +112,7 @@ impl Decoder for Codec {
110112 type Error = ParseError ;
111113
112114 fn decode ( & mut self , src : & mut BytesMut ) -> Result < Option < Self :: Item > , Self :: Error > {
115+ #[ allow( clippy:: collapsible_else_if) ]
113116 if let Some ( ref mut payload) = self . payload {
114117 Ok ( match payload. decode ( src) ? {
115118 Some ( PayloadItem :: Chunk ( chunk) ) => Some ( Message :: Chunk ( Some ( chunk) ) ) ,
@@ -119,29 +122,49 @@ impl Decoder for Codec {
119122 }
120123 None => None ,
121124 } )
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- }
125+ } else {
126+ #[ cfg( feature = "proxy-protocol" ) ]
127+ let proxy_protocol = if self . config . proxy_protocol ( ) {
128+ let p = ProxyProtocol :: decode ( src) ?;
129+ if p. is_none ( ) {
130+ return Ok ( None ) ;
131+ }
132+ p
133+ } else {
134+ None
135+ } ;
136+
137+ if let Some ( ( req, payload) ) = self . decoder . decode ( src) ? {
138+ let head = req. head ( ) ;
139+ self . flags . set ( Flags :: HEAD , head. method == Method :: HEAD ) ;
140+ self . version = head. version ;
141+ self . conn_type = head. connection_type ( ) ;
142+
143+ if self . conn_type == ConnectionType :: KeepAlive
144+ && !self . flags . contains ( Flags :: KEEP_ALIVE_ENABLED )
145+ {
146+ self . conn_type = ConnectionType :: Close
147+ }
133148
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 ) ;
149+ #[ cfg( feature = "proxy-protocol" ) ]
150+ // set proxy protocol
151+ if let Some ( proxy_protocol) = proxy_protocol {
152+ let mut extensions = req. extensions_mut ( ) ;
153+ extensions. insert ( proxy_protocol) ;
140154 }
155+
156+ match payload {
157+ PayloadType :: None => self . payload = None ,
158+ PayloadType :: Payload ( pl) => self . payload = Some ( pl) ,
159+ PayloadType :: Stream ( pl) => {
160+ self . payload = Some ( pl) ;
161+ self . flags . insert ( Flags :: STREAM ) ;
162+ }
163+ }
164+ Ok ( Some ( Message :: Item ( req) ) )
165+ } else {
166+ Ok ( None )
141167 }
142- Ok ( Some ( Message :: Item ( req) ) )
143- } else {
144- Ok ( None )
145168 }
146169 }
147170}
0 commit comments