From fcdd918170e8909f86793764436315b83cc381e2 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Tue, 18 Jan 2011 00:13:35 +0000 Subject: [PATCH] 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 --- lib/OpenLayers/Lang/en.js | 3 +++ lib/OpenLayers/Lang/fr.js | 3 ++- lib/OpenLayers/Request.js | 30 ++++++++++++++++++++++++++---- tests/Request.html | 19 ++++++++++++++----- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/OpenLayers/Lang/en.js b/lib/OpenLayers/Lang/en.js index f687d2e25d..5d42de5aee 100644 --- a/lib/OpenLayers/Lang/en.js +++ b/lib/OpenLayers/Lang/en.js @@ -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': '' diff --git a/lib/OpenLayers/Lang/fr.js b/lib/OpenLayers/Lang/fr.js index a3a66cc204..2f1f6a508f 100644 --- a/lib/OpenLayers/Lang/fr.js +++ b/lib/OpenLayers/Lang/fr.js @@ -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" }); diff --git a/lib/OpenLayers/Request.js b/lib/OpenLayers/Request.js index 5f26193f32..59ca6c2401 100644 --- a/lib/OpenLayers/Request.js +++ b/lib/OpenLayers/Request.js @@ -35,6 +35,11 @@ OpenLayers.Request = { scope: null }, + /** + * Constant: URL_SPLIT_REGEX + */ + URL_SPLIT_REGEX: /([^:]*:)\/\/([^:]*:?[^@]*@)?([^:\/\?]*):?([^\/\?]*)/, + /** * APIProperty: 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( diff --git a/tests/Request.html b/tests/Request.html index c1dcef2f51..20e7a7579f 100644 --- a/tests/Request.html +++ b/tests/Request.html @@ -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