From c70b8cb5bf6a68037ad0220b284a67fff6c7b912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 5 Oct 2017 18:05:46 +0200 Subject: [PATCH] Set X-Served-By-DB-Host header in db-conn-setup middleware --- lib/cartodb/controllers/base.js | 4 ---- lib/cartodb/middleware/context/db-conn-setup.js | 4 +++- lib/cartodb/middleware/error-middleware.js | 4 ---- test/unit/cartodb/prepare-context.test.js | 4 ++-- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/cartodb/controllers/base.js b/lib/cartodb/controllers/base.js index 7462f6ba..9a502bd4 100644 --- a/lib/cartodb/controllers/base.js +++ b/lib/cartodb/controllers/base.js @@ -7,10 +7,6 @@ module.exports = BaseController; // jshint maxcomplexity:9 BaseController.prototype.send = function(req, res, body, status, headers) { - if (req.params.dbhost) { - res.set('X-Served-By-DB-Host', req.params.dbhost); - } - res.set('X-Tiler-Profiler', req.profiler.toJSONString()); if (headers) { diff --git a/lib/cartodb/middleware/context/db-conn-setup.js b/lib/cartodb/middleware/context/db-conn-setup.js index 8b05d36b..f1974ce4 100644 --- a/lib/cartodb/middleware/context/db-conn-setup.js +++ b/lib/cartodb/middleware/context/db-conn-setup.js @@ -1,7 +1,7 @@ const _ = require('underscore'); module.exports = function dbConnSetupMiddleware(pgConnection) { - return function (req, res, next) { + return function dbConnSetup(req, res, next) { const user = req.context.user; // FIXME: this function shouldn't be able to change `req.params`. It should return an @@ -24,6 +24,8 @@ module.exports = function dbConnSetupMiddleware(pgConnection) { dbport: global.environment.postgres.port }); + res.set('X-Served-By-DB-Host', req.params.dbhost); + // FIXME: Temporary hack to share data between middlewares. Express overrides req.params to // parse url params to an object and it's performed after matching path and controller. if (!res.locals) { diff --git a/lib/cartodb/middleware/error-middleware.js b/lib/cartodb/middleware/error-middleware.js index 71f6c411..fd19e24f 100644 --- a/lib/cartodb/middleware/error-middleware.js +++ b/lib/cartodb/middleware/error-middleware.js @@ -31,10 +31,6 @@ module.exports = function errorMiddleware (/* options */) { errors_with_context: allErrors.map(errorMessageWithContext) }; - if (res.locals && res.locals.dbhost) { - res.set('X-Served-By-DB-Host', res.locals.dbhost); - } - res.set('X-Tiler-Profiler', req.profiler.toJSONString()); res.status(statusCode); diff --git a/test/unit/cartodb/prepare-context.test.js b/test/unit/cartodb/prepare-context.test.js index af40f1da..1ae4ffe2 100644 --- a/test/unit/cartodb/prepare-context.test.js +++ b/test/unit/cartodb/prepare-context.test.js @@ -68,7 +68,7 @@ describe('prepare-context', function() { it('sets dbname from redis metadata', function(done){ var req = {headers: { host:'localhost' }, query: {}, locals: {} }; - var res = {}; + var res = { set: function () {} }; dbConnSetup(prepareRequest(req), res, function(err) { if ( err ) { done(err); return; } @@ -84,7 +84,7 @@ describe('prepare-context', function() { it('sets also dbuser for authenticated requests', function(done){ var req = { headers: { host: 'localhost' }, query: { map_key: '1234' }, locals: {} }; - var res = {}; + var res = { set: function () {} }; // FIXME: review authorize-pgconnsetup workflow, It might we are doing authorization twice. authorize(prepareRequest(req), res, function (err) {