54 lines
1 KiB
JavaScript
54 lines
1 KiB
JavaScript
var trim = require('trim');
|
|
|
|
var CookieBag = function() {
|
|
this.bag = [];
|
|
}
|
|
|
|
CookieBag.prototype.add = function(name, value) {
|
|
this.bag[name] = name + '=' + value;
|
|
}
|
|
|
|
CookieBag.prototype.get = function(name) {
|
|
var str = this.bag[name] || null;
|
|
|
|
if (!str) {
|
|
return;
|
|
}
|
|
|
|
var index = str.indexOf('=');
|
|
|
|
return str.substr(index + 1);
|
|
}
|
|
|
|
CookieBag.prototype.toString = function() {
|
|
var data = [];
|
|
|
|
for (var i in this.bag) {
|
|
data.push(this.bag[i]);
|
|
}
|
|
|
|
return data.join('; ');
|
|
}
|
|
|
|
CookieBag.prototype.handleResponse = function(response) {
|
|
var headers = response.headers || {};
|
|
var cookies = headers['set-cookie'] || [];
|
|
var that = this;
|
|
|
|
cookies.forEach(function(cookie) {
|
|
var def = cookie.split(';')[0];
|
|
var index = def.indexOf('=');
|
|
|
|
var name = def.substr(0, index);
|
|
var value = def.substr(index + 1);
|
|
|
|
that.add(name, value);
|
|
});
|
|
}
|
|
|
|
CookieBag.prototype.clean = function() {
|
|
this.bag = [];
|
|
}
|
|
|
|
module.exports = CookieBag;
|