From c10dcf4916a3df7551226cd8e67fd0f81f524cae Mon Sep 17 00:00:00 2001 From: Lorenzo Natali Date: Tue, 9 May 2017 12:07:30 +0200 Subject: [PATCH] Fix #1795. Use proxyUtils to guess CORS policies (#1796) --- .../cesium/__tests__/Layer-test-chrome.jsx | 54 ++++++++++++++++++- .../components/map/cesium/plugins/WMSLayer.js | 25 +++------ .../map/cesium/plugins/WMTSLayer.js | 13 +---- web/client/utils/ProxyUtils.js | 12 +++-- web/client/utils/__tests__/ProxyUtils-test.js | 7 ++- 5 files changed, 74 insertions(+), 37 deletions(-) diff --git a/web/client/components/map/cesium/__tests__/Layer-test-chrome.jsx b/web/client/components/map/cesium/__tests__/Layer-test-chrome.jsx index 84787e85eb..d9437985e7 100644 --- a/web/client/components/map/cesium/__tests__/Layer-test-chrome.jsx +++ b/web/client/components/map/cesium/__tests__/Layer-test-chrome.jsx @@ -139,7 +139,7 @@ describe('Cesium layer', () => { expect(layer).toExist(); }); - it('creates a wms layer for CesiumLayer map', () => { + it('creates a wms layer for Cesium map', () => { var options = { "type": "wms", "visibility": true, @@ -157,9 +157,30 @@ describe('Cesium layer', () => { expect(map.imageryLayers.length).toBe(1); expect(map.imageryLayers._layers[0]._imageryProvider._url).toBe('{s}'); expect(map.imageryLayers._layers[0]._imageryProvider._tileProvider._subdomains.length).toBe(1); + expect(map.imageryLayers._layers[0]._imageryProvider.proxy.proxy).toExist(); }); + it('check wms layer proxy skip for relative urls', () => { + var options = { + "type": "wms", + "visibility": true, + "name": "nurc:Arc_Sample", + "group": "Meteo", + "format": "image/png", + "url": "/geoserver/wms" + }; + // create layers + var layer = ReactDOM.render( + , document.getElementById("container")); - it('creates a wmts layer for openlayers map', () => { + expect(layer).toExist(); + expect(map.imageryLayers.length).toBe(1); + expect(map.imageryLayers._layers[0]._imageryProvider._url).toBe('{s}'); + expect(map.imageryLayers._layers[0]._imageryProvider._tileProvider._subdomains.length).toBe(1); + expect(map.imageryLayers._layers[0]._imageryProvider.proxy.proxy).toNotExist(); + }); + + it('creates a wmts layer for Cesium map', () => { var options = { "type": "wmts", "visibility": true, @@ -187,6 +208,35 @@ describe('Cesium layer', () => { // count layers expect(map.imageryLayers.length).toBe(1); expect(map.imageryLayers._layers[0]._imageryProvider._url).toExist(); + expect(map.imageryLayers._layers[0]._imageryProvider.proxy.proxy).toExist(); + }); + it('check a wmts layer skips proxy config', () => { + var options = { + "type": "wmts", + "visibility": true, + "name": "nurc:Arc_Sample", + "group": "Meteo", + "format": "image/png", + "tileMatrixSet": "EPSG:900913", + "matrixIds": { + "EPSG:4326": [{ + ranges: { + cols: {max: 0, min: 0}, + rows: {max: 0, min: 0} + } + }] + }, + "url": "/geoserver/gwc/service/wmts" + }; + // create layers + var layer = ReactDOM.render( + , document.getElementById("container")); + expect(layer).toExist(); + // count layers + expect(map.imageryLayers.length).toBe(1); + expect(map.imageryLayers._layers[0]._imageryProvider._url).toExist(); + expect(map.imageryLayers._layers[0]._imageryProvider.proxy.proxy).toNotExist(); }); it('creates a wms layer with single tile for CesiumLayer map', () => { diff --git a/web/client/components/map/cesium/plugins/WMSLayer.js b/web/client/components/map/cesium/plugins/WMSLayer.js index 0348e5d80b..5af9dfcec9 100644 --- a/web/client/components/map/cesium/plugins/WMSLayer.js +++ b/web/client/components/map/cesium/plugins/WMSLayer.js @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015, GeoSolutions Sas. * All rights reserved. * @@ -6,11 +6,12 @@ * LICENSE file in the root directory of this source tree. */ -var Layers = require('../../../../utils/cesium/Layers'); -var ConfigUtils = require('../../../../utils/ConfigUtils'); -var Cesium = require('../../../../libs/cesium'); -var assign = require('object-assign'); -var {isObject, isArray} = require('lodash'); +const Layers = require('../../../../utils/cesium/Layers'); +const ConfigUtils = require('../../../../utils/ConfigUtils'); +const ProxyUtils = require('../../../../utils/ProxyUtils'); +const Cesium = require('../../../../libs/cesium'); +const assign = require('object-assign'); +const {isArray} = require('lodash'); function getWMSURLs( urls ) { return urls.map((url) => url.split("\?")[0]); @@ -76,17 +77,7 @@ function wmsToCesiumOptions(options) { let proxyUrl = ConfigUtils.getProxyUrl({}); let proxy; if (proxyUrl) { - let useCORS = []; - if (isObject(proxyUrl)) { - useCORS = proxyUrl.useCORS || []; - proxyUrl = proxyUrl.url; - } - let url = options.url; - if (isArray(url)) { - url = url[0]; - } - const isCORS = useCORS.reduce((found, current) => found || url.indexOf(current) === 0, false); - proxy = !isCORS && proxyUrl; + proxy = ProxyUtils.needProxy(options.url) && proxyUrl; } // NOTE: can we use opacity to manage visibility? return assign({ diff --git a/web/client/components/map/cesium/plugins/WMTSLayer.js b/web/client/components/map/cesium/plugins/WMTSLayer.js index 049f37d623..b017e1ff1b 100644 --- a/web/client/components/map/cesium/plugins/WMTSLayer.js +++ b/web/client/components/map/cesium/plugins/WMTSLayer.js @@ -9,6 +9,7 @@ var Layers = require('../../../../utils/cesium/Layers'); var ConfigUtils = require('../../../../utils/ConfigUtils'); const CoordinatesUtils = require('../../../../utils/CoordinatesUtils'); +const ProxyUtils = require('../../../../utils/ProxyUtils'); const WMTSUtils = require('../../../../utils/WMTSUtils'); var Cesium = require('../../../../libs/cesium'); var assign = require('object-assign'); @@ -95,17 +96,7 @@ function wmtsToCesiumOptions(options) { let proxyUrl = ConfigUtils.getProxyUrl({}); let proxy; if (proxyUrl) { - let useCORS = []; - if (isObject(proxyUrl)) { - useCORS = proxyUrl.useCORS || []; - proxyUrl = proxyUrl.url; - } - let url = options.url; - if (isArray(url)) { - url = url[0]; - } - const isCORS = useCORS.reduce((found, current) => found || url.indexOf(current) === 0, false); - proxy = !isCORS && proxyUrl; + proxy = ProxyUtils.needProxy(options.url) && proxyUrl; } // NOTE: can we use opacity to manage visibility? const isValid = isValidTile(options.matrixIds && options.matrixIds[tileMatrixSet]); diff --git a/web/client/utils/ProxyUtils.js b/web/client/utils/ProxyUtils.js index c50d98405c..1ea9606df1 100644 --- a/web/client/utils/ProxyUtils.js +++ b/web/client/utils/ProxyUtils.js @@ -7,13 +7,15 @@ */ const ConfigUtils = require('./ConfigUtils'); -const {isObject} = require('lodash'); - +const {isArray, isObject} = require('lodash'); var ProxyUtils = { needProxy: function(uri, config = {}) { - var needed = false; - var sameOrigin = !(uri.indexOf("http") === 0); - var urlParts = !sameOrigin && uri.match(/([^:]*:)\/\/([^:]*:?[^@]*@)?([^:\/\?]*):?([^\/\?]*)/); + if ( isArray(uri) ) { + return uri.reduce((result, current) => ProxyUtils.needProxy(current) && result, true); + } + let needed = false; + let sameOrigin = !(uri.indexOf("http") === 0); + let urlParts = !sameOrigin && uri.match(/([^:]*:)\/\/([^:]*:?[^@]*@)?([^:\/\?]*):?([^\/\?]*)/); if (urlParts) { let location = window.location; sameOrigin = diff --git a/web/client/utils/__tests__/ProxyUtils-test.js b/web/client/utils/__tests__/ProxyUtils-test.js index 53f8cb2416..6996fa1d4c 100644 --- a/web/client/utils/__tests__/ProxyUtils-test.js +++ b/web/client/utils/__tests__/ProxyUtils-test.js @@ -10,8 +10,11 @@ var ProxyUtils = require('../ProxyUtils'); describe('ProxyUtils test', () => { it('Need Proxy', () => { - let res = ProxyUtils.needProxy("http:someurl.com"); - expect(res).toBe(true); + expect(ProxyUtils.needProxy("http:someurl.com")).toBe(true); + expect(ProxyUtils.needProxy(["http:someurl.com", "http:someotherurl.com"])).toBe(true); + expect(ProxyUtils.needProxy("/geoserver")).toBe(false); + expect(ProxyUtils.needProxy(["/geoserver", "/geoserver1"])).toBe(false); + expect(ProxyUtils.needProxy([location.href])).toBe(false); }); it('GetProxyUrl', () => { let res = ProxyUtils.getProxyUrl({proxyUrl: "http:someurl.com"});