cookies.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Simple cookie handling implementation based on the standard RFC 6265.
  2. //
  3. // This module just has two functionalities:
  4. // - Parse a set-cookie-header as a key value object
  5. // - Write a cookie-string from a key value object
  6. //
  7. // All cookie attributes are ignored.
  8. var unescape = require('querystring').unescape;
  9. var COOKIE_PAIR = /^([^=\s]+)\s*=\s*("?)\s*(.*)\s*\2\s*$/;
  10. var EXCLUDED_CHARS = /[\x00-\x1F\x7F\x3B\x3B\s\"\,\\"%]/g;
  11. var TRAILING_SEMICOLON = /\x3B+$/;
  12. var SEP_SEMICOLON = /\s*\x3B\s*/;
  13. // i know these should be 'const', but I'd like to keep
  14. // supporting earlier node.js versions as long as I can. :)
  15. var KEY_INDEX = 1; // index of key from COOKIE_PAIR match
  16. var VALUE_INDEX = 3; // index of value from COOKIE_PAIR match
  17. // Returns a copy str trimmed and without trainling semicolon.
  18. function cleanCookieString(str) {
  19. return str.trim().replace(/\x3B+$/, '');
  20. }
  21. function getFirstPair(str) {
  22. var index = str.indexOf('\x3B');
  23. return index === -1 ? str : str.substr(0, index);
  24. }
  25. // Returns a encoded copy of str based on RFC6265 S4.1.1.
  26. function encodeCookieComponent(str) {
  27. return str.toString().replace(EXCLUDED_CHARS, encodeURIComponent);
  28. }
  29. // Parses a set-cookie-string based on the standard defined in RFC6265 S4.1.1.
  30. function parseSetCookieString(str) {
  31. str = cleanCookieString(str);
  32. str = getFirstPair(str);
  33. var res = COOKIE_PAIR.exec(str);
  34. if (!res || !res[VALUE_INDEX]) return null;
  35. return {
  36. name : unescape(res[KEY_INDEX]),
  37. value : unescape(res[VALUE_INDEX])
  38. };
  39. }
  40. // Parses a set-cookie-header and returns a key/value object.
  41. // Each key represents the name of a cookie.
  42. function parseSetCookieHeader(header) {
  43. if (!header) return {};
  44. header = Array.isArray(header) ? header : [header];
  45. return header.reduce(function(res, str) {
  46. var cookie = parseSetCookieString(str);
  47. if (cookie) res[cookie.name] = cookie.value;
  48. return res;
  49. }, {});
  50. }
  51. // Writes a set-cookie-string based on the standard definded in RFC6265 S4.1.1.
  52. function writeCookieString(obj) {
  53. return Object.keys(obj).reduce(function(str, name) {
  54. var encodedName = encodeCookieComponent(name);
  55. var encodedValue = encodeCookieComponent(obj[name]);
  56. str += (str ? '; ' : '') + encodedName + '=' + encodedValue;
  57. return str;
  58. }, '');
  59. }
  60. // returns a key/val object from an array of cookie strings
  61. exports.read = parseSetCookieHeader;
  62. // writes a cookie string header
  63. exports.write = writeCookieString;