Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions ILSpy/Updates/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
Expand All @@ -27,7 +28,7 @@ namespace ICSharpCode.ILSpy.Updates
{
internal static class UpdateService
{
static readonly Uri UpdateUrl = new Uri("https://ilspy.net/updates.xml");
static readonly Uri UpdateUrl = new Uri("https://icsharpcode.github.io/ILSpy/updates.xml");
const string band = "stable";

public static AvailableVersionInfo LatestAvailableVersion { get; private set; }
Expand All @@ -36,12 +37,12 @@ public static async Task<AvailableVersionInfo> GetLatestVersionAsync()
{
var client = new HttpClient(new HttpClientHandler() {
UseProxy = true,
UseDefaultCredentials = true,
UseDefaultCredentials = true
});
string data = await client.GetStringAsync(UpdateUrl).ConfigureAwait(false);
string data = await GetWithRedirectsAsync(client, UpdateUrl).ConfigureAwait(false);

XDocument doc = XDocument.Load(new StringReader(data));
var bands = doc.Root.Elements("band");
var bands = doc.Root.Elements("band").ToList();
var currentBand = bands.FirstOrDefault(b => (string)b.Attribute("id") == band) ?? bands.First();
Version version = new Version((string)currentBand.Element("latestVersion"));
string url = (string)currentBand.Element("downloadUrl");
Expand All @@ -52,6 +53,29 @@ public static async Task<AvailableVersionInfo> GetLatestVersionAsync()
return LatestAvailableVersion;
}

static async Task<string> GetWithRedirectsAsync(HttpClient client, Uri uri, int maxRedirects = 5)
{
for (int i = 0; i <= maxRedirects; i++)
{
using var response = await client.GetAsync(uri).ConfigureAwait(false);

if (response.StatusCode is HttpStatusCode.MovedPermanently
or HttpStatusCode.Found
or HttpStatusCode.TemporaryRedirect
or HttpStatusCode.PermanentRedirect)
{
var location = response.Headers.Location;
uri = location?.IsAbsoluteUri == true ? location : new Uri(uri, location);
continue;
}

response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}

throw new HttpRequestException($"Exceeded maximum redirect limit ({maxRedirects}).");
}

/// <summary>
/// If automatic update checking is enabled, checks if there are any updates available.
/// Returns the download URL if an update is available.
Expand Down
Loading