Skip to content

Instantly share code, notes, and snippets.

@shinout
Created October 16, 2011 08:23
Show Gist options
  • Save shinout/1290655 to your computer and use it in GitHub Desktop.
Save shinout/1290655 to your computer and use it in GitHub Desktop.
managing cookies
var u2r = require('./url2request'),
parse = require('url').parse;
function CookieManager() {
this.domains = {};
}
CookieManager.prototype.set = function(url, cookies) {
var domain = getDomain.call(this, parse(url).hostname);
// parsing cookie
cookies.forEach(function(cookie) {
var obj = cookie.split(";").reduce(function(sofar, v, k) {
var val = v.split("=");
if (val.length == 1) sofar[v] = true;
else sofar[val[0].trim()] = val[1].trim();
return sofar;
}, {});
obj.key = cookie.split("=", 1)[0];
obj.value = obj[obj.key];
delete obj[obj.key];
obj.Expires = new Date(obj.Expires);
(obj.Domain) ? getDomain.call(this, obj.Domain).$.push(obj) : domain.$.push(obj);
}, this);
return this;
};
CookieManager.prototype.get = function(url) {
// parsing url
var op = u2r(url),
isHTTP = !!op.protocol.match(/^http/),
isSecure = (op.protocol == "https"),
time = new Date().getTime(),
canditates = getCookieList.call(this, op.host),
path = op.path;
if (!canditates) return "";
var vals = canditates.reduce(function(sofar, cookieInfo) {
if (cookieInfo.HttpOnly && !isHTTP) return sofar;
if (cookieInfo.Secure && !isSecure) return sofar;
if (cookieInfo.Expires && cookieInfo.Expires.getTime() < time) return sofar;
if (cookieInfo.Path && path.indexOf(cookieInfo.Path) != 0) return sofar;
sofar[cookieInfo.key] = cookieInfo.value;
return sofar;
}, {});
return Object.keys(vals).reduce(function(arr, k) {
arr.push(k + "=" + vals[k]);
return arr;
}, []).join('; ');
};
function getCookieList(hostname) {
return hostname.split('.').reverse().reduce(function(sofar, dom) {
sofar.current = sofar.current[dom] || {};
if (sofar.current.$) sofar.current.$.forEach(function(v) { sofar.arr.push(v) });
return sofar;
}, {current: this.domains, arr: []}).arr;
}
function getDomain(hostname) {
return hostname.split(".").reverse().reduce(function(current, dom) {
if (!current[dom]) current[dom] = {$: []};
if (!dom) return current;
return current[dom];
}, this.domains);
}
module.exports = CookieManager;
module.exports = function(url, ret) {
if (typeof url != "string") {
throw new Error("type of url must be string");
}
var op = require('url').parse(url);
if (!op.hostname) {
if (op.pathname.charAt(0) != '/') {
var splits = op.pathname.split("/");
op.hostname = splits.shift();
op.pathname = splits.join("/");
}
}
ret || (ret = {});
ret.method = ret.method || 'GET';
ret.host = op.hostname;
ret.port = op.port || (op.protocol == 'https:') ? 443 : 80;
ret.path = ( op.pathname
? ((op.pathname.slice(0,1) == "/") ? "" : "/") + op.pathname + (op.search || "")
: "/");
ret.protocol = op.protocol ? op.protocol.slice(0, -1) : 'http';
if (ret.method != 'GET') {
ret.body = require('querystring').stringify(ret.data);
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment