From ef86bacf7fa53728fa1fc36e4d8067de203a28b3 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 17 Sep 2015 02:03:09 +0200 Subject: [PATCH] Set headers with set method --- lib/cartodb/cache/surrogate_keys_cache.js | 4 ++-- lib/cartodb/controllers/base.js | 21 +++++++++++---------- lib/cartodb/controllers/layergroup.js | 6 +++--- lib/cartodb/controllers/map.js | 8 ++++---- lib/cartodb/controllers/named_maps.js | 10 +++++----- lib/cartodb/middleware/cors.js | 6 +++--- lib/cartodb/server.js | 2 +- 7 files changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/cartodb/cache/surrogate_keys_cache.js b/lib/cartodb/cache/surrogate_keys_cache.js index 237ac906..907e6345 100644 --- a/lib/cartodb/cache/surrogate_keys_cache.js +++ b/lib/cartodb/cache/surrogate_keys_cache.js @@ -17,8 +17,8 @@ module.exports = SurrogateKeysCache; */ SurrogateKeysCache.prototype.tag = function(response, cacheObject) { var newKey = cacheObject.key(); - response.header('Surrogate-Key', appendSurrogateKey( - response.header('Surrogate-Key'), + response.set('Surrogate-Key', appendSurrogateKey( + response.get('Surrogate-Key'), Array.isArray(newKey) ? cacheObject.key().join(' ') : newKey )); diff --git a/lib/cartodb/controllers/base.js b/lib/cartodb/controllers/base.js index 7860e1ed..f9df51d9 100644 --- a/lib/cartodb/controllers/base.js +++ b/lib/cartodb/controllers/base.js @@ -149,18 +149,18 @@ BaseController.prototype.req2params = function(req, callback){ }; // jshint maxcomplexity:6 - -BaseController.prototype.send = function(req, res, args) { - if (global.environment && global.environment.api_hostname) { - res.header('X-Served-By-Host', global.environment.api_hostname); - } - - if (req.params && req.params.dbhost) { - res.header('X-Served-By-DB-Host', req.params.dbhost); +// 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); } if (req.profiler) { - res.header('X-Tiler-Profiler', req.profiler.toJSONString()); + res.set('X-Tiler-Profiler', req.profiler.toJSONString()); + } + + if (headers) { + res.set(headers); } res.send.apply(res, args); @@ -175,6 +175,7 @@ BaseController.prototype.send = function(req, res, args) { } } }; +// jshint maxcomplexity:6 BaseController.prototype.sendError = function(req, res, err, label) { label = label || 'UNKNOWN'; @@ -204,7 +205,7 @@ BaseController.prototype.sendError = function(req, res, err, label) { var errorResponseBody = { errors: [message] }; - this.send(req, res, [errorResponseBody, statusCode]); + this.send(req, res, errorResponseBody, statusCode); }; function findStatusCode(err) { diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 59e7a98b..0fe8ffa3 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -221,7 +221,7 @@ LayergroupController.prototype.staticMap = function(req, res, width, height, zoo LayergroupController.prototype.sendResponse = function(req, res, args) { var self = this; - res.header('Cache-Control', 'public,max-age=31536000'); + res.set('Cache-Control', 'public,max-age=31536000'); // Set Last-Modified header var lastUpdated; @@ -231,7 +231,7 @@ LayergroupController.prototype.sendResponse = function(req, res, args) { } else { lastUpdated = new Date(); } - res.header('Last-Modified', lastUpdated.toUTCString()); + res.set('Last-Modified', lastUpdated.toUTCString()); var dbName = req.params.dbname; step( @@ -245,7 +245,7 @@ LayergroupController.prototype.sendResponse = function(req, res, args) { } if (!!affectedTables) { var tablesCacheEntry = new TablesCacheEntry(dbName, affectedTables); - res.header('X-Cache-Channel', tablesCacheEntry.getCacheChannel()); + res.set('X-Cache-Channel', tablesCacheEntry.getCacheChannel()); self.surrogateKeysCache.tag(res, tablesCacheEntry); } self.send(req, res, args); diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index ffa776a7..f7054912 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -223,7 +223,7 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn var templateHash = self.templateMaps.fingerPrint(mapConfigProvider.template).substring(0, 8); layergroup.layergroupid = cdbuser + '@' + templateHash + '@' + layergroup.layergroupid; - res.header('X-Layergroup-Id', layergroup.layergroupid); + res.set('X-Layergroup-Id', layergroup.layergroupid); self.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(cdbuser, mapConfigProvider.getTemplateName())); self.send(req, res, [layergroup, 200]); @@ -309,9 +309,9 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, la if (req.method === 'GET') { var tableCacheEntry = new TablesCacheEntry(dbName, result.affectedTables); var ttl = global.environment.varnish.layergroupTtl || 86400; - res.header('Cache-Control', 'public,max-age='+ttl+',must-revalidate'); - res.header('Last-Modified', (new Date()).toUTCString()); - res.header('X-Cache-Channel', tableCacheEntry.getCacheChannel()); + res.set('Cache-Control', 'public,max-age='+ttl+',must-revalidate'); + res.set('Last-Modified', (new Date()).toUTCString()); + res.set('X-Cache-Channel', tableCacheEntry.getCacheChannel()); if (result.affectedTables && result.affectedTables.length > 0) { self.surrogateKeysCache.tag(res, tableCacheEntry); } diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index d921b956..51b0e1cf 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -35,8 +35,8 @@ NamedMapsController.prototype.register = function(app) { NamedMapsController.prototype.sendResponse = function(req, res, resource, headers, namedMapProvider) { this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(req.context.user, namedMapProvider.getTemplateName())); - res.header('Content-Type', headers['content-type'] || headers['Content-Type'] || 'image/png'); - res.header('Cache-Control', 'public,max-age=7200,must-revalidate'); + res.set('Content-Type', headers['content-type'] || headers['Content-Type'] || 'image/png'); + res.set('Cache-Control', 'public,max-age=7200,must-revalidate'); var self = this; @@ -52,7 +52,7 @@ NamedMapsController.prototype.sendResponse = function(req, res, resource, header } if (!result || !!result.affectedTables) { // we increase cache control as we can invalidate it - res.header('Cache-Control', 'public,max-age=31536000'); + res.set('Cache-Control', 'public,max-age=31536000'); var lastModifiedDate; if (Number.isFinite(result.lastUpdatedTime)) { @@ -60,10 +60,10 @@ NamedMapsController.prototype.sendResponse = function(req, res, resource, header } else { lastModifiedDate = new Date(); } - res.header('Last-Modified', lastModifiedDate.toUTCString()); + res.set('Last-Modified', lastModifiedDate.toUTCString()); var tablesCacheEntry = new TablesCacheEntry(dbName, result.affectedTables); - res.header('X-Cache-Channel', tablesCacheEntry.getCacheChannel()); + res.set('X-Cache-Channel', tablesCacheEntry.getCacheChannel()); if (result.affectedTables.length > 0) { self.surrogateKeysCache.tag(res, tablesCacheEntry); } diff --git a/lib/cartodb/middleware/cors.js b/lib/cartodb/middleware/cors.js index f34e4d42..227bb477 100644 --- a/lib/cartodb/middleware/cors.js +++ b/lib/cartodb/middleware/cors.js @@ -4,8 +4,8 @@ module.exports = function cors(extraHeaders) { if(extraHeaders) { baseHeaders += ", " + extraHeaders; } - res.header("Access-Control-Allow-Origin", "*"); - res.header("Access-Control-Allow-Headers", baseHeaders); + res.set("Access-Control-Allow-Origin", "*"); + res.set("Access-Control-Allow-Headers", baseHeaders); next(); }; -}; \ No newline at end of file +}; diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index c4e98f88..4ae759b3 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -256,7 +256,7 @@ function bootstrap(opts) { }); if (global.environment && global.environment.api_hostname) { - res.header('X-Served-By-Host', global.environment.api_hostname); + res.set('X-Served-By-Host', global.environment.api_hostname); } next();