Skip to content

Commit

Permalink
Get rid of MailService.ConnectNetwork(..., bool doAsync, ...)
Browse files Browse the repository at this point in the history
This was no longer used except by the unit tests.
  • Loading branch information
jstedfast committed Dec 28, 2023
1 parent d70707d commit 355be2e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
9 changes: 0 additions & 9 deletions MailKit/MailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,15 +604,6 @@ internal async Task<Stream> ConnectNetworkAsync (string host, int port, Cancella
return new NetworkStream (socket, true);
}

internal Task<Stream> ConnectNetwork (string host, int port, bool doAsync, CancellationToken cancellationToken)
{
if (doAsync)
return ConnectNetworkAsync (host, port, cancellationToken);

var stream = ConnectNetwork (host, port, cancellationToken);
return Task.FromResult (stream);
}

/// <summary>
/// Establish a connection to the specified mail server.
/// </summary>
Expand Down
31 changes: 17 additions & 14 deletions UnitTests/Security/SslHandshakeExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,15 @@ bool ValidateRemoteCertificate (object sender, X509Certificate certificate, X509
return valid;
}

async Task ConnectAsync (string host, int port, SecureSocketOptions options, bool doAsync, CancellationToken cancellationToken)
public override void Connect (string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default (CancellationToken))
{
using (var stream = await ConnectNetwork (host, port, doAsync, cancellationToken).ConfigureAwait (false)) {
using (var stream = ConnectNetwork (host, port, cancellationToken)) {
hostName = host;

var ssl = new SslStream (stream, false, ValidateRemoteCertificate);

try {
if (doAsync) {
await ssl.AuthenticateAsClientAsync (host, ClientCertificates, SslProtocols, CheckCertificateRevocation).ConfigureAwait (false);
} else {
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);
}
ssl.AuthenticateAsClient (host, ClientCertificates, SslProtocols, CheckCertificateRevocation);
} catch (Exception ex) {
ssl.Dispose ();

Expand All @@ -261,11 +257,6 @@ async Task ConnectAsync (string host, int port, SecureSocketOptions options, boo
}
}

public override void Connect (string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default (CancellationToken))
{
ConnectAsync (host, port, options, false, cancellationToken).GetAwaiter ().GetResult ();
}

public override void Connect (Socket socket, string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default (CancellationToken))
{
throw new NotImplementedException ();
Expand All @@ -276,9 +267,21 @@ async Task ConnectAsync (string host, int port, SecureSocketOptions options, boo
throw new NotImplementedException ();
}

public override Task ConnectAsync (string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default (CancellationToken))
public override async Task ConnectAsync (string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default (CancellationToken))
{
return ConnectAsync (host, port, options, false, cancellationToken);
using (var stream = await ConnectNetworkAsync (host, port, cancellationToken).ConfigureAwait (false)) {
hostName = host;

var ssl = new SslStream (stream, false, ValidateRemoteCertificate);

try {
await ssl.AuthenticateAsClientAsync (host, ClientCertificates, SslProtocols, CheckCertificateRevocation).ConfigureAwait (false);
} catch (Exception ex) {
ssl.Dispose ();

throw SslHandshakeException.Create (ref sslValidationInfo, ex, false, "HTTP", host, port, 443, 80);
}
}
}

public override Task ConnectAsync (Socket socket, string host, int port = 0, SecureSocketOptions options = SecureSocketOptions.Auto, CancellationToken cancellationToken = default (CancellationToken))
Expand Down

0 comments on commit 355be2e

Please sign in to comment.