|
1 | | -Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
2 | | - |
3 | | -function Cookie(s, host) { |
4 | | - this.parse(s, host); |
5 | | -} |
6 | | -Cookie.computeId = function(c) { |
7 | | - return c.name + ";" + c.host + "/" + c.path; |
8 | | -}; |
9 | | -Cookie.find = function(f) { |
10 | | - var cc = Cookie.prototype.cookieManager.enumerator; |
11 | | - var c; |
12 | | - while (cc.hasMoreElements()) { |
13 | | - if (f(c = cc.getNext())) return c; |
14 | | - } |
15 | | - return null; |
16 | | -}; |
17 | | - |
18 | | -Cookie.attributes = { host: 'domain', path: 'path', expires: 'expires', isHttpOnly: 'HttpOnly', isSecure: 'Secure' }; |
19 | | -Cookie.prototype = { |
20 | | - |
21 | | - name: '', |
22 | | - value: '', |
23 | | - source: '', |
24 | | - domain: '', |
25 | | - host: '', |
26 | | - rawHost: '', |
27 | | - path: '', |
28 | | - secure: false, |
29 | | - httponly: false, |
30 | | - session: true, |
31 | | - expires: 0, |
32 | | - |
33 | | - id: '', |
34 | | - |
35 | | - |
36 | | - toString: function() { |
37 | | - var c = [this['name'] + "=" + this.value]; |
38 | | - var v; |
39 | | - const aa = Cookie.attributes; |
40 | | - for (var k in aa) { |
41 | | - var p = aa[k]; |
42 | | - v = this[k]; |
43 | | - switch(typeof(v)) { |
44 | | - case "string": |
45 | | - if (v) c.push(p + "=" + v); |
46 | | - break; |
47 | | - case "boolean": |
48 | | - if (v) c.push(p); |
49 | | - break; |
50 | | - case "number": |
51 | | - if (!this.isSession) c.push(p + "=" + new Date(v * 1000).toUTCString()); |
52 | | - break; |
53 | | - } |
54 | | - } |
55 | | - return c.join("; "); |
56 | | - }, |
57 | | - parse: function(s, host) { |
58 | | - var p; |
59 | | - if (this.source) { |
60 | | - // cleanup for recycle |
61 | | - for (p in this) { |
62 | | - if (typeof (p) != "function") delete this[p]; |
63 | | - } |
64 | | - } |
65 | | - this.source = s; |
66 | | - this.host = host; |
67 | | - |
68 | | - var parts = s.split(/;\s*/); |
69 | | - var nv = parts.shift().split("="); |
70 | | - |
71 | | - this.name = nv.shift() || ''; |
72 | | - this.value = nv.join('=') || ''; |
73 | | - |
74 | | - var n, v; |
75 | | - for each (p in parts) { |
76 | | - nv = p.split("="); |
77 | | - switch (n = nv[0].toLowerCase()) { |
78 | | - case 'expires': |
79 | | - v = Math.round(Date.parse((nv[1] || '').replace(/\-/g, ' ')) / 1000); |
80 | | - break; |
81 | | - case 'domain': |
82 | | - case 'path': |
83 | | - v = nv[1] || ''; |
84 | | - break; |
85 | | - case 'secure': |
86 | | - case 'httponly': |
87 | | - v = true; |
88 | | - break; |
89 | | - default: |
90 | | - n = 'unknown'; |
91 | | - } |
92 | | - this[n] = v; |
93 | | - } |
94 | | - if (!this.expires) { |
95 | | - this.session = true; |
96 | | - this.expires = Math.round(new Date() / 1000) + 31536000; |
97 | | - } |
98 | | - if (this.domain) { |
99 | | - if (!this.isDomain) this.domain = "." + this.domain; |
100 | | - this.host = this.domain; |
101 | | - } |
102 | | - this.rawHost = this.host.replace(/^\./, ''); |
103 | | - |
104 | | - this.id = Cookie.computeId(this); |
105 | | - }, |
106 | | - |
107 | | - |
108 | | - get cookieManager() { |
109 | | - delete Cookie.prototype.cookieManager; |
110 | | - var cman = Cc["@mozilla.org/cookiemanager;1"] |
111 | | - .getService(Ci.nsICookieManager2).QueryInterface(Ci.nsICookieManager); |
112 | | - return Cookie.prototype.cookieManager = cman; |
113 | | - }, |
114 | | - belongsTo: function(host, path) { |
115 | | - if (path && this.path && path.indexOf(this.path) != 0) return false; |
116 | | - if (host == this.rawHost) return true; |
117 | | - var d = this.domain; |
118 | | - return d && (host == d || this.isDomain && host.slice(-d.length) == d); |
119 | | - }, |
120 | | - save: function() { |
121 | | - this.save = ("cookieExists" in this.cookieManager) |
122 | | - ? function() { this.cookieManager.add(this.host, this.path, this.name, this.value, this.secure, this.httponly, this.session, this.expires); } |
123 | | - : function() { this.cookieManager.add(this.host, this.path, this.name, this.value, this.secure, this.session, this.expires);} |
124 | | - ; |
125 | | - return this.save(); |
126 | | - }, |
127 | | - exists: function() { |
128 | | - var cc = this.cookieManager.enumerator; |
129 | | - while(cc.hasMoreElements()) { |
130 | | - if (this.sameAs(cc.getNext())) return true; |
131 | | - } |
132 | | - return false; |
133 | | - }, |
134 | | - |
135 | | - sameAs: function(c) { |
136 | | - (c instanceof Ci.nsICookie) && (c instanceof Ci.nsICookie2); |
137 | | - return Cookie.computeId(c) == this.id; |
138 | | - }, |
139 | | - |
140 | | - // nsICookie2 interface extras |
141 | | - get isSecure() { return this.secure; }, |
142 | | - get expiry() { return this.expires; }, |
143 | | - get isSession() { return this.session; }, |
144 | | - get isHttpOnly() { return this.httponly; }, |
145 | | - get isDomain() { return this.domain && this.domain[0] == '.'; }, |
146 | | - policy: 0, |
147 | | - status: 0, |
148 | | - QueryInterface: XPCOMUtils.generateQI([Ci.nsICookie, Ci.nsICookie2]) |
149 | | - |
150 | | -}; |
| 1 | +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
| 2 | + |
| 3 | +function Cookie(s, host) { |
| 4 | + this.parse(s, host); |
| 5 | +} |
| 6 | +Cookie.computeId = function(c) { |
| 7 | + return c.name + ";" + c.host + "/" + c.path; |
| 8 | +}; |
| 9 | +Cookie.find = function(f) { |
| 10 | + var cc = Cookie.prototype.cookieManager.enumerator; |
| 11 | + var c; |
| 12 | + while (cc.hasMoreElements()) { |
| 13 | + if (f(c = cc.getNext())) return c; |
| 14 | + } |
| 15 | + return null; |
| 16 | +}; |
| 17 | + |
| 18 | +Cookie.attributes = { host: 'domain', path: 'path', expires: 'expires', isHttpOnly: 'HttpOnly', isSecure: 'Secure' }; |
| 19 | +Cookie.prototype = { |
| 20 | + |
| 21 | + name: '', |
| 22 | + value: '', |
| 23 | + source: '', |
| 24 | + domain: '', |
| 25 | + host: '', |
| 26 | + rawHost: '', |
| 27 | + path: '', |
| 28 | + secure: false, |
| 29 | + httponly: false, |
| 30 | + session: true, |
| 31 | + expires: 0, |
| 32 | + |
| 33 | + id: '', |
| 34 | + |
| 35 | + |
| 36 | + toString: function() { |
| 37 | + var c = [this['name'] + "=" + this.value]; |
| 38 | + var v; |
| 39 | + const aa = Cookie.attributes; |
| 40 | + for (var k in aa) { |
| 41 | + var p = aa[k]; |
| 42 | + v = this[k]; |
| 43 | + switch(typeof(v)) { |
| 44 | + case "string": |
| 45 | + if (v) c.push(p + "=" + v); |
| 46 | + break; |
| 47 | + case "boolean": |
| 48 | + if (v) c.push(p); |
| 49 | + break; |
| 50 | + case "number": |
| 51 | + if (!this.isSession) c.push(p + "=" + new Date(v * 1000).toUTCString()); |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + return c.join("; "); |
| 56 | + }, |
| 57 | + parse: function(s, host) { |
| 58 | + var p; |
| 59 | + if (this.source) { |
| 60 | + // cleanup for recycle |
| 61 | + for (p in this) { |
| 62 | + if (typeof (p) != "function") delete this[p]; |
| 63 | + } |
| 64 | + } |
| 65 | + this.source = s; |
| 66 | + this.host = host; |
| 67 | + |
| 68 | + var parts = s.split(/;\s*/); |
| 69 | + var nv = parts.shift().split("="); |
| 70 | + |
| 71 | + this.name = nv.shift() || ''; |
| 72 | + this.value = nv.join('=') || ''; |
| 73 | + |
| 74 | + var n, v; |
| 75 | + for each (p in parts) { |
| 76 | + nv = p.split("="); |
| 77 | + switch (n = nv[0].toLowerCase()) { |
| 78 | + case 'expires': |
| 79 | + v = Math.round(Date.parse((nv[1] || '').replace(/\-/g, ' ')) / 1000); |
| 80 | + break; |
| 81 | + case 'domain': |
| 82 | + case 'path': |
| 83 | + v = nv[1] || ''; |
| 84 | + break; |
| 85 | + case 'secure': |
| 86 | + case 'httponly': |
| 87 | + v = true; |
| 88 | + break; |
| 89 | + default: |
| 90 | + n = 'unknown'; |
| 91 | + } |
| 92 | + this[n] = v; |
| 93 | + } |
| 94 | + if (!this.expires) { |
| 95 | + this.session = true; |
| 96 | + this.expires = Math.round(new Date() / 1000) + 31536000; |
| 97 | + } |
| 98 | + if (this.domain) { |
| 99 | + if (!this.isDomain) this.domain = "." + this.domain; |
| 100 | + this.host = this.domain; |
| 101 | + } |
| 102 | + this.rawHost = this.host.replace(/^\./, ''); |
| 103 | + |
| 104 | + this.id = Cookie.computeId(this); |
| 105 | + }, |
| 106 | + |
| 107 | + |
| 108 | + get cookieManager() { |
| 109 | + delete Cookie.prototype.cookieManager; |
| 110 | + var cman = Cc["@mozilla.org/cookiemanager;1"] |
| 111 | + .getService(Ci.nsICookieManager2).QueryInterface(Ci.nsICookieManager); |
| 112 | + return Cookie.prototype.cookieManager = cman; |
| 113 | + }, |
| 114 | + belongsTo: function(host, path) { |
| 115 | + if (path && this.path && path.indexOf(this.path) != 0) return false; |
| 116 | + if (host == this.rawHost) return true; |
| 117 | + var d = this.domain; |
| 118 | + return d && (host == d || this.isDomain && host.slice(-d.length) == d); |
| 119 | + }, |
| 120 | + save: function() { |
| 121 | + this.save = ("cookieExists" in this.cookieManager) |
| 122 | + ? function() { this.cookieManager.add(this.host, this.path, this.name, this.value, this.secure, this.httponly, this.session, this.expires); } |
| 123 | + : function() { this.cookieManager.add(this.host, this.path, this.name, this.value, this.secure, this.session, this.expires);} |
| 124 | + ; |
| 125 | + return this.save(); |
| 126 | + }, |
| 127 | + exists: function() { |
| 128 | + var cc = this.cookieManager.enumerator; |
| 129 | + while(cc.hasMoreElements()) { |
| 130 | + if (this.sameAs(cc.getNext())) return true; |
| 131 | + } |
| 132 | + return false; |
| 133 | + }, |
| 134 | + |
| 135 | + sameAs: function(c) { |
| 136 | + (c instanceof Ci.nsICookie) && (c instanceof Ci.nsICookie2); |
| 137 | + return Cookie.computeId(c) == this.id; |
| 138 | + }, |
| 139 | + |
| 140 | + // nsICookie2 interface extras |
| 141 | + get isSecure() { return this.secure; }, |
| 142 | + get expiry() { return this.expires; }, |
| 143 | + get isSession() { return this.session; }, |
| 144 | + get isHttpOnly() { return this.httponly; }, |
| 145 | + get isDomain() { return this.domain && this.domain[0] == '.'; }, |
| 146 | + policy: 0, |
| 147 | + status: 0, |
| 148 | + QueryInterface: XPCOMUtils.generateQI([Ci.nsICookie, Ci.nsICookie2]) |
| 149 | + |
| 150 | +}; |
0 commit comments