Skip to content

Commit

Permalink
Fix #1795. Use proxyUtils to guess CORS policies (#1796)
Browse files Browse the repository at this point in the history
  • Loading branch information
offtherailz authored May 9, 2017
1 parent cf91f48 commit c10dcf4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 37 deletions.
54 changes: 52 additions & 2 deletions web/client/components/map/cesium/__tests__/Layer-test-chrome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
<CesiumLayer type="wms"
options={options} map={map}/>, 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,
Expand Down Expand Up @@ -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(
<CesiumLayer type="wmts"
options={options} map={map}/>, 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', () => {
Expand Down
25 changes: 8 additions & 17 deletions web/client/components/map/cesium/plugins/WMSLayer.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/**
/*
* Copyright 2015, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* 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]);
Expand Down Expand Up @@ -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({
Expand Down
13 changes: 2 additions & 11 deletions web/client/components/map/cesium/plugins/WMTSLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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]);
Expand Down
12 changes: 7 additions & 5 deletions web/client/utils/ProxyUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
7 changes: 5 additions & 2 deletions web/client/utils/__tests__/ProxyUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"});
Expand Down

0 comments on commit c10dcf4

Please sign in to comment.