From 1f345b585f2e4c999dcd307afc5679fa65489275 Mon Sep 17 00:00:00 2001 From: gorhill Date: Fri, 22 Jan 2016 11:13:29 -0500 Subject: [PATCH] this should fix #1276 --- platform/chromium/vapi-background.js | 15 ++++------ platform/firefox/vapi-background.js | 14 ---------- src/js/traffic.js | 41 ++++++++++++---------------- src/js/uritools.js | 8 ++++++ 4 files changed, 31 insertions(+), 47 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index b2737fdab..2f819c370 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -834,37 +834,34 @@ vAPI.net.registerListeners = function() { }; var normalizeRequestDetails = function(details) { - µburi.set(details.url); - details.tabId = details.tabId.toString(); - details.hostname = µburi.hostnameFromURI(details.url); // The rest of the function code is to normalize type if ( details.type !== 'other' ) { return; } - var tail = µburi.path.slice(-6); - var pos = tail.lastIndexOf('.'); + var path = µburi.pathFromURI(details.url); + var pos = path.indexOf('.', path.length - 6); // https://github.com/chrisaljoudi/uBlock/issues/862 // If no transposition possible, transpose to `object` as per // Chromium bug 410382 (see below) if ( pos !== -1 ) { - var ext = tail.slice(pos) + '.'; - if ( '.eot.ttf.otf.svg.woff.woff2.'.indexOf(ext) !== -1 ) { + var needle = path.slice(pos) + '.'; + if ( '.eot.ttf.otf.svg.woff.woff2.'.indexOf(needle) !== -1 ) { details.type = 'font'; return; } - if ( '.mp3.mp4.webm.'.indexOf(ext) !== -1 ) { + if ( '.mp3.mp4.webm.'.indexOf(needle) !== -1 ) { details.type = 'media'; return; } // Still need this because often behind-the-scene requests are wrongly // categorized as 'other' - if ( '.ico.png.gif.jpg.jpeg.webp.'.indexOf(ext) !== -1 ) { + if ( '.ico.png.gif.jpg.jpeg.webp.'.indexOf(needle) !== -1 ) { details.type = 'image'; return; } diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index 36b8f7f3d..6d7449838 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -2022,15 +2022,8 @@ var httpObserver = { return false; } - // https://github.com/gorhill/uBlock/issues/966 - var hostname = URI.asciiHost; - if ( hostname.endsWith('.') ) { - hostname = hostname.slice(0, -1); - } - var result = this.onBeforeRequest({ frameId: details.frameId, - hostname: hostname, parentFrameId: details.parentFrameId, tabId: details.tabId, type: type, @@ -2084,14 +2077,7 @@ var httpObserver = { } } - // https://github.com/gorhill/uBlock/issues/966 - var hostname = URI.asciiHost; - if ( hostname.endsWith('.') ) { - hostname = hostname.slice(0, -1); - } - var result = this.onHeadersReceived({ - hostname: hostname, parentFrameId: channelData[1], responseHeaders: responseHeaders, tabId: channelData[2], diff --git a/src/js/traffic.js b/src/js/traffic.js index 5005fc6e2..15d38be56 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -38,9 +38,6 @@ var exports = {}; // Intercept and filter web requests. var onBeforeRequest = function(details) { - //console.debug('µBlock.webRequest/onBeforeRequest(): "%s": %o', details.url, details); - //console.debug('µBlock.webRequest/onBeforeRequest(): "type=%s, id=%d, parent id=%d, url=%s', details.type, details.frameId, details.parentFrameId, details.url); - // Special handling for root document. // https://github.com/chrisaljoudi/uBlock/issues/1001 // This must be executed regardless of whether the request is @@ -84,7 +81,7 @@ var onBeforeRequest = function(details) { // Setup context and evaluate var requestURL = details.url; requestContext.requestURL = requestURL; - requestContext.requestHostname = details.hostname; + requestContext.requestHostname = µb.URI.hostnameFromURI(requestURL); requestContext.requestType = requestType; var result = pageStore.filterRequest(requestContext); @@ -107,8 +104,6 @@ var onBeforeRequest = function(details) { // Not blocked if ( µb.isAllowResult(result) ) { - //console.debug('traffic.js > onBeforeRequest(): ALLOW "%s" (%o) because "%s"', details.url, details, result); - // https://github.com/chrisaljoudi/uBlock/issues/114 frameId = details.frameId; if ( frameId > 0 ) { @@ -123,7 +118,6 @@ var onBeforeRequest = function(details) { } // Blocked - //console.debug('traffic.js > onBeforeRequest(): BLOCK "%s" (%o) because "%s"', details.url, details, result); // https://github.com/chrisaljoudi/uBlock/issues/905#issuecomment-76543649 // No point updating the badge if it's not being displayed. @@ -165,8 +159,9 @@ var onBeforeRootFrameRequest = function(details) { // https://github.com/chrisaljoudi/uBlock/issues/1001 // This must be executed regardless of whether the request is // behind-the-scene - var requestHostname = details.hostname; - var requestDomain = µb.URI.domainFromHostname(requestHostname) || requestHostname; + var µburi = µb.URI; + var requestHostname = µburi.hostnameFromURI(requestURL); + var requestDomain = µburi.domainFromHostname(requestHostname) || requestHostname; var context = { rootHostname: requestHostname, rootDomain: requestDomain, @@ -286,8 +281,6 @@ var toBlockDocResult = function(url, hostname, result) { // Intercept and filter behind-the-scene requests. var onBeforeBehindTheSceneRequest = function(details) { - //console.debug('traffic.js > onBeforeBehindTheSceneRequest(): "%s": %o', details.url, details); - var µb = µBlock; var pageStore = µb.pageStoreFromTabId(vAPI.noTabId); if ( !pageStore ) { @@ -295,8 +288,9 @@ var onBeforeBehindTheSceneRequest = function(details) { } var context = pageStore.createContextFromPage(); - context.requestURL = details.url; - context.requestHostname = details.hostname; + var requestURL = details.url; + context.requestURL = requestURL; + context.requestHostname = µb.URI.hostnameFromURI(requestURL); context.requestType = details.type; // Blocking behind-the-scene requests can break a lot of stuff: prevent @@ -316,7 +310,7 @@ var onBeforeBehindTheSceneRequest = function(details) { 'net', result, details.type, - details.url, + requestURL, context.rootHostname, context.rootHostname ); @@ -324,13 +318,10 @@ var onBeforeBehindTheSceneRequest = function(details) { // Not blocked if ( µb.isAllowResult(result) ) { - //console.debug('traffic.js > onBeforeBehindTheSceneRequest(): ALLOW "%s" (%o) because "%s"', details.url, details, result); return; } // Blocked - //console.debug('traffic.js > onBeforeBehindTheSceneRequest(): BLOCK "%s" (%o) because "%s"', details.url, details, result); - return { 'cancel': true }; }; @@ -367,8 +358,9 @@ var onHeadersReceived = function(details) { var onRootFrameHeadersReceived = function(details) { var µb = µBlock; var tabId = details.tabId; + var requestURL = details.url; - µb.tabContextManager.push(tabId, details.url); + µb.tabContextManager.push(tabId, requestURL); // Lookup the page store associated with this tab id. var pageStore = µb.pageStoreFromTabId(tabId); @@ -378,8 +370,8 @@ var onRootFrameHeadersReceived = function(details) { // I can't think of how pageStore could be null at this point. var context = pageStore.createContextFromPage(); - context.requestURL = details.url; - context.requestHostname = details.hostname; + context.requestURL = requestURL; + context.requestHostname = µb.URI.hostnameFromURI(requestURL); context.requestType = 'inline-script'; var result = pageStore.filterRequestNoCache(context); @@ -392,7 +384,7 @@ var onRootFrameHeadersReceived = function(details) { 'net', result, 'inline-script', - details.url, + requestURL, context.rootHostname, context.pageHostname ); @@ -423,8 +415,9 @@ var onFrameHeadersReceived = function(details) { // Frame id of frame request is their own id, while the request is made // in the context of the parent. var context = pageStore.createContextFromFrameId(details.parentFrameId); - context.requestURL = details.url; - context.requestHostname = details.hostname; + var requestURL = details.url; + context.requestURL = requestURL; + context.requestHostname = µb.URI.hostnameFromURI(requestURL); context.requestType = 'inline-script'; var result = pageStore.filterRequestNoCache(context); @@ -437,7 +430,7 @@ var onFrameHeadersReceived = function(details) { 'net', result, 'inline-script', - details.url, + requestURL, context.rootHostname, context.pageHostname ); diff --git a/src/js/uritools.js b/src/js/uritools.js index bdc23e14f..9c4c74303 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -50,6 +50,7 @@ var reRFC3986 = /^([^:\/?#]+:)?(\/\/[^\/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?/; var reSchemeFromURI = /^[^:\/?#]+:/; var reAuthorityFromURI = /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/; var reCommonHostnameFromURL = /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//; +var rePathFromURI = /^(?:[^:\/?#]+:)?(?:\/\/[^\/?#]*)?([^?#]*)/; // These are to parse authority field, not parsed by above official regex // IPv6 is seen as an exception: a non-compatible IPv6 is first tried, and @@ -303,6 +304,13 @@ var psl = publicSuffixList; /******************************************************************************/ +URI.pathFromURI = function(uri) { + var matches = rePathFromURI.exec(uri); + return matches !== null ? matches[1] : ''; +}; + +/******************************************************************************/ + // Trying to alleviate the worries of looking up too often the domain name from // a hostname. With a cache, uBlock benefits given that it deals with a // specific set of hostnames within a narrow time span -- in other words, I