cookies.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. var cookie_str_splitter=/[:](?=\s*[a-zA-Z0-9_\-]+\s*[=])/g
  2. function stringify (cookie) {
  3. var str=[cookie.name+"="+cookie.value];
  4. if(cookie.expiration_date !== Infinity) {
  5. str.push("expires="+(new Date(cookie.expiration_date)).toGMTString());
  6. }
  7. if(cookie.domain) {
  8. str.push("domain="+cookie.domain);
  9. }
  10. if(cookie.path) {
  11. str.push("path="+cookie.path);
  12. }
  13. if(cookie.secure) {
  14. str.push("secure");
  15. }
  16. if(cookie.noscript) {
  17. str.push("httponly");
  18. }
  19. return str.join("; ");
  20. }
  21. function Jar () {
  22. this.cookies = []
  23. }
  24. Jar.prototype.setCookies = function (cookieString) {
  25. }
  26. Jar.prototype.getHeader = function (host, path) {
  27. }
  28. Jar.prototype.getCookies = function (access_info) {
  29. var matches=[];
  30. for(var cookie_name in cookies) {
  31. var cookie=this.getCookie(cookie_name,access_info);
  32. if (cookie) {
  33. matches.push(cookie);
  34. }
  35. }
  36. matches.toString=function toString(){return matches.join(":");}
  37. return matches;
  38. }
  39. Jar.prototype.getCookie = function (host, path) {
  40. var cookies_list = self.cookies[cookie_name];
  41. for(var i=0;i<cookies_list.length;i++) {
  42. var cookie = cookies_list[i];
  43. if(cookie.expiration_date <= Date.now()) {
  44. if(cookies_list.length===0) {
  45. delete cookies[cookie.name]
  46. }
  47. continue;
  48. }
  49. if(cookie.matches(access_info)) {
  50. return cookie;
  51. }
  52. }
  53. }
  54. Jar.prototype.setCookie = function (){}
  55. Jar.prototype.parseCookie = function (str) {
  56. var cookies = str.split(cookie_str_splitter)
  57. , successful = []
  58. , self = this
  59. ;
  60. cookies.forEach(function (cookie) {
  61. self.parseCookie(cookie);
  62. })
  63. }
  64. Jar.prototype.parseCookie = function (str) {
  65. var parts = str.split(";")
  66. , pai r= parts[0].match(/([^=]+)=((?:.|\n)*)/)
  67. , key = pair[1]
  68. , value = pair[2]
  69. , cookie =
  70. { name: null
  71. , value: value
  72. , expiration_date: = Infinity
  73. , path: '/'
  74. , domain: null
  75. , secure: false
  76. , noscript: false
  77. }
  78. ;
  79. cookie.name = key;
  80. cookie.value = value;
  81. for(var i=1;i<parts.length;i++) {
  82. var pair = parts[i].match(/([^=]+)=((?:.|\n)*)/)
  83. , key = pair[1].trim().toLowerCase()
  84. , value = pair[2]
  85. ;
  86. switch(key) {
  87. case "httponly":
  88. cookie.noscript = true;
  89. break;
  90. case "expires":
  91. cookie.expiration_date = value
  92. ? Number(Date.parse(value))
  93. : Infinity;
  94. break;
  95. case "path":
  96. cookie.path = value
  97. ? value.trim()
  98. : "";
  99. break;
  100. case "domain":
  101. cookie.domain = value
  102. ? value.trim()
  103. : "";
  104. break;
  105. case "secure":
  106. cookie.secure = true;
  107. break
  108. }
  109. }
  110. if(cookie.name in this.cookies) {
  111. var cookies_list = this.cookies[cookie.name];
  112. for(var i=0;i<this.cookies_list.length;i++) {
  113. var collidable_cookie = cookies_list[i];
  114. if(collidable_cookie.collidesWith(cookie)) {
  115. if(remove) {
  116. cookies_list.splice(i,1);
  117. if(cookies_list.length===0) {
  118. delete this.cookies[cookie.name]
  119. }
  120. return false;
  121. }
  122. else {
  123. return cookies_list[i]=cookie;
  124. }
  125. }
  126. }
  127. if(remove) {
  128. return false;
  129. }
  130. cookies_list.push(cookie);
  131. return cookie;
  132. }
  133. else if(remove){
  134. return false;
  135. }
  136. else {
  137. return this.cookies[cookie.name]=[cookie];
  138. }
  139. }