3
Copyright Ā© 2008-2011 WOT Services Oy <info@mywot.com>
5
This file is part of WOT.
7
WOT is free software: you can redistribute it and/or modify it
8
under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
12
WOT is distributed in the hope that it will be useful, but WITHOUT
13
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15
License for more details.
17
You should have received a copy of the GNU General Public License
18
along with WOT. If not, see <http://www.gnu.org/licenses/>.
21
const WOT_PREFIX_SHARED = "wot_shared";
25
set: "abcdefghijklmnopqrstuvwxyz234567",
28
load_delayed: function()
33
dump("wot_shared.load: failed with " + e + "\n");
37
parse: function(shared)
43
for (i = 0; i < shared.length; ++i) {
44
var attr = shared[i].attributes.getNamedItem(
45
WOT_SERVICE_XML_UPDATE_SHARED_LEVEL);
47
if (!attr || !attr.nodeValue) {
51
var level = Number(attr.nodeValue);
57
var attr = shared[i].attributes.getNamedItem(
58
WOT_SERVICE_XML_UPDATE_SHARED_DOMAINS);
60
if (!attr || !attr.nodeValue) {
68
data[level] = data[level].concat(attr.nodeValue.split(","));
72
wot_prefs.setChar("shared." + i, data[i].join(","));
77
dump("wot_shared.parse: failed with " + e + "\n");
84
var branch = wot_prefs.ps.getBranch(WOT_PREF + "shared.");
85
var children = branch.getChildList("", {});
87
for (var i = 0; i < children.length; ++i) {
88
var level = Number(children[i]);
94
var data = wot_prefs.getChar("shared." + children[i]);
96
if (!data || !data.length) {
100
var domains = data.split(",");
102
for (var j = 0; j < domains.length; ++j) {
103
if (!domains[j].length) {
107
var pn = wot_idn.utftoidn(domains[j]);
113
wot_hashtable.set(WOT_PREFIX_SHARED + ":" + pn, level);
117
dump("wot_shared.load: failed with " + e + "\n");
121
isshared: function(host)
124
return wot_hashtable.get(WOT_PREFIX_SHARED + ":" + host);
126
dump("wot_shared.isshared: failed with " + e + "\n");
132
isencodedhostname: function(host)
135
return /^_p_[a-z2-7]+\..+$/.test(host);
137
dump("url.isencodedhostname: failed with " + e + "\n");
143
encodehostname: function(host, path)
146
if (!host || !path) {
150
/* Clean up the path, drop query string and hash */
151
path = path.replace(/^\s+/, "")
153
.replace(/[\?#].*$/, "");
155
if (path.length < 2 || path[0] != "/") {
159
var h = wot_idn.utftoidn(host);
165
var c = path.split("/");
167
if (!c || !c.length) {
171
/* Drop a suspected filename from the end */
172
if (path[path.length - 1] != "/" &&
173
/\.[^\.]{1,6}$/.test(c[c.length - 1])) {
179
for (var i = c.length; !level && i > 0; --i) {
180
level = this.isshared(h + c.slice(0, i).join("/"));
187
var p = c.slice(0, level + 1).join("/").replace(/^\//, "");
189
if (!p || !p.length) {
193
var encoded = this.base32encode(p);
195
if (encoded == null) {
199
return "_p_" + encoded + "." + host;
201
dump("wot_shared.encodehostname: failed with " + e + "\n");
207
decodehostname: function(host)
210
var m = /^_p_([a-z2-7]+)\.(.+)$/.exec(host);
212
if (!m || !m[1] || !m[2]) {
216
var decoded = this.base32decode(m[1]);
218
if (decoded == null) {
222
return m[2] + "/" + decoded;
224
dump("wot_shared.decodehostname: failed with " + e + "\n");
230
base32encode: function(s)
233
/* Unicode to UTF-8 */
234
s = unescape(encodeURIComponent(decodeURIComponent(s)));
240
for (var i = 0; i < s.length; ++i) {
241
var n = s.charCodeAt(i);
244
return null; /* Invalid input */
252
r += this.set[(b >> l) & 0x1F];
257
r += this.set[(b << (5 - l)) & 0x1F];
262
dump("wot_shared.base32encode: failed with " + e + "\n");
268
base32decode: function(s)
271
/* Build a reverse lookup table */
275
for (var i = 0; i < this.set.length; ++i) {
276
this.rev[this.set.charAt(i)] = i;
284
for (var i = 0; i < s.length; ++i) {
285
var n = this.rev[s.charAt(i)];
288
return null; /* Invalid input */
296
r += String.fromCharCode((b >> l) & 0xFF);
301
return null; /* Invalid input */
304
/* UTF-8 to Unicode */
305
return decodeURIComponent(escape(r));
307
dump("wot_shared.base32decode: failed with " + e + "\n");
314
wot_modules.push({ name: "wot_shared", obj: wot_shared });