Skip to content

Commit

Permalink
Closes #1017
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Aug 15, 2013
1 parent a44f5b1 commit 7c3dd38
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/response/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.location = function (uri, request) {
};


exports.cache = function (response, request) {
exports.cache = function (response, request, passThrough) {

var ttl = response.getTtl();

Expand All @@ -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');
}
};
Expand Down
61 changes: 31 additions & 30 deletions lib/response/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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']) {
Expand Down
7 changes: 4 additions & 3 deletions test/integration/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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 } } },
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 7c3dd38

Please sign in to comment.