check if using a proxy is required, and if so, issue a warning if none is configured. p=fvanderbiest,me r=me (closes #3015)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@11038 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -122,6 +122,9 @@ OpenLayers.Lang.en = {
|
||||
// console message
|
||||
'filterEvaluateNotImplemented': "evaluate is not implemented for this filter type.",
|
||||
|
||||
'proxyNeeded': "You probably need to set OpenLayers.ProxyHost to access ${url}."+
|
||||
"See http://trac.osgeo.org/openlayers/wiki/FrequentlyAskedQuestions#ProxyHost",
|
||||
|
||||
// **** end ****
|
||||
'end': ''
|
||||
|
||||
|
||||
@@ -75,6 +75,7 @@ OpenLayers.Lang["fr"] = OpenLayers.Util.applyDefaults({
|
||||
|
||||
'pagePositionFailed': "OpenLayers.Util.pagePosition a échoué: l\'élément d\'id ${elemId} pourrait être mal positionné.",
|
||||
|
||||
'filterEvaluateNotImplemented': "évaluer n\'a pas encore été implémenté pour ce type de filtre."
|
||||
'filterEvaluateNotImplemented': "évaluer n\'a pas encore été implémenté pour ce type de filtre.",
|
||||
|
||||
'proxyNeeded': "Vous avez très probablement besoin de renseigner OpenLayers.ProxyHost pour accéder à ${url}. Voir http://trac.osgeo.org/openlayers/wiki/FrequentlyAskedQuestions#ProxyHost"
|
||||
});
|
||||
|
||||
@@ -35,6 +35,11 @@ OpenLayers.Request = {
|
||||
scope: null
|
||||
},
|
||||
|
||||
/**
|
||||
* Constant: URL_SPLIT_REGEX
|
||||
*/
|
||||
URL_SPLIT_REGEX: /([^:]*:)\/\/([^:]*:?[^@]*@)?([^:\/\?]*):?([^\/\?]*)/,
|
||||
|
||||
/**
|
||||
* APIProperty: events
|
||||
* {<OpenLayers.Events>} An events object that handles all
|
||||
@@ -128,11 +133,28 @@ OpenLayers.Request = {
|
||||
url += separator + paramString;
|
||||
}
|
||||
}
|
||||
if(config.proxy && (url.indexOf("http") == 0)) {
|
||||
if(typeof config.proxy == "function") {
|
||||
url = config.proxy(url);
|
||||
var sameOrigin = !(url.indexOf("http") == 0);
|
||||
var urlParts = !sameOrigin && url.match(this.URL_SPLIT_REGEX);
|
||||
if (urlParts) {
|
||||
var location = window.location;
|
||||
sameOrigin =
|
||||
urlParts[1] == location.protocol &&
|
||||
urlParts[3] == location.hostname;
|
||||
var uPort = urlParts[4], lPort = location.port;
|
||||
if (uPort != 80 && uPort != "" || lPort != "80" && lPort != "") {
|
||||
sameOrigin = sameOrigin && uPort == lPort;
|
||||
}
|
||||
}
|
||||
if (!sameOrigin) {
|
||||
if (config.proxy) {
|
||||
if (typeof config.proxy == "function") {
|
||||
url = config.proxy(url);
|
||||
} else {
|
||||
url = config.proxy + encodeURIComponent(url);
|
||||
}
|
||||
} else {
|
||||
url = config.proxy + encodeURIComponent(url);
|
||||
OpenLayers.Console.warn(
|
||||
OpenLayers.i18n("proxyNeeded"), {url: url});
|
||||
}
|
||||
}
|
||||
request.open(
|
||||
|
||||
@@ -319,7 +319,7 @@
|
||||
}
|
||||
|
||||
function test_ProxyHost(t) {
|
||||
t.plan(4);
|
||||
t.plan(5);
|
||||
|
||||
/*
|
||||
* Setup
|
||||
@@ -334,6 +334,7 @@
|
||||
var proto = OpenLayers.Request.XMLHttpRequest.prototype;
|
||||
var _open = proto.open;
|
||||
var log = [];
|
||||
var port;
|
||||
proto.open = function(method, url, async, user, password) {
|
||||
log.push(url);
|
||||
};
|
||||
@@ -349,9 +350,17 @@
|
||||
OpenLayers.Request.GET({url: "http://bar?k1=v1&k2=v2"});
|
||||
t.eq(log.length, 1, "[1] XHR.open called once");
|
||||
t.eq(log[0], expectedURL, "[1] the URL used for XHR is correct (" + log[0] + ")");
|
||||
|
||||
|
||||
// 1 test
|
||||
log = [];
|
||||
OpenLayers.ProxyHost = "http://fooproxy/?url=";
|
||||
port = window.location.port ? ':'+window.location.port : '';
|
||||
expectedURL = window.location.protocol+"//"+window.location.hostname+port+"/service";
|
||||
OpenLayers.Request.GET({url: expectedURL});
|
||||
t.eq(log[0], expectedURL, "[2] proxy is not used when requesting the same server");
|
||||
|
||||
// 2 tests
|
||||
log = []
|
||||
log = [];
|
||||
OpenLayers.ProxyHost = function(url) {
|
||||
var p = OpenLayers.Util.getParameters(url);
|
||||
var p = OpenLayers.Util.getParameterString(p);
|
||||
@@ -359,8 +368,8 @@
|
||||
};
|
||||
expectedURL = "http://barproxy/?k1=v1&k2=v2";
|
||||
OpenLayers.Request.GET({url: "http://bar?k1=v1&k2=v2"});
|
||||
t.eq(log.length, 1, "[2] XHR.open called once");
|
||||
t.eq(log[0], expectedURL, "[2] the URL used for XHR is correct (" + log[0] + ")");
|
||||
t.eq(log.length, 1, "[3] XHR.open called once");
|
||||
t.eq(log[0], expectedURL, "[3] the URL used for XHR is correct (" + log[0] + ")");
|
||||
|
||||
/*
|
||||
* Teardown
|
||||
|
||||
Reference in New Issue
Block a user