Skip to content

Commit

Permalink
GetResponseCode wrongly downloading the entire file + headers support
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefNemec committed May 3, 2023
1 parent ee15d63 commit 39e7ff0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,8 @@ public string SelectGoogleImage(string searchTerm, string tempFileName, double i
return null;
}

var response = HttpDownloader.GetResponseCode(url);
if (response != HttpStatusCode.OK)
var response = HttpDownloader.GetResponseCode(url, out var _);
if (!response.IsSuccess())
{
logger.Warn("Original Google image request failed: " + response.ToString());
url = model.SelectedImage.ThumbUrl;
Expand Down
16 changes: 14 additions & 2 deletions source/Playnite/Common/Web/HttpDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,23 @@ public static void DownloadFile(string url, string path, CancellationToken cance
downloader.DownloadFile(url, path, cancelToken);
}

public static HttpStatusCode GetResponseCode(string url)
public static HttpStatusCode GetResponseCode(string url, out Dictionary<string, string> headers)
{
headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

try
{
var response = httpClient.GetAsync(url).GetAwaiter().GetResult();
var response = httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, url)).GetAwaiter().GetResult();
foreach (var header in response.Headers)
{
headers.Add(header.Key, string.Join(",", header.Value));
}

foreach (var header in response.Content.Headers)
{
headers.Add(header.Key, string.Join(",", header.Value));
}

return response.StatusCode;
}
catch (Exception e)
Expand Down
8 changes: 6 additions & 2 deletions source/Tests/Playnite.Tests/Web/HttpDownloaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ public class HttpDownloaderTests
[Test]
public void GetResponseCodeTest()
{
Assert.AreEqual(HttpStatusCode.OK, HttpDownloader.GetResponseCode(@"https://playnite.link/favicon.ico"));
Assert.AreEqual(HttpStatusCode.NotFound, HttpDownloader.GetResponseCode(@"https://playnite.link/test.tst"));
var resp = HttpDownloader.GetResponseCode(@"https://playnite.link/favicon.ico", out var headers);
Assert.AreEqual(HttpStatusCode.OK, resp);
Assert.AreEqual("15086", headers["Content-Length"]);

resp = HttpDownloader.GetResponseCode(@"https://playnite.link/test.tst", out headers);
Assert.AreEqual(HttpStatusCode.NotFound, resp);
}
}
}
2 changes: 1 addition & 1 deletion source/Tools/Playnite.Toolbox/Verify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static bool CheckPropertyURL(string url, string propertyName, bool manda
return false;
}

if (!HttpDownloader.GetResponseCode(url).IsSuccess())
if (!HttpDownloader.GetResponseCode(url, out var _).IsSuccess())
{
logger.Error($"{propertyName} doesn't point to reachable HTTP location.\n{url}");
passRes = false;
Expand Down

0 comments on commit 39e7ff0

Please sign in to comment.