2021-09-27 12:42:46 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2022-02-05 15:47:15 +00:00
|
|
|
using System.IO;
|
2021-09-27 12:42:46 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
2022-02-05 15:47:15 +00:00
|
|
|
using System.Net.Http.Headers;
|
2021-09-27 12:42:46 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Wabbajack.Common;
|
|
|
|
using Wabbajack.Downloaders.Interfaces;
|
|
|
|
using Wabbajack.DTOs;
|
|
|
|
using Wabbajack.DTOs.DownloadStates;
|
|
|
|
using Wabbajack.DTOs.Validation;
|
|
|
|
using Wabbajack.Hashing.xxHash64;
|
2022-02-05 15:47:15 +00:00
|
|
|
using Wabbajack.Networking.Http;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Wabbajack.Networking.Http.Interfaces;
|
|
|
|
using Wabbajack.Paths;
|
|
|
|
using Wabbajack.Paths.IO;
|
|
|
|
using Wabbajack.RateLimiter;
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.Downloaders.Http;
|
|
|
|
|
2022-02-05 15:47:15 +00:00
|
|
|
public class HttpDownloader : ADownloader<DTOs.DownloadStates.Http>, IUrlDownloader, IUpgradingDownloader, IChunkedSeekableStreamDownloader
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
private readonly HttpClient _client;
|
|
|
|
private readonly IHttpDownloader _downloader;
|
|
|
|
private readonly ILogger<HttpDownloader> _logger;
|
|
|
|
|
|
|
|
public HttpDownloader(ILogger<HttpDownloader> logger, HttpClient client, IHttpDownloader downloader)
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
_client = client;
|
|
|
|
_logger = logger;
|
|
|
|
_downloader = downloader;
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public async Task<Archive?> TryGetUpgrade(Archive archive, IJob job, TemporaryFileManager temporaryFileManager,
|
|
|
|
CancellationToken token)
|
|
|
|
{
|
|
|
|
var state = (DTOs.DownloadStates.Http) archive.State;
|
|
|
|
await using var file = temporaryFileManager.CreateFile();
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
var newHash = await Download(archive, file.Path, job, token);
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
return new Archive
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
Hash = newHash,
|
|
|
|
Size = file.Path.Size(),
|
|
|
|
State = archive.State,
|
|
|
|
Name = archive.Name
|
|
|
|
};
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public override IDownloadState? Resolve(IReadOnlyDictionary<string, string> iniData)
|
|
|
|
{
|
2022-08-21 20:23:11 +00:00
|
|
|
if (iniData.ContainsKey("directURL") && Uri.TryCreate(iniData["directURL"].CleanIniString(), UriKind.Absolute, out var uri))
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
var state = new DTOs.DownloadStates.Http
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
Url = uri
|
|
|
|
};
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
if (iniData.TryGetValue("directURLHeaders", out var headers)) state.Headers = headers.Split("|").ToArray();
|
|
|
|
|
|
|
|
return state;
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2022-10-07 22:14:01 +00:00
|
|
|
public override Task<bool> Prepare()
|
2021-10-23 16:51:17 +00:00
|
|
|
{
|
2022-10-07 22:14:01 +00:00
|
|
|
return Task.FromResult(true);
|
2021-10-23 16:51:17 +00:00
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public override bool IsAllowed(ServerAllowList allowList, IDownloadState state)
|
|
|
|
{
|
|
|
|
return allowList.AllowedPrefixes.Any(p => ((DTOs.DownloadStates.Http) state).Url.ToString().StartsWith(p));
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public IDownloadState? Parse(Uri uri)
|
|
|
|
{
|
|
|
|
return new DTOs.DownloadStates.Http {Url = uri};
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public Uri UnParse(IDownloadState state)
|
|
|
|
{
|
|
|
|
return ((DTOs.DownloadStates.Http) state).Url;
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public override Priority Priority => Priority.Low;
|
|
|
|
|
|
|
|
public override async Task<Hash> Download(Archive archive, DTOs.DownloadStates.Http state,
|
|
|
|
AbsolutePath destination, IJob job, CancellationToken token)
|
|
|
|
{
|
|
|
|
return await _downloader.Download(MakeMessage(state), destination, job, token);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<HttpResponseMessage> GetResponse(DTOs.DownloadStates.Http state, CancellationToken token)
|
|
|
|
{
|
|
|
|
var msg = MakeMessage(state);
|
|
|
|
|
2022-05-28 22:53:52 +00:00
|
|
|
return await _client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead, token);
|
2021-10-23 16:51:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 15:47:15 +00:00
|
|
|
internal static HttpRequestMessage MakeMessage(DTOs.DownloadStates.Http state)
|
2021-10-23 16:51:17 +00:00
|
|
|
{
|
|
|
|
var msg = new HttpRequestMessage(HttpMethod.Get, state.Url);
|
|
|
|
foreach (var header in state.Headers)
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
var idx = header.IndexOf(':');
|
|
|
|
var k = header[..idx];
|
|
|
|
var v = header[(idx + 1)..];
|
|
|
|
msg.Headers.Add(k, v);
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
msg.AddChromeAgent();
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task<bool> Verify(Archive archive, DTOs.DownloadStates.Http archiveState,
|
|
|
|
IJob job, CancellationToken token)
|
|
|
|
{
|
|
|
|
var response = await GetResponse(archiveState, token);
|
|
|
|
if (!response.IsSuccessStatusCode) return false;
|
|
|
|
|
|
|
|
var headerVar = archive.Size == 0 ? "1" : archive.Size.ToString();
|
|
|
|
ulong headerContentSize = 0;
|
|
|
|
if (response.Content.Headers.Contains("Content-Length"))
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
headerVar = response.Content.Headers.GetValues("Content-Length").FirstOrDefault();
|
|
|
|
if (headerVar != null)
|
|
|
|
if (!ulong.TryParse(headerVar, out headerContentSize))
|
|
|
|
return true;
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
job.Size = 1024;
|
|
|
|
await job.Report(1024, token);
|
|
|
|
|
|
|
|
response.Dispose();
|
|
|
|
if (archive.Size != 0 && headerContentSize != 0)
|
|
|
|
return archive.Size == (long) headerContentSize;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override IEnumerable<string> MetaIni(Archive a, DTOs.DownloadStates.Http state)
|
|
|
|
{
|
|
|
|
if (state.Headers.Length > 0)
|
|
|
|
return new[]
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
$"directURL={state.Url}",
|
|
|
|
$"directURLHeaders={string.Join("|", state.Headers)}"
|
2021-09-27 12:42:46 +00:00
|
|
|
};
|
2021-10-23 16:51:17 +00:00
|
|
|
return new[] {$"directURL={state.Url}"};
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
2022-02-05 15:47:15 +00:00
|
|
|
|
2022-10-07 22:14:01 +00:00
|
|
|
public ValueTask<Stream> GetChunkedSeekableStream(Archive archive, CancellationToken token)
|
2022-02-05 15:47:15 +00:00
|
|
|
{
|
|
|
|
var state = archive.State as DTOs.DownloadStates.Http;
|
2022-10-07 22:14:01 +00:00
|
|
|
return ValueTask.FromResult<Stream>(new ChunkedSeekableDownloader(this, archive, state!));
|
2022-02-05 15:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class ChunkedSeekableDownloader : AChunkedBufferingStream
|
|
|
|
{
|
|
|
|
private readonly DTOs.DownloadStates.Http _state;
|
|
|
|
private readonly Archive _archive;
|
|
|
|
private readonly HttpDownloader _downloader;
|
|
|
|
|
|
|
|
public ChunkedSeekableDownloader(HttpDownloader downloader, Archive archive, DTOs.DownloadStates.Http state) : base(21, archive.Size, 8)
|
|
|
|
{
|
|
|
|
_downloader = downloader;
|
|
|
|
_archive = archive;
|
|
|
|
_state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task<byte[]> LoadChunk(long offset, int size)
|
|
|
|
{
|
2022-04-22 12:29:38 +00:00
|
|
|
return await CircuitBreaker.WithAutoRetryAllAsync(_downloader._logger, async () =>
|
|
|
|
{
|
|
|
|
var msg = HttpDownloader.MakeMessage(_state);
|
|
|
|
msg.Headers.Range = new RangeHeaderValue(offset, offset + size);
|
|
|
|
using var response = await _downloader._client.SendAsync(msg);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
throw new HttpException(response);
|
|
|
|
|
|
|
|
return await response.Content.ReadAsByteArrayAsync();
|
|
|
|
});
|
2022-02-05 15:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|