forked from hughsk/npm-quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookies.js
150 lines (139 loc) · 3.07 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
var cookie_str_splitter=/[:](?=\s*[a-zA-Z0-9_\-]+\s*[=])/g
function stringify (cookie) {
var str=[cookie.name+"="+cookie.value];
if(cookie.expiration_date !== Infinity) {
str.push("expires="+(new Date(cookie.expiration_date)).toGMTString());
}
if(cookie.domain) {
str.push("domain="+cookie.domain);
}
if(cookie.path) {
str.push("path="+cookie.path);
}
if(cookie.secure) {
str.push("secure");
}
if(cookie.noscript) {
str.push("httponly");
}
return str.join("; ");
}
function Jar () {
this.cookies = []
}
Jar.prototype.setCookies = function (cookieString) {
}
Jar.prototype.getHeader = function (host, path) {
}
Jar.prototype.getCookies = function (access_info) {
var matches=[];
for(var cookie_name in cookies) {
var cookie=this.getCookie(cookie_name,access_info);
if (cookie) {
matches.push(cookie);
}
}
matches.toString=function toString(){return matches.join(":");}
return matches;
}
Jar.prototype.getCookie = function (host, path) {
var cookies_list = self.cookies[cookie_name];
for(var i=0;i<cookies_list.length;i++) {
var cookie = cookies_list[i];
if(cookie.expiration_date <= Date.now()) {
if(cookies_list.length===0) {
delete cookies[cookie.name]
}
continue;
}
if(cookie.matches(access_info)) {
return cookie;
}
}
}
Jar.prototype.setCookie = function (){}
Jar.prototype.parseCookie = function (str) {
var cookies = str.split(cookie_str_splitter)
, successful = []
, self = this
;
cookies.forEach(function (cookie) {
self.parseCookie(cookie);
})
}
Jar.prototype.parseCookie = function (str) {
var parts = str.split(";")
, pai r= parts[0].match(/([^=]+)=((?:.|\n)*)/)
, key = pair[1]
, value = pair[2]
, cookie =
{ name: null
, value: value
, expiration_date: = Infinity
, path: '/'
, domain: null
, secure: false
, noscript: false
}
;
cookie.name = key;
cookie.value = value;
for(var i=1;i<parts.length;i++) {
var pair = parts[i].match(/([^=]+)=((?:.|\n)*)/)
, key = pair[1].trim().toLowerCase()
, value = pair[2]
;
switch(key) {
case "httponly":
cookie.noscript = true;
break;
case "expires":
cookie.expiration_date = value
? Number(Date.parse(value))
: Infinity;
break;
case "path":
cookie.path = value
? value.trim()
: "";
break;
case "domain":
cookie.domain = value
? value.trim()
: "";
break;
case "secure":
cookie.secure = true;
break
}
}
if(cookie.name in this.cookies) {
var cookies_list = this.cookies[cookie.name];
for(var i=0;i<this.cookies_list.length;i++) {
var collidable_cookie = cookies_list[i];
if(collidable_cookie.collidesWith(cookie)) {
if(remove) {
cookies_list.splice(i,1);
if(cookies_list.length===0) {
delete this.cookies[cookie.name]
}
return false;
}
else {
return cookies_list[i]=cookie;
}
}
}
if(remove) {
return false;
}
cookies_list.push(cookie);
return cookie;
}
else if(remove){
return false;
}
else {
return this.cookies[cookie.name]=[cookie];
}
}