Manage cors with a middleware

This commit is contained in:
Raul Ochoa
2015-07-08 13:27:56 +02:00
parent 8cd3807100
commit fa84813a37
7 changed files with 38 additions and 68 deletions
+9 -19
View File
@@ -2,6 +2,8 @@ var assert = require('assert');
var step = require('step');
var cors = require('../middleware/cors');
/**
* @param app
* @param {MapBackend} mapBackend
@@ -18,29 +20,20 @@ module.exports = MapController;
MapController.prototype.register = function(app) {
app.get(app.base_url_mapconfig + '/:token/:z/:x/:y@:scale_factor?x.:format', this.tile.bind(this));
app.get(app.base_url_mapconfig + '/:token/:z/:x/:y.:format', this.tile.bind(this));
app.get(app.base_url_mapconfig + '/:token/:layer/:z/:x/:y.(:format)', this.layer.bind(this));
app.options(app.base_url_mapconfig, this.cors.bind(this));
app.get(app.base_url_mapconfig, this.createGet.bind(this));
app.post(app.base_url_mapconfig, this.createPost.bind(this));
app.get(app.base_url_mapconfig + '/:token/:layer/attributes/:fid', this.attributes.bind(this));
app.get(app.base_url_mapconfig + '/:token/:z/:x/:y@:scale_factor?x.:format', cors(), this.tile.bind(this));
app.get(app.base_url_mapconfig + '/:token/:z/:x/:y.:format', cors(), this.tile.bind(this));
app.get(app.base_url_mapconfig + '/:token/:layer/:z/:x/:y.(:format)', cors(), this.layer.bind(this));
app.options(app.base_url_mapconfig, cors('Content-Type'));
app.get(app.base_url_mapconfig, cors(), this.createGet.bind(this));
app.post(app.base_url_mapconfig, cors(), this.createPost.bind(this));
app.get(app.base_url_mapconfig + '/:token/:layer/attributes/:fid', cors(), this.attributes.bind(this));
};
// send CORS headers when client send options.
MapController.prototype.cors = function(req, res, next) {
this._app.doCORS(res, "Content-Type");
return next();
};
// Gets attributes for a given layer feature
MapController.prototype.attributes = function(req, res) {
var self = this;
req.profiler.start('windshaft.maplayer_attribute');
this._app.doCORS(res);
step(
function setupParams() {
self._app.req2params(req, this);
@@ -71,8 +64,6 @@ MapController.prototype.attributes = function(req, res) {
MapController.prototype.create = function(req, res, prepareConfigFn) {
var self = this;
this._app.doCORS(res);
var layergroupDecorator = {
beforeLayergroupCreate: function(requestMapConfig, callback) {
self._layergroupRequestDecorator.beforeLayergroupCreate(req, requestMapConfig, callback);
@@ -146,7 +137,6 @@ MapController.prototype.layer = function(req, res, next) {
MapController.prototype.tileOrLayer = function (req, res) {
var self = this;
this._app.doCORS(res);
step(
function mapController$prepareParams() {
self._app.req2params(req, this);
+4 -13
View File
@@ -4,6 +4,7 @@ var _ = require('underscore');
var templateName = require('../backends/template_maps').templateName;
var CdbRequest = require('../models/cdb_request');
var NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
var cors = require('../middleware/cors');
function NamedMapsController(app, templateMaps, metadataBackend, mapBackend, templateBaseUrl, surrogateKeysCache,
layergroupRequestDecorator) {
@@ -21,17 +22,14 @@ module.exports = NamedMapsController;
var cdbRequest = new CdbRequest();
NamedMapsController.prototype.register = function(app) {
app.get(this.templateBaseUrl + '/:template_id/:layer/:z/:x/:y.:format', this.tile.bind(this));
app.get(this.templateBaseUrl + '/:template_id/jsonp', this.jsonp.bind(this));
app.options(this.templateBaseUrl + '/:template_id', this.options.bind(this));
app.post(this.templateBaseUrl + '/:template_id', this.instantiate.bind(this));
app.get(this.templateBaseUrl + '/:template_id/:layer/:z/:x/:y.:format', cors(), this.tile.bind(this));
app.get(this.templateBaseUrl + '/:template_id/jsonp', cors(), this.jsonp.bind(this));
app.post(this.templateBaseUrl + '/:template_id', cors(), this.instantiate.bind(this));
};
NamedMapsController.prototype.tile = function(req, res) {
var self = this;
this.app.doCORS(res);
var cdbUser = cdbRequest.userByReq(req);
var template;
var layergroupConfig;
@@ -172,11 +170,6 @@ NamedMapsController.prototype.instantiate = function(req, res) {
);
};
NamedMapsController.prototype.options = function(req, res, next) {
this.app.doCORS(res, "Content-Type");
return next();
};
/**
* jsonp endpoint, allows to instantiate a template with a json call.
* callback query argument is mandatory
@@ -212,8 +205,6 @@ NamedMapsController.prototype.jsonp = function(req, res) {
NamedMapsController.prototype.instantiateTemplate = function(req, res, template_params, callback) {
var self = this;
this.app.doCORS(res);
var layergroupDecorator = {
beforeLayergroupCreate: function(requestMapConfig, callback) {
self.layergroupRequestDecorator.beforeLayergroupCreate(req, requestMapConfig, callback);
+7 -19
View File
@@ -3,6 +3,7 @@ var assert = require('assert');
var _ = require('underscore');
var templateName = require('../backends/template_maps').templateName;
var CdbRequest = require('../models/cdb_request');
var cors = require('../middleware/cors');
function NamedMapsAdminController(app, templateMaps, templateBaseUrl) {
@@ -16,19 +17,17 @@ module.exports = NamedMapsAdminController;
var cdbRequest = new CdbRequest();
NamedMapsAdminController.prototype.register = function(app) {
app.post(this.templateBaseUrl, this.create.bind(this));
app.put(this.templateBaseUrl + '/:template_id', this.update.bind(this));
app.get(this.templateBaseUrl + '/:template_id', this.retrieve.bind(this));
app.del(this.templateBaseUrl + '/:template_id', this.destroy.bind(this));
app.get(this.templateBaseUrl, this.list.bind(this));
app.options(this.templateBaseUrl + '/:template_id', this.options.bind(this));
app.post(this.templateBaseUrl, cors(), this.create.bind(this));
app.put(this.templateBaseUrl + '/:template_id', cors(), this.update.bind(this));
app.get(this.templateBaseUrl + '/:template_id', cors(), this.retrieve.bind(this));
app.del(this.templateBaseUrl + '/:template_id', cors(), this.destroy.bind(this));
app.get(this.templateBaseUrl, cors(), this.list.bind(this));
app.options(this.templateBaseUrl + '/:template_id', cors('Content-Type'));
};
NamedMapsAdminController.prototype.create = function(req, res) {
var self = this;
this.app.doCORS(res);
var cdbuser = cdbRequest.userByReq(req);
step(
@@ -53,8 +52,6 @@ NamedMapsAdminController.prototype.create = function(req, res) {
NamedMapsAdminController.prototype.update = function(req, res) {
var self = this;
this.app.doCORS(res);
var cdbuser = cdbRequest.userByReq(req);
var template;
var tpl_id;
@@ -87,8 +84,6 @@ NamedMapsAdminController.prototype.retrieve = function(req, res) {
req.profiler.start('windshaft-cartodb.get_template');
}
this.app.doCORS(res);
var cdbuser = cdbRequest.userByReq(req);
var tpl_id;
step(
@@ -124,7 +119,6 @@ NamedMapsAdminController.prototype.destroy = function(req, res) {
if (req.profiler) {
req.profiler.start('windshaft-cartodb.delete_template');
}
this.app.doCORS(res);
var cdbuser = cdbRequest.userByReq(req);
var tpl_id;
@@ -152,7 +146,6 @@ NamedMapsAdminController.prototype.list = function(req, res) {
if ( req.profiler ) {
req.profiler.start('windshaft-cartodb.get_template_list');
}
this.app.doCORS(res);
var cdbuser = cdbRequest.userByReq(req);
@@ -174,11 +167,6 @@ NamedMapsAdminController.prototype.list = function(req, res) {
);
};
NamedMapsAdminController.prototype.options = function(req, res, next) {
this.app.doCORS(res, "Content-Type");
return next();
};
function finishFn(app, res, description, okResponse) {
return function finish(err, response){
var statusCode = 200;
+4 -2
View File
@@ -4,6 +4,7 @@ var templateName = require('../backends/template_maps').templateName;
var CdbRequest = require('../models/cdb_request');
var NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
var _ = require('underscore');
var cors = require('../middleware/cors');
function NamedStaticMapsController(app, serverOptions, templateMaps, mapBackend, staticMapBackend, surrogateKeysCache,
tablesExtentApi, layergroupRequestDecorator) {
@@ -22,13 +23,14 @@ module.exports = NamedStaticMapsController;
var cdbRequest = new CdbRequest();
NamedStaticMapsController.prototype.register = function(app) {
app.get(app.base_url_mapconfig + '/static/named/:template_id/:width/:height.:format', this.named.bind(this));
app.get(
app.base_url_mapconfig + '/static/named/:template_id/:width/:height.:format', cors(), this.named.bind(this)
);
};
NamedStaticMapsController.prototype.named = function(req, res) {
var self = this;
this.app.doCORS(res);
var cdbUser = cdbRequest.userByReq(req);
var format = req.params.format === 'jpg' ? 'jpeg' : 'png';
+3 -4
View File
@@ -1,5 +1,6 @@
var step = require('step');
var assert = require('assert');
var cors = require('../middleware/cors');
function StaticMapsController(app, staticMapBackend) {
this._app = app;
@@ -10,10 +11,10 @@ module.exports = StaticMapsController;
StaticMapsController.prototype.register = function(app) {
app.get(app.base_url_mapconfig + '/static/center/:token/:z/:lat/:lng/:width/:height.:format',
app.get(app.base_url_mapconfig + '/static/center/:token/:z/:lat/:lng/:width/:height.:format', cors(),
this.center.bind(this));
app.get(app.base_url_mapconfig + '/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format',
app.get(app.base_url_mapconfig + '/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format', cors(),
this.bbox.bind(this));
};
@@ -34,8 +35,6 @@ StaticMapsController.prototype.center = function(req, res) {
};
StaticMapsController.prototype.staticMap = function(req, res, width, height, zoom /* bounds */, center) {
this._app.doCORS(res);
var format = req.params.format === 'jpg' ? 'jpeg' : 'png';
var self = this;
+11
View File
@@ -0,0 +1,11 @@
module.exports = function cors(extraHeaders) {
return function(req, res, next) {
var baseHeaders = "X-Requested-With, X-Prototype-Version, X-CSRF-Token";
if(extraHeaders) {
baseHeaders += ", " + extraHeaders;
}
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", baseHeaders);
next();
};
};
-11
View File
@@ -927,17 +927,6 @@ module.exports = function(serverOptions) {
);
};
app.doCORS = function(res, extraHeaders) {
if (serverOptions.enable_cors) {
var baseHeaders = "X-Requested-With, X-Prototype-Version, X-CSRF-Token";
if(extraHeaders) {
baseHeaders += ", " + extraHeaders;
}
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", baseHeaders);
}
};
return app;
};