Skip to content

Commit ec8b217

Browse files
committed
Async mostly works.
1 parent 5785ef1 commit ec8b217

File tree

5 files changed

+113
-74
lines changed

5 files changed

+113
-74
lines changed

src/chrome/content/code/ApplicableList.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ ApplicableList.prototype = {
7373
populate_list: function() {
7474
// The base URI of the dom tends to be loaded from some /other/
7575
// ApplicableList, so pretend we're loading it from here.
76-
HTTPSEverywhere.instance.https_rules.rewrittenURI(this, this.uri);
76+
HTTPSEverywhere.instance.https_rules.rewrittenURI(this, this.uri, function() {
77+
// do nothing
78+
});
7779
this.log(DBUG, "populating using alist #" + this.serial);
7880
},
7981

src/chrome/content/code/HTTPS.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,26 @@ const HTTPS = {
3939
* untouched or aborted.
4040
*/
4141
replaceChannel: function(applicable_list, channel, httpNowhereEnabled) {
42-
var blob = HTTPSRules.rewrittenURI(applicable_list, channel.URI.clone());
42+
try {
43+
channel.suspend();
44+
} catch (e) {
45+
this.log(WARN, 'Failed to suspend ' + channel.URI.spec + ': ' + e);
46+
return;
47+
}
48+
this.log(WARN, 'Succeeded to suspend ' + channel.URI.spec);
49+
var that = this;
50+
HTTPSRules.rewrittenURI(applicable_list, channel.URI.clone(), function(blob) {
51+
that.replaceChannelCallback(applicable_list, channel, httpNowhereEnabled, blob);
52+
});
53+
},
54+
55+
replaceChannelCallback: function(applicable_list, channel, httpNowhereEnabled, blob) {
56+
try {
57+
channel.resume();
58+
} catch (e) {
59+
this.log(WARN, 'Failed to resume ' + channel.URI.spec + ': ' + e);
60+
return;
61+
}
4362
var isSTS = securityService.isSecureURI(
4463
CI.nsISiteSecurityService.HEADER_HSTS, channel.URI, 0);
4564
if (blob === null) {

src/chrome/content/code/HTTPSRules.js

Lines changed: 86 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,6 @@ const HTTPSRules = {
421421
var t2 = new Date().getTime();
422422
this.log(NOTE,"Loading targets took " + (t2 - t1) / 1000.0 + " seconds");
423423

424-
var gitCommitQuery = rulesetDBConn.createStatement("select git_commit from git_commit");
425-
if (gitCommitQuery.executeStep()) {
426-
this.GITCommitID = gitCommitQuery.row.git_commit;
427-
}
428424
return;
429425
},
430426

@@ -476,8 +472,7 @@ const HTTPSRules = {
476472
}
477473
},
478474

479-
480-
rewrittenURI: function(alist, input_uri) {
475+
rewrittenURI: function(alist, input_uri, callback) {
481476
// This function oversees the task of working out if a uri should be
482477
// rewritten, what it should be rewritten to, and recordkeeping of which
483478
// applicable rulesets are and aren't active. Previously this returned
@@ -491,44 +486,52 @@ const HTTPSRules = {
491486
if (!alist) this.log(DBUG, "No applicable list rewriting " + input_uri.spec);
492487
this.log(DBUG, "Processing " + input_uri.spec);
493488

494-
var uri = this.sanitiseURI(input_uri);
495-
496489
// Get the list of rulesets that target this host
497490
try {
498-
var rs = this.potentiallyApplicableRulesets(uri.host);
491+
var host = input_uri.host
499492
} catch(e) {
500-
this.log(NOTE, 'Could not check applicable rules for '+uri.spec + '\n'+e);
501-
return null;
502-
}
503-
504-
// ponder each potentially applicable ruleset, working out if it applies
505-
// and recording it as active/inactive/moot/breaking in the applicable list
506-
for (i = 0; i < rs.length; ++i) {
507-
if (!rs[i].active) {
508-
if (alist && rs[i].wouldMatch(uri, alist))
509-
alist.inactive_rule(rs[i]);
510-
continue;
511-
}
512-
blob.newuri = rs[i].transformURI(uri);
513-
if (blob.newuri) {
514-
if (alist) {
515-
if (uri.spec in https_everywhere_blacklist)
516-
alist.breaking_rule(rs[i]);
517-
else
518-
alist.active_rule(rs[i]);
519-
}
520-
if (userpass_present) blob.newuri.userPass = input_uri.userPass;
521-
blob.applied_ruleset = rs[i];
522-
return blob;
493+
// NS_ERROR_FAILURE is normal for accessing uri.host. It just means that
494+
// host is not applicable for the URI scheme, e.g. about: URIs.
495+
// If that happens we quietly return null. If another exception happens
496+
// we noisily return null.
497+
if (e.name != "NS_ERROR_FAILURE") {
498+
this.log(WARN, 'Could not get host from ' + input_uri.spec + ': ' + e);
523499
}
524-
if (uri.scheme == "https" && alist) {
525-
// we didn't rewrite but the rule applies to this domain and the
526-
// requests are going over https
527-
if (rs[i].wouldMatch(uri, alist)) alist.moot_rule(rs[i]);
528-
continue;
529-
}
530-
}
531-
return null;
500+
callback(null);
501+
return;
502+
}
503+
var that = this;
504+
this.potentiallyApplicableRulesets(host, function(rs) {
505+
var uri = that.sanitiseURI(input_uri);
506+
// ponder each potentially applicable ruleset, working out if it applies
507+
// and recording it as active/inactive/moot/breaking in the applicable list
508+
for (i = 0; i < rs.length; ++i) {
509+
if (!rs[i].active) {
510+
if (alist && rs[i].wouldMatch(uri, alist))
511+
alist.inactive_rule(rs[i]);
512+
continue;
513+
}
514+
blob.newuri = rs[i].transformURI(uri);
515+
if (blob.newuri) {
516+
if (alist) {
517+
if (uri.spec in https_everywhere_blacklist)
518+
alist.breaking_rule(rs[i]);
519+
else
520+
alist.active_rule(rs[i]);
521+
}
522+
if (userpass_present) blob.newuri.userPass = input_uri.userPass;
523+
blob.applied_ruleset = rs[i];
524+
callback(blob);
525+
}
526+
if (uri.scheme == "https" && alist) {
527+
// we didn't rewrite but the rule applies to this domain and the
528+
// requests are going over https
529+
if (rs[i].wouldMatch(uri, alist)) alist.moot_rule(rs[i]);
530+
continue;
531+
}
532+
}
533+
callback(null);
534+
});
532535
},
533536

534537
sanitiseURI: function(input_uri) {
@@ -599,25 +602,30 @@ const HTTPSRules = {
599602
},
600603

601604
// Get all rulesets matching a given target, lazy-loading from DB as necessary.
602-
rulesetsByTarget: function(target) {
603-
var rulesetIds = this.targets[target];
604-
605+
rulesetsByTargets: function(targets, callback) {
605606
var output = [];
606-
if (rulesetIds) {
607-
this.log(INFO, "For target " + target + ", found ids " + rulesetIds.toString());
608-
for (var i = 0; i < rulesetIds.length; i++) {
609-
var id = rulesetIds[i];
610-
if (!this.rulesetsByID[id]) {
611-
this.loadRulesetById(id);
612-
}
613-
if (this.rulesetsByID[id]) {
614-
output.push(this.rulesetsByID[id]);
607+
var foundIds = [];
608+
var that = this;
609+
targets.forEach(function(target) {
610+
var rulesetIds = that.targets[target];
611+
612+
if (rulesetIds) {
613+
for (var i = 0; i < rulesetIds.length; i++) {
614+
var id = rulesetIds[i];
615+
if (!that.rulesetsByID[id]) {
616+
that.loadRulesetById(id);
617+
}
618+
if (that.rulesetsByID[id]) {
619+
foundIds.push(id);
620+
output.push(that.rulesetsByID[id]);
621+
}
615622
}
623+
} else {
624+
that.log(DBUG, "For target " + target + ", found no ids in DB");
616625
}
617-
} else {
618-
this.log(DBUG, "For target " + target + ", found no ids in DB");
619-
}
620-
return output;
626+
});
627+
that.log(DBUG, "For targets " + targets.join(' ') + ", found ids " + foundIds.join(','));
628+
callback(output);
621629
},
622630

623631
/**
@@ -628,15 +636,14 @@ const HTTPSRules = {
628636
* @param host {string}
629637
* @return {Array.<RuleSet>}
630638
*/
631-
potentiallyApplicableRulesets: function(host) {
639+
potentiallyApplicableRulesets: function(host, callback) {
640+
// XXX fix before submit: this should never happen.
641+
if (!callback) {
642+
this.log(WARN, 'Bad problem: potentiallyApplicableRulesets called without callback.');
643+
return;
644+
}
632645
var i, tmp, t;
633-
var results = [];
634-
635-
var attempt = function(target) {
636-
this.setInsert(results, this.rulesetsByTarget(target));
637-
}.bind(this);
638-
639-
attempt(host);
646+
var targetsToTry = [host];
640647

641648
// replace each portion of the domain with a * in turn
642649
var segmented = host.split(".");
@@ -649,18 +656,22 @@ const HTTPSRules = {
649656
segmented[i] = "*";
650657
t = segmented.join(".");
651658
segmented[i] = tmp;
652-
attempt(t);
659+
targetsToTry.push(t);
653660
}
654661
// now eat away from the left, with *, so that for x.y.z.google.com we
655662
// check *.z.google.com and *.google.com (we did *.y.z.google.com above)
656663
for (i = 2; i <= segmented.length - 2; ++i) {
657664
t = "*." + segmented.slice(i,segmented.length).join(".");
658-
attempt(t);
659-
}
660-
this.log(DBUG,"Potentially applicable rules for " + host + ":");
661-
for (i = 0; i < results.length; ++i)
662-
this.log(DBUG, " " + results[i].name);
663-
return results;
665+
targetsToTry.push(t)
666+
}
667+
var that = this;
668+
this.rulesetsByTargets(targetsToTry, function(rulesets) {
669+
that.log(DBUG,"Potentially applicable rules for " + host + ":");
670+
for (i = 0; i < rulesets.length; ++i)
671+
that.log(DBUG, " " + rulesets[i].name);
672+
callback(rulesets);
673+
});
674+
return;
664675
},
665676

666677
/**
@@ -696,6 +707,8 @@ const HTTPSRules = {
696707
var i,j;
697708
// potentiallyApplicableRulesets is defined on hostnames not cookie-style
698709
// "domain" attributes, so we strip a leading dot before calling.
710+
// XXX FIX FOR ASYNC
711+
return;
699712
var rs = this.potentiallyApplicableRulesets(this.hostFromCookieDomain(c.host));
700713
for (i = 0; i < rs.length; ++i) {
701714
var ruleset = rs[i];
@@ -765,6 +778,8 @@ const HTTPSRules = {
765778
this.log(DBUG, "Testing securecookie applicability with " + test_uri);
766779
// potentiallyApplicableRulesets is defined on hostnames not cookie-style
767780
// "domain" attributes, so we strip a leading dot before calling.
781+
// XXX FIX FOR ASYNC
782+
return
768783
var rs = this.potentiallyApplicableRulesets(this.hostFromCookieDomain(domain));
769784
for (var i = 0; i < rs.length; ++i) {
770785
if (!rs[i].active) continue;

src/components/https-everywhere.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,9 @@ HTTPSEverywhere.prototype = {
652652
return;
653653
}
654654
var alist = this.juggleApplicableListsDuringRedirection(oldChannel, newChannel);
655-
HTTPS.replaceChannel(alist, newChannel, this.httpNowhereEnabled);
655+
// XXX: This causes NS_ERROR_UNAVAILABLE when replaceChannel tries to
656+
// suspend the channel.
657+
//HTTPS.replaceChannel(alist, newChannel, this.httpNowhereEnabled);
656658
},
657659

658660
juggleApplicableListsDuringRedirection: function(oldChannel, newChannel) {

test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pushd $TEST_ADDON_PATH
5555
# If you just want to run Firefox with the latest code:
5656
if [ "$1" == "--justrun" ]; then
5757
echo "running firefox"
58+
shift
5859
firefox -no-remote -profile "$PROFILE_DIRECTORY" "$@"
5960
else
6061
echo "running tests"

0 commit comments

Comments
 (0)