You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cookiejar.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* jshint node: true */
  2. (function () {
  3. "use strict";
  4. function CookieAccessInfo(domain, path, secure, script) {
  5. if (this instanceof CookieAccessInfo) {
  6. this.domain = domain || undefined;
  7. this.path = path || "/";
  8. this.secure = !!secure;
  9. this.script = !!script;
  10. return this;
  11. }
  12. return new CookieAccessInfo(domain, path, secure, script);
  13. }
  14. CookieAccessInfo.All = Object.freeze(Object.create(null));
  15. exports.CookieAccessInfo = CookieAccessInfo;
  16. function Cookie(cookiestr, request_domain, request_path) {
  17. if (cookiestr instanceof Cookie) {
  18. return cookiestr;
  19. }
  20. if (this instanceof Cookie) {
  21. this.name = null;
  22. this.value = null;
  23. this.expiration_date = Infinity;
  24. this.path = String(request_path || "/");
  25. this.explicit_path = false;
  26. this.domain = request_domain || null;
  27. this.explicit_domain = false;
  28. this.secure = false; //how to define default?
  29. this.noscript = false; //httponly
  30. if (cookiestr) {
  31. this.parse(cookiestr, request_domain, request_path);
  32. }
  33. return this;
  34. }
  35. return new Cookie(cookiestr, request_domain, request_path);
  36. }
  37. exports.Cookie = Cookie;
  38. Cookie.prototype.toString = function toString() {
  39. var str = [this.name + "=" + this.value];
  40. if (this.expiration_date !== Infinity) {
  41. str.push("expires=" + (new Date(this.expiration_date)).toGMTString());
  42. }
  43. if (this.domain) {
  44. str.push("domain=" + this.domain);
  45. }
  46. if (this.path) {
  47. str.push("path=" + this.path);
  48. }
  49. if (this.secure) {
  50. str.push("secure");
  51. }
  52. if (this.noscript) {
  53. str.push("httponly");
  54. }
  55. return str.join("; ");
  56. };
  57. Cookie.prototype.toValueString = function toValueString() {
  58. return this.name + "=" + this.value;
  59. };
  60. var cookie_str_splitter = /[:](?=\s*[a-zA-Z0-9_\-]+\s*[=])/g;
  61. Cookie.prototype.parse = function parse(str, request_domain, request_path) {
  62. if (this instanceof Cookie) {
  63. var parts = str.split(";").filter(function (value) {
  64. return !!value;
  65. });
  66. var i;
  67. var pair = parts[0].match(/([^=]+)=([\s\S]*)/);
  68. if (!pair) {
  69. console.warn("Invalid cookie header encountered. Header: '"+str+"'");
  70. return;
  71. }
  72. var key = pair[1];
  73. var value = pair[2];
  74. if ( typeof key !== 'string' || key.length === 0 || typeof value !== 'string' ) {
  75. console.warn("Unable to extract values from cookie header. Cookie: '"+str+"'");
  76. return;
  77. }
  78. this.name = key;
  79. this.value = value;
  80. for (i = 1; i < parts.length; i += 1) {
  81. pair = parts[i].match(/([^=]+)(?:=([\s\S]*))?/);
  82. key = pair[1].trim().toLowerCase();
  83. value = pair[2];
  84. switch (key) {
  85. case "httponly":
  86. this.noscript = true;
  87. break;
  88. case "expires":
  89. this.expiration_date = value ?
  90. Number(Date.parse(value)) :
  91. Infinity;
  92. break;
  93. case "path":
  94. this.path = value ?
  95. value.trim() :
  96. "";
  97. this.explicit_path = true;
  98. break;
  99. case "domain":
  100. this.domain = value ?
  101. value.trim() :
  102. "";
  103. this.explicit_domain = !!this.domain;
  104. break;
  105. case "secure":
  106. this.secure = true;
  107. break;
  108. }
  109. }
  110. if (!this.explicit_path) {
  111. this.path = request_path || "/";
  112. }
  113. if (!this.explicit_domain) {
  114. this.domain = request_domain;
  115. }
  116. return this;
  117. }
  118. return new Cookie().parse(str, request_domain, request_path);
  119. };
  120. Cookie.prototype.matches = function matches(access_info) {
  121. if (access_info === CookieAccessInfo.All) {
  122. return true;
  123. }
  124. if (this.noscript && access_info.script ||
  125. this.secure && !access_info.secure ||
  126. !this.collidesWith(access_info)) {
  127. return false;
  128. }
  129. return true;
  130. };
  131. Cookie.prototype.collidesWith = function collidesWith(access_info) {
  132. if ((this.path && !access_info.path) || (this.domain && !access_info.domain)) {
  133. return false;
  134. }
  135. if (this.path && access_info.path.indexOf(this.path) !== 0) {
  136. return false;
  137. }
  138. if (this.explicit_path && access_info.path.indexOf( this.path ) !== 0) {
  139. return false;
  140. }
  141. var access_domain = access_info.domain && access_info.domain.replace(/^[\.]/,'');
  142. var cookie_domain = this.domain && this.domain.replace(/^[\.]/,'');
  143. if (cookie_domain === access_domain) {
  144. return true;
  145. }
  146. if (cookie_domain) {
  147. if (!this.explicit_domain) {
  148. return false; // we already checked if the domains were exactly the same
  149. }
  150. var wildcard = access_domain.indexOf(cookie_domain);
  151. if (wildcard === -1 || wildcard !== access_domain.length - cookie_domain.length) {
  152. return false;
  153. }
  154. return true;
  155. }
  156. return true;
  157. };
  158. function CookieJar() {
  159. var cookies, cookies_list, collidable_cookie;
  160. if (this instanceof CookieJar) {
  161. cookies = Object.create(null); //name: [Cookie]
  162. this.setCookie = function setCookie(cookie, request_domain, request_path) {
  163. var remove, i;
  164. cookie = new Cookie(cookie, request_domain, request_path);
  165. //Delete the cookie if the set is past the current time
  166. remove = cookie.expiration_date <= Date.now();
  167. if (cookies[cookie.name] !== undefined) {
  168. cookies_list = cookies[cookie.name];
  169. for (i = 0; i < cookies_list.length; i += 1) {
  170. collidable_cookie = cookies_list[i];
  171. if (collidable_cookie.collidesWith(cookie)) {
  172. if (remove) {
  173. cookies_list.splice(i, 1);
  174. if (cookies_list.length === 0) {
  175. delete cookies[cookie.name];
  176. }
  177. return false;
  178. }
  179. cookies_list[i] = cookie;
  180. return cookie;
  181. }
  182. }
  183. if (remove) {
  184. return false;
  185. }
  186. cookies_list.push(cookie);
  187. return cookie;
  188. }
  189. if (remove) {
  190. return false;
  191. }
  192. cookies[cookie.name] = [cookie];
  193. return cookies[cookie.name];
  194. };
  195. //returns a cookie
  196. this.getCookie = function getCookie(cookie_name, access_info) {
  197. var cookie, i;
  198. cookies_list = cookies[cookie_name];
  199. if (!cookies_list) {
  200. return;
  201. }
  202. for (i = 0; i < cookies_list.length; i += 1) {
  203. cookie = cookies_list[i];
  204. if (cookie.expiration_date <= Date.now()) {
  205. if (cookies_list.length === 0) {
  206. delete cookies[cookie.name];
  207. }
  208. continue;
  209. }
  210. if (cookie.matches(access_info)) {
  211. return cookie;
  212. }
  213. }
  214. };
  215. //returns a list of cookies
  216. this.getCookies = function getCookies(access_info) {
  217. var matches = [], cookie_name, cookie;
  218. for (cookie_name in cookies) {
  219. cookie = this.getCookie(cookie_name, access_info);
  220. if (cookie) {
  221. matches.push(cookie);
  222. }
  223. }
  224. matches.toString = function toString() {
  225. return matches.join(":");
  226. };
  227. matches.toValueString = function toValueString() {
  228. return matches.map(function (c) {
  229. return c.toValueString();
  230. }).join(';');
  231. };
  232. return matches;
  233. };
  234. return this;
  235. }
  236. return new CookieJar();
  237. }
  238. exports.CookieJar = CookieJar;
  239. //returns list of cookies that were set correctly. Cookies that are expired and removed are not returned.
  240. CookieJar.prototype.setCookies = function setCookies(cookies, request_domain, request_path) {
  241. cookies = Array.isArray(cookies) ?
  242. cookies :
  243. cookies.split(cookie_str_splitter);
  244. var successful = [],
  245. i,
  246. cookie;
  247. cookies = cookies.map(function(item){
  248. return new Cookie(item, request_domain, request_path);
  249. });
  250. for (i = 0; i < cookies.length; i += 1) {
  251. cookie = cookies[i];
  252. if (this.setCookie(cookie, request_domain, request_path)) {
  253. successful.push(cookie);
  254. }
  255. }
  256. return successful;
  257. };
  258. }());