Skip to content

Commit

Permalink
Merge branch 'master' into encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Heckmann committed May 14, 2010
2 parents 4e68705 + 5dc152c commit 1aed6b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
23 changes: 16 additions & 7 deletions lib/express/plugins/cookie.js
Expand Up @@ -5,7 +5,8 @@
* Module dependencies.
*/

var Request = require('express/request').Request
var Request = require('express/request').Request,
queryString = require('querystring')

/**
* Parse an HTTP _cookie_ string into a hash.
Expand All @@ -16,10 +17,18 @@ var Request = require('express/request').Request
*/

exports.parseCookie = function(cookie) {
return cookie.replace(/^ *| *$/g, '').split(/ *; */).reduce(function(hash, pair){
var parts = pair.split(/ *= */)
hash[parts[0]] = parts[1]
return hash
return cookie.split(/[;,] */).reduce(function(cookies, pair) {
var eql = pair.indexOf('=')
if (eql === -1) return cookies
var key = queryString.unescape(pair.slice(0, eql).trim(), true)
var val = queryString.unescape(pair.slice(eql + 1).trim(), true)
var captures = val.match(/^("|')([^\1]*)\1$/)
if (captures) val = captures[2].replace('\\' + captures[1], captures[1])
//RFC2109 states the most specific path will be
//listed first
if (cookies[key] === undefined)
cookies[key] = val
return cookies
}, {})
}

Expand Down Expand Up @@ -115,7 +124,7 @@ exports.Cookie = Plugin.extend({
response: function(event) {
if (event.response.cookies &&
event.response.cookies.length)
event.request.header('Set-Cookie', event.response.cookies.join('\nSet-Cookie: '))
event.request.header('Set-Cookie', event.response.cookies.join('\r\nSet-Cookie: '))
}
}
})
})
2 changes: 1 addition & 1 deletion lib/support/haml
Submodule haml updated from 1dfd33 to 25c3e5
19 changes: 17 additions & 2 deletions spec/spec.plugins.cookie.js
Expand Up @@ -55,6 +55,21 @@ describe 'Express'
var attrs = ' SID = 1232431234234 ; data = foo'
parseCookie(attrs).should.eql { SID: '1232431234234', data: 'foo' }
end

it 'should support complex quoted values'
var attrs = 'SID="123456789"; fbs_0011223355="uid=0987654321&name=Test+User"'
parseCookie(attrs).should.eql { SID: '123456789', fbs_0011223355: 'uid=0987654321&name=Test User' }
end

it 'should not override when a duplicate key is found'
var attrs = 'SID=1234; SID=9999'
parseCookie(attrs).should.eql { SID: '1234' }
end

it 'should support malformed cookies'
var attrs = 'SID'
parseCookie(attrs).should.eql {}
end
end

describe 'on'
Expand Down Expand Up @@ -82,7 +97,7 @@ describe 'Express'
this.cookie('foo', 'bar')
return ''
})
get('/user').headers['Set-Cookie'].should.eql 'SID=732423sdfs73243; path=/; secure\nSet-Cookie: foo=bar; path=/'
get('/user').headers['Set-Cookie'].should.eql 'SID=732423sdfs73243; path=/; secure\r\nSet-Cookie: foo=bar; path=/'
end
end
end
Expand All @@ -97,4 +112,4 @@ describe 'Express'
end

end
end
end

0 comments on commit 1aed6b5

Please sign in to comment.