Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Fix #2682: Make getBaseDomain use LRU cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy Bruns committed Jul 24, 2016
1 parent f8ef2c4 commit d6ce718
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function registerForBeforeSendHeaders (session) {
requestHeaders['Cookie'] = undefined
}
if (requestHeaders['Referer'] &&
!refererExceptions.includes(parsedUrl.hostname)) {
!refererExceptions.includes(parsedUrl.hostname) && !refererExceptions.includes(getBaseDomain(parsedUrl.hostname))) {
requestHeaders['Referer'] = undefined
}
}
Expand Down
15 changes: 14 additions & 1 deletion js/lib/baseDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
const punycode = require('punycode')
const publicSuffixes = require('./psl')

const LRUCache = require('lru_cache/core').LRUCache

let cachedBaseDomain = new LRUCache(50)

/**
* Returns base domain for specified host based on Public Suffix List.
* @param {string} hostname The name of the host to get the base domain for
*/

module.exports.getBaseDomain = function (hostname) {
// decode punycode if exists
if (hostname.indexOf('xn--') >= 0) {
hostname = punycode.toUnicode(hostname)
}

let baseDomain = cachedBaseDomain.get(hostname)
if (baseDomain) {
return baseDomain
}

// search through PSL
var prevDomains = []
var curDomain = hostname
Expand Down Expand Up @@ -44,5 +54,8 @@ module.exports.getBaseDomain = function (hostname) {
tld--
}

return curDomain
baseDomain = curDomain
cachedBaseDomain.put(baseDomain)

return baseDomain
}

0 comments on commit d6ce718

Please sign in to comment.