Skip to content

Commit

Permalink
Fix cookies so that attributes are case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
vpulim committed Mar 14, 2012
1 parent 75ca7a2 commit 3b9f0fd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions tests/test-cookie.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var Cookie = require('../vendor/cookie')
, assert = require('assert');

var str = 'sid="s543qactge.wKE61E01Bs%2BKhzmxrwrnug="; path=/; httpOnly; expires=Sat, 04 Dec 2010 23:27:28 GMT';
var str = 'Sid="s543qactge.wKE61E01Bs%2BKhzmxrwrnug="; Path=/; httpOnly; Expires=Sat, 04 Dec 2010 23:27:28 GMT';
var cookie = new Cookie(str);

// test .toString()
Expand All @@ -14,7 +14,7 @@ assert.equal(cookie.path, '/');
assert.equal(cookie.httpOnly, true);

// test .name
assert.equal(cookie.name, 'sid');
assert.equal(cookie.name, 'Sid');

// test .value
assert.equal(cookie.value, '"s543qactge.wKE61E01Bs%2BKhzmxrwrnug="');
Expand Down
25 changes: 15 additions & 10 deletions vendor/cookie/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@ var url = require('url');
var Cookie = exports = module.exports = function Cookie(str, req) {
this.str = str;

// First key is the name
this.name = str.substr(0, str.indexOf('=')).trim();

// Map the key/val pairs
str.split(/ *; */).reduce(function(obj, pair){
var p = pair.indexOf('=');
if(p > 0)
obj[pair.substring(0, p).trim()] = pair.substring(p + 1).trim();
else
obj[pair.trim()] = true;
var key = p > 0 ? pair.substring(0, p).trim() : pair.trim();
var lowerCasedKey = key.toLowerCase();
var value = p > 0 ? pair.substring(p + 1).trim() : true;

if (!obj.name) {
// First key is the name
obj.name = key;
obj.value = value;
}
else if (lowerCasedKey === 'httponly') {
obj.httpOnly = value;
}
else {
obj[lowerCasedKey] = value;
}
return obj;
}, this);

// Assign value
this.value = this[this.name];

// Expires
this.expires = this.expires
? new Date(this.expires)
Expand Down

0 comments on commit 3b9f0fd

Please sign in to comment.