httpclient: treat "refresh" as redirect (#15646)

This commit is contained in:
Bogdan
2024-10-14 16:13:46 +03:00
committed by GitHub
parent ad26f91ecd
commit 962c9d1c95
5 changed files with 55 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using com.LandonKey.SocksWebProxy;
@@ -17,6 +18,8 @@ namespace Jackett.Common.Utils.Clients
{
public abstract class WebClient : IObserver<ServerConfig>
{
private static readonly Regex _RefreshHeaderRegex = new Regex("^(.*?url)=(.*?)(?:;|$)", RegexOptions.Compiled);
protected IDisposable ServerConfigUnsubscriber;
protected Logger logger;
protected IConfigurationService configService;
@@ -263,5 +266,31 @@ namespace Jackett.Common.Utils.Clients
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
return content;
}
protected static Uri RedirectUri(HttpResponseMessage response)
{
var newUri = response.Headers.Location;
if (newUri == null && response.Headers.TryGetValues("Refresh", out var refreshHeaders))
{
var refreshHeader = refreshHeaders.FirstOrDefault();
if (refreshHeader == null)
{
return null;
}
var match = _RefreshHeaderRegex.Match(refreshHeader);
if (match.Success)
{
return new Uri(response.RequestMessage.RequestUri, match.Groups[2].Value);
}
return null;
}
return newUri;
}
}
}