Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

61 lignes
1.1 KiB

  1. /*!
  2. * Tobi - Cookie
  3. * Copyright(c) 2010 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var url = require('url');
  10. /**
  11. * Initialize a new `Cookie` with the given cookie `str` and `req`.
  12. *
  13. * @param {String} str
  14. * @param {IncomingRequest} req
  15. * @api private
  16. */
  17. var Cookie = exports = module.exports = function Cookie(str, req) {
  18. this.str = str;
  19. // First key is the name
  20. this.name = str.substr(0, str.indexOf('=')).trim();
  21. // Map the key/val pairs
  22. str.split(/ *; */).reduce(function(obj, pair){
  23. var p = pair.indexOf('=');
  24. if(p > 0)
  25. obj[pair.substring(0, p).trim()] = pair.substring(p + 1).trim();
  26. else
  27. obj[pair.trim()] = true;
  28. return obj;
  29. }, this);
  30. // Assign value
  31. this.value = this[this.name];
  32. // Expires
  33. this.expires = this.expires
  34. ? new Date(this.expires)
  35. : Infinity;
  36. // Default or trim path
  37. this.path = this.path
  38. ? this.path.trim(): req
  39. ? url.parse(req.url).pathname: '/';
  40. };
  41. /**
  42. * Return the original cookie string.
  43. *
  44. * @return {String}
  45. * @api public
  46. */
  47. Cookie.prototype.toString = function(){
  48. return this.str;
  49. };