@@ -50,7 +50,7 @@ function RuleSet(name, match_rule, default_off) {
5050
5151RuleSet . prototype = {
5252 _apply : function ( urispec ) {
53- // return 0 would have applied but is inactive, null if it does not apply
53+ // return null if it does not apply
5454 // and the new url if it does apply
5555 var i ;
5656 var returl = null ;
@@ -60,20 +60,19 @@ RuleSet.prototype = {
6060 return null ;
6161 }
6262 // Even so, if we're covered by an exclusion, go home
63- for ( i = 0 ; i < this . exclusions . length ; ++ i ) {
63+ for ( i = 0 ; i < this . exclusions . length ; ++ i ) {
6464 if ( this . exclusions [ i ] . pattern_c . test ( urispec ) ) {
6565 this . log ( DBUG , "excluded uri " + urispec ) ;
6666 return null ;
6767 }
6868 }
6969 // Okay, now find the first rule that triggers
70- for ( i = 0 ; i < this . rules . length ; ++ i ) {
70+ for ( i = 0 ; i < this . rules . length ; ++ i ) {
71+ // This is just for displaying inactive rules
7172 returl = urispec . replace ( this . rules [ i ] . from_c , this . rules [ i ] . to ) ;
72- if ( returl != urispec ) {
73- if ( this . active ) return returl
74- else return 0 ;
75- }
73+ if ( returl != urispec ) return returl ;
7674 }
75+
7776 if ( this . ruleset_match_c ) {
7877 // This is not an error, because we do not insist the matchrule
7978 // precisely describes to target space of URLs ot redirected
@@ -85,13 +84,33 @@ RuleSet.prototype = {
8584 log : function ( level , msg ) {
8685 https_everywhereLog ( level , msg ) ;
8786 } ,
87+
88+ wouldMatch : function ( hypothetical_uri , alist ) {
89+ // return true if this ruleset would match the uri, assuming it were http
90+ // used for judging moot / inactive rulesets
91+ this . log ( DBUG , "Would " + this . name + " match " + hypothetical_uri . spec + "? serial " + alist . serial ) ;
92+ var uri = hypothetical_uri . clone ( ) ;
93+ if ( uri . scheme == "https" ) uri . scheme = "http" ;
94+ var urispec = uri . spec ;
95+
96+ if ( this . ruleset_match_c && ! this . ruleset_match_c . test ( urispec ) )
97+ return false ;
98+
99+ for ( i = 0 ; i < this . exclusions . length ; ++ i )
100+ if ( this . exclusions [ i ] . pattern_c . test ( urispec ) ) return false ;
101+
102+ for ( i = 0 ; i < this . rules . length ; ++ i )
103+ if ( this . rules [ i ] . from_c . test ( urispec ) ) return true ;
104+ this . log ( DBUG , "No, we fell through" ) ;
105+ return false ;
106+ } ,
88107
89108 transformURI : function ( uri ) {
90109 // If no rule applies, return null; if a rule would have applied but was
91110 // inactive, return 0; otherwise, return a fresh uri instance
92111 // for the target
93112 var newurl = this . _apply ( uri . spec ) ;
94- if ( null == newurl )
113+ if ( null == newurl )
95114 return null ;
96115 if ( 0 == newurl )
97116 return 0 ;
@@ -324,37 +343,32 @@ const HTTPSRules = {
324343 }
325344 } ,
326345
327- rewrittenURI : function ( applicable_list , uri ) {
346+ rewrittenURI : function ( alist , uri ) {
328347 // This function oversees the task of working out if a uri should be
329348 // rewritten, what it should be rewritten to, and recordkeeping of which
330349 // applicable rulesets are and aren't active.
331350 var i = 0 ;
332351 var newuri = null
333352 var rs = this . potentiallyApplicableRulesets ( uri . host ) ;
334- if ( ! applicable_list )
353+ if ( ! alist )
335354 this . log ( DBUG , "No applicable list rewriting " + uri . spec ) ;
336355 for ( i = 0 ; i < rs . length ; ++ i ) {
356+ if ( ! rs [ i ] . active ) {
357+ if ( alist && rs [ i ] . wouldMatch ( uri , alist ) )
358+ alist . inactive_rule ( rs [ i ] ) ;
359+ continue ;
360+ }
361+ if ( uri . scheme == "https" && alist ) {
362+ // we didn't rewrite but the rule applies to this domain and the
363+ // requests are going over https
364+ if ( rs [ i ] . wouldMatch ( uri , alist ) ) alist . moot_rule ( rs [ i ] ) ;
365+ continue ;
366+ }
337367 newuri = rs [ i ] . transformURI ( uri ) ;
338368 if ( newuri ) {
339369 // we rewrote the uri
340- if ( applicable_list ) {
341- this . log ( "Adding active rule: " + rs [ i ] . name ) ;
342- applicable_list . active_rule ( rs [ i ] ) ;
343- //applicable_list.show_applicable();
344- }
370+ if ( alist ) alist . active_rule ( rs [ i ] ) ;
345371 return newuri ;
346- } else if ( 0 == newuri ) {
347- // the ruleset is inactive
348- if ( applicable_list ) {
349- this . log ( "Adding inactive rule: " + rs [ i ] . name ) ;
350- applicable_list . inactive_rule ( rs [ i ] ) ;
351- //applicable_list.show_applicable();
352- }
353- } else if ( uri . scheme == "https" && applicable_list && rs [ i ] . active ) {
354- // we didn't rewrite but the rule applies to this domain and the
355- // requests are going over https
356- applicable_list . moot_rule ( rs [ i ] ) ;
357- //applicable_list.show_applicable();
358372 }
359373 }
360374 return null ;
@@ -383,7 +397,7 @@ const HTTPSRules = {
383397 if ( this . targets [ t ] )
384398 results = results . concat ( this . targets [ t ] ) ;
385399 }
386- this . log ( DBUG , "Applicable rules for " + host + ":" ) ;
400+ this . log ( DBUG , "Potentially applicable rules for " + host + ":" ) ;
387401 for ( i = 0 ; i < results . length ; ++ i )
388402 this . log ( DBUG , " " + results [ i ] . name ) ;
389403 return results ;
0 commit comments