3 * Copyright(c) 2010 LearnBoost <dev@learnboost.com>
11 var url = require('url');
14 * Initialize a new `Cookie` with the given cookie `str` and `req`.
17 * @param {IncomingRequest} req
21 var Cookie = exports = module.exports = function Cookie(str, req) {
24 // Map the key/val pairs
25 str.split(/ *; */).reduce(function(obj, pair){
26 var p = pair.indexOf('=');
27 var key = p > 0 ? pair.substring(0, p).trim() : pair.trim();
28 var lowerCasedKey = key.toLowerCase();
29 var value = p > 0 ? pair.substring(p + 1).trim() : true;
32 // First key is the name
36 else if (lowerCasedKey === 'httponly') {
40 obj[lowerCasedKey] = value;
46 this.expires = this.expires
47 ? new Date(this.expires)
50 // Default or trim path
52 ? this.path.trim(): req
53 ? url.parse(req.url).pathname: '/';
57 * Return the original cookie string.
63 Cookie.prototype.toString = function(){