forked from Esri/arcgis-js-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlUtils.js
25 lines (24 loc) · 5 KB
/
urlUtils.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
// COPYRIGHT © 201 Esri
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// This material is licensed for use under the Esri Master License
// Agreement (MLA), and is bound by the terms of that agreement.
// You may redistribute and use this code without modification,
// provided you adhere to the terms of the MLA and include this
// copyright notice.
//
// See use restrictions at http://www.esri.com/legal/pdfs/mla_e204_e300/english
//
// For additional information, contact:
// Environmental Systems Research Institute, Inc.
// Attn: Contracts and Legal Services Department
// 380 New York Street
// Redlands, California, USA 92373
// USA
//
// email: [email protected]
//
// See http://js.arcgis.com/3.30/esri/copyright.txt for details.
define(["dojo/_base/lang","dojo/_base/array","dojo/_base/url","dojo/global","dojo/io-query","./kernel","./lang","./config","./sniff","dojo/i18n!./nls/jsapi"],function(e,r,t,o,n,i,a,s,l,u){var c={},h=s.defaults.io,f=/^[a-z][a-z0-9\+\-\.]*:/i,p=/^\s*http:/i,g=/^\s*https:/i,x=/:\d+$/,d=/^https?:\/\/[^\/]+\.arcgis.com\/sharing(\/|$)/i;return c.isHTTP=function(e){var r=o.location.protocol;return null==e?"http:"===r||"https:"===r:e?"https:"===r:"http:"===r},c.getProtocolForWebResource=function(e){return c.isHTTP()?o.location.protocol:e?"https:":"http:"},c.urlToObject=function(e){var r={},o=new t(e),i=e.indexOf("?");return null===o.query?r={path:e,query:null}:(r.path=e.substring(0,i),r.query=n.queryToObject(o.query)),o.fragment&&(r.hash=o.fragment,null===o.query&&(r.path=r.path.substring(0,r.path.length-(o.fragment.length+1)))),r},c.getProxyUrl=function(r,t){var n,i,a,s,l=e.isString(r)?0===e.trim(r).toLowerCase().indexOf("https:"):r,f=h.proxyUrl,p=u.io.proxyNotSet;if(e.isString(r)&&(s=c.getProxyRule(r))&&(f=s.proxyUrl),!f)throw console.log(p),new Error(p);return l&&!1!==t&&0!==o.location.href.toLowerCase().indexOf("https:")&&(i=f,0!==i.toLowerCase().indexOf("http")&&(i=c.getAbsoluteUrl(i)),i=i.replace(/^http:/i,"https:"),c.canUseXhr(i)&&(f=i,a=1)),n=c.urlToObject(f),n._xo=a,n},c.addProxy=function(r){var t,o,i,a=c.getProxyRule(r);return a?t=c.urlToObject(a.proxyUrl):h.alwaysUseProxy&&(t=c.getProxyUrl()),t&&(o=c.urlToObject(r),r=t.path+"?"+o.path,(i=n.objectToQuery(e.mixin(t.query||{},o.query)))&&(r+="?"+i)),r},c.addProxyRule=function(e){var r,t,o=e.urlPrefix=c.urlToObject(e.urlPrefix).path.replace(/([^\/])$/,"$1/").replace(/^https?:\/\//gi,"").toLowerCase(),n=h.proxyRules,i=n.length,a=i;for(r=0;r<i;r++){if(t=n[r].urlPrefix,0===o.indexOf(t)){if(o.length===t)return-1;a=r;break}0===t.indexOf(o)&&(a=r+1)}return n.splice(a,0,e),a},c.getProxyRule=function(e){var r,t,o=h.proxyRules,n=o.length,i=c.urlToObject(e).path.replace(/([^\/])$/,"$1/").replace(/^https?:\/\//gi,"").toLowerCase();for(r=0;r<n;r++)if(0===i.indexOf(o[r].urlPrefix)){t=o[r];break}return t},c.hasSameOrigin=function(r,n,i){r=r.toLowerCase(),n=n.toLowerCase();var a=o.location.href.toLowerCase();return r=0===r.indexOf("http")?new t(r):a=new t(a),n=0===n.indexOf("http")?new t(n):e.isString(a)?new t(a):a,(i||r.scheme===n.scheme)&&r.host===n.host&&r.port===n.port},c.canUseXhr=function(e,t){var o,n=!!l("esri-phonegap"),i=c.hasSameOrigin,a=h.corsEnabledServers,s=-1;return!n&&l("esri-cors")&&a&&a.length&&(n=r.some(a,function(r,t){var n=!r||"object"!=typeof r||r instanceof RegExp?r:r.host;if(n instanceof RegExp){if(n.test(e))return s=t,!0}else if(n&&(o=0!==n.trim().toLowerCase().indexOf("http"),i(e,o?"http://"+n:n)||o&&i(e,"https://"+n)))return s=t,!0;return!1})),t?s:n},c.getAbsoluteUrl=function(r){var t=c.getProtocolForWebResource();return e.isString(r)&&!f.test(r)?0===r.indexOf("//")?t+r:0===r.indexOf("/")?t+"//"+o.location.host+r:i._appBaseUrl+r:r},c.fixUrl=function(r){return r=e.trim(r),r=c.getAbsoluteUrl(r),r=c.normalizeSlashes(r),r=r.replace(/^(https?:\/\/)(arcgis\.com)/i,"$1www.$2"),r=c.downgradeToHTTP(r),r=c.upgradeToHTTPS(r)},c.normalize=function(e){return c.fixUrl(e)},c.normalizeSlashes=function(e){if(/^https?:\/\//i.test(e)){var r,t,o=e.indexOf("?");o>-1?(r=e.slice(0,o),t=e.slice(o+1)):r=e,e=r.replace(/\/{2,}/g,"/"),e=e.replace("/","//"),t&&(e+="?"+t)}return e},c.downgradeToHTTP=function(e){return c.isHTTP(!1)&&g.test(e)&&c.hasSameOrigin(o.location.href,e,!0)&&!c.canUseXhr(e)?e.replace(g,"http:"):e},c.upgradeToHTTPS=function(r){var t=h.httpsDomains,n=c.isHTTP(!1),i=c.isHTTP(!0);if(!p.test(r))return r;r=e.trim(r);var s,l=r.indexOf("/",7);if(s=-1===l?r:r.slice(0,l),s=s.toLowerCase().slice(7),x.test(s)){if(!a.endsWith(s,":80"))return r;s=s.slice(0,-3),r=r.replace(":80","")}if(n&&s===o.location.host&&(!d.test(r)||!c.canUseXhr(r)))return r;var u=!1;if(i&&s===o.location.host)u=!0;else if(t)for(var f=0;f<t.length;f++){var g=t[f];if(s===g||a.endsWith(s,"."+g)){u=!0;break}}return u||!i||h.alwaysUseProxy||c.getProxyRule(r)||(u=!0),u&&(r=r.replace(p,"https:")),r},l("extend-esri")&&(e.mixin(i,c),i._getProxyUrl=c.getProxyUrl,i._getProxiedUrl=c.addProxy,i._hasSameOrigin=c.hasSameOrigin,i._canDoXOXHR=c.canUseXhr,i._getAbsoluteUrl=c.getAbsoluteUrl,i.fixUrl=c.fixUrl),c});