Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
Fixes #1
Fixes #2
Fixes #3
  • Loading branch information
Jesse Nicholson committed May 14, 2017
1 parent 8cfb27b commit 6e68a9a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
2 changes: 0 additions & 2 deletions DistillNET/DistillNET/DistillNET/AbpFormatRuleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ private Filter ParseUrlFilter(string rule, int optionsStartOffset, bool hasOptio
// Get applicable and exception domains. Exception domains in the list start with tilde,
// applicable domains don't. Applicable here meaning that the rule should apply to such
// a domain.
//applicableDomains = rawDomains.Where(d => !d.StartsWith("~")).ToList();
//exceptionDomains = rawDomains.Where(d => d.StartsWith("~")).Select(d => d.Substring(1)).ToList();

var domainsLen = rawDomains.Length;
for(int i = 0; i < domainsLen; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace DistillNET.Extensions
{
Expand Down
39 changes: 30 additions & 9 deletions DistillNET/DistillNET/DistillNET/FilterDbCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

using DistillNET.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

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

ConfigureDatabase();

if(isNew)
{
CreateTables();
}
CreateTables();

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

var allPossibleVariations = GetAllPossibleSubdomains(domain);

using(var tsx = m_connection.BeginTransaction())
using(var cmd = m_connection.CreateCommand())
{
switch(isWhitelist)
Expand All @@ -378,22 +380,41 @@ private async Task<List<UrlFilter>> GetFiltersForDomain(string domain, bool isWh
}

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

using(var reader = await cmd.ExecuteReaderAsync())
foreach(var sub in allPossibleVariations)
{
while(await reader.ReadAsync())
cmd.Parameters[0].Value = sub;

using(var reader = await cmd.ExecuteReaderAsync())
{
short catId = reader.GetInt16(1);
retVal.Add((UrlFilter)m_ruleParser.ParseAbpFormattedRule(reader.GetString(3), catId));
while(await reader.ReadAsync())
{
short catId = reader.GetInt16(1);
retVal.Add((UrlFilter)m_ruleParser.ParseAbpFormattedRule(reader.GetString(3), catId));
}
}
}
}

return retVal;
}

private List<string> GetAllPossibleSubdomains(string inputDomain)
{
var retVal = new List<string>() { inputDomain };
int subPos = inputDomain.IndexOfQuick('.');

while(subPos != -1)
{
inputDomain = inputDomain.Substring(subPos + 1);
retVal.Add(inputDomain);
subPos = inputDomain.IndexOfQuick('.');
}

return retVal;
}

public List<Filter> GetFiltersForRequest(Uri requestString, string referer = "")
{
return null;
Expand Down
22 changes: 9 additions & 13 deletions DistillNET/DistillNET/DistillNET/UrlFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Text;

namespace DistillNET
{
Expand Down Expand Up @@ -385,34 +387,28 @@ public string Domain
get;
private set;
} = string.Empty;

public AnchoredDomainFragment(string domain)
{
Domain = domain;
}

public AnchoredDomainFragment()
{

}

public override int IsMatch(Uri source, int lastPosition)
{
if(Domain.Length > source.AbsoluteUri.Length)
if(Domain.Length > source.Host.Length)
{
return -1;
}

// Why + 3? Because the Uri.Scheme property will give us things like "http" and
// "https". We need to remove that AND the following "://".
var schemeLength = source.Scheme.Length + 3;
var withoutScheme = source.AbsoluteUri.Substring(schemeLength);

if(Domain.Length < withoutScheme.Length)

if(Domain.Equals(source.Host.Substring(source.Host.Length - Domain.Length), StringComparison.OrdinalIgnoreCase))
{
if(Domain.Equals(withoutScheme.Substring(0, Domain.Length), StringComparison.OrdinalIgnoreCase))
{
return schemeLength + Domain.Length;
}
// Why + 3? Because of "://". The scheme doesn't include this.
return source.Scheme.Length + 3 + source.Host.Length;
}

return -1;
Expand Down

0 comments on commit 6e68a9a

Please sign in to comment.