Skip to content

Commit 256e174

Browse files
committed
fix: Properly handle relative uris + depth limit
1 parent 0c7f0fc commit 256e174

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

Tools/WebTools.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ public static class WebTools
77
/// </summary>
88
/// <param name="url">The shortened URL</param>
99
/// <returns>The URL the redirect leads to</returns>
10-
public static async Task<string> UnshortenUrl(string url, bool UseHeadMethod = true)
10+
public static async Task<string> UnshortenUrl(string url, bool UseHeadMethod = true, int depth = 0, int maxDepth = 30)
1111
{
12+
if (depth > maxDepth)
13+
{
14+
throw new DepthLimitReachedException();
15+
}
16+
1217
_logger?.LogDebug("Unshortening Url '{Url}', using head method: {UseHeadMethod}", url, UseHeadMethod);
1318

1419
HttpClient client = new(new HttpClientHandler()
1520
{
1621
AllowAutoRedirect = false,
1722
AutomaticDecompression = DecompressionMethods.GZip,
18-
1923
});
2024
client.Timeout = TimeSpan.FromSeconds(60);
21-
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36");
25+
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36");
2226
client.DefaultRequestHeaders.Add("upgrade-insecure-requests", "1");
2327
client.DefaultRequestHeaders.Add("accept-encoding", "gzip, deflate, br");
2428
client.DefaultRequestHeaders.Add("accept-language", "en-US,en;q=0.9");
@@ -36,7 +40,7 @@ public static async Task<string> UnshortenUrl(string url, bool UseHeadMethod = t
3640
catch (Exception)
3741
{
3842
if (UseHeadMethod)
39-
return await UnshortenUrl(url, false);
43+
return await UnshortenUrl(url, false, depth + 1, maxDepth);
4044

4145
throw;
4246
}
@@ -47,7 +51,7 @@ public static async Task<string> UnshortenUrl(string url, bool UseHeadMethod = t
4751
if (UseHeadMethod && request_task.IsFaulted && request_task.Exception.InnerException.GetType() == typeof(HttpRequestException))
4852
{
4953
_logger?.LogWarning("Unshortening Url '{Url}' failed, falling back to non-head method", url);
50-
return await UnshortenUrl(url, false);
54+
return await UnshortenUrl(url, false, depth + 1, maxDepth);
5155
}
5256

5357
var statuscode = request_task.Result.StatusCode;
@@ -56,7 +60,7 @@ public static async Task<string> UnshortenUrl(string url, bool UseHeadMethod = t
5660
if (UseHeadMethod && statuscode is HttpStatusCode.NotFound or HttpStatusCode.InternalServerError)
5761
{
5862
_logger?.LogWarning("Unshortening Url '{Url}' failed, falling back to non-head method", url);
59-
return await UnshortenUrl(url, false);
63+
return await UnshortenUrl(url, false, depth + 1, maxDepth);
6064
}
6165

6266
if (statuscode is HttpStatusCode.Found
@@ -68,11 +72,19 @@ or HttpStatusCode.PermanentRedirect
6872
or HttpStatusCode.TemporaryRedirect)
6973
{
7074
if (header is not null && header.Location is not null)
71-
return await UnshortenUrl(header.Location.AbsoluteUri);
75+
{
76+
77+
if (header.Location.IsAbsoluteUri)
78+
return await UnshortenUrl(header.Location.AbsoluteUri, UseHeadMethod, depth + 1, maxDepth);
79+
else
80+
return await UnshortenUrl(new Uri(requestMessage.RequestUri.GetLeftPart(UriPartial.Authority).ToString() + header.Location.ToString()).AbsoluteUri, UseHeadMethod, depth + 1, maxDepth);
81+
}
7282
else
7383
return url;
7484
}
7585
else
7686
return url;
7787
}
7888
}
89+
90+
public class DepthLimitReachedException : Exception { }

0 commit comments

Comments
 (0)