cookiejar.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. if ( str.length > 32768 ) {
  64. console.warn("Cookie too long for parsing (>32768 characters)");
  65. return;
  66. }
  67. var parts = str.split(";").filter(function (value) {
  68. return !!value;
  69. });
  70. var i;
  71. var pair = parts[0].match(/([^=]+)=([\s\S]*)/);
  72. if (!pair) {
  73. console.warn("Invalid cookie header encountered. Header: '"+str+"'");
  74. return;
  75. }
  76. var key = pair[1];
  77. var value = pair[2];
  78. if ( typeof key !== 'string' || key.length === 0 || typeof value !== 'string' ) {
  79. console.warn("Unable to extract values from cookie header. Cookie: '"+str+"'");
  80. return;
  81. }
  82. this.name = key;
  83. this.value = value;
  84. for (i = 1; i < parts.length; i += 1) {
  85. pair = parts[i].match(/([^=]+)(?:=([\s\S]*))?/);
  86. key = pair[1].trim().toLowerCase();
  87. value = pair[2];
  88. switch (key) {
  89. case "httponly":
  90. this.noscript = true;
  91. break;
  92. case "expires":
  93. this.expiration_date = value ?
  94. Number(Date.parse(value)) :
  95. Infinity;
  96. break;
  97. case "path":
  98. this.path = value ?
  99. value.trim() :
  100. "";
  101. this.explicit_path = true;
  102. break;
  103. case "domain":
  104. this.domain = value ?
  105. value.trim() :
  106. "";
  107. this.explicit_domain = !!this.domain;
  108. break;
  109. case "secure":
  110. this.secure = true;
  111. break;
  112. }
  113. }
  114. if (!this.explicit_path) {
  115. this.path = request_path || "/";
  116. }
  117. if (!this.explicit_domain) {
  118. this.domain = request_domain;
  119. }
  120. return this;
  121. }
  122. return new Cookie().parse(str, request_domain, request_path);
  123. };
  124. Cookie.prototype.matches = function matches(access_info) {
  125. if (access_info === CookieAccessInfo.All) {
  126. return true;
  127. }
  128. if (this.noscript && access_info.script ||
  129. this.secure && !access_info.secure ||
  130. !this.collidesWith(access_info)) {
  131. return false;
  132. }
  133. return true;
  134. };
  135. Cookie.prototype.collidesWith = function collidesWith(access_info) {
  136. if ((this.path && !access_info.path) || (this.domain && !access_info.domain)) {
  137. return false;
  138. }
  139. if (this.path && access_info.path.indexOf(this.path) !== 0) {
  140. return false;
  141. }
  142. if (this.explicit_path && access_info.path.indexOf( this.path ) !== 0) {
  143. return false;
  144. }
  145. var access_domain = access_info.domain && access_info.domain.replace(/^[\.]/,'');
  146. var cookie_domain = this.domain && this.domain.replace(/^[\.]/,'');
  147. if (cookie_domain === access_domain) {
  148. return true;
  149. }
  150. if (cookie_domain) {
  151. if (!this.explicit_domain) {
  152. return false; // we already checked if the domains were exactly the same
  153. }
  154. var wildcard = access_domain.indexOf(cookie_domain);
  155. if (wildcard === -1 || wildcard !== access_domain.length - cookie_domain.length) {
  156. return false;
  157. }
  158. return true;
  159. }
  160. return true;
  161. };
  162. function CookieJar() {
  163. var cookies, cookies_list, collidable_cookie;
  164. if (this instanceof CookieJar) {
  165. cookies = Object.create(null); //name: [Cookie]
  166. this.setCookie = function setCookie(cookie, request_domain, request_path) {
  167. var remove, i;
  168. cookie = new Cookie(cookie, request_domain, request_path);
  169. //Delete the cookie if the set is past the current time
  170. remove = cookie.expiration_date <= Date.now();
  171. if (cookies[cookie.name] !== undefined) {
  172. cookies_list = cookies[cookie.name];
  173. for (i = 0; i < cookies_list.length; i += 1) {
  174. collidable_cookie = cookies_list[i];
  175. if (collidable_cookie.collidesWith(cookie)) {
  176. if (remove) {
  177. cookies_list.splice(i, 1);
  178. if (cookies_list.length === 0) {
  179. delete cookies[cookie.name];
  180. }
  181. return false;
  182. }
  183. cookies_list[i] = cookie;
  184. return cookie;
  185. }
  186. }
  187. if (remove) {
  188. return false;
  189. }
  190. cookies_list.push(cookie);
  191. return cookie;
  192. }
  193. if (remove) {
  194. return false;
  195. }
  196. cookies[cookie.name] = [cookie];
  197. return cookies[cookie.name];
  198. };
  199. //returns a cookie
  200. this.getCookie = function getCookie(cookie_name, access_info) {
  201. var cookie, i;
  202. cookies_list = cookies[cookie_name];
  203. if (!cookies_list) {
  204. return;
  205. }
  206. for (i = 0; i < cookies_list.length; i += 1) {
  207. cookie = cookies_list[i];
  208. if (cookie.expiration_date <= Date.now()) {
  209. if (cookies_list.length === 0) {
  210. delete cookies[cookie.name];
  211. }
  212. continue;
  213. }
  214. if (cookie.matches(access_info)) {
  215. return cookie;
  216. }
  217. }
  218. };
  219. //returns a list of cookies
  220. this.getCookies = function getCookies(access_info) {
  221. var matches = [], cookie_name, cookie;
  222. for (cookie_name in cookies) {
  223. cookie = this.getCookie(cookie_name, access_info);
  224. if (cookie) {
  225. matches.push(cookie);
  226. }
  227. }
  228. matches.toString = function toString() {
  229. return matches.join(":");
  230. };
  231. matches.toValueString = function toValueString() {
  232. return matches.map(function (c) {
  233. return c.toValueString();
  234. }).join('; ');
  235. };
  236. return matches;
  237. };
  238. return this;
  239. }
  240. return new CookieJar();
  241. }
  242. exports.CookieJar = CookieJar;
  243. //returns list of cookies that were set correctly. Cookies that are expired and removed are not returned.
  244. CookieJar.prototype.setCookies = function setCookies(cookies, request_domain, request_path) {
  245. cookies = Array.isArray(cookies) ?
  246. cookies :
  247. cookies.split(cookie_str_splitter);
  248. var successful = [],
  249. i,
  250. cookie;
  251. cookies = cookies.map(function(item){
  252. return new Cookie(item, request_domain, request_path);
  253. });
  254. for (i = 0; i < cookies.length; i += 1) {
  255. cookie = cookies[i];
  256. if (this.setCookie(cookie, request_domain, request_path)) {
  257. successful.push(cookie);
  258. }
  259. }
  260. return successful;
  261. };
  262. }());