Skip to content

Commit 916e5b5

Browse files
committed
use httpclient to download cdn file
1 parent cb9cf08 commit 916e5b5

7 files changed

Lines changed: 20 additions & 78 deletions

File tree

LibCurlImpersonate/CurlHttp.cs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -75,66 +75,6 @@ public static string Fetch(
7575
}
7676
}
7777

78-
public static void DownloadFile(
79-
string url,
80-
string path,
81-
bool overwrite = true,
82-
bool http3 = false,
83-
Action<string>? onHttpVersion = null,
84-
Action<double>? onProgress = null,
85-
CancellationToken ct = default
86-
)
87-
{
88-
IntPtr handle = LibCurl.curl_easy_init();
89-
if (handle == IntPtr.Zero)
90-
throw new InvalidOperationException("curl_easy_init failed");
91-
92-
using var fs = new FileStream(
93-
path,
94-
overwrite ? FileMode.Create : FileMode.CreateNew,
95-
FileAccess.Write
96-
);
97-
var writePin = GCHandle.Alloc(fs);
98-
var xferPin = InstallXferCallback(handle, ct, onProgress);
99-
try
100-
{
101-
LibCurl.curl_easy_impersonate(handle, Impersonation, 1);
102-
LibCurl.curl_easy_setopt_str(handle, LibCurl.CURLOPT_ACCEPT_ENCODING, "");
103-
LibCurl.curl_easy_setopt_str(handle, LibCurl.CURLOPT_URL, url);
104-
LibCurl.curl_easy_setopt_cb(handle, LibCurl.CURLOPT_WRITEFUNCTION, OnWriteFile);
105-
LibCurl.curl_easy_setopt_ptr(
106-
handle,
107-
LibCurl.CURLOPT_WRITEDATA,
108-
GCHandle.ToIntPtr(writePin)
109-
);
110-
LibCurl.curl_easy_setopt_long(handle, LibCurl.CURLOPT_FOLLOWLOCATION, 1L);
111-
LibCurl.curl_easy_setopt_str(handle, LibCurl.CURLOPT_CAINFO, "cacert.pem");
112-
if (http3)
113-
LibCurl.curl_easy_setopt_long(
114-
handle,
115-
LibCurl.CURLOPT_HTTP_VERSION,
116-
LibCurl.CURL_HTTP_VERSION_3
117-
);
118-
119-
int code = LibCurl.curl_easy_perform(handle);
120-
if (code == LibCurl.CURLE_ABORTED_BY_CALLBACK)
121-
ct.ThrowIfCancellationRequested();
122-
if (code != LibCurl.CURLE_OK)
123-
throw new InvalidOperationException(
124-
$"curl_easy_perform returned error code {code}"
125-
);
126-
127-
ReportHttpVersion(handle, onHttpVersion);
128-
}
129-
finally
130-
{
131-
writePin.Free();
132-
if (xferPin.IsAllocated)
133-
xferPin.Free();
134-
LibCurl.curl_easy_cleanup(handle);
135-
}
136-
}
137-
13878
public static Dictionary<string, string> GetHeaders(
13979
string url,
14080
bool http3 = false,

Stalker.Gamma/Extensions/ServiceCollectionExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static IServiceCollection RegisterCoreGammaServices(this IServiceCollecti
2222
{
2323
s.AddHttpClient()
2424
.AddHttpClient(
25-
"githubDlArchive",
25+
"dlAddon",
2626
client =>
2727
{
2828
client.DefaultRequestHeaders.Add("User-Agent", "stalker-gamma-clone/1.0");
@@ -34,7 +34,8 @@ public static IServiceCollection RegisterCoreGammaServices(this IServiceCollecti
3434
EnableMultipleHttp2Connections = true,
3535
AutomaticDecompression = DecompressionMethods.None,
3636
}
37-
);
37+
)
38+
.AddStandardResilienceHandler();
3839
s.AddSingleton<StalkerGammaSettings>().AddSingleton<GammaProgress, GammaProgress>();
3940
return s.AddScoped<IDownloadModOrganizerService, DownloadModOrganizerService>()
4041
.AddScoped<ArchiveService>()

Stalker.Gamma/ModOrganizer/DownloadModOrganizer/DownloadModOrganizerService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public async Task DownloadAsync(
114114
Directory.CreateDirectory(cachePath);
115115
Directory.CreateDirectory(extractPath);
116116

117-
var hc = hcf.CreateClient("githubDlArchive");
117+
var hc = hcf.CreateClient("dlAddon");
118118
var getReleaseByTagResponse = await hc.GetAsync(
119119
$"https://api.github.com/repos/ModOrganizer2/modorganizer/releases/tags/{version}",
120120
cancellationToken

Stalker.Gamma/Models/GithubRecord.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ArchiveService archiveService
2626
public string DownloadPath => Path.Join(_gammaDir, "downloads", ArchiveName);
2727
private string ExtractPath => Path.Join(_gammaDir, "mods", _outputDirName);
2828
private IList<string> Instructions { get; } = instructions;
29-
private readonly HttpClient _hc = hcf.CreateClient("githubDlArchive");
29+
private readonly HttpClient _hc = hcf.CreateClient("dlAddon");
3030
private readonly GammaProgress _gammaProgress = gammaProgress;
3131
private readonly string _gammaDir = gammaDir;
3232
private readonly string _outputDirName = outputDirName;

Stalker.Gamma/Services/CurlService.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using LibCurlImpersonate;
2+
using Stalker.Gamma.Utilities;
23

34
namespace Stalker.Gamma.Services;
45

5-
public class CurlService
6+
public class CurlService(IHttpClientFactory hcf)
67
{
78
public Task<Dictionary<string, string>> GetHeadersAsync(
89
string url,
@@ -13,25 +14,22 @@ public Task<Dictionary<string, string>> GetHeadersAsync(
1314
cancellationToken
1415
);
1516

16-
public Task DownloadFileAsync(
17+
public async Task DownloadFileAsync(
1718
string url,
1819
string pathToDownloads,
1920
string fileName,
2021
Action<double>? onProgress = null,
2122
CancellationToken cancellationToken = default
22-
) =>
23-
Task.Run(
24-
() =>
25-
CurlHttp.DownloadFile(
26-
url,
27-
Path.Join(pathToDownloads, fileName),
28-
overwrite: true,
29-
http3: true,
30-
onProgress: onProgress,
31-
ct: cancellationToken
32-
),
23+
)
24+
{
25+
await DownloadFileFast.DownloadAsync(
26+
_dlAddonHc,
27+
url,
28+
Path.Join(pathToDownloads, fileName),
29+
onProgress,
3330
cancellationToken
3431
);
32+
}
3533

3634
public Task<string> GetStringAsync(string url, CancellationToken cancellationToken = default) =>
3735
Task.Run(() => CurlHttp.Fetch(url, http3: true, ct: cancellationToken), cancellationToken);
@@ -40,6 +38,8 @@ public Task<string> GetStringAsync(string url, CancellationToken cancellationTok
4038
/// Whether curl service found curl-impersonate-win.exe and can execute.
4139
/// </summary>
4240
public bool Ready => true;
41+
42+
private readonly HttpClient _dlAddonHc = hcf.CreateClient("dlAddon");
4343
}
4444

4545
public class ModDbBotDetectedException(string msg) : Exception(msg);

Stalker.Gamma/Stalker.Gamma.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="LibGit2Sharp" Version="0.31.0" />
1111
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.8" />
1212
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.8" />
13+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.6.0" />
1314
<PackageReference Include="Microsoft.Extensions.Resilience" Version="10.6.0" />
1415
</ItemGroup>
1516
<ItemGroup>

stalker-gamma-cli/Services/GetRemoteGitRepoCommit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ await _httpClient.GetFromJsonAsync(
2222

2323
private const string GitHubApiUrl = "https://api.github.com/repos/{0}/{1}/commits?per_page=1";
2424

25-
private readonly HttpClient _httpClient = hcf.CreateClient("githubDlArchive");
25+
private readonly HttpClient _httpClient = hcf.CreateClient("dlAddon");
2626
}
2727

2828
[JsonSerializable(typeof(List<RootObject>))]

0 commit comments

Comments
 (0)