Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix cookies so that attributes are case insensitive

  • Loading branch information...
commit 3b9f0fd3da4ae74de9ec76e7c66c57a7f8641df2 1 parent 75ca7a2
Vinay Pulim authored
Showing with 17 additions and 12 deletions.
  1. +2 −2 tests/test-cookie.js
  2. +15 −10 vendor/cookie/index.js
View
4 tests/test-cookie.js
@@ -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()
@@ -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="');
View
25 vendor/cookie/index.js
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.