diff --git a/lib/response/headers.js b/lib/response/headers.js index d1615baaa..9d7706a8b 100755 --- a/lib/response/headers.js +++ b/lib/response/headers.js @@ -18,7 +18,7 @@ exports.location = function (uri, request) { }; -exports.cache = function (response, request) { +exports.cache = function (response, request, passThrough) { var ttl = response.getTtl(); @@ -41,7 +41,7 @@ exports.cache = function (response, request) { var privacy = request.route.cache.privacy; response.header('Cache-Control', 'max-age=' + Math.floor(ttl / 1000) + ', must-revalidate' + (privacy !== 'default' ? ', ' + privacy : '')); } - else { + else if (!passThrough) { response.header('Cache-Control', 'no-cache'); } }; diff --git a/lib/response/stream.js b/lib/response/stream.js index 7f2492bd1..4721c22a3 100755 --- a/lib/response/stream.js +++ b/lib/response/stream.js @@ -73,11 +73,15 @@ internals.Stream.prototype._prepare = function (request, callback) { this._wasPrepared = true; + if (this._passThrough.code) { + this._code = this._passThrough.code; + } + if (this._flags.location) { this._headers.Location = Headers.location(this._flags.location, request); } - Headers.cache(this, request); + Headers.cache(this, request, (self._passThrough.headers && self._passThrough.headers['cache-control'])); Headers.cors(this, request); Headers.state(this, request, function (err) { @@ -87,44 +91,41 @@ internals.Stream.prototype._prepare = function (request, callback) { Headers.auth(self, request, function (err) { - return callback(err || self); - }); - }); -}; - + if (!err) { -internals.Stream.prototype._transmit = function (request, callback) { + // Apply passthrough headers - var self = this; + if (self._passThrough.headers && + (!request._route.proxy || request._route.proxy.settings.passThrough)) { - // Apply passthrough code and headers + var localCookies = Utils.clone(self._headers['Set-Cookie']); + var localHeaders = self._headers; + self._headers = Utils.clone(self._passThrough.headers); + Utils.merge(self._headers, localHeaders); - if (this._passThrough.headers && - (!request._route.proxy || request._route.proxy.settings.passThrough)) { + if (localCookies) { + var headerKeys = Object.keys(self._passThrough.headers); + for (var i = 0, il = headerKeys.length; i < il; ++i) { - if (this._passThrough.headers) { - var localCookies = Utils.clone(this._headers['Set-Cookie']); - var localHeaders = this._headers; - this._headers = Utils.clone(this._passThrough.headers); - Utils.merge(this._headers, localHeaders); - - if (localCookies) { - var headerKeys = Object.keys(this._passThrough.headers); - for (var i = 0, il = headerKeys.length; i < il; ++i) { - - if (headerKeys[i].toLowerCase() === 'set-cookie') { - delete this._headers[headerKeys[i]]; - this._headers['Set-Cookie'] = [].concat(this._passThrough.headers[headerKeys[i]]).concat(localCookies); - break; + if (headerKeys[i].toLowerCase() === 'set-cookie') { + delete self._headers[headerKeys[i]]; + self._headers['Set-Cookie'] = [].concat(self._passThrough.headers[headerKeys[i]]).concat(localCookies); + break; + } + } } } } - } - } - if (this._passThrough.code) { - this._code = this._passThrough.code; - } + return callback(err || self); + }); + }); +}; + + +internals.Stream.prototype._transmit = function (request, callback) { + + var self = this; var encoder = null; if (!this._headers['content-encoding']) { diff --git a/test/integration/proxy.js b/test/integration/proxy.js index 265cf8114..4749c3c1b 100755 --- a/test/integration/proxy.js +++ b/test/integration/proxy.js @@ -145,7 +145,7 @@ describe('Proxy', function () { var upstream = new Hapi.Server(0); upstream.route([ - { method: 'GET', path: '/profile', handler: profile }, + { method: 'GET', path: '/profile', handler: profile, config: { cache: { expiresIn: 2000 } } }, { method: 'GET', path: '/item', handler: activeItem }, { method: 'GET', path: '/proxyerror', handler: activeItem }, { method: 'POST', path: '/item', handler: item }, @@ -235,7 +235,7 @@ describe('Proxy', function () { { method: 'GET', path: '/sslDefault', handler: { proxy: { mapUri: mapSslUri } } } ]); - timeoutServer = new Hapi.Server(0, { timeout: { server: 5 }}); + timeoutServer = new Hapi.Server(0, { timeout: { server: 5 } }); timeoutServer.route([ { method: 'GET', path: '/timeout1', handler: { proxy: { host: 'localhost', port: backendPort, timeout: 15 } } }, { method: 'GET', path: '/timeout2', handler: { proxy: { host: 'localhost', port: backendPort, timeout: 2 } } }, @@ -292,6 +292,7 @@ describe('Proxy', function () { expect(res.statusCode).to.equal(200); expect(res.payload).to.contain('John Doe'); expect(res.headers['set-cookie']).to.deep.equal(['test=123', 'auto=xyz']); + expect(res.headers['cache-control']).to.equal('max-age=2, must-revalidate'); server.inject('/profile', function (res) { @@ -621,7 +622,7 @@ describe('Proxy', function () { var client = Http.get('http://127.0.0.1:' + server.info.port + '/profile', function (res) { - res.on('data', function () {}); + res.on('data', function () { }); }); client.once('socket', function () {