Skip to content

Commit

Permalink
Turn DownloadAndGetAppCastData into an async method and use GetAwaite…
Browse files Browse the repository at this point in the history
…r().GetResult() in the original. Other things of note: libraries should always go for ConfigureAwait(false) and never use .Result unless you are sure the result is already there.
  • Loading branch information
christophwille authored and Deadpikle committed Jul 4, 2023
1 parent fc18a59 commit 7a6a8aa
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/NetSparkle/Downloaders/WebRequestAppCastDataDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,23 @@ public WebRequestAppCastDataDownloader()

/// <inheritdoc/>
public string DownloadAndGetAppCastData(string url)
{
return DownloadAndGetAppCastDataAsync(url).GetAwaiter().GetResult();
}

/// <inheritdoc/>
public async Task<string> DownloadAndGetAppCastDataAsync(string url)
{
_appcastUrl = url;
// configure ssl cert link
ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
// use HttpClient synchronously: https://stackoverflow.com/a/53529122/3938401

var handler = new HttpClientHandler();
if (RedirectHandler != null)
{
handler.AllowAutoRedirect = false;
}

if (TrustEverySSLConnection)
{
#if NETCORE
Expand All @@ -75,49 +82,46 @@ public string DownloadAndGetAppCastData(string url)
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new StringContent(ExtraJsonData, Encoding.UTF8, "application/json");
var postTask = Task.Run(() => httpClient.SendAsync(request));
postTask.Wait();
if (postTask.Result.IsSuccessStatusCode)
HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var postTaskStream = Task.Run(() => postTask.Result.Content.ReadAsStreamAsync());
postTask.Wait();
Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

ServicePointManager.ServerCertificateValidationCallback -= ValidateRemoteCertificate;
using (StreamReader reader = new StreamReader(postTaskStream.Result, GetAppCastEncoding()))
using (StreamReader reader = new StreamReader(responseStream, GetAppCastEncoding()))
{
return reader.ReadToEnd();
return await reader.ReadToEndAsync().ConfigureAwait(false);
}
}
}
else
{
if (RedirectHandler != null)
{
var task = Task.Run(() => httpClient.GetAsync(url));
task.Wait();
var response = task.Result;
HttpResponseMessage response = await httpClient.GetAsync(url).ConfigureAwait(false);

if ((int)response.StatusCode >= 300 && (int)response.StatusCode <= 399)
{
var redirectURI = response.Headers.Location;
if (RedirectHandler.Invoke(url, redirectURI.ToString(), response))
{
return DownloadAndGetAppCastData(redirectURI.ToString());
return await DownloadAndGetAppCastDataAsync(redirectURI.ToString()).ConfigureAwait(false);
}
}
else if (response.IsSuccessStatusCode)
{
var readTask = Task.Run(() => response.Content.ReadAsStringAsync());
readTask.Wait();
return readTask.Result;
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
else
else
{
var task = Task.Run(() => httpClient.GetStreamAsync(url));
var responseStream = task.Result;
Stream responseStream = await httpClient.GetStreamAsync(url).ConfigureAwait(false);

ServicePointManager.ServerCertificateValidationCallback -= ValidateRemoteCertificate;
using (StreamReader reader = new StreamReader(responseStream, GetAppCastEncoding()))
{
return reader.ReadToEnd();
return await reader.ReadToEndAsync().ConfigureAwait(false);
}
}
}
Expand Down

0 comments on commit 7a6a8aa

Please sign in to comment.