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

Auto cookie value #717

Merged
merged 1 commit into from
Mar 28, 2013
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,7 @@ Cookies can be set directly via the response _'state(name, value, options)'_ int
- `isHttpOnly` - sets the 'HttpOnly' flag.
- `path` - the path scope.
- `domain` - the domain scope.
- `autoValue` - if present and the cookie was not received or explicitly set by the route handler, the cookie is automatically added to the response with the provided value. Used only when the state definition is registered via `server.state(name, options)`.
- `encoding` - encoding performs on the provided value before serialization. Options are:
- 'none' - no encoding. This is the default value. Value must be a string.
- 'base64' - string value is encoded using Base64.
Expand All @@ -1592,7 +1593,7 @@ Cookies can be set directly via the response _'state(name, value, options)'_ int

Cookie definitions can be registered with the server using the server's _'state(name, options)'_ method, where 'options' is the same as above.
If a cookie definition is found, the options are used for that cookie as defaults before other options specified at the time of state() invocation
are applied. In addition, the `encoding` option is used when receiving a cookie from the client to parse the cookie's value.
are applied. In addition, the `encoding` option is used when receiving a cookie from the client to parse the cookie's value, and `autoSet`


## Server Logging
Expand Down
1 change: 1 addition & 0 deletions lib/response/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ internals.Base.prototype.state = function (name, value, options) { // o
};

if (options) {
Utils.assert(!options.autoValue, 'Cannot set autoValue directly in a response');
state.options = Utils.clone(options);
}

Expand Down
15 changes: 14 additions & 1 deletion lib/response/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,32 @@ exports.state = function (response, request, callback) {

// Merge response cookies with request cookies (set while response wasn't ready)

var names = {};
var states = [];
Object.keys(response._states).forEach(function (name) {

names[name] = true;
states.push(response._states[name]);
});

Object.keys(request._states).forEach(function (name) {

if (!response._states[name]) {
if (!names[name]) {
names[name] = true;
states.push(request._states[name]);
}
});

Object.keys(request.server._stateDefinitions).forEach(function (name) {

if (request.server._stateDefinitions[name].autoValue &&
!names[name]) {

names[name] = true;
states.push({ name: name, value: request.server._stateDefinitions[name].autoValue});
}
});

if (!states.length) {
return callback();
}
Expand Down
3 changes: 2 additions & 1 deletion test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('Response', function () {
server.route({ method: 'GET', path: '/', config: { handler: handler, cache: { expiresIn: 9999 } } });
server.route({ method: 'GET', path: '/bound', config: { handler: handlerBound } });
server.state('sid', { encoding: 'base64' });
server.state('always', { autoValue: 'present' });
server.ext('onPostHandler', function (request, next) {

request.setState('test', '123');
Expand All @@ -64,7 +65,7 @@ describe('Response', function () {
expect(res.headers['cache-control']).to.equal('max-age=1, must-revalidate');
expect(res.headers['access-control-allow-origin']).to.equal('test.example.com www.example.com');
expect(res.headers['access-control-allow-credentials']).to.not.exist;
expect(res.headers['set-cookie']).to.deep.equal(['sid=YWJjZGVmZzEyMzQ1Ng==', 'other=something; Secure', 'x=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', "test=123", "empty=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT"]);
expect(res.headers['set-cookie']).to.deep.equal(['sid=YWJjZGVmZzEyMzQ1Ng==', 'other=something; Secure', 'x=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', "test=123", "empty=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT", "always=present"]);

server.inject({ method: 'GET', url: '/bound', headers: { origin: 'www.example.com' } }, function (res) {

Expand Down