Skip to content

Commit 6e68a9a

Browse files
author
Jesse Nicholson
committed
Fixes
Fixes #1 Fixes #2 Fixes #3
1 parent 8cfb27b commit 6e68a9a

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

DistillNET/DistillNET/DistillNET/AbpFormatRuleParser.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ private Filter ParseUrlFilter(string rule, int optionsStartOffset, bool hasOptio
281281
// Get applicable and exception domains. Exception domains in the list start with tilde,
282282
// applicable domains don't. Applicable here meaning that the rule should apply to such
283283
// a domain.
284-
//applicableDomains = rawDomains.Where(d => !d.StartsWith("~")).ToList();
285-
//exceptionDomains = rawDomains.Where(d => d.StartsWith("~")).Select(d => d.Substring(1)).ToList();
286284

287285
var domainsLen = rawDomains.Length;
288286
for(int i = 0; i < domainsLen; ++i)

DistillNET/DistillNET/DistillNET/Extensions/StringExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77

88
using System;
9+
using System.Collections.Generic;
10+
using System.Text;
911

1012
namespace DistillNET.Extensions
1113
{

DistillNET/DistillNET/DistillNET/FilterDbCollection.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8+
using DistillNET.Extensions;
89
using System;
910
using System.Collections.Generic;
1011
using System.Data;
1112
using System.Data.SQLite;
1213
using System.IO;
14+
using System.Linq;
1315
using System.Threading.Tasks;
1416

1517
namespace DistillNET
@@ -76,10 +78,7 @@ public FilterDbCollection(string dbAbsolutePath, bool overwrite = true, bool use
7678

7779
ConfigureDatabase();
7880

79-
if(isNew)
80-
{
81-
CreateTables();
82-
}
81+
CreateTables();
8382

8483
m_globalKey = "global";
8584
}
@@ -360,6 +359,9 @@ private async Task<List<UrlFilter>> GetFiltersForDomain(string domain, bool isWh
360359
{
361360
var retVal = new List<UrlFilter>();
362361

362+
var allPossibleVariations = GetAllPossibleSubdomains(domain);
363+
364+
using(var tsx = m_connection.BeginTransaction())
363365
using(var cmd = m_connection.CreateCommand())
364366
{
365367
switch(isWhitelist)
@@ -378,22 +380,41 @@ private async Task<List<UrlFilter>> GetFiltersForDomain(string domain, bool isWh
378380
}
379381

380382
var domainSumParam = new SQLiteParameter("$domainId", System.Data.DbType.String);
381-
domainSumParam.Value = domain;
382383
cmd.Parameters.Add(domainSumParam);
383384

384-
using(var reader = await cmd.ExecuteReaderAsync())
385+
foreach(var sub in allPossibleVariations)
385386
{
386-
while(await reader.ReadAsync())
387+
cmd.Parameters[0].Value = sub;
388+
389+
using(var reader = await cmd.ExecuteReaderAsync())
387390
{
388-
short catId = reader.GetInt16(1);
389-
retVal.Add((UrlFilter)m_ruleParser.ParseAbpFormattedRule(reader.GetString(3), catId));
391+
while(await reader.ReadAsync())
392+
{
393+
short catId = reader.GetInt16(1);
394+
retVal.Add((UrlFilter)m_ruleParser.ParseAbpFormattedRule(reader.GetString(3), catId));
395+
}
390396
}
391397
}
392398
}
393399

394400
return retVal;
395401
}
396402

403+
private List<string> GetAllPossibleSubdomains(string inputDomain)
404+
{
405+
var retVal = new List<string>() { inputDomain };
406+
int subPos = inputDomain.IndexOfQuick('.');
407+
408+
while(subPos != -1)
409+
{
410+
inputDomain = inputDomain.Substring(subPos + 1);
411+
retVal.Add(inputDomain);
412+
subPos = inputDomain.IndexOfQuick('.');
413+
}
414+
415+
return retVal;
416+
}
417+
397418
public List<Filter> GetFiltersForRequest(Uri requestString, string referer = "")
398419
{
399420
return null;

DistillNET/DistillNET/DistillNET/UrlFilter.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using System;
1010
using System.Collections.Generic;
1111
using System.Collections.Specialized;
12+
using System.Diagnostics;
13+
using System.Text;
1214

1315
namespace DistillNET
1416
{
@@ -385,34 +387,28 @@ public string Domain
385387
get;
386388
private set;
387389
} = string.Empty;
388-
390+
389391
public AnchoredDomainFragment(string domain)
390392
{
391393
Domain = domain;
392394
}
393395

394396
public AnchoredDomainFragment()
395397
{
398+
396399
}
397400

398401
public override int IsMatch(Uri source, int lastPosition)
399402
{
400-
if(Domain.Length > source.AbsoluteUri.Length)
403+
if(Domain.Length > source.Host.Length)
401404
{
402405
return -1;
403406
}
404-
405-
// Why + 3? Because the Uri.Scheme property will give us things like "http" and
406-
// "https". We need to remove that AND the following "://".
407-
var schemeLength = source.Scheme.Length + 3;
408-
var withoutScheme = source.AbsoluteUri.Substring(schemeLength);
409-
410-
if(Domain.Length < withoutScheme.Length)
407+
408+
if(Domain.Equals(source.Host.Substring(source.Host.Length - Domain.Length), StringComparison.OrdinalIgnoreCase))
411409
{
412-
if(Domain.Equals(withoutScheme.Substring(0, Domain.Length), StringComparison.OrdinalIgnoreCase))
413-
{
414-
return schemeLength + Domain.Length;
415-
}
410+
// Why + 3? Because of "://". The scheme doesn't include this.
411+
return source.Scheme.Length + 3 + source.Host.Length;
416412
}
417413

418414
return -1;

0 commit comments

Comments
 (0)