Skip to content

Commit

Permalink
Refactor Pop3Client Connect/ConnectAsync methods a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Aug 19, 2023
1 parent c76af8c commit b535335
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
27 changes: 14 additions & 13 deletions MailKit/Net/Pop3/AsyncPop3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ public override async Task AuthenticateAsync (Encoding encoding, ICredentials cr
await OnAuthenticatedAsync (message, cancellationToken).ConfigureAwait (false);
}

async Task SslHandshakeAsync (SslStream ssl, string host, CancellationToken cancellationToken)
{
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
await ssl.AuthenticateAsClientAsync (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate), cancellationToken).ConfigureAwait (false);
#else
await ssl.AuthenticateAsClientAsync (host, ClientCertificates, SslProtocols, CheckCertificateRevocation).ConfigureAwait (false);
#endif
}

/// <summary>
/// Asynchronously establish a connection to the specified POP3 or POP3/S server.
/// </summary>
Expand Down Expand Up @@ -349,7 +358,7 @@ public override async Task AuthenticateAsync (Encoding encoding, ICredentials cr
/// </exception>
public override async Task ConnectAsync (string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default)
{
ValidateArguments (host, port);
CheckCanConnect (host, port);

ComputeDefaultValues (host, ref port, ref options, out var uri, out var starttls);

Expand All @@ -363,11 +372,7 @@ public override async Task ConnectAsync (string host, int port = 0, SecureSocket
var ssl = new SslStream (stream, false, ValidateRemoteCertificate);

try {
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
await ssl.AuthenticateAsClientAsync (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate), cancellationToken).ConfigureAwait (false);
#else
await ssl.AuthenticateAsClientAsync (host, ClientCertificates, SslProtocols, CheckCertificateRevocation).ConfigureAwait (false);
#endif
await SslHandshakeAsync (ssl, host, cancellationToken).ConfigureAwait (false);
} catch (Exception ex) {
ssl.Dispose ();

Expand Down Expand Up @@ -407,11 +412,7 @@ public override async Task ConnectAsync (string host, int port = 0, SecureSocket
var tls = new SslStream (stream, false, ValidateRemoteCertificate);
engine.Stream.Stream = tls;

#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
await tls.AuthenticateAsClientAsync (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate), cancellationToken).ConfigureAwait (false);
#else
await tls.AuthenticateAsClientAsync (host, ClientCertificates, SslProtocols, CheckCertificateRevocation).ConfigureAwait (false);
#endif
await SslHandshakeAsync (tls, host, cancellationToken).ConfigureAwait (false);
} catch (Exception ex) {
throw SslHandshakeException.Create (ref sslValidationInfo, ex, true, "POP3", host, port, 995, 110);
}
Expand Down Expand Up @@ -498,7 +499,7 @@ public override async Task ConnectAsync (string host, int port = 0, SecureSocket
/// </exception>
public override Task ConnectAsync (Socket socket, string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default)
{
ValidateArguments (socket, host, port);
CheckCanConnect (socket, host, port);

return ConnectAsync (new NetworkStream (socket, true), host, port, options, cancellationToken);
}
Expand Down Expand Up @@ -568,7 +569,7 @@ public override Task ConnectAsync (Socket socket, string host, int port = 0, Sec
/// </exception>
public override async Task ConnectAsync (Stream stream, string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default)
{
ValidateArguments (stream, host, port);
CheckCanConnect (stream, host, port);

Stream network;

Expand Down
49 changes: 21 additions & 28 deletions MailKit/Net/Pop3/Pop3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ internal static void ComputeDefaultValues (string host, ref int port, ref Secure
}
}

void ValidateArguments (string host, int port)
void CheckCanConnect (string host, int port)
{
if (host == null)
throw new ArgumentNullException (nameof (host));
Expand All @@ -1095,6 +1095,15 @@ void ValidateArguments (string host, int port)
throw new InvalidOperationException ("The Pop3Client is already connected.");
}

void SslHandshake (SslStream ssl, string host, CancellationToken cancellationToken)
{
#if NET5_0_OR_GREATER
ssl.AuthenticateAsClient (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate));
#else
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);
#endif
}

/// <summary>
/// Establish a connection to the specified POP3 or POP3/S server.
/// </summary>
Expand Down Expand Up @@ -1161,7 +1170,7 @@ void ValidateArguments (string host, int port)
/// </exception>
public override void Connect (string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default)
{
ValidateArguments (host, port);
CheckCanConnect (host, port);

ComputeDefaultValues (host, ref port, ref options, out var uri, out var starttls);

Expand All @@ -1175,11 +1184,7 @@ public override void Connect (string host, int port = 0, SecureSocketOptions opt
var ssl = new SslStream (stream, false, ValidateRemoteCertificate);

try {
#if NET5_0_OR_GREATER
ssl.AuthenticateAsClient (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate));
#else
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);
#endif
SslHandshake (ssl, host, cancellationToken);
} catch (Exception ex) {
ssl.Dispose ();

Expand Down Expand Up @@ -1219,11 +1224,7 @@ public override void Connect (string host, int port = 0, SecureSocketOptions opt
var tls = new SslStream (stream, false, ValidateRemoteCertificate);
engine.Stream.Stream = tls;

#if NET5_0_OR_GREATER
tls.AuthenticateAsClient (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate));
#else
tls.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);
#endif
SslHandshake (tls, host, cancellationToken);
} catch (Exception ex) {
throw SslHandshakeException.Create (ref sslValidationInfo, ex, true, "POP3", host, port, 995, 110);
}
Expand All @@ -1243,23 +1244,23 @@ public override void Connect (string host, int port = 0, SecureSocketOptions opt
OnConnected (host, port, options);
}

void ValidateArguments (Stream stream, string host, int port)
void CheckCanConnect (Stream stream, string host, int port)
{
if (stream == null)
throw new ArgumentNullException (nameof (stream));

ValidateArguments (host, port);
CheckCanConnect (host, port);
}

void ValidateArguments (Socket socket, string host, int port)
void CheckCanConnect (Socket socket, string host, int port)
{
if (socket == null)
throw new ArgumentNullException (nameof (socket));

if (!socket.Connected)
throw new ArgumentException ("The socket is not connected.", nameof (socket));

ValidateArguments (host, port);
CheckCanConnect (host, port);
}

/// <summary>
Expand Down Expand Up @@ -1328,7 +1329,7 @@ void ValidateArguments (Socket socket, string host, int port)
/// </exception>
public override void Connect (Socket socket, string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default)
{
ValidateArguments (socket, host, port);
CheckCanConnect (socket, host, port);

Connect (new NetworkStream (socket, true), host, port, options, cancellationToken);
}
Expand Down Expand Up @@ -1397,7 +1398,7 @@ public override void Connect (Socket socket, string host, int port = 0, SecureSo
/// </exception>
public override void Connect (Stream stream, string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default)
{
ValidateArguments (stream, host, port);
CheckCanConnect (stream, host, port);

Stream network;

Expand All @@ -1409,11 +1410,7 @@ public override void Connect (Stream stream, string host, int port = 0, SecureSo
var ssl = new SslStream (stream, false, ValidateRemoteCertificate);

try {
#if NET5_0_OR_GREATER
ssl.AuthenticateAsClient (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate));
#else
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);
#endif
SslHandshake (ssl, host, cancellationToken);
} catch (Exception ex) {
ssl.Dispose ();

Expand Down Expand Up @@ -1458,11 +1455,7 @@ public override void Connect (Stream stream, string host, int port = 0, SecureSo
engine.Stream.Stream = tls;

try {
#if NET5_0_OR_GREATER
tls.AuthenticateAsClient (GetSslClientAuthenticationOptions (host, ValidateRemoteCertificate));
#else
tls.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);
#endif
SslHandshake (tls, host, cancellationToken);
} catch (Exception ex) {
throw SslHandshakeException.Create (ref sslValidationInfo, ex, true, "POP3", host, port, 995, 110);
}
Expand Down

0 comments on commit b535335

Please sign in to comment.