Skip to content

Commit

Permalink
Reinstate RedirectWebClient to fix redirects on Mono
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Nov 28, 2023
1 parent b2120ae commit 7996256
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
54 changes: 42 additions & 12 deletions Core/Net/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using System.Threading;

Expand Down Expand Up @@ -258,30 +257,61 @@ public static Uri ResolveRedirect(Uri url)
const int maxRedirects = 6;
for (int redirects = 0; redirects <= maxRedirects; ++redirects)
{
var req = new HttpRequestMessage(HttpMethod.Head, url);
req.Headers.UserAgent.Clear();
req.Headers.UserAgent.ParseAdd(UserAgentString);
var response = nonRedirectingHttpClient.SendAsync(
req, HttpCompletionOption.ResponseHeadersRead).Result;
if (response.Headers.Location == null)
#if NETFRAMEWORK

// HttpClient doesn't handle redirects well on Mono, but net7.0 considers WebClient obsolete

var rwClient = new RedirectWebClient();
using (rwClient.OpenRead(url)) { }
var location = rwClient.ResponseHeaders["Location"];
if (location == null)
{
return url;
}

var location = response.Headers.Location.ToString();
if (Uri.IsWellFormedUriString(location, UriKind.Absolute))
else if (Uri.IsWellFormedUriString(location, UriKind.Absolute))
{
url = response.Headers.Location;
url = new Uri(location);
}
else if (Uri.IsWellFormedUriString(location, UriKind.Relative))
{
url = new Uri(url, response.Headers.Location);
url = new Uri(url, location);
log.DebugFormat("Relative URL {0} is absolute URL {1}", location, url);
}
else
{
throw new Kraken(string.Format(Properties.Resources.NetInvalidLocation, location));
}

#else

var req = new HttpRequestMessage(HttpMethod.Head, url);
req.Headers.UserAgent.Clear();
req.Headers.UserAgent.ParseAdd(UserAgentString);
using (var response = nonRedirectingHttpClient.SendAsync(
req, HttpCompletionOption.ResponseHeadersRead).Result)
{
if (response.Headers.Location == null)
{
return url;
}

var location = response.Headers.Location.ToString();
if (Uri.IsWellFormedUriString(location, UriKind.Absolute))
{
url = response.Headers.Location;
}
else if (Uri.IsWellFormedUriString(location, UriKind.Relative))
{
url = new Uri(url, response.Headers.Location);
log.DebugFormat("Relative URL {0} is absolute URL {1}", location, url);
}
else
{
throw new Kraken(string.Format(Properties.Resources.NetInvalidLocation, location));
}
}

#endif
}
return null;
}
Expand Down
27 changes: 27 additions & 0 deletions Core/Net/RedirectWebClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if NETFRAMEWORK

using System;
using System.Net;

namespace CKAN
{
// HttpClient doesn't handle redirects well on Mono, but net7.0 considers WebClient obsolete
internal sealed class RedirectWebClient : WebClient
{
public RedirectWebClient()
{
Headers.Add("User-Agent", Net.UserAgentString);
}

protected override WebRequest GetWebRequest(Uri address)
{
var webRequest = (HttpWebRequest)base.GetWebRequest(address);
webRequest.AllowAutoRedirect = false;
webRequest.Method = "HEAD";

return webRequest;
}
}
}

#endif

0 comments on commit 7996256

Please sign in to comment.