Avoid creating a session if there are no jobs

This commit is contained in:
Raymond Hill 2023-11-05 14:54:59 -05:00
parent ce835bb300
commit 1055203542
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 36 additions and 37 deletions

View File

@ -27,6 +27,7 @@ import logger from './logger.js';
import µb from './background.js'; import µb from './background.js';
import { sessionFirewall } from './filtering-engines.js'; import { sessionFirewall } from './filtering-engines.js';
import { StaticExtFilteringHostnameDB } from './static-ext-filtering-db.js'; import { StaticExtFilteringHostnameDB } from './static-ext-filtering-db.js';
import { entityFromDomain } from './uri-utils.js';
/******************************************************************************/ /******************************************************************************/
@ -380,19 +381,21 @@ htmlFilteringEngine.fromCompiledContent = function(reader) {
} }
}; };
htmlFilteringEngine.retrieve = function(details) { htmlFilteringEngine.retrieve = function(fctxt) {
const hostname = details.hostname;
const plains = new Set(); const plains = new Set();
const procedurals = new Set(); const procedurals = new Set();
const exceptions = new Set(); const exceptions = new Set();
const retrieveSets = [ plains, exceptions, procedurals, exceptions ]; const retrieveSets = [ plains, exceptions, procedurals, exceptions ];
const hostname = fctxt.getHostname();
filterDB.retrieve(hostname, retrieveSets); filterDB.retrieve(hostname, retrieveSets);
const entity = details.entity !== ''
? `${hostname.slice(0, -details.domain.length)}${details.entity}` const domain = fctxt.getDomain();
const entity = entityFromDomain(domain);
const hostnameEntity = entity !== ''
? `${hostname.slice(0, -domain.length)}${entity}`
: '*'; : '*';
filterDB.retrieve(entity, retrieveSets, 1); filterDB.retrieve(hostnameEntity, retrieveSets, 1);
if ( plains.size === 0 && procedurals.size === 0 ) { return; } if ( plains.size === 0 && procedurals.size === 0 ) { return; }
@ -414,12 +417,12 @@ htmlFilteringEngine.retrieve = function(details) {
for ( const selector of exceptions ) { for ( const selector of exceptions ) {
if ( plains.has(selector) ) { if ( plains.has(selector) ) {
plains.delete(selector); plains.delete(selector);
logOne(details, 1, selector); logOne(fctxt, 1, selector);
continue; continue;
} }
if ( procedurals.has(selector) ) { if ( procedurals.has(selector) ) {
procedurals.delete(selector); procedurals.delete(selector);
logOne(details, 1, JSON.parse(selector).raw); logOne(fctxt, 1, JSON.parse(selector).raw);
continue; continue;
} }
} }

View File

@ -34,6 +34,7 @@ import textEncode from './text-encode.js';
import µb from './background.js'; import µb from './background.js';
import * as sfp from './static-filtering-parser.js'; import * as sfp from './static-filtering-parser.js';
import * as fc from './filtering-context.js'; import * as fc from './filtering-context.js';
import { isNetworkURI } from './uri-utils.js';
import { import {
sessionFirewall, sessionFirewall,
@ -41,10 +42,6 @@ import {
sessionURLFiltering, sessionURLFiltering,
} from './filtering-engines.js'; } from './filtering-engines.js';
import {
entityFromDomain,
isNetworkURI,
} from './uri-utils.js';
/******************************************************************************/ /******************************************************************************/
@ -538,28 +535,30 @@ const onHeadersReceived = function(details) {
} }
} }
const bodyFilterSession = bodyFilterer.canFilter(fctxt, details); if ( bodyFilterer.canFilter(fctxt, details) ) {
if ( bodyFilterSession !== undefined ) { const jobs = [];
// `replace=` filter option // `replace=` filter option
const replaceDirectives = const replaceDirectives =
staticNetFilteringEngine.matchAndFetchModifiers(fctxt, 'replace'); staticNetFilteringEngine.matchAndFetchModifiers(fctxt, 'replace');
if ( replaceDirectives ) { if ( replaceDirectives ) {
bodyFilterSession.addJob({ jobs.push({
fn: textResponseFilterer, fn: textResponseFilterer,
args: [ replaceDirectives ], args: [ replaceDirectives ],
}); });
} }
// html filtering // html filtering
if ( isRootDoc || fctxt.itype === fctxt.SUB_FRAME ) { if ( isRootDoc || fctxt.itype === fctxt.SUB_FRAME ) {
const selectors = htmlFilteringEngine.retrieve(bodyFilterSession); const selectors = htmlFilteringEngine.retrieve(fctxt);
if ( selectors ) { if ( selectors ) {
bodyFilterSession.addJob({ jobs.push({
fn: htmlResponseFilterer, fn: htmlResponseFilterer,
args: [ selectors ], args: [ selectors ],
}); });
} }
} }
bodyFilterSession.launch(); if ( jobs.length !== 0 ) {
bodyFilterer.doFilter(fctxt, jobs);
}
} }
let modifiedHeaders = false; let modifiedHeaders = false;
@ -698,6 +697,8 @@ const bodyFilterer = (( ) => {
const MAX_BUFFER_LENGTH = 3 * 1024 * 1024; const MAX_BUFFER_LENGTH = 3 * 1024 * 1024;
let textDecoder, textEncoder; let textDecoder, textEncoder;
let mime = '';
let charset = '';
const contentTypeFromDetails = details => { const contentTypeFromDetails = details => {
switch ( details.type ) { switch ( details.type ) {
@ -863,16 +864,15 @@ const bodyFilterer = (( ) => {
}; };
return class Session extends µb.FilteringContext { return class Session extends µb.FilteringContext {
constructor(fctxt, mime, charset) { constructor(fctxt, mime, charset, jobs) {
super(fctxt); super(fctxt);
this.entity = entityFromDomain(this.getDomain());
this.stream = null; this.stream = null;
this.buffer = null; this.buffer = null;
this.mime = mime; this.mime = mime;
this.charset = charset; this.charset = charset;
this.str = null; this.str = null;
this.modified = false; this.modified = false;
this.jobs = []; this.jobs = jobs;
} }
getString() { getString() {
if ( this.str !== null ) { return this.str; } if ( this.str !== null ) { return this.str; }
@ -891,17 +891,14 @@ const bodyFilterer = (( ) => {
this.str = s; this.str = s;
this.modified = true; this.modified = true;
} }
addJob(job) { static doFilter(fctxt, jobs) {
this.jobs.push(job); if ( jobs.length === 0 ) { return; }
} const session = new Session(fctxt, mime, charset, jobs);
launch() { session.stream = browser.webRequest.filterResponseData(session.id);
if ( this.jobs.length === 0 ) { return; } session.stream.ondata = onStreamData;
this.stream = browser.webRequest.filterResponseData(this.id); session.stream.onstop = onStreamStop;
this.stream.ondata = onStreamData; session.stream.onerror = onStreamError;
this.stream.onstop = onStreamStop; sessions.set(session.stream, session);
this.stream.onerror = onStreamError;
sessions.set(this.stream, this);
return true;
} }
static canFilter(fctxt, details) { static canFilter(fctxt, details) {
if ( µb.canFilterResponseData !== true ) { return; } if ( µb.canFilterResponseData !== true ) { return; }
@ -925,12 +922,11 @@ const bodyFilterer = (( ) => {
const headers = details.responseHeaders; const headers = details.responseHeaders;
const disposition = headerValueFromName('content-disposition', headers); const disposition = headerValueFromName('content-disposition', headers);
if ( disposition !== '' ) { if ( disposition !== '' ) {
if ( disposition.startsWith('inline') === false ) { if ( disposition.startsWith('inline') === false ) { return; }
return;
}
} }
let mime = 'text/plain', charset = 'utf-8'; mime = 'text/plain';
charset = 'utf-8';
const contentType = headerValueFromName('content-type', headers) || const contentType = headerValueFromName('content-type', headers) ||
contentTypeFromDetails(details); contentTypeFromDetails(details);
if ( contentType !== '' ) { if ( contentType !== '' ) {
@ -949,7 +945,7 @@ const bodyFilterer = (( ) => {
if ( otherValidMimes.has(mime) === false ) { return; } if ( otherValidMimes.has(mime) === false ) { return; }
} }
return new Session(fctxt, mime, charset); return true;
} }
}; };
})(); })();