-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
149 lines (113 loc) · 3.97 KB
/
index.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
var _ = require('lodash');
var Absolurl = require('absolurl');
var url = require('url');
var Oven = function(options){
function _getUrlObject(myUrl){
if (!myUrl) return;
return url.parse(absolurl.resolve(myUrl));
}
function _doesPathMatch(path, cookiePath){
if (!path || !cookiePath) return true;
if (path === cookiePath) return true;
cookiePath = cookiePath.replace('/', '\\/');
if (!/\/$/.test(cookiePath)) cookiePath += '/';
if (!/\/$/.test(path)) path += '/';
return new RegExp('^' + cookiePath).test(path);
}
function _doesDomainMatch(domain, cookieDomain){
if (!domain || !cookieDomain) return true;
if (!/^\./.test(cookieDomain)) cookieDomain = '.' + cookieDomain;
if (!/^\./.test(domain)) domain = '.' + domain;
cookieDomain = cookieDomain.replace('.', '\\.');
return new RegExp(cookieDomain + '$').test(domain);
}
function _isCookieExpired(currentTime, cookieExpiration){
if (!cookieExpiration) return false;
return currentTime >= cookieExpiration;
}
function _getCookieName(headerValue){
var match = headerValue.match(/^\s*([^=]+)/i);
return match ? match[1] : undefined;
}
function _parseCookieField(o, fieldName){
var regex = new RegExp('\\s*;\\s*' + fieldName, 'i');
var match = o.value.match(regex);
o[fieldName] = !!match;
o.value = o.value.replace(regex, '');
}
function _parseCookieFieldValue(o, fieldName, defaultFieldValue){
var regex = new RegExp(fieldName + '\\s*=([^;]+)(;|$)', 'i');
var match = o.value.match(regex);
var fieldValue;
if (match && match[1]) {
fieldValue = (match[1]||'').trim();
// don't include port in domain
// re: https://github.com/chevett/oven/pull/3#issuecomment-44389062
if (fieldName == 'domain') fieldValue = fieldValue.split(':')[0];
}
o[fieldName] = fieldValue || defaultFieldValue;
o.value = o.value.replace(regex, '');
}
function _parseSetCookieHeader(header){
var o = new ParsedCookie();
o.value = header;
_parseCookieFieldValue(o, 'expires');
_parseCookieFieldValue(o, 'path', oDefaultUrl.pathname);
_parseCookieFieldValue(o, 'domain', oDefaultUrl.hostname);
_parseCookieFieldValue(o, 'priority');
_parseCookieField(o, 'secure');
_parseCookieField(o, 'httpOnly');
o.expires = o.expires ? Date.parse(o.expires) : undefined;
o.values = _.chain(o.value.split(/;/g))
.flatten()
.map(function(v){return v.trim();})
.filter(function(v){ return v; })
.value();
o.name = _getCookieName(header);
o.id = o.name + '_' + o.domain;
return o;
}
if (!options || !options.url) throw {};
var cookies = {},
absolurl = new Absolurl(),
oDefaultUrl = _getUrlObject(options.url);
this.setCookie = function(rawSetCookieHeaderValue){
var oCookie = _parseSetCookieHeader(rawSetCookieHeaderValue);
cookies[oCookie.id] = oCookie;
return oCookie;
};
this.getCookies = function(myUrl, dateTime){
var oUrl = _getUrlObject(absolurl.resolve(myUrl, options.url) || options.url) || {};
dateTime = dateTime || new Date();
return _.chain(cookies)
.filter(function(c){ return !_isCookieExpired(dateTime, c.expires); })
.filter(function(c){ return _doesDomainMatch(oUrl.hostname, c.domain); })
.filter(function(c){ return _doesPathMatch(oUrl.pathname, c.path); })
.filter(function(c){ return !c.secure || /^https/i.test(oUrl.protocol); })
.value();
};
this.getCookie = this.getCookieHeader = function(myUrl, dateTime){
return _.chain(this.getCookies(myUrl, dateTime))
.map(function(c){ return c.values;})
.flatten()
.value()
.join('; ');
};
};
var ParsedCookie = function(){
};
Oven.prototype.setCookies = function(lst){
for (var i=0; i<lst.length; i++){
this.setCookie(lst[i]);
}
};
ParsedCookie.prototype.toString = function(){
var str = this.values.join('; ')
+ '; Domain='+this.domain
+ '; Path=' +this.path;
if (this.expires) str+= '; Expires=' + new Date(this.expires).toUTCString();
if (this.secure) str += '; secure';
if (this.httpOnly) str += '; httponly';
return str;
};
module.exports = Oven;