3 * Copyright(c) 2010 LearnBoost <dev@learnboost.com>
11 var url
= require('url');
14 * Initialize a new `CookieJar`.
19 var CookieJar
= exports
= module
.exports
= function CookieJar() {
24 * Add the given `cookie` to the jar.
26 * @param {Cookie} cookie
30 CookieJar
.prototype.add = function(cookie
){
31 this.cookies
= this.cookies
.filter(function(c
){
32 // Avoid duplication (same path, same name)
33 return !(c
.name
== cookie
.name
&& c
.path
== cookie
.path
);
35 this.cookies
.push(cookie
);
39 * Get cookies for the given `req`.
41 * @param {IncomingRequest} req
46 CookieJar
.prototype.get = function(req
){
47 var path
= url
.parse(req
.url
).pathname
50 return this.cookies
.filter(function(cookie
){
51 if (0 == path
.indexOf(cookie
.path
) && now
< cookie
.expires
52 && cookie
.path
.length
> (specificity
[cookie
.name
] || 0))
53 return specificity
[cookie
.name
] = cookie
.path
.length
;
58 * Return Cookie string for the given `req`.
60 * @param {IncomingRequest} req
65 CookieJar
.prototype.cookieString = function(req
){
66 var cookies
= this.get(req
);
68 return cookies
.map(function(cookie
){
69 return cookie
.name
+ '=' + cookie
.value
;