BotanSS/net/components/Cookie.js

65 lines
1.1 KiB
JavaScript
Raw Normal View History

2015-04-15 04:17:21 +00:00
var encodeCookie = function( cookie )
{
var cookieStr = "";
2015-04-15 10:38:44 +00:00
var p = "";
2015-04-18 10:42:18 +00:00
var e = "";
2015-04-15 04:17:21 +00:00
for( var i in cookie )
{
2015-04-18 10:42:18 +00:00
switch( i.toLowerCase() )
2015-04-15 10:38:44 +00:00
{
2015-04-18 10:42:18 +00:00
case "path":
p = cookie[i];
continue;
case "expires":
e = cookie[i];
continue;
2015-04-15 10:38:44 +00:00
}
2015-04-18 10:42:18 +00:00
cookieStr += i + "=" + cookie[i] + ";";
2015-04-15 04:17:21 +00:00
}
2015-04-15 10:38:44 +00:00
// Path at tail
2015-04-18 10:42:18 +00:00
cookieStr += "Path=" + p + ";" + " Expires=" + e + ";";
2015-04-15 10:38:44 +00:00
2015-04-15 04:17:21 +00:00
return cookieStr;
};
var Cookie = function( cookieStr, HTTP )
{
var list = {};
cookieStr && cookieStr.split( ";" ).forEach( function( cookie )
{
var parts = cookie.split( "=" );
list[ parts.shift().trim() ] = decodeURI( parts.join( "=" ) );
} );
this.__cookie = list;
this.HTTP = HTTP;
};
Cookie.prototype.set = function( name, value )
{
this.__cookie[ name ] = value;
};
Cookie.prototype.seth = function( name, value )
{
this.set( name, value );
this.HTTP.response.headers[ "Set-Cookie" ] = this.toString();
};
Cookie.prototype.get = function( name )
{
return this.__cookie[ name ];
};
Cookie.prototype.toString = function()
{
return encodeCookie( this.__cookie );
};
module.exports = Cookie;