Skip to content

Commit e16ba09

Browse files
committed
Updated with download factory
1 parent 8bc5d07 commit e16ba09

File tree

7 files changed

+65
-15
lines changed

7 files changed

+65
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Released on Thursday, May 2 2019.
55
- Reference latest AngleSharp
66
- Added `InputFile` class as a standard `IFile` implementation
77
- Added `AppendFile` extensions for `IHtmlInputElement`
8+
- Added ability for binary data restore on assets (#19)
89

910
# 0.10.1
1011

src/AngleSharp.Io.Tests/Network/CookieHandlingTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public async Task SettingCookieIsPreservedViaRedirect()
9595
}
9696

9797
[Test]
98-
public async Task SettingCookieIsNotPreservedViaRedirectToDifferentProtocol()
98+
public async Task SettingCookieIsPreservedViaRedirectToDifferentProtocol()
9999
{
100100
if (Helper.IsNetworkAvailable())
101101
{
@@ -107,7 +107,9 @@ public async Task SettingCookieIsNotPreservedViaRedirectToDifferentProtocol()
107107
var document = await context.OpenAsync(redirectUrl);
108108

109109
Assert.AreEqual(@"{
110-
""cookies"": {}
110+
""cookies"": {
111+
""test"": ""baz""
112+
}
111113
}
112114
".Replace(Environment.NewLine, "\n"), document.Body.TextContent);
113115
}

src/AngleSharp.Io.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<copyright>Copyright 2016-2019, AngleSharp</copyright>
1515
<tags>html html5 css css3 dom requester http https io filesystem storage httpclient cache</tags>
1616
<dependencies>
17-
<dependency id="AngleSharp" version="0.10.1" />
17+
<dependency id="AngleSharp" version="0.12.0" />
1818
</dependencies>
1919
</metadata>
2020
</package>

src/AngleSharp.Io/AngleSharp.Io.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="AngleSharp" Version="0.10.1" />
12+
<PackageReference Include="AngleSharp" Version="0.12.0" />
1313
</ItemGroup>
1414

1515
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">

src/AngleSharp.Io/GeneralExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace AngleSharp.Io
1+
namespace AngleSharp.Io
22
{
33
using System;
44
using System.Threading.Tasks;

src/AngleSharp.Io/IoConfigurationExtensions.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
1-
namespace AngleSharp
2-
{
1+
namespace AngleSharp
2+
{
3+
using AngleSharp.Dom;
4+
using AngleSharp.Io;
35
using AngleSharp.Io.Network;
4-
using System;
5-
using System.Net.Http;
6-
6+
using System;
7+
using System.Linq;
8+
using System.Net.Http;
9+
710
/// <summary>
811
/// Additional extensions for improved requesters.
912
/// </summary>
1013
public static class IoConfigurationExtensions
1114
{
15+
/// <summary>
16+
/// Adds capability to start a download when following some link to the
17+
/// configuration.
18+
/// </summary>
19+
/// <param name="configuration">The configuration to extend.</param>
20+
/// <param name="download">
21+
/// The callback to invoke when a download should be started. Returns true
22+
/// to signal an interest in downloading the response, otherwise false.
23+
/// </param>
24+
/// <returns>The configuration.</returns>
25+
public static IConfiguration WithDownload(this IConfiguration configuration, Func<MimeType, IResponse, Boolean> download)
26+
{
27+
var oldFactory = configuration.Services.OfType<IDocumentFactory>().FirstOrDefault();
28+
var newFactory = new DownloadFactory(oldFactory, download);
29+
return configuration.WithOnly<IDocumentFactory>(newFactory);
30+
}
31+
1232
/// <summary>
1333
/// Adds the requesters from the AngleSharp.Io package.
1434
/// </summary>
1535
/// <param name="configuration">The configuration to use.</param>
1636
/// <returns>The new configuration.</returns>
17-
public static IConfiguration WithRequesters(this IConfiguration configuration)
18-
{
19-
return configuration.WithRequesters(new HttpClientHandler { UseCookies = false, AllowAutoRedirect = false });
20-
}
37+
public static IConfiguration WithRequesters(this IConfiguration configuration) =>
38+
configuration.WithRequesters(new HttpClientHandler { UseCookies = false, AllowAutoRedirect = false });
2139

2240
/// <summary>
2341
/// Adds the requesters from the AngleSharp.Io package.
@@ -36,7 +54,7 @@ public static IConfiguration WithRequesters(this IConfiguration configuration, H
3654
new DataRequester(),
3755
new FtpRequester(),
3856
new FileRequester(),
39-
new AboutRequester()
57+
new AboutRequester(),
4058
});
4159
}
4260
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace AngleSharp.Io.Network
2+
{
3+
using AngleSharp.Dom;
4+
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
sealed class DownloadFactory : DefaultDocumentFactory
9+
{
10+
private readonly IDocumentFactory _documentFactory;
11+
private readonly Func<MimeType, IResponse, Boolean> _download;
12+
13+
public DownloadFactory(IDocumentFactory documentFactory, Func<MimeType, IResponse, Boolean> download)
14+
{
15+
_documentFactory = documentFactory;
16+
_download = download;
17+
}
18+
19+
protected override Task<IDocument> CreateDefaultAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken)
20+
{
21+
if (_download(options.ContentType, options.Response))
22+
{
23+
return context.OpenNewAsync(options.Response.Address.Href, cancellationToken);
24+
}
25+
26+
return _documentFactory.CreateAsync(context, options, cancellationToken);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)