Skip to content

Commit

Permalink
Merge #3960 Oops, HttpClient actually sucks
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Dec 18, 2023
2 parents 597dd43 + 354ff97 commit 892043a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 75 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## v1.34.3

### Bugfixes

- [Core] Oops, `HttpClient` actually sucks (#3960 by: HebaruSan)

### Internal

- [Netkan] Re-sort module properties after merging (#3957 by: HebaruSan)
Expand Down
2 changes: 1 addition & 1 deletion Cmdline/CKAN-cmdline.csproj
Expand Up @@ -24,7 +24,7 @@
<FileAlignment>512</FileAlignment>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>IDE1006</NoWarn>
<NoWarn>IDE1006,NU1701</NoWarn>
<CoreCompileDependsOn>PrepareResources;$(CompileDependsOn)</CoreCompileDependsOn>
</PropertyGroup>
<ItemGroup>
Expand Down
22 changes: 11 additions & 11 deletions Core/Games/KerbalSpaceProgram.cs
Expand Up @@ -266,34 +266,34 @@ public GameVersion DetectVersion(DirectoryInfo where)

public Uri MetadataBugtrackerURL => new Uri("https://github.com/KSP-CKAN/NetKAN/issues/new/choose");

private string Missions(GameInstance inst)
private static string Missions(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(inst.GameDir(), "Missions"));

private string Ships(GameInstance inst)
private static string Ships(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(inst.GameDir(), "Ships"));

private string ShipsVab(GameInstance inst)
private static string ShipsVab(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(Ships(inst), "VAB"));

private string ShipsSph(GameInstance inst)
private static string ShipsSph(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(Ships(inst), "SPH"));

private string ShipsThumbs(GameInstance inst)
private static string ShipsThumbs(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(Ships(inst), "@thumbs"));

private string ShipsThumbsSPH(GameInstance inst)
private static string ShipsThumbsSPH(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(ShipsThumbs(inst), "SPH"));

private string ShipsThumbsVAB(GameInstance inst)
private static string ShipsThumbsVAB(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(ShipsThumbs(inst), "VAB"));

private string ShipsScript(GameInstance inst)
private static string ShipsScript(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(Ships(inst), "Script"));

private string Tutorial(GameInstance inst)
private static string Tutorial(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(inst.GameDir(), "saves", "training"));

private string Scenarios(GameInstance inst)
private static string Scenarios(GameInstance inst)
=> CKANPathUtils.NormalizePath(Path.Combine(inst.GameDir(), "saves", "scenarios"));

private readonly Dictionary<string, string> allowedFolders = new Dictionary<string, string>
Expand Down Expand Up @@ -343,7 +343,7 @@ private static string KSPDirectory(string steamPath)
/// <returns>
/// args or args minus parameter
/// </returns>
private string[] filterCmdLineArgs(string[] args, GameVersion installedVersion, GameVersionRange crashyKspRange, string parameter)
private static string[] filterCmdLineArgs(string[] args, GameVersion installedVersion, GameVersionRange crashyKspRange, string parameter)
{
var installedRange = installedVersion.ToVersionRange();
if (crashyKspRange.IntersectWith(installedRange) != null
Expand Down
68 changes: 11 additions & 57 deletions Core/Net/Net.cs
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;

Expand Down Expand Up @@ -46,27 +45,17 @@ public static class Net
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
/// </returns>
public static string CurrentETag(Uri url)
=> httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, url),
HttpCompletionOption.ResponseHeadersRead)
.Result
.Headers.ETag.ToString()
.Replace("\"", "");

/// <summary>
/// "HttpClient is intended to be instantiated once and reused
/// throughout the life of an application. In .NET Core and .NET 5+,
/// HttpClient pools connections inside the handler instance and
/// reuses a connection across multiple requests. If you instantiate
/// an HttpClient class for every request, the number of sockets
/// available under heavy loads will be exhausted. This exhaustion
/// will result in SocketException errors."
/// </summary>
public static readonly HttpClient httpClient = new HttpClient();
public static readonly HttpClient nonRedirectingHttpClient = new HttpClient(
new HttpClientHandler()
{
AllowAutoRedirect = false,
});
{
// HttpClient apparently is worse than what it was supposed to replace
#pragma warning disable SYSLIB0014
WebRequest req = WebRequest.Create(url);
#pragma warning restore SYSLIB0014
req.Method = "HEAD";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
string val = resp.Headers["ETag"]?.Replace("\"", "");
resp.Close();
return val;
}

/// <summary>
/// Downloads the specified url, and stores it in the filename given.
Expand Down Expand Up @@ -257,10 +246,6 @@ public static Uri ResolveRedirect(Uri url)
const int maxRedirects = 6;
for (int redirects = 0; redirects <= maxRedirects; ++redirects)
{
#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"];
Expand All @@ -281,37 +266,6 @@ public static Uri ResolveRedirect(Uri url)
{
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
6 changes: 2 additions & 4 deletions Core/Net/RedirectWebClient.cs
@@ -1,8 +1,8 @@
#if NETFRAMEWORK

using System;
using System.Net;

#pragma warning disable SYSLIB0014

namespace CKAN
{
// HttpClient doesn't handle redirects well on Mono, but net7.0 considers WebClient obsolete
Expand All @@ -23,5 +23,3 @@ protected override WebRequest GetWebRequest(Uri address)
}
}
}

#endif
15 changes: 15 additions & 0 deletions GUI/Main/MainRepo.cs
Expand Up @@ -202,7 +202,22 @@ private void PostUpdateRepo(object sender, RunWorkerCompletedEventArgs e)
EnableMainWindow();
break;

case AggregateException exc:
foreach (var inner in exc.InnerExceptions
.SelectMany(inner =>
inner.TraverseNodes(ex => ex.InnerException)
.Reverse()))
{
log.Error(inner.Message, inner);
currentUser.RaiseMessage(inner.Message);
}
AddStatusMessage(Properties.Resources.MainRepoFailed);
Wait.Finish();
EnableMainWindow();
break;

case Exception exc:
log.Error(exc.Message, exc);
currentUser.RaiseMessage(exc.Message);
AddStatusMessage(Properties.Resources.MainRepoFailed);
Wait.Finish();
Expand Down
2 changes: 1 addition & 1 deletion Netkan/CKAN-netkan.csproj
Expand Up @@ -23,7 +23,7 @@
<FileAlignment>512</FileAlignment>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>IDE1006</NoWarn>
<NoWarn>IDE1006,NU1701</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="4.9.4" />
Expand Down
2 changes: 1 addition & 1 deletion Tests/Tests.csproj
Expand Up @@ -18,7 +18,7 @@
<Configurations>Debug;Release;NoGUI</Configurations>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>7.3</LangVersion>
<NoWarn>IDE1006</NoWarn>
<NoWarn>IDE1006,NU1701</NoWarn>
<TargetFrameworks>net48;net7.0;net7.0-windows</TargetFrameworks>
<BaseTargetFramework>$(TargetFramework.Replace("-windows", ""))</BaseTargetFramework>
<DefaultItemExcludes Condition=" '$(TargetFramework)' == 'net7.0' ">$(DefaultItemExcludes);AutoUpdate\**;GUI\**</DefaultItemExcludes>
Expand Down

0 comments on commit 892043a

Please sign in to comment.