Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support For Headers #152

Merged
merged 11 commits into from
Feb 12, 2025
2 changes: 1 addition & 1 deletion .github/workflows/deploy-to-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
with:
dotnet-version: |
6.0.x
- uses: actions/cache@v2
- uses: actions/cache@v3
id: cacheStep
with:
path: ~/.nuget/packages
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
with:
dotnet-version: |
6.0.x
- uses: actions/cache@v2
- uses: actions/cache@v3
id: cacheStep
with:
path: ~/.nuget/packages
Expand Down
2 changes: 1 addition & 1 deletion BenchmarkOctaneProject/BenchmarkOctaneProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog" Version="4.2.0" />
Expand Down
4 changes: 2 additions & 2 deletions BenchmarkOctaneProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public void GlobalSetup()
[Benchmark]
public async Task BenchmarkOctane()
{
await _OctaneEngine.DownloadFile(Url, "output0.zip", pauseTokenSource, cancelTokenSource);
await _OctaneEngine.DownloadFile(new OctaneRequest(Url, "output0.zip"), pauseTokenSource, cancelTokenSource);
}

[Benchmark]
public async Task BenchmarkOctaneLowMemory()
{
await _OctaneEngine2.DownloadFile(Url, "output1.zip", pauseTokenSource, cancelTokenSource);
await _OctaneEngine2.DownloadFile(new OctaneRequest(Url, "output1.zip"), pauseTokenSource, cancelTokenSource);
}
[Benchmark]
public async Task BenchmarkHttpClient()
Expand Down
15 changes: 14 additions & 1 deletion OctaneEngine/Clients/DefaultClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Net.Http;
Expand Down Expand Up @@ -54,7 +55,19 @@
var basePart = new Uri(new Uri(url).GetLeftPart(UriPartial.Authority));
_httpClient.BaseAddress = basePart;
}


public void SetHeaders(Dictionary<string, string>? headers)

