@@ -222,33 +222,31 @@ public async Task ConnectAsync(ConnectionSettings cs, IOBehavior ioBehavior, Can
222222 ServerVersion = new ServerVersion ( Encoding . ASCII . GetString ( initialHandshake . ServerVersion ) ) ;
223223 ConnectionId = initialHandshake . ConnectionId ;
224224 AuthPluginData = initialHandshake . AuthPluginData ;
225- if ( cs . UseCompression && ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . Compress ) == 0 )
226- cs = cs . WithUseCompression ( false ) ;
225+ m_useCompression = cs . UseCompression && ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . Compress ) != 0 ;
227226
228227 var serverSupportsSsl = ( initialHandshake . ProtocolCapabilities & ProtocolCapabilities . Ssl ) != 0 ;
229228 if ( cs . SslMode != MySqlSslMode . None && ( cs . SslMode != MySqlSslMode . Preferred || serverSupportsSsl ) )
230229 {
231230 if ( ! serverSupportsSsl )
232231 throw new MySqlException ( "Server does not support SSL" ) ;
233232 await InitSslAsync ( initialHandshake . ProtocolCapabilities , cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
234- cs = cs . WithSecureConnection ( true ) ;
235233 }
236234
237- var response = HandshakeResponse41Packet . Create ( initialHandshake , cs ) ;
235+ var response = HandshakeResponse41Packet . Create ( initialHandshake , cs , m_useCompression ) ;
238236 payload = new PayloadData ( new ArraySegment < byte > ( response ) ) ;
239237 await SendReplyAsync ( payload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
240238 payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
241239
242240 // if server doesn't support the authentication fast path, it will send a new challenge
243241 if ( payload . HeaderByte == AuthenticationMethodSwitchRequestPayload . Signature )
244242 {
245- await SwitchAuthenticationAsync ( payload , cs , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
243+ await SwitchAuthenticationAsync ( payload , cs . Password , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
246244 payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
247245 }
248246
249247 OkPayload . Create ( payload ) ;
250248
251- if ( cs . UseCompression )
249+ if ( m_useCompression )
252250 m_payloadHandler = new CompressedPayloadHandler ( m_payloadHandler . ByteHandler ) ;
253251 }
254252
@@ -276,30 +274,30 @@ public async Task ResetConnectionAsync(ConnectionSettings cs, IOBehavior ioBehav
276274 payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
277275 if ( payload . HeaderByte == AuthenticationMethodSwitchRequestPayload . Signature )
278276 {
279- await SwitchAuthenticationAsync ( payload , cs , ioBehavior , cancellationToken ) ;
277+ await SwitchAuthenticationAsync ( payload , cs . Password , ioBehavior , cancellationToken ) ;
280278 payload = await ReceiveReplyAsync ( ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
281279 }
282280 OkPayload . Create ( payload ) ;
283281 }
284282 }
285283
286- private async Task SwitchAuthenticationAsync ( PayloadData payload , ConnectionSettings cs , IOBehavior ioBehavior , CancellationToken cancellationToken )
284+ private async Task SwitchAuthenticationAsync ( PayloadData payload , string password , IOBehavior ioBehavior , CancellationToken cancellationToken )
287285 {
288286 // if the server didn't support the hashed password; rehash with the new challenge
289287 var switchRequest = AuthenticationMethodSwitchRequestPayload . Create ( payload ) ;
290288 switch ( switchRequest . Name )
291289 {
292290 case "mysql_native_password" :
293291 AuthPluginData = switchRequest . Data ;
294- var hashedPassword = AuthenticationUtility . CreateAuthenticationResponse ( AuthPluginData , 0 , cs . Password ) ;
292+ var hashedPassword = AuthenticationUtility . CreateAuthenticationResponse ( AuthPluginData , 0 , password ) ;
295293 payload = new PayloadData ( new ArraySegment < byte > ( hashedPassword ) ) ;
296294 await SendReplyAsync ( payload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
297295 break ;
298296
299297 case "mysql_clear_password" :
300- if ( ! cs . IsSecureConnection )
298+ if ( ! m_isSecureConnection )
301299 throw new MySqlException ( "Authentication method '{0}' requires a secure connection." . FormatInvariant ( switchRequest . Name ) ) ;
302- payload = new PayloadData ( new ArraySegment < byte > ( Encoding . UTF8 . GetBytes ( cs . Password ) ) ) ;
300+ payload = new PayloadData ( new ArraySegment < byte > ( Encoding . UTF8 . GetBytes ( password ) ) ) ;
303301 await SendReplyAsync ( payload , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
304302 break ;
305303
@@ -577,7 +575,7 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
577575
578576 var checkCertificateRevocation = cs . SslMode == MySqlSslMode . VerifyFull ;
579577
580- var initSsl = new PayloadData ( new ArraySegment < byte > ( HandshakeResponse41Packet . InitSsl ( serverCapabilities , cs ) ) ) ;
578+ var initSsl = new PayloadData ( new ArraySegment < byte > ( HandshakeResponse41Packet . InitSsl ( serverCapabilities , cs , m_useCompression ) ) ) ;
581579 await SendReplyAsync ( initSsl , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
582580
583581 try
@@ -596,6 +594,7 @@ private async Task InitSslAsync(ProtocolCapabilities serverCapabilities, Connect
596594 }
597595 var sslByteHandler = new StreamByteHandler ( sslStream ) ;
598596 m_payloadHandler . ByteHandler = sslByteHandler ;
597+ m_isSecureConnection = true ;
599598 }
600599 catch ( Exception ex )
601600 {
@@ -730,5 +729,7 @@ private enum State
730729 IPayloadHandler m_payloadHandler ;
731730 MySqlCommand m_activeCommand ;
732731 MySqlDataReader m_activeReader ;
732+ bool m_useCompression ;
733+ bool m_isSecureConnection ;
733734 }
734735}
0 commit comments