Cookie class

This commit is contained in:
斟酌 鵬兄 2015-04-15 12:17:21 +08:00
parent 097c9e160c
commit 1b361227e7
2 changed files with 54 additions and 0 deletions

50
net/Components/Cookie.js Normal file
View File

@ -0,0 +1,50 @@
var encodeCookie = function( cookie )
{
var cookieStr = "";
for( var i in cookie )
{
cookieStr += i + "=" + encodeURI( cookie[i] ) + ";";
}
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;

View File

@ -1,4 +1,5 @@
var Dragonfly = global.Dragonfly;
var Cookie = require( "./Components/Cookie" );
var HTTP = function( req, res )
{
@ -27,15 +28,18 @@ var HTTP = function( req, res )
}
}
, content: ''
, cookie: new Cookie( "", this )
, raw: res
};
this.request = {
uri: require('url').parse( req.url )
, isPost: ( req.method == 'POST' )
, cookie: new Cookie( req.headers.cookie, this )
, remoteAddr: req.connection.remoteAddress
, raw: req
};
};