2
var tough = require('./cookie');
3
var Store = require('./store').Store;
4
var permuteDomain = tough.permuteDomain;
5
var permutePath = tough.permutePath;
6
var util = require('util');
8
function MemoryCookieStore() {
12
util.inherits(MemoryCookieStore, Store);
13
exports.MemoryCookieStore = MemoryCookieStore;
14
MemoryCookieStore.prototype.idx = null;
15
MemoryCookieStore.prototype.synchronous = true;
17
// force a default depth:
18
MemoryCookieStore.prototype.inspect = function() {
19
return "{ idx: "+util.inspect(this.idx, false, 2)+' }';
22
MemoryCookieStore.prototype.findCookie = function(domain, path, key, cb) {
23
if (!this.idx[domain]) {
24
return cb(null,undefined);
26
if (!this.idx[domain][path]) {
27
return cb(null,undefined);
29
return cb(null,this.idx[domain][path][key]||null);
32
MemoryCookieStore.prototype.findCookies = function(domain, path, cb) {
40
// null or '/' means "all paths"
41
pathMatcher = function matchAll(domainIndex) {
42
for (var curPath in domainIndex) {
43
var pathIndex = domainIndex[curPath];
44
for (var key in pathIndex) {
45
results.push(pathIndex[key]);
50
} else if (path === '/') {
51
pathMatcher = function matchSlash(domainIndex) {
52
var pathIndex = domainIndex['/'];
56
for (var key in pathIndex) {
57
results.push(pathIndex[key]);
62
var paths = permutePath(path) || [path];
63
pathMatcher = function matchRFC(domainIndex) {
64
paths.forEach(function(curPath) {
65
var pathIndex = domainIndex[curPath];
69
for (var key in pathIndex) {
70
results.push(pathIndex[key]);
76
var domains = permuteDomain(domain) || [domain];
78
domains.forEach(function(curDomain) {
79
var domainIndex = idx[curDomain];
83
pathMatcher(domainIndex);
89
MemoryCookieStore.prototype.putCookie = function(cookie, cb) {
90
if (!this.idx[cookie.domain]) {
91
this.idx[cookie.domain] = {};
93
if (!this.idx[cookie.domain][cookie.path]) {
94
this.idx[cookie.domain][cookie.path] = {};
96
this.idx[cookie.domain][cookie.path][cookie.key] = cookie;
100
MemoryCookieStore.prototype.updateCookie = function updateCookie(oldCookie, newCookie, cb) {
101
// updateCookie() may avoid updating cookies that are identical. For example,
102
// lastAccessed may not be important to some stores and an equality
103
// comparison could exclude that field.
104
this.putCookie(newCookie,cb);
107
MemoryCookieStore.prototype.removeCookie = function removeCookie(domain, path, key, cb) {
108
if (this.idx[domain] && this.idx[domain][path] && this.idx[domain][path][key]) {
109
delete this.idx[domain][path][key];
114
MemoryCookieStore.prototype.removeCookies = function removeCookies(domain, path, cb) {
115
if (this.idx[domain]) {
117
delete this.idx[domain][path];
119
delete this.idx[domain];