Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve Cookies.prototype #60

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 70 additions & 74 deletions lib/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,65 @@ function Cookies(request, response, keys) {
}
}

Cookies.prototype = {
get: function(name, opts) {
var sigName = name + ".sig"
, header, match, value, remote, data, index
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys
Cookies.prototype.get = function(name, opts) {
var sigName = name + ".sig"
, header, match, value, remote, data, index
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys

header = this.request.headers["cookie"]
if (!header) return
header = this.request.headers["cookie"]
if (!header) return

match = header.match(getPattern(name))
if (!match) return
match = header.match(getPattern(name))
if (!match) return

value = match[1]
if (!opts || !signed) return value
value = match[1]
if (!opts || !signed) return value

remote = this.get(sigName)
if (!remote) return
remote = this.get(sigName)
if (!remote) return

data = name + "=" + value
if (!this.keys) throw new Error('.keys required for signed cookies');
index = this.keys.index(data, remote)

if (index < 0) {
this.set(sigName, null, {path: "/", signed: false })
} else {
index && this.set(sigName, this.keys.sign(data), { signed: false })
return value
}
},

set: function(name, value, opts) {
var res = this.response
, req = this.request
, headers = res.getHeader("Set-Cookie") || []
, secure = req.protocol === 'https' || req.connection.encrypted
, cookie = new Cookie(name, value, opts)
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys

if (typeof headers == "string") headers = [headers]

if (!secure && opts && opts.secure) {
throw new Error('Cannot send secure cookie over unencrypted connection')
}

cookie.secure = secure
if (opts && "secure" in opts) cookie.secure = opts.secure
if (opts && "secureProxy" in opts) cookie.secure = opts.secureProxy
headers = pushCookie(headers, cookie)
data = name + "=" + value
if (!this.keys) throw new Error('.keys required for signed cookies');
index = this.keys.index(data, remote)

if (index < 0) {
this.set(sigName, null, {path: "/", signed: false })
} else {
index && this.set(sigName, this.keys.sign(data), { signed: false })
return value
}
};

if (opts && signed) {
if (!this.keys) throw new Error('.keys required for signed cookies');
cookie.value = this.keys.sign(cookie.toString())
cookie.name += ".sig"
headers = pushCookie(headers, cookie)
}
Cookies.prototype.set = function(name, value, opts) {
var res = this.response
, req = this.request
, headers = res.getHeader("Set-Cookie") || []
, secure = req.protocol === 'https' || req.connection.encrypted
, cookie = new Cookie(name, value, opts)
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys

var setHeader = res.set ? http.OutgoingMessage.prototype.setHeader : res.setHeader
setHeader.call(res, 'Set-Cookie', headers)
return this
if (typeof headers == "string") headers = [headers]

if (!secure && opts && opts.secure) {
throw new Error('Cannot send secure cookie over unencrypted connection')
}
}

cookie.secure = secure
if (opts && "secure" in opts) cookie.secure = opts.secure
if (opts && "secureProxy" in opts) cookie.secure = opts.secureProxy
headers = pushCookie(headers, cookie)

if (opts && signed) {
if (!this.keys) throw new Error('.keys required for signed cookies');
cookie.value = this.keys.sign(cookie.toString())
cookie.name += ".sig"
headers = pushCookie(headers, cookie)
}

var setHeader = res.set ? http.OutgoingMessage.prototype.setHeader : res.setHeader
setHeader.call(res, 'Set-Cookie', headers)
return this
};

function Cookie(name, value, attrs) {
value || (this.expires = new Date(0))
Expand All @@ -88,32 +86,30 @@ function Cookie(name, value, attrs) {
for (var name in attrs) this[name] = attrs[name]
}

Cookie.prototype = {
path: "/",
expires: undefined,
domain: undefined,
httpOnly: true,
secure: false,
overwrite: false,
Cookie.prototype.path = "/";
Cookie.prototype.expires = undefined;
Cookie.prototype.domain = undefined;
Cookie.prototype.httpOnly = true;
Cookie.prototype.secure = false;
Cookie.prototype.overwrite = false;

toString: function() {
return this.name + "=" + this.value
},
Cookie.prototype.toString = function() {
return this.name + "=" + this.value
};

toHeader: function() {
var header = this.toString()
Cookie.prototype.toHeader = function() {
var header = this.toString()

if (this.maxAge) this.expires = new Date(Date.now() + this.maxAge);
if (this.maxAge) this.expires = new Date(Date.now() + this.maxAge);

if (this.path ) header += "; path=" + this.path
if (this.expires ) header += "; expires=" + this.expires.toUTCString()
if (this.domain ) header += "; domain=" + this.domain
if (this.secure ) header += "; secure"
if (this.httpOnly ) header += "; httponly"
if (this.path ) header += "; path=" + this.path
if (this.expires ) header += "; expires=" + this.expires.toUTCString()
if (this.domain ) header += "; domain=" + this.domain
if (this.secure ) header += "; secure"
if (this.httpOnly ) header += "; httponly"

return header
}
}
return header
};

// back-compat so maxage mirrors maxAge
Object.defineProperty(Cookie.prototype, 'maxage', {
Expand Down