~ps-jenkins/ubuntu-push/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to docs/example-server/node_modules/request/node_modules/tough-cookie/lib/memstore.js

  • Committer: Roberto Alsina
  • Date: 2014-09-05 14:57:17 UTC
  • mto: (91.179.25 automatic)
  • mto: This revision was merged to the branch mainline in revision 129.
  • Revision ID: roberto.alsina@canonical.com-20140905145717-0ufcsv27w25i1nnu
added example app server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'use strict';
 
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');
 
7
 
 
8
function MemoryCookieStore() {
 
9
  Store.call(this);
 
10
  this.idx = {};
 
11
}
 
12
util.inherits(MemoryCookieStore, Store);
 
13
exports.MemoryCookieStore = MemoryCookieStore;
 
14
MemoryCookieStore.prototype.idx = null;
 
15
MemoryCookieStore.prototype.synchronous = true;
 
16
 
 
17
// force a default depth:
 
18
MemoryCookieStore.prototype.inspect = function() {
 
19
  return "{ idx: "+util.inspect(this.idx, false, 2)+' }';
 
20
};
 
21
 
 
22
MemoryCookieStore.prototype.findCookie = function(domain, path, key, cb) {
 
23
  if (!this.idx[domain]) {
 
24
    return cb(null,undefined);
 
25
  }
 
26
  if (!this.idx[domain][path]) {
 
27
    return cb(null,undefined);
 
28
  }
 
29
  return cb(null,this.idx[domain][path][key]||null);
 
30
};
 
31
 
 
32
MemoryCookieStore.prototype.findCookies = function(domain, path, cb) {
 
33
  var results = [];
 
34
  if (!domain) {
 
35
    return cb(null,[]);
 
36
  }
 
37
 
 
38
  var pathMatcher;
 
39
  if (!path) {
 
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]);
 
46
        }
 
47
      }
 
48
    };
 
49
 
 
50
  } else if (path === '/') {
 
51
    pathMatcher = function matchSlash(domainIndex) {
 
52
      var pathIndex = domainIndex['/'];
 
53
      if (!pathIndex) {
 
54
        return;
 
55
      }
 
56
      for (var key in pathIndex) {
 
57
        results.push(pathIndex[key]);
 
58
      }
 
59
    };
 
60
 
 
61
  } else {
 
62
    var paths = permutePath(path) || [path];
 
63
    pathMatcher = function matchRFC(domainIndex) {
 
64
      paths.forEach(function(curPath) {
 
65
        var pathIndex = domainIndex[curPath];
 
66
        if (!pathIndex) {
 
67
          return;
 
68
        }
 
69
        for (var key in pathIndex) {
 
70
          results.push(pathIndex[key]);
 
71
        }
 
72
      });
 
73
    };
 
74
  }
 
75
 
 
76
  var domains = permuteDomain(domain) || [domain];
 
77
  var idx = this.idx;
 
78
  domains.forEach(function(curDomain) {
 
79
    var domainIndex = idx[curDomain];
 
80
    if (!domainIndex) {
 
81
      return;
 
82
    }
 
83
    pathMatcher(domainIndex);
 
84
  });
 
85
 
 
86
  cb(null,results);
 
87
};
 
88
 
 
89
MemoryCookieStore.prototype.putCookie = function(cookie, cb) {
 
90
  if (!this.idx[cookie.domain]) {
 
91
    this.idx[cookie.domain] = {};
 
92
  }
 
93
  if (!this.idx[cookie.domain][cookie.path]) {
 
94
    this.idx[cookie.domain][cookie.path] = {};
 
95
  }
 
96
  this.idx[cookie.domain][cookie.path][cookie.key] = cookie;
 
97
  cb(null);
 
98
};
 
99
 
 
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);
 
105
};
 
106
 
 
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];
 
110
  }
 
111
  cb(null);
 
112
};
 
113
 
 
114
MemoryCookieStore.prototype.removeCookies = function removeCookies(domain, path, cb) {
 
115
  if (this.idx[domain]) {
 
116
    if (path) {
 
117
      delete this.idx[domain][path];
 
118
    } else {
 
119
      delete this.idx[domain];
 
120
    }
 
121
  }
 
122
  return cb(null);
 
123
};