Skip to content

Commit

Permalink
Matching functions now use NameValueCollection
Browse files Browse the repository at this point in the history
Matching functions will now use NameValueCollection, as they should,
since we're expecting HTTP headers.
  • Loading branch information
Jesse Nicholson committed May 9, 2017
1 parent e2aa16c commit 8cfb27b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Ignore stuff like nuget.exe
*.exe

# Build results
[Dd]ebug/
[Dd]ebugPublic/
Expand Down
26 changes: 26 additions & 0 deletions DistillNET/DistillNET/DistillNET.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>DistillNET</id>
<version>1.0.1</version>
<title>DistillNET</title>
<authors>TechnikEmpire</authors>
<owners>TechnikEmpire</owners>
<licenseUrl>https://www.mozilla.org/en-US/MPL/2.0/</licenseUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>DistillNET is a library for matching and filtering HTTP requests and HTML response content using the Adblock Plus Filter format.</description>
<releaseNotes>Matching function(s) now use NameValueCollection.</releaseNotes>
<copyright>Copyright 2017 Jesse Nicholson</copyright>
<tags>DistillNET Adblock AdblockPlus Adblock-Plus URL-Filter URL-Filtering Content-Filter Filter</tags>
<dependencies>
<dependency id="EntityFramework" version="6.1.3"/>
<dependency id="System.Data.SQLite" version="1.0.105.0"/>
<dependency id="System.Data.SQLite.Core" version="1.0.105.0"/>
<dependency id="System.Data.SQLite.EF6" version="1.0.105.0"/>
<dependency id="System.Data.SQLite.Linq" version="1.0.105.0"/>
</dependencies>
</metadata>
<files>
<file src="bin\Release\DistillNET.dll" target="lib\DistillNET.dll"/>
</files>
</package>
11 changes: 6 additions & 5 deletions DistillNET/DistillNET/DistillNET/UrlFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using DistillNET.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace DistillNET
{
Expand Down Expand Up @@ -563,14 +564,14 @@ internal UrlFilter(string originalRule, List<UrlFilteringRuleFragment> parts, Ur
/// <returns>
/// True if this filter is a positive match against the supplied URI, false otherwise.
/// </returns>
public bool IsMatch(Uri uri, Dictionary<string, string> rawHeaders)
public bool IsMatch(Uri uri, NameValueCollection rawHeaders)
{
// Make sure that the headers match up with our options.
if(this.Options != UrlFilterOptions.None)
{
string headerVal = string.Empty;
string headerVal = null;
long xmlHttpRequestBits = ((OptionsLong & (long)UrlFilterOptions.ExceptXmlHttpRequest) | (OptionsLong & (long)UrlFilterOptions.XmlHttpRequest));
if(rawHeaders.TryGetValue("X-Requested-With", out headerVal))
if((headerVal = rawHeaders.Get("X-Requested-With")) != null)
{
if(headerVal.Equals("XMLHttpRequest", StringComparison.OrdinalIgnoreCase))
{
Expand All @@ -589,7 +590,7 @@ public bool IsMatch(Uri uri, Dictionary<string, string> rawHeaders)
}

long thirdPartyBits = ((OptionsLong & (long)UrlFilterOptions.ThirdParty) | (OptionsLong & (long)UrlFilterOptions.ExceptThirdParty));
if(rawHeaders.TryGetValue("Referer", out headerVal))
if((headerVal = rawHeaders.Get("Referer")) != null)
{
if(headerVal.Equals(uri.Host, StringComparison.OrdinalIgnoreCase))
{
Expand Down Expand Up @@ -617,7 +618,7 @@ public bool IsMatch(Uri uri, Dictionary<string, string> rawHeaders)

long contentTypeBits = ((OptionsLong & (long)UrlFilterOptions.Image) | (OptionsLong & (long)UrlFilterOptions.Script) | (OptionsLong & (long)UrlFilterOptions.StyleSheet) | (OptionsLong & (long)UrlFilterOptions.ExceptImage) | (OptionsLong & (long)UrlFilterOptions.ExceptScript) | (OptionsLong & (long)UrlFilterOptions.ExceptStyleSheet));

if(rawHeaders.TryGetValue("Content-Type", out headerVal))
if((headerVal = rawHeaders.Get("Content-Type")) != null)
{
if(headerVal.IndexOfQuick("script") != -1)
{
Expand Down
4 changes: 2 additions & 2 deletions DistillNET/DistillNET/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
8 changes: 4 additions & 4 deletions DistillNET/Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;

Expand All @@ -15,7 +16,7 @@ namespace DistillNET
internal class Program
{
private static void Main(string[] args)
{
{
var parser = new AbpFormatRuleParser();

string easylistPath = AppDomain.CurrentDomain.BaseDirectory + "easylist.txt";
Expand Down Expand Up @@ -108,7 +109,7 @@ private static void TestFilterMatching()
// "in-the-wild" kind of stuff.
var rp = new AbpFormatRuleParser();
var filter = rp.ParseAbpFormattedRule("||silly.com^stoopid^url^*1$xmlhttprequest,script,~third-party", 1) as UrlFilter;
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
var headers = new NameValueCollection(StringComparer.OrdinalIgnoreCase)
{
{ "X-Requested-With", "XmlHttpRequest" },
{ "Content-Type", "script" },
Expand All @@ -118,8 +119,7 @@ private static void TestFilterMatching()

double d = 10000000;
var results = new List<bool>((int)d);
var sw2 = new Stopwatch();

var sw2 = new Stopwatch();
Console.WriteLine("Roughly Benchmarking Filter Matching Speed");
sw2.Start();
for(int i = 0; i < d; ++i)
Expand Down

0 comments on commit 8cfb27b

Please sign in to comment.