Skip to content

Commit

Permalink
Added response "header" event allowing augmentation
Browse files Browse the repository at this point in the history
this will be used in the session middleware, and could
be used elsewhere. Ideally Node would provide a hook for us...
  • Loading branch information
tj committed Jul 20, 2011
1 parent 04af2c6 commit 1ce13cb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 29 deletions.
74 changes: 45 additions & 29 deletions lib/patch.js
Expand Up @@ -16,36 +16,52 @@ var http = require('http')


var setHeader = res.setHeader; var setHeader = res.setHeader;


// original _renderHeaders()


if (!res._hasConnectPatch) { var _renderHeaders = res._renderHeaders;


/** if (res._hasConnectPatch) return;
* Set header `field` to `val`, special-casing
* the `Set-Cookie` field for multiple support. /**
* * Set header `field` to `val`, special-casing
* @param {String} field * the `Set-Cookie` field for multiple support.
* @param {String} val *
* @api public * @param {String} field
*/ * @param {String} val

* @api public
res.setHeader = function(field, val){ */
var key = field.toLowerCase()
, prev; res.setHeader = function(field, val){

var key = field.toLowerCase()
// special-case Set-Cookie , prev;
if (this._headers && 'set-cookie' == key) {
if (prev = this.getHeader(field)) { // special-case Set-Cookie
val = Array.isArray(prev) if (this._headers && 'set-cookie' == key) {
? prev.concat(val) if (prev = this.getHeader(field)) {
: [prev, val]; val = Array.isArray(prev)
} ? prev.concat(val)
// charset : [prev, val];
} else if ('content-type' == key && this.charset) {
val += '; charset=' + this.charset;
} }
// charset
} else if ('content-type' == key && this.charset) {
val += '; charset=' + this.charset;
}

return setHeader.call(this, field, val);
};

/**
* Proxy `res.end()` to expose a 'header' event,
* allowing arbitrary augmentation before the header
* fields are written to the socket.
*
* NOTE: this _only_ supports node's progressive header
* field API aka `res.setHeader()`.
*/


return setHeader.call(this, field, val); res._renderHeaders = function(){
}; this.emit('header');
return _renderHeaders.call(this);
};


res._hasConnectPatch = true; res._hasConnectPatch = true;
}
31 changes: 31 additions & 0 deletions test/connect.test.js
Expand Up @@ -52,6 +52,37 @@ module.exports = {
{ body: 'blog', status: 200 }); { body: 'blog', status: 200 });
}, },


'test "header" event': function(){
var app = connect.createServer();

app.use(function(req, res, next){
res.on('header', function(){
if (req.headers['x-foo']) {
res.setHeader('X-Bar', 'baz');
}
});

next();
});

app.use(function(req, res){
// FIXME: this fails if you do not have any res._headers
res.setHeader('Content-Length', 5);
res.end('hello');
});

assert.response(app,
{ url: '/' },
function(res){
res.headers.should.not.have.property('x-foo');
res.headers.should.not.have.property('x-bar');
});

assert.response(app,
{ url: '/', headers: { 'X-Foo': 'bar' }},
{ body: 'hello', headers: { 'X-Bar': 'baz' }});
},

'test path matching': function(){ 'test path matching': function(){
var n = 0 var n = 0
, app = connect.createServer(); , app = connect.createServer();
Expand Down

0 comments on commit 1ce13cb

Please sign in to comment.