1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-17 07:52:42 +01:00
uBlock/src/js/redirect-engine.js

276 lines
7.8 KiB
JavaScript
Raw Normal View History

/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2015 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
/* global µBlock */
/******************************************************************************/
µBlock.redirectEngine = (function(){
'use strict';
/******************************************************************************/
var toBroaderHostname = function(hostname) {
var pos = hostname.indexOf('.');
2015-11-23 15:49:50 +01:00
if ( pos !== -1 ) {
return hostname.slice(pos + 1);
}
2015-11-23 15:49:50 +01:00
return hostname !== '*' && hostname !== '' ? '*' : '';
};
/******************************************************************************/
var RedirectEngine = function() {
2015-11-24 05:34:03 +01:00
this.resources = Object.create(null);
this.reset();
};
/******************************************************************************/
RedirectEngine.prototype.reset = function() {
this.rules = Object.create(null);
};
/******************************************************************************/
RedirectEngine.prototype.freeze = function() {
};
/******************************************************************************/
RedirectEngine.prototype.lookup = function(context) {
var typeEntry = this.rules[context.requestType];
if ( typeEntry === undefined ) {
return;
}
var src, des = context.requestHostname,
srcHostname = context.pageHostname,
reqURL = context.requestURL,
desEntry, entries, i, entry;
for (;;) {
desEntry = typeEntry[des];
if ( desEntry !== undefined ) {
src = srcHostname;
for (;;) {
entries = desEntry[src];
if ( entries !== undefined ) {
i = entries.length;
while ( i-- ) {
entry = entries[i];
if ( entry.c.test(reqURL) ) {
2015-11-24 05:34:03 +01:00
return this.resources[entry.r];
}
}
}
2015-11-23 15:49:50 +01:00
src = toBroaderHostname(src);
if ( src === '' ) {
break;
}
}
}
des = toBroaderHostname(des);
if ( des === '' ) {
break;
}
}
};
/******************************************************************************/
RedirectEngine.prototype.addRule = function(src, des, type, pattern, redirect) {
var typeEntry = this.rules[type];
if ( typeEntry === undefined ) {
typeEntry = this.rules[type] = Object.create(null);
}
var desEntry = typeEntry[des];
if ( desEntry === undefined ) {
desEntry = typeEntry[des] = Object.create(null);
}
var ruleEntries = desEntry[src];
if ( ruleEntries === undefined ) {
ruleEntries = desEntry[src] = [];
}
ruleEntries.push({
c: new RegExp(pattern),
r: redirect
});
};
/******************************************************************************/
RedirectEngine.prototype.fromCompiledRule = function(line) {
var fields = line.split('\t');
if ( fields.length !== 5 ) {
return;
}
this.addRule(fields[0], fields[1], fields[2], fields[3], fields[4]);
};
/******************************************************************************/
RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) {
var matches = this.reFilterParser.exec(line);
if ( matches === null || matches.length !== 4 ) {
return '';
}
var pattern = (matches[1] + matches[2]).replace(/[.+?{}()|[\]\\]/g, '\\$&')
.replace(/\^/g, '[^\\w\\d%-]')
.replace(/\*/g, '.*?');
var des = matches[1];
var types = [];
var redirect = '';
var srcs = [];
var options = matches[3].split(','), option;
while ( (option = options.pop()) ) {
if ( option.lastIndexOf('redirect=', 0) === 0 ) {
redirect = option.slice(9);
continue;
}
if ( option.lastIndexOf('domain=', 0) === 0 ) {
srcs = option.slice(7).split('|');
continue;
}
if ( option in this.supportedTypes ) {
types.push(option);
continue;
}
}
2015-11-24 05:34:03 +01:00
// Need a resource token.
if ( redirect === '' ) {
return '';
}
// Need one single type (not negated).
if ( types.length !== 1 || types[0].charAt(0) === '~' ) {
return '';
}
if ( des === '' ) {
des = '*';
}
if ( srcs.length === 0 ) {
srcs.push('*');
}
var out = [];
2015-11-24 05:34:03 +01:00
var i = srcs.length, src;
while ( i-- ) {
2015-11-24 05:34:03 +01:00
src = srcs[i];
if ( src.charAt(0) === '~' ) {
continue;
}
2015-11-24 05:34:03 +01:00
out.push(srcs[i] + '\t' + des + '\t' + types[0] + '\t' + pattern + '\t' + redirect);
}
return out;
};
/******************************************************************************/
RedirectEngine.prototype.reFilterParser = /^\|\|([^\/\?#]+)([^$]+)\$([^$]+)$/;
RedirectEngine.prototype.supportedTypes = (function() {
var types = Object.create(null);
types.stylesheet = 'stylesheet';
types.image = 'image';
types.object = 'object';
types.script = 'script';
types.xmlhttprequest = 'xmlhttprequest';
types.subdocument = 'sub_frame';
types.font = 'font';
return types;
})();
/******************************************************************************/
// TODO: combine same key-redirect pairs into a single regex.
2015-11-24 05:34:03 +01:00
RedirectEngine.prototype.resourcesFromString = function(text) {
var textEnd = text.length;
var lineBeg = 0, lineEnd;
2015-11-24 19:21:14 +01:00
var line, fields;
var resourceFromFields = function(fields) {
var encoded = fields[1].indexOf(';') !== -1;
var data = fields.slice(2).join(encoded ? '' : '\n');
this.resources[fields[0]] =
'data:' +
fields[1] +
(encoded ? '' : ';base64') +
',' +
(encoded ? data : btoa(data));
}.bind(this);
2015-11-24 05:34:03 +01:00
this.resources = Object.create(null);
while ( lineBeg < textEnd ) {
lineEnd = text.indexOf('\n', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = text.indexOf('\r', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = textEnd;
}
}
line = text.slice(lineBeg, lineEnd).trim();
lineBeg = lineEnd + 1;
if ( line.charAt(0) === '#' ) {
continue;
}
2015-11-24 19:21:14 +01:00
if ( fields === undefined ) {
fields = line.split(/\s+/);
if ( fields.length !== 2 ) {
2015-11-24 19:21:14 +01:00
fields = undefined;
}
continue;
}
2015-11-24 05:34:03 +01:00
if ( line !== '' ) {
2015-11-24 19:21:14 +01:00
fields.push(line);
continue;
}
2015-11-24 05:34:03 +01:00
2015-11-24 19:21:14 +01:00
// No more data, add the resource.
resourceFromFields(fields);
fields = undefined;
}
2015-11-24 05:34:03 +01:00
2015-11-24 19:21:14 +01:00
// Process pending resource data.
if ( fields !== undefined ) {
resourceFromFields(fields);
}
};
/******************************************************************************/
return new RedirectEngine();
/******************************************************************************/
})();