-
Notifications
You must be signed in to change notification settings - Fork 388
/
ProxyUtils.js
54 lines (50 loc) · 1.69 KB
/
ProxyUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* 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.
*/
import ConfigUtils from './ConfigUtils';
import { isArray, isObject } from 'lodash';
export const needProxy = function(uri, config = {}) {
if ( isArray(uri) ) {
return uri.reduce((result, current) => 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 =
urlParts[1] === location.protocol &&
urlParts[3] === location.hostname;
let uPort = urlParts[4];
let lPort = location.port;
if (uPort !== 80 && uPort !== "" || lPort !== "80" && lPort !== "") {
sameOrigin = sameOrigin && uPort === lPort;
}
}
if (!sameOrigin) {
let proxyUrl = ConfigUtils.getProxyUrl(config);
if (proxyUrl) {
let useCORS = [];
if (isObject(proxyUrl)) {
useCORS = proxyUrl.useCORS || [];
proxyUrl = proxyUrl.url;
}
const isCORS = useCORS.reduce((found, current) => found || uri.indexOf(current) === 0, false);
if (!isCORS) {
needed = true;
}
}
}
return needed;
};
export const getProxyUrl = function(config = {}) {
let proxyUrl = ConfigUtils.getProxyUrl(config);
if (proxyUrl && isObject(proxyUrl)) {
proxyUrl = proxyUrl.url;
}
return proxyUrl;
};