diff options
author | Ruben Rodriguez <ruben@gnu.org> | 2015-11-28 15:24:36 -0600 |
---|---|---|
committer | Ruben Rodriguez <ruben@gnu.org> | 2015-11-28 16:27:06 -0600 |
commit | e4a3586a14996bbece3b26c9e3b7704ea6af8615 (patch) | |
tree | 499bdd16b3a90c30b01e4b47a5882d13b4800f50 /data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer | |
parent | 4dbc2fae927bb02ef243c87938e638af9afee8fa (diff) |
LibreJS upgraded to 6.0.10
Diffstat (limited to 'data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer')
5 files changed, 834 insertions, 0 deletions
diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/allowed_referrers.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/allowed_referrers.js new file mode 100644 index 0000000..06d860b --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/allowed_referrers.js @@ -0,0 +1,69 @@ +/** + * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. + * * + * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros + * Copyright (C) 2014, 2015 Nik Nyby + * + * This file is part of GNU LibreJS. + * + * GNU LibreJS 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. + * + * GNU LibreJS 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 GNU LibreJS. If not, see <http://www.gnu.org/licenses/>. + */ + +var prefChange = require("../addon_management/prefchange"); + +var allowed = {}; + +/** + * Contains a list of pages that are allowed + * to execute JavaScript regardless of whether it is + * nonfree and nontrivial. + */ +var AllowedReferrers = function() { +}; + +AllowedReferrers.prototype.addPage = function(url) { + allowed[url] = 1; +}; + +AllowedReferrers.prototype.urlInAllowedReferrers = function (url) { + if (allowed[url] === 1) { + return true; + } + // check if whitelisted. + return this.urlInWhitelist(url); +}; + +AllowedReferrers.prototype.urlInWhitelist = function(url) { + var whitelist = prefChange.getWhitelist(); + var i = 0, le = whitelist.length; + for (; i < le; i++) { + if (whitelist[i].test(url)) { + return true; + } + } +}; + +AllowedReferrers.prototype.clearSinglePageEntry = function(url) { + var index = allowed[url]; + + if (allowed[url] === 1) { + delete allowed[url]; + } +}; + +AllowedReferrers.prototype.clearAllEntries = function() { + allowed = {}; +}; + +exports.allowedReferrers = new AllowedReferrers(); diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/caching.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/caching.js new file mode 100644 index 0000000..285f95a --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/caching.js @@ -0,0 +1,32 @@ +/** + * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. + * * + * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros + * Copyright (C) 2014, 2015 Nik Nyby + * + * This file is part of GNU LibreJS. + * + * GNU LibreJS 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. + * + * GNU LibreJS 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 GNU LibreJS. If not, see <http://www.gnu.org/licenses/>. + */ + +var {Cc, Ci, Cu, Cm, Cr} = require("chrome"); + +const nsICacheService = Ci.nsICacheService; +const cacheService = Cc["@mozilla.org/network/cache-service;1"].getService(nsICacheService); + +exports.clearAllCache = function () { + cacheService.evictEntries(Ci.nsICache.STORE_ON_DISK); + cacheService.evictEntries(Ci.nsICache.STORE_IN_MEMORY); +}; + diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/http_request_observer.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/http_request_observer.js new file mode 100644 index 0000000..0b6e14d --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/http_request_observer.js @@ -0,0 +1,149 @@ +/** + * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. + * * + * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros + * Copyright (C) 2014, 2015 Nik Nyby + * + * This file is part of GNU LibreJS. + * + * GNU LibreJS 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. + * + * GNU LibreJS 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 GNU LibreJS. If not, see <http://www.gnu.org/licenses/>. + */ + +var {Cc, Ci, Cu, Cm, Cr} = require("chrome"); + +var observerService = Cc["@mozilla.org/observer-service;1"] + .getService(Ci.nsIObserverService); + +// these are our target mime types for response interception. +var targetMimeTypes = /.*(javascript|ecmascript|html).*/i; +//var targetMimeTypes = /.*(html).*/i; + +// ensure xhr won't create an infinite loop +// with html content. +var urlTester = require("../html_script_finder/url_seen_tester") + .urlSeenTester; +var streamLoader = require("../http_observer/stream_loader").streamLoader; + +var httpRequestObserver = { + observe: function(request, aTopic, aData) { + console.debug('atopic is', aTopic); + var url, newListener, status; + + if (aTopic === "http-on-examine-response" || + aTopic === "http-on-examine-cached-response" || + aTopic === "http-on-examine-merged-response" + ) { + request.QueryInterface(Ci.nsIHttpChannel); + + if (request.URI.scheme !== 'chrome' && + (request.responseStatus < 300 || + request.responseStatus > 399) && + (targetMimeTypes.test(request.contentType) || + request.contentType === undefined) && + (!urlTester.isWhitelisted(request.URI.spec) && + !urlTester.isWhitelisted(request.originalURI.spec)) + ) { + newListener = new TracingListener(); + request.QueryInterface(Ci.nsITraceableChannel); + newListener.originalListener = request.setNewListener(newListener); + } else if (urlTester.isWhitelisted(request.URI.spec) || + urlTester.isWhitelisted(request.originalURI.spec) + ) { + urlTester.clearUrl(request.URI.spec); + urlTester.clearUrl(request.originalURI.spec); + } + + } + }, + + QueryInterface: function (aIID) { + if (aIID.equals(Ci.nsIObserver) || + aIID.equals(Ci.nsISupports) + ) { + return this; + } + throw Cr.NS_NOINTERFACE; + } +}; + +// Copy response listener implementation. +function TracingListener() { + this.originalListener = null; + this.streamLoader = streamLoader(); +} + +TracingListener.prototype = { + onDataAvailable: function(request, context, inputStream, offset, count) { + try { + this.streamLoader.loader.onDataAvailable( + request, context, inputStream, offset, count); + } catch (x) { + console.debug(x, x.lineNumber, x.fileName, "In this case, charset is"); + } + }, + + onStartRequest: function(request, context) { + this.streamLoader.setOriginalListener(this.originalListener); + this.streamLoader.loader.onStartRequest(request, context); + this.originalListener.onStartRequest(request, context); + }, + + onStopRequest: function(request, context, statusCode) { + try { + this.streamLoader.loader.onStopRequest(request, context, statusCode); + } catch (e) { + console.debug('error in onStopRequest', e, e.lineNumber); + } + }, + + QueryInterface: function (aIID) { + if (aIID.equals(Ci.nsIStreamListener) || + aIID.equals(Ci.nsISupports) + ) { + return this; + } + throw Cr.NS_NOINTERFACE; + }, +}; + +exports.startHttpObserver = function() { + try { + observerService.addObserver(httpRequestObserver, + "http-on-examine-response", false); + observerService.addObserver(httpRequestObserver, + "http-on-examine-cached-response", false); + observerService.addObserver(httpRequestObserver, + "http-on-examine-merged-response", false); + console.debug('turned on http observer'); + } catch (e) { + console.debug(e); + } +}; + +exports.startHttpObserver(); + +/* remove observer */ +exports.removeHttpObserver = function() { + try { + observerService.removeObserver(httpRequestObserver, + "http-on-examine-response"); + observerService.removeObserver(httpRequestObserver, + "http-on-examine-cached-response"); + observerService.removeObserver(httpRequestObserver, + "http-on-examine-merged-response"); + console.debug('turned off http observer'); + } catch (e) { + console.debug(e); + } +}; diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/process_response.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/process_response.js new file mode 100644 index 0000000..72f236c --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/process_response.js @@ -0,0 +1,424 @@ +/** + * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. + * * + * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros + * Copyright (C) 2014, 2015 Nik Nyby + * + * This file is part of GNU LibreJS. + * + * GNU LibreJS 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. + * + * GNU LibreJS 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 GNU LibreJS. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * This module checks http responses by mime type and returns a + * modified response. + */ + +var {Cc, Ci, Cu, Cm, Cr} = require("chrome"); + +var jsChecker = require("../js_checker/js_checker"); + +const types = require("../js_checker/constant_types"); +var checkTypes = types.checkTypes; + +// check if scripts embedded dynamically have a jsWebLabel entry indexed by referrer. +var jsWebLabelEntries = require("../html_script_finder/web_labels/js_web_labels") + .jsWebLabelEntries; + +var htmlParser = require("../html_script_finder/html_parser"); + +var removedScripts = require("../script_entries/removed_scripts") + .removedScripts; +var allowedRef = require('../http_observer/allowed_referrers') + .allowedReferrers; + +var acceptedScripts = require("../script_entries/accepted_scripts") + .acceptedScripts; + +// used to display info when a url is whitelisted. +var dryRunScripts = require("../script_entries/dryrun_scripts").dryRunScripts; + +// node.js url module. Makes it easier to resolve +// urls in that datauri loaded dom +var urlHandler = require("../url_handler/url_handler"); +var isDryRun = require("../addon_management/prefchange").isDryRun; + +var jsMimeTypeRe = /.*(javascript|ecmascript).*/i; +var htmlMimeTypeRe = /.*(xhtml\+xml|html|multipart\/x-mixed-replace).*/i; + + +var processResponseObject = { + data: null, + myParser: null, + url: null, + scriptFinder: null, + jsCheckString: null, + referrer: null, + contentType: null, + resInfo: null, + listener: null, + req: null, + + /** + * starts the handling of a new response. + */ + init: function (listener, resInfo) { + this.resInfo = resInfo; + this.req = resInfo.request; + /* needed for this.req.referrer */ + this.req.QueryInterface(Ci.nsIHttpChannel); + this.listener = listener; + this.setData(); + this.setContentType(); + this.setUrls(); + }, + + /** + * Gather the data gathered from onDataAvailable. + */ + setData: function () { + this.data = this.resInfo.receivedData; + //console.debug("\n\nDump of whole data:\n\n", this.data, "\n\n end of dump"); + // Prevents the http response body from being empty, + // which would throw an error. + if (this.data == '' || this.data == undefined) { + this.data = " "; + } + }, + + /** + * Set a standardized lowercase mime type. + */ + setContentType: function() { + if (this.req.contentType != undefined) { + this.contentType = String(this.req.contentType).toLowerCase(); + } + }, + + /** + * setUrls + * Set the current URL of the response, and + * set referrer if applicable. + */ + setUrls: function() { + + if (this.req.URI != undefined) { + this.fragment = urlHandler.getFragment(this.req.URI.spec); + console.debug('fragment is', this.fragment); + this.url = urlHandler.removeFragment(this.req.URI.spec); + } + if (this.req.referrer != undefined) { + this.referrerFragment = urlHandler.getFragment(this.req.referrer.spec); + this.referrer = urlHandler.removeFragment(this.req.referrer.spec); + } + }, + + /** + * processHTML + * Modifies a string of html + */ + processHTML: function() { + var charset = this.req.contentCharset, myParser; + + if (this.req.contentCharset != undefined && this.req.contentCharset != "") { + charset = this.req.contentCharset; + } else { + charset = ""; + } + acceptedScripts.clearScripts(this.req.URI.spec); + removedScripts.clearScripts(this.req.URI.spec); + dryRunScripts.clearScripts(this.req.URI.spec); + + console.debug('charset is', charset); + console.debug( + 'responseStatus for', this.url, 'is', this.req.responseStatus); + + // send data to htmlParser, and pass on modified data to + // originalListener. + + myParser = htmlParser.htmlParser().parse( + this.data, + charset, + this.contentType, + this.url, + this.fragment, + this.req.responseStatus, + this.htmlParseCallback.bind(this)); + }, + + /** + * + * htmlParseCallback + * + * Passed on the callback result to + * the originalListener. + * + */ + htmlParseCallback: function(result) { + + var len = result.length; + + try { + + this.listener.onDataAvailable( + this.req, + this.resInfo.context, + result.newInputStream(0), 0, len); + + + } catch (e) { + + this.req.cancel(this.req.NS_BINDING_ABORTED); + + } + + this.listener.onStopRequest( + this.req, + this.resInfo.context, this.resInfo.statusCode); + + }, + + /** + * processJS + * Process and modify a string of JavaScript. + */ + processJS: function() { + var checker, check, jsCheckString, + that = this; + + try { + // make sure script isn't already listed as free + // in a JS web labels table. + if (this.checkJsWebLabelsForScript()) { + // this is free. we are done. + this.jsListenerCallback(); + return; + } + + // analyze javascript in response. + checker = jsChecker.jsChecker(); + check = checker.searchJs(this.data, function () { + //console.debug("Has been analyzing", that.data); + that.processJsCallback(checker); + }, that.url); + + } catch(e) { + + // any error is considered nontrivial. + console.debug('js error in js app, removing script', e); + console.debug("error", e, e.lineNumber); + // modify data that will be sent to the browser. + this.data = '// LibreJS: Script contents were removed when it was loaded from a page, because another script attempted to load this one dynamically. Please place your cursor in the url bar and press the enter key to see the source.'; + this.jsListenerCallback(); + } + + }, + + /** + * checkJsWebLabelsForScript + * + * check whether script that's been received has an entry + * in a js web labels table (lookup referrer.) + * + */ + checkJsWebLabelsForScript: function () { + + console.debug('checking script', this.url); + console.debug('current list is', JSON.stringify(jsWebLabelEntries)); + if (jsWebLabelEntries[this.referrer] != undefined) { + + var scriptList = jsWebLabelEntries[this.referrer], + i = 0, + len = scriptList.length; + + for (; i < len; i++) { + + if (scriptList[i].fileUrl === this.url && + scriptList[i].free === true) { + + console.debug(this.url, "is free and dynamic!"); + + var scriptObj = { + inline: false, + url: this.url, + contents: this.url, + reason: "This script is free (see JS Web Labels page for detail)" + }; + + acceptedScripts.addAScript( + this.req.referrer.spec, scriptObj, "Script is free"); + + return true; + + } + } + } + }, + + processJsCallback: function(checker) { + try { + var scriptObj; + + var jsCheckString = checker.parseTree.freeTrivialCheck; + console.debug("analyzing js callback for", this.url); + // for testing only. + //var jsCheckString = {'type': checkTypes.FREE_SINGLE_ITEM }; + console.debug('jscheckstring is', jsCheckString.type); + + if (jsCheckString.type === checkTypes.NONTRIVIAL) { + if (!allowedRef.urlInAllowedReferrers(this.req.referrer.spec)) { + //if (true) { + console.debug( + "url", + this.url, + " is found nontrivial", + "with reason", + jsCheckString.reason); + scriptObj = { + inline: false, + contents: '', + removalReason: 'nontrivial', + reason: jsCheckString.reason, + url: this.url, + hash: checker.hash + }; + removedScripts.addAScript(this.req.referrer.spec, scriptObj); + + // modify data that will be sent to the browser. + this.data = '// LibreJS: Script contents were removed when it was loaded from a page, because another script attempted to load this one dynamically and its contents appear to be nonfree/nontrivial. Please hit enter in the location bar to see the actual source.'; + } else { + console.debug("writing to dry run", this.url); + scriptObj = { + inline:false, + contents: '', + removalReason: 'nontrivial', + reason: jsCheckString.reason, + url: this.url, + hash:checker.hash + }; + dryRunScripts.addAScript(this.req.referrer.spec, scriptObj); + } + + this.jsListenerCallback(); + + } else if (jsCheckString.type === checkTypes.FREE || + jsCheckString.type === checkTypes.FREE_SINGLE_ITEM || + jsCheckString.type === checkTypes.TRIVIAL || + jsCheckString.type === checkTypes.TRIVIAL_DEFINES_FUNCTION || + jsCheckString.type === checkTypes.WHITELISTED) { + console.debug( + "found a free script for", + this.url, + this.req.referrer.spec, + jsCheckString.reason); + console.debug('found a free script', this.req.referrer.spec); + + scriptObj = {inline: false, + contents: '', + reason: jsCheckString.reason, + url: this.url, + hash:checker.hash}; + + acceptedScripts.addAScript(this.req.referrer.spec, scriptObj); + this.jsListenerCallback(); + } + + //var end = Date.now(); + console.debug('exec time', this.url, ' -- ', end - start); + } catch (x) { + console.debug('error', x); + } + }, + + /** + * ProcessAllTypes + * Calls processHTML or JS if it finds an appropriate content + * type. For everything else it just passes on the data to the + * original listener. + */ + processAllTypes: function() { + // toggle xlibrejs if X-LibreJS is set. + + // process HTML + if ((htmlMimeTypeRe.test(this.contentType) || + this.req.contentType === undefined)) { + this.processHTML(); + return; + } + + else { + // process external JS files that are called from another + // file (and hence have a referrer). + + if (this.referrer != undefined && + jsMimeTypeRe.test(this.contentType) && + !(acceptedScripts.isFound(this.referrer, { + inline: false, contents: this.url})) && + !(acceptedScripts.isFound(this.referrer, { + inline:false, contents:this.req.originalURI.spec}))) { + + // console.debug('process js triggered for', this.url); + this.processJS(); + + } else { + this.jsListenerCallback(); + } + + } + + }, + + jsListenerCallback: function () { + + var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] + .createInstance(Ci.nsIScriptableUnicodeConverter); + + if (typeof this.req.contentCharset !== 'undefined' && + this.req.contentCharset !== '' && + this.req.contentCharset !== null + ) { + converter.charset = this.req.contentCharset; + } else { + converter.charset = "UTF-8"; + } + + var stream = converter.convertToInputStream(this.data); + + try { + this.listener.onDataAvailable( + this.req, + this.resInfo.context, + stream, + 0, stream.available()); + } catch (e) { + this.req.cancel(this.req.NS_BINDING_ABORTED); + } + + this.listener.onStopRequest( + this.req, + this.resInfo.context, + this.resInfo.statusCode); + + } + + +}; + +// creates an instance of processResponseObject. +exports.ProcessResponse = function (listener, resInfo) { + console.debug('triggered'); + var procResponse = Object.create(processResponseObject); + procResponse.init(listener, resInfo); + return procResponse; +}; diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/stream_loader.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/stream_loader.js new file mode 100644 index 0000000..c790fe2 --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/lib/http_observer/stream_loader.js @@ -0,0 +1,160 @@ +/** + * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. + * * + * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros + * Copyright (C) 2014, 2015 Nik Nyby + * + * This file is part of GNU LibreJS. + * + * GNU LibreJS 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. + * + * GNU LibreJS 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 GNU LibreJS. If not, see <http://www.gnu.org/licenses/>. + */ +var {Cc, Ci, Cu, Cm, Cr} = require("chrome"); + +const processResponse = require('./process_response'); +const CHARSETS = [ + '866', 'ansi_x3.4-1968', 'arabic', 'ascii', 'asmo-708', 'big5', + 'big5-hkscs', 'chinese', 'cn-big5', 'cp1250', 'cp1251', 'cp1252', + 'cp1253', 'cp1254', 'cp1255', 'cp1256', 'cp1257', 'cp1258', + 'cp819', 'cp866', 'csbig5', 'cseuckr', 'cseucpkdfmtjapanese', + 'csgb2312', 'csibm866', 'csiso2022jp', 'csiso2022kr', 'csiso58gb231280', + 'csiso88596e', 'csiso88596i', 'csiso88598e', 'csiso88598i', 'csisolatin1', + 'csisolatin2', 'csisolatin3', 'csisolatin4', 'csisolatin5', 'csisolatin6', + 'csisolatin9', 'csisolatinarabic', 'csisolatincyrillic', + 'csisolatingreek', 'csisolatinhebrew', 'cskoi8r', 'csksc56011987', + 'csmacintosh', 'csshiftjis', 'cyrillic', 'dos-874', 'ecma-114', + 'ecma-118', 'elot_928', 'euc-jp', 'euc-kr', 'gb18030', 'gb2312', + 'gb_2312', 'gb_2312-80', 'gbk', 'greek', 'greek8', 'hebrew', + 'hz-gb-2312', 'ibm819', 'ibm866', 'iso-2022-cn', 'iso-2022-cn-ext', + 'iso-2022-jp', 'iso-2022-kr', 'iso88591', 'iso_8859-1', 'iso-8859-1', + 'iso8859-1', 'iso885910', 'iso-8859-10', 'iso8859-10', 'iso885911', + 'iso-8859-11', 'iso8859-11', 'iso_8859-1:1987', 'iso885913', 'iso-8859-13', + 'iso8859-13', 'iso885914', 'iso-8859-14', 'iso8859-14', 'iso885915', + 'iso-8859-15', 'iso8859-15', 'iso-8859-16', 'iso88592', 'iso_8859-2', + 'iso-8859-2', 'iso8859-2', 'iso_8859-2:1987', 'iso88593', 'iso_8859-3', + 'iso-8859-3', 'iso8859-3', 'iso_8859-3:1988', 'iso88594', 'iso_8859-4', + 'iso-8859-4', 'iso8859-4', 'iso_8859-4:1988', 'iso88595', 'iso_8859-5', + 'iso-8859-5', 'iso_8859-5:1988', 'iso88596', 'iso_8859-6', 'iso-8859-6', + 'iso8859-6', 'iso_8859-6:1987', 'iso-8859-6-e', 'iso-8859-6-i', 'iso88597', + 'iso_8859-7', 'iso-8859-7', 'iso8859-7', 'iso_8859-7:1987', 'iso88598', + 'iso_8859-8', 'iso-8859-8', 'iso8859-8', 'iso_8859-8:1988', 'iso-8859-8-e', + 'iso-8859-8i', 'iso-8859-8-i', 'iso88599', 'iso_8859-9', 'iso-8859-9', + 'iso8859-9', 'iso_8859-9:1989', 'iso-ir-100', 'iso-ir-101', 'iso-ir-109', + 'iso-ir-110', 'iso-ir-126', 'iso-ir-127', 'iso-ir-138', 'iso-ir-144', + 'iso-ir-148', 'iso-ir-149', 'iso-ir-157', 'iso-ir-58', 'koi', 'koi8', + 'koi8_r', 'koi8-r', 'koi8-u', 'korean', 'ksc5601', 'ksc_5601', + 'ks_c_5601-1987', 'ks_c_5601-1989', 'l1', 'l2', 'l3', 'l4', 'l5', 'l6', + 'l9', 'latin1', 'latin2', 'latin3', 'latin4', 'latin5', 'latin6', 'latin9', + 'logical', 'mac', 'macintosh', 'ms_kanji', 'replacement', 'shift_jis', + 'shift-jis', 'sjis', 'sun_eu_greek', 'tis-620', 'unicode-1-1-utf-8', + 'us-ascii', 'utf-16', 'utf-16be', 'utf-16le', 'utf8', 'utf-8', 'visual', + 'windows-1250', 'windows-1251', 'windows-1252', 'windows-1253', + 'windows-1254', 'windows-1255', 'windows-1256', 'windows-1257', + 'windows-1258', 'windows-31j', 'windows-874', 'windows-949', 'x-cp1250', + 'x-cp1251', 'x-cp1252', 'x-cp1253', 'x-cp1254', 'x-cp1255', 'x-cp1256', + 'x-cp1257', 'x-cp1258', 'x-euc-jp', 'x-gbk', 'x-mac-cyrillic', + 'x-mac-roman', 'x-mac-ukrainian', 'x-sjis', 'x-user-defined', 'x-x-big5' +]; + +var StreamLoader = function() { + this.loader = null; + this.listener = null; + this.originalListener = null; +}; + +StreamLoader.prototype.setOriginalListener = function(listener) { + this.originalListener = listener; +}; + +StreamLoader.prototype.init = function() { + try { + var that = this; + this.listener = new StreamListener(); + + this.listener.callback = function (loader, context, status, data) { + //console.debug("here is the data", data); + var responseInfo = {'request': loader.channel, + 'context': context, + 'statusCode': status, + 'receivedData': data}; + var responseHandler = processResponse.ProcessResponse(that.originalListener, responseInfo); + responseHandler.processAllTypes(); + + that.destroy(); + }; + + this.loader = Cc["@mozilla.org/network/unichar-stream-loader;1"]. + createInstance(Ci.nsIUnicharStreamLoader); + + this.loader.init(this.listener); + } catch (e) { + console.debug(e); + } +}; + +StreamLoader.prototype.destroy = function () { + this.loader = null; + this.listener = null; +}; + +var getRegexForContentType = function (contentType) { + if (/xhtml/i.test(contentType)) { + return /<\?[^>]*?encoding=(?:["']*)([^"'\s\?>]+)(?:["']*)/i; + } + + // return the regular html regexp for anything else. + return /<meta[^>]*?charset=(?:["']*)([^"'\s>]+)(?:["']*)/i; +}; + +var StreamListener = function() {}; + +StreamListener.prototype.QueryInterface = function listener_qi(iid) { + if (iid.equals(Ci.nsISupports) || + iid.equals(Ci.nsIUnicharStreamLoaderObserver)) { + return this; + } + throw Cr.NS_ERROR_NO_INTERFACE; +}; + +StreamListener.prototype.onStreamComplete = function onStreamComplete( + loader, context, status, data) { + this.callback(loader, context, status, data); +}; + +StreamListener.prototype.onDetermineCharset = function onDetermineCharset( + loader, context, data) { + var match, regex; + if (loader.channel.contentCharset !== undefined && + loader.channel.contentCharset !== "" + ) { + return loader.channel.contentCharset; + } else { + match = getRegexForContentType(loader.channel.contentType).exec(data); + if (typeof match !== 'undefined' && + match !== null && + match.length > 0 && + CHARSETS.indexOf(match[1].toLowerCase()) >= 0 + ) { + loader.channel.contentCharset = match[1]; + return match[1]; + } else { + return "UTF-8"; + } + } +}; + +exports.streamLoader = function () { + var l = new StreamLoader(); + l.init(); + return l; +}; |