Permalink
Browse files

rewrited cookie lib, fix stupid bug

  • Loading branch information...
Grigore147 committed Jan 7, 2013
1 parent 9352682 commit 96c016ea2cf2d7304362e07cbd33f5505a93ad98
Showing with 75 additions and 95 deletions.
  1. +75 −95 lib/cookie.js
View
@@ -1,121 +1,101 @@
-var Log = App.Log;
-
var sanitize = require('./utils').sanitize;
-var config = App.Config().cookie;
+var cookies = {};
-Class('Katana.Core.Cookie', {
- have: {
- data: {}
- },
-
- methods: {
- parse: function(Request) {
- var Cookies = this;
-
- if (Request.request.headers['cookie']) {
- var c = Request.request.headers['cookie'].split(';');
+cookies.CookieCollection = function (req) {
+ this.collection = {};
+ if (req.headers['cookie']) {
+ this.parse(req.headers['cookie'] || {});
+ }
+};
- var item, parsed, name, value;
+cookies.CookieCollection.prototype = new function () {
+ this.parse = function (header) {
+ var c = header.split(';');
+ var item;
+ var parsed, name, value;
+ for (var i = 0; i < c.length; i++) {
+ item = sanitize(c[i]).trim();
+ parsed = item.split('=');
+ name = parsed[0];
+ value = parsed[1];
+ this.collection[name] = new cookies.Cookie(name, value);
+ }
+ };
- for (var i=0; i<c.length; i++) {
- item = sanitize(c[i]).trim();
+ this.toArray = function () {
+ var send = [];
+ var c;
+ for (var p in this.collection) {
+ c = this.collection[p];
+ if (c.send) {
+ send.push(c.toString());
+ }
+ }
+ return send;
+ };
- parsed = item.split('=');
+ this.get = function (name) {
+ var c = this.collection[name] || {};
+ return c.value;
+ };
- name = parsed[0];
- value = parsed[1];
+ this.set = function (name, value, opts) {
+ var c = new cookies.Cookie(name, value, opts);
+ c.send = true;
+ this.collection[name] = c;
+ };
- Cookies.data[name] = new Cookie(name, value, config);
- }
- }
- },
-
- to_array: function() {
- var send = [];
-
- for (var cookie in this.data) {
- var c = this.data[cookie];
-
- if (c.send) {
- send.push(c.toString());
- }
- }
-
- return send;
- },
-
- get: function(name, def_value) {
- if (!name) { return this.data; }
-
- var cookie = this.data[name] !== undefined ? this.data[name].value : def_value;
-
- return cookie;
- },
-
- set: function(name, value, options) {
- this.data[name] = new Cookie(name, value, options);
- },
-
- delete: function(name) {
- this.set(name, null, { lifetime: 0 });
- }
- }
-});
+ this.delete = function(name) {
+ this.set(name, null, { expires: 0 });
+ };
+}();
-var Cookie = function(name, value, options) {
+cookies.Cookie = function (name, value, o) {
+ var opts = o || {};
this.name = name;
this.value = value;
-
- options = options || {};
- this.path = options.path || '/';
-
- this.expires = null;
-
- if (options.lifetime != null) {
- this.expires = new Date(Date.now() + options.lifetime).toGMTString();
+ this.path = opts.path || '/';
+ this.expires = opts.expires || null;
+ this.domain = opts.domain || null;
+ this.httpOnly = opts.httpOnly || false;
+ this.send = false;
+
+ if (opts.lifetime != null) {
+ this.expires = new Date(Date.now() + opts.lifetime).toGMTString();
}
-
- this.domain = options.domain || null;
- this.httpOnly = options.httpOnly || true;
- this.send = true;
-
- this.toString = function() {
- var arr = [this.name +'='+ this.value];
- var keys = ['path', 'expires', 'domain'];
-
- for (var i=0; i<keys.length; i++) {
- key = keys[i];
-
- if (this[key]) {
- arr.push(key +'='+ this[key]);
- }
- }
-
- if (this.httpOnly) {
- arr.push('HttpOnly');
+};
+
+cookies.Cookie.prototype.toString = function () {
+ var res = [this.name + '=' + this.value];
+ var keys = ['path', 'expires', 'domain'];
+ var key;
+ var str;
+ for (var i = 0; i < keys.length; i++) {
+ key = keys[i];
+ if (this[key]) {
+ res.push(key + '=' + this[key]);
}
-
- return arr.join('; ');
}
-}
+ if (this.httpOnly) {
+ res.push('HttpOnly');
+ }
+ str = res.join('; ');
+ return str;
+};
+
+module.exports = cookies;
App.on('request', function(Request, Response, callback) {
- var Cookies = new Katana.Core.Cookie;
-
- Cookies.parse(Request);
-
- Request.cookie = Cookies;
+ Request.cookie = new cookies.CookieCollection(Request.request);
callback();
});
App.on('send_response', function(Response, Request, callback) {
App.series('cookie.send', function() {
- Response.header('Set-Cookie', Request.cookie.to_array());
+ Response.header('Set-Cookie', Request.cookie.toArray());
callback();
}, Response, Request);
});
-
-module.exports = Katana.Core.Cookie;

0 comments on commit 96c016e

Please sign in to comment.