This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcookies.js
185 lines (165 loc) · 4.08 KB
/
cookies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var fs = require('fs'),
_ = require('./underscore')._,
wsrc = require('./wsrc');
var rc = wsrc.get();
var cookieJar = rc.cookies || {};
function endsWith(needle, hayStack) {
return hayStack.match(needle + "$") == needle;
}
function startsWith(needle, hayStack) {
return hayStack.match("^" + needle) == needle;
}
function clear() {
cookieJar = {};
}
function remove(site, key) {
if (cookieJar[site]) {
if (key) {
delete cookieJar[site][key];
} else {
delete cookieJar[site];
}
}
}
function set(site, key, value, options) {
cookieJar[site] = cookieJar[site] || {};
cookieJar[site][key] = {value: value};
_.extend(cookieJar[site][key], options);
}
function get(domain, key) {
cookieJar = compactJar(false);
for (var site in cookieJar) {
if (endsWith(site, domain)) {
var jar = cookieJar[site];
if (!key) {
return _.extend({}, jar);
} else if (jar[key]) {
return _.extend({}, jar[key]);
}
}
}
return null;
}
function parseCookie(string) {
var first = true;
var cookie = {http_only: false};
var parts;
string.trim().split(";").forEach(function(component) {
if (first) {
parts = component.trim().split('=');
cookie.key = parts[0];
cookie.value = parts.slice(1).join('=');
first = false;
} else {
if (component.trim().toLowerCase() === 'httponly') {
cookie.http_only = true;
} else {
parts = component.trim().split('=');
cookie[parts[0]] = parts[1];
}
}
});
if (cookie.expires) {
cookie.expires = new Date(cookie.expires);
}
if (!cookie.path) {
cookie.path = "/";
}
return cookie;
}
function mergeCookies(domain, cookies, cookieJar) {
//only include cookies with legal domains
_.filter(cookies, function(cookie) {
return endsWith(cookie.domain, domain);
});
var now = new Date();
_.each(cookies, function(cookie) {
var jar = cookieJar[cookie.domain] || {};
jar[cookie.key] = cookie;
if (cookie.expires && cookie.expires < now) {
delete jar[cookie.key];
}
cookieJar[cookie.domain] = jar;
});
}
function update(domain, header) {
if (header) {
if (!_.isArray(header)) {
header = [header];
}
var cookies = header.map(parseCookie);
_.each(cookies, function(cookie) {
if (!cookie.domain) {
cookie.domain = domain;
}
});
mergeCookies(domain, cookies, cookieJar);
}
}
function compactJar(expireSessionCookies) {
var now = new Date();
_.map(cookieJar, function(site) {
for (var key in site) {
if ((expireSessionCookies && !site[key].expires) || (site[key].expires && site[key].expires < now)) {
delete site[key];
}
}
return site;
});
return _.extend({}, cookieJar);
}
function headerFor(url) {
var kvs = [];
for (var site in cookieJar) {
if (endsWith(site, url.hostname)) {
var jar = cookieJar[site];
for (var key in jar) {
if (startsWith(jar[key].path, url.pathname)) {
kvs.push(key + "=" + jar[key].value);
}
}
}
}
return kvs.length > 0 ? kvs.join('; ') : undefined;
}
/* internal use only */
function get_raw() {
return cookieJar;
}
/* internal use only */
function set_raw(jar) {
cookieJar = jar;
}
exports.clear = clear;
exports.remove = remove;
exports.set = set;
exports.get = get;
exports.update = update;
exports.compactJar = compactJar;
exports.headerFor = headerFor;
exports.__set_raw__ = set_raw;
exports.__get_raw__ = get_raw;
/*
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
*/