Check warning on line 59 in OctaneEngine/Clients/DefaultClient.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 59 in OctaneEngine/Clients/DefaultClient.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 59 in OctaneEngine/Clients/DefaultClient.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (headers is not null)
{
_httpClient.DefaultRequestHeaders.Clear();
foreach (var header in headers)
{
_httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}

public bool IsRangeSupported()
{
return false;
Expand Down
2 changes: 2 additions & 0 deletions OctaneEngine/Clients/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Net.Http;
using System.Threading;
Expand All @@ -36,6 +37,7 @@
{
public bool IsRangeSupported();
public void SetBaseAddress(string url);
public void SetHeaders(Dictionary<string, string>? headers);

Check warning on line 40 in OctaneEngine/Clients/IClient.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in OctaneEngine/Clients/IClient.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 40 in OctaneEngine/Clients/IClient.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public void SetMmf(MemoryMappedFile file);
public void SetProgressbar(ProgressBar bar);
public void SetArrayPool(ArrayPool<Byte> pool);
Expand Down
13 changes: 13 additions & 0 deletions OctaneEngine/Clients/OctaneClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.MemoryMappedFiles;
using System.Net.Http;
Expand Down Expand Up @@ -62,6 +63,18 @@
_client.BaseAddress = basePart;
}

public void SetHeaders(Dictionary<string, string>? headers)

Check warning on line 66 in OctaneEngine/Clients/OctaneClient.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 66 in OctaneEngine/Clients/OctaneClient.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 66 in OctaneEngine/Clients/OctaneClient.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (headers is not null)
{
_client.DefaultRequestHeaders.Clear();
foreach (var header in headers)
{
_client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}

public bool IsRangeSupported()
{
return true;
Expand Down
13 changes: 7 additions & 6 deletions OctaneEngine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void SetProxy(IWebProxy proxy)
/// <param name="outFile">The output file name of the download. Use 'null' to get file name from url.</param>
/// <param name="pauseTokenSource">The pause token source to use for pausing and resuming.</param>
/// <param name="cancelTokenSource">The cancellation token for canceling the task.</param>
public async Task DownloadFile(string url, string outFile = null, PauseTokenSource pauseTokenSource = null, CancellationTokenSource cancelTokenSource = null)
public async Task DownloadFile(OctaneRequest request, PauseTokenSource pauseTokenSource = null, CancellationTokenSource cancelTokenSource = null)
{
var stopwatch = new Stopwatch();
var logger = _factory.CreateLogger<Engine>();
Expand All @@ -145,10 +145,10 @@ public async Task DownloadFile(string url, string outFile = null, PauseTokenSour

try
{
var (_length, _range) = await getFileSizeAndRangeSupport(url);
var (_length, _range) = await getFileSizeAndRangeSupport(request.Url);

#region Varible Initilization
filename = outFile ?? Path.GetFileName(new Uri(url).LocalPath);
filename = request.OutFile ?? Path.GetFileName(new Uri(request.Url).LocalPath);
var cancellation_token = Helpers.CreateCancellationToken(cancelTokenSource, _config);
var pause_token = pauseTokenSource ?? new PauseTokenSource(_factory);
var memPool = ArrayPool<byte>.Create(_config.BufferSize, _config.Parts);
Expand All @@ -172,7 +172,8 @@ public async Task DownloadFile(string url, string outFile = null, PauseTokenSour
logger.LogInformation("PART SIZE: {partSize}", NetworkAnalyzer.PrettySize(partSize));

stopwatch.Start();
_client.SetBaseAddress(url);
_client.SetBaseAddress(request.Url);
_client.SetHeaders(request.Headers);

using (var mmf = MemoryMappedFile.CreateFromFile(filename, FileMode.OpenOrCreate, null, _length, MemoryMappedFileAccess.ReadWrite))
{
Expand Down Expand Up @@ -201,7 +202,7 @@ public async Task DownloadFile(string url, string outFile = null, PauseTokenSour
{
await Parallel.ForEachAsync(pieces, options, async (piece, token) =>
{
await _client.Download(url, piece, cancellation_token, pause_token.Token);
await _client.Download(request.Url, piece, cancellation_token, pause_token.Token);

Interlocked.Increment(ref tasksDone);

Expand Down Expand Up @@ -232,7 +233,7 @@ await Parallel.ForEachAsync(pieces, options, async (piece, token) =>
clientType = "Normal";
try
{
await _normalClient.Download(url, (0, 0), cancellation_token, pause_token.Token).ConfigureAwait(false);
await _normalClient.Download(request.Url, (0, 0), cancellation_token, pause_token.Token).ConfigureAwait(false);
success = true;
}
catch (Exception)
Expand Down
2 changes: 1 addition & 1 deletion OctaneEngine/IEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace OctaneEngineCore;

public interface IEngine
{
public Task DownloadFile(string url, string outFile = null, PauseTokenSource pauseTokenSource = null, CancellationTokenSource cancelTokenSource = null);
public Task DownloadFile(OctaneRequest request, PauseTokenSource pauseTokenSource = null, CancellationTokenSource cancelTokenSource = null);
public void SetProgressCallback(Action<double> callback);
public void SetDoneCallback(Action<bool> callback);
public void SetProxy(IWebProxy proxy);
Expand Down
2 changes: 1 addition & 1 deletion OctaneEngine/OctaneEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<PackageReference Include="AsyncEnumerator" Version="4.0.2" Condition="'$(TargetFramework)' == 'net461' OR '$(TargetFramework)' == 'net472'" />
<PackageReference Include="Autofac" Version="8.2.0" />
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="PooledAwait" Version="1.0.49" />
Expand Down
17 changes: 17 additions & 0 deletions OctaneEngine/OctaneRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;

namespace OctaneEngineCore;

public class OctaneRequest
{
public string Url { get; set; }
public string? OutFile { get; set; } = null;

Check warning on line 8 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 8 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public Dictionary<string, string>? Headers { get; set; } = null;

Check warning on line 9 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 9 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 9 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

public OctaneRequest(string url, string? outFile = null, Dictionary<string, string>? headers = null)

Check warning on line 11 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 11 in OctaneEngine/OctaneRequest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
Url = url;
OutFile = outFile;
Headers = headers;
}
}
3 changes: 2 additions & 1 deletion OctaneEngine/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dotnet add package OctaneEngineCore
* Proxy Support
* Pause/Resume Support
* JSON/Microsoft.Extensions.Configuration Support
* Headers

# Usage
```csharp
Expand Down Expand Up @@ -55,7 +56,7 @@ private static void Main()
var cancelTokenSource = new CancellationTokenSource();

var octaneEngine = new Engine(factory, config);
octaneEngine.DownloadFile(Url, null, pauseTokenSource, cancelTokenSource).Wait(cancelTokenSource.Token);
engine.DownloadFile(new OctaneRequest(Url, null), pauseTokenSource, cancelTokenSource).Wait();

```

Expand Down
2 changes: 1 addition & 1 deletion OctaneTestProject/DownloadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void DownloadFile()
Assert.That(File.Exists(_outFile), Is.True);
});
engine.SetProgressCallback(Console.WriteLine);
engine.DownloadFile(url, _outFile, _pauseTokenSource, _cancelTokenSource).Wait();
engine.DownloadFile(new OctaneRequest(url, _outFile), _pauseTokenSource, _cancelTokenSource).Wait();
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion OctaneTestProject/EqualityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void FileEqualityTest()
engine.SetProgressCallback(Console.WriteLine);
engine.SetProxy(null);

var t = engine.DownloadFile(url, outFile, _pauseTokenSource, _cancelTokenSource);
var t = engine.DownloadFile(new OctaneRequest(url, outFile), _pauseTokenSource, _cancelTokenSource);
t.Wait();
}
}
Expand Down
2 changes: 1 addition & 1 deletion OctaneTestProject/FailureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void TestFailure()
Assert.That(status, Is.False);
});
engine.SetProgressCallback(Console.WriteLine);
engine.DownloadFile(url, _outFile, _pauseTokenSource, _cancelTokenSource).Wait();
engine.DownloadFile(new OctaneRequest(url, _outFile), _pauseTokenSource, _cancelTokenSource).Wait();
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion OctaneTestProject/OctaneTestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
<PackageReference Include="coverlet.collector" Version="6.0.4">
Expand Down
2 changes: 1 addition & 1 deletion OctaneTestProject/PauseResumeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void PauseResumeFile()
engine.SetProxy(null);
Parallel.Invoke(
() => Action(_pauseTokenSource),
() => engine.DownloadFile(url, _outFile, _pauseTokenSource, _cancelTokenSource).Wait()
() => engine.DownloadFile(new OctaneRequest(url, _outFile), _pauseTokenSource, _cancelTokenSource).Wait()
);
}

Expand Down
2 changes: 1 addition & 1 deletion OctaneTester/Examples/Autofac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public void AutofacExample()
containerBuilder.AddOctane();
var engineContainer = containerBuilder.Build();
var engine = engineContainer.Resolve<IEngine>();
engine.DownloadFile("", "", pauseTokenSource, cancelTokenSource);
engine.DownloadFile(new OctaneRequest("", ""), pauseTokenSource, cancelTokenSource);
}
}
2 changes: 1 addition & 1 deletion OctaneTester/Examples/NoLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public void NoLoggerExample()
containerBuilder.AddOctane();
var engineContainer = containerBuilder.Build();
var engine = engineContainer.Resolve<IEngine>();
engine.DownloadFile("", "", pauseTokenSource, cancelTokenSource);
engine.DownloadFile(new OctaneRequest("", ""), pauseTokenSource, cancelTokenSource);
}
}
2 changes: 1 addition & 1 deletion OctaneTester/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static void Main()
containerBuilder.AddOctane();
var engineContainer = containerBuilder.Build();
var engine = engineContainer.Resolve<IEngine>();
engine.DownloadFile(Url, null, pauseTokenSource, cancelTokenSource).Wait();
engine.DownloadFile(new OctaneRequest(Url, null), pauseTokenSource, cancelTokenSource).Wait();
}
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dotnet add package OctaneEngineCore
* Proxy Support
* Pause/Resume Support
* JSON/Microsoft.Extensions.Configuration Support
* Headers

# Usage
```csharp
Expand Down Expand Up @@ -65,7 +66,7 @@ private static void Main(){
containerBuilder.AddOctane();
var engineContainer = containerBuilder.Build();
var engine = engineContainer.Resolve<IEngine>();
engine.DownloadFile(Url, null, pauseTokenSource, cancelTokenSource).Wait();
engine.DownloadFile(new OctaneRequest(Url, null), pauseTokenSource, cancelTokenSource).Wait();
}

```
Expand Down
Loading