-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRemotePublicSuffixListProvider.cs
113 lines (75 loc) · 3.49 KB
/
RemotePublicSuffixListProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Gsemac.IO.Logging;
using Gsemac.Net.Http;
using Gsemac.Net.Http.Extensions;
using System;
using System.Net;
namespace Gsemac.Net {
public sealed class RemotePublicSuffixListProvider :
PublicSuffixListProviderBase {
// Public members
public RemotePublicSuffixListProvider() :
this(HttpWebRequestFactory.Default) {
}
public RemotePublicSuffixListProvider(ILogger logger) :
this(HttpWebRequestFactory.Default, logger) {
}
public RemotePublicSuffixListProvider(IHttpWebRequestFactory httpWebRequestFactory) :
this(DefaultPublicSuffixListUri, httpWebRequestFactory) {
}
public RemotePublicSuffixListProvider(IHttpWebRequestFactory httpWebRequestFactory, ILogger logger) :
this(DefaultPublicSuffixListUri, httpWebRequestFactory, logger) {
}
public RemotePublicSuffixListProvider(Uri uri, IHttpWebRequestFactory httpWebRequestFactory) :
this(uri, httpWebRequestFactory, Logger.Null) {
}
public RemotePublicSuffixListProvider(Uri uri, IHttpWebRequestFactory httpWebRequestFactory, ILogger logger) {
if (uri is null)
throw new ArgumentNullException(nameof(uri));
if (httpWebRequestFactory is null)
throw new ArgumentNullException(nameof(httpWebRequestFactory));
if (logger is null)
throw new ArgumentNullException(nameof(logger));
this.uri = uri;
this.httpWebRequestFactory = httpWebRequestFactory;
this.logger = new NamedLogger(logger, nameof(RemotePublicSuffixListProvider));
// The recommended default is refreshing the list every 24 hours.
// https://publicsuffix.org/list/
TimeToLive = TimeSpan.FromDays(1);
}
public override IPublicSuffixList GetList() {
lock (mutex) {
bool isCacheInvalid = cache is null ||
(TimeToLive != default && (cacheLastUpdated == default || DateTimeOffset.Now > cacheLastUpdated + TimeToLive));
if (isCacheInvalid) {
cache = GetListInternal();
cacheLastUpdated = DateTimeOffset.Now;
}
return cache;
}
}
// Private members
private readonly Uri uri;
private readonly IHttpWebRequestFactory httpWebRequestFactory;
private readonly ILogger logger;
private readonly object mutex = new object();
private IPublicSuffixList cache;
private DateTimeOffset cacheLastUpdated;
private static readonly Uri DefaultPublicSuffixListUri = new Uri("https://publicsuffix.org/list/public_suffix_list.dat");
private IPublicSuffixList GetListInternal() {
try {
using (IWebClient webClient = httpWebRequestFactory.CreateWebClient()) {
string publicSuffixListStr = webClient.DownloadString(uri);
return new PublicSuffixList(ParseList(publicSuffixListStr));
}
}
catch (WebException ex) {
if (!FallbackEnabled)
throw;
logger.Error(ex);
logger.Warning($"An exception occurred while downloading the public suffix list from {uri}, using internal list");
}
// Return the default list if we weren't able to open a file.
return base.GetList();
}
}
}