forked from Esri/arcgis-js-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
25 lines (24 loc) · 11.8 KB
/
request.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.34/esri/copyright.txt for details.
define(["require","dojo/_base/array","dojo/_base/config","dojo/_base/Deferred","dojo/_base/lang","dojo/_base/url","dojo/_base/xhr","dojo/request/xhr","./core/request/script","dojo/request/iframe","dojo/dom-construct","dojo/io-query","./kernel","./config","./sniff","./lang","./urlUtils","./deferredUtils"],(function(e,r,n,t,o,i,s,a,l,d,u,c,f,p,g,h,m,w){var v,k,_=null,b=p.defaults.io,C=["COM_0056","COM_0057","SB_0008"],x=0,D=/%[0-9A-F]{2}/i,O=((k=new t).resolve(),k.promise);function y(e){return((e=new i(e)).host+(e.port?":"+e.port:"")).toLowerCase()}function j(e){return this._xhr?this._xhr.getResponseHeader(e):null}function A(e,i,p,w){var k,_,C=!1,O=!1,y=!1;h.isDefined(i)&&(o.isObject(i)?(C=!!i.useProxy,O=!!i.usePost,y=!!i.returnProgress,k=i.crossOrigin):C=!!i),(e=o.mixin({},e))._ssl&&(e.url=e.url.replace(/^http:/i,"https:")),g("ie")<10&&!D.test(e.url)&&(e.url=encodeURI(e.url));var j=e.content,A=e.url,E=p&&e.form,q=b;k=h.isDefined(k)?k:q.useCors,e.load=function(r){var t;return r&&(r.error?(t=o.mixin(new Error,r.error)).log=!!n.isDebug:"error"===r.status&&((t=o.mixin(new Error,r)).log=!!n.isDebug),t&&(e.failOk=!t.log,h.isDefined(t.httpCode)||(t.httpCode=t.code))),t||r},e.error=function(r,t){return t&&t.xhr&&t.xhr.abort(),r instanceof Error||(r=o.mixin(new Error,r)),r.log=!!n.isDebug,e.failOk=!r.log,q.errorHandler(r,t),r},e._token&&(e.content=e.content||{},e.content.token=e._token);var R,L=0;j&&A&&(L=(R=c.objectToQuery(j)).length+A.length+1,g("esri-url-encodes-apostrophe")&&(L=R.replace(/'/g,"%27").length+A.length+1)),e.timeout=h.isDefined(e.timeout)?e.timeout:q.timeout,e.handleAs=e.handleAs||"json";try{var S,P,U=k&&m.canUseXhr(e.url)&&!/https?:\/\/[^\/]+\/[^\/]+\/admin\/?(\/.*)?$/i.test(e.url),T=m.hasSameOrigin(e.url,window.location.href)||U,F=!!(O||p||L>q.postLength),I=!(T||-1===e.handleAs.indexOf("json")||!e.callbackParamName||p),W=!(!(m.getProxyRule(e.url)||q.alwaysUseProxy||C)&&("image"===e.handleAs&&!i.allowImageDataAccess||I&&!F||T));if(p&&!g("esri-file-upload")&&!W&&U&&(W=!0),W)if(P=(S=m.getProxyUrl(A,k)).path,S._xo&&(U=!0),!F&&P.length+1+L>q.postLength&&(F=!0),e.url=P+"?"+A,F)e.content=o.mixin(S.query||{},j);else{var X=c.objectToQuery(o.mixin(S.query||{},j));X&&(e.url+="?"+X),e.content=null}if(!I||F||W){var B=e.headers;if(!U||B&&B.hasOwnProperty("X-Requested-With")||((B=e.headers=B||{})["X-Requested-With"]=null),p){var N,H,Q,M,$,z=e.callbackParamName||"callback.html",J=e.callbackElementName||"textarea",G=E.elements?E.elements.length:0;if(j=e.content)for(N in j.token&&A.toLowerCase().indexOf("/sharing/servers/")>-1&&(A+=(-1===A.indexOf("?")?"?":"&")+"token="+j.token,e.url=W?P+"?"+A:A,delete j.token),j)if(Q=j[N],h.isDefined(Q)){for(H=null,M=0;M<G;M++)if(($=E.elements[M]).name===N){H=$;break}H?H.value=Q:w?E.append(N,Q):E.appendChild(u.create("input",{type:"hidden",name:N,value:Q}))}if(g("esri-file-upload")){r.forEach(E.elements,(function(e){e.name===z&&E.removeChild(e)}));var K=w?E:new FormData(E);if(g("safari")>=11&&"entries"in K&&"delete"in K){for(var V=[],Y=K.entries(),Z=Y.next();!Z.done;){var ee=Z.value;ee[1]instanceof File&&""===ee[1].name&&V.push(ee[0]),Z=Y.next()}V.forEach((function(e){K.delete(e)}))}e.contentType=!1,e.postData=K,delete e.form}else E.enctype="multipart/form-data",g("ie")<9&&(E.encoding="multipart/form-data"),E.method="post",r.some(E.elements,(function(e){return e.name===z}))||E.appendChild(u.create("input",{type:"hidden",name:z,value:J})),-1===A.toLowerCase().indexOf("addattachment")&&-1===A.toLowerCase().indexOf("updateattachment")||(A+=(-1===A.indexOf("?")?"?":"&")+z+"="+J,e.url=W?P+"?"+A:A),delete e.content}if(U&&!e.hasOwnProperty("withCredentials")&&"with-credentials"===b.useCors){var re=W?P:A,ne=m.canUseXhr(re,!0),te=ne>-1?b.corsEnabledServers[ne]:null;if(te&&te.hasOwnProperty("withCredentials"))te.withCredentials&&(e.withCredentials=!0);else if(f.id){var oe=f.id.findServerInfo(re);oe&&oe.webTierAuth&&(e.withCredentials=!0)}}if("image"===(e=v?v(e):e).handleAs)return function(e,r){var n=c.objectToQuery(e.content);if(n&&(e.url+=(-1===e.url.indexOf("?")?"?":"&")+n),e.url.length>2e3){var i;if("data:"!==e.url.toLowerCase().slice(0,5))return(i=new t).reject(o.mixin(new Error,{message:"When using responseType 'image', URL length cannot exceed 2000 characters."})),i;if(e.url.length>3e6)return(i=new t).reject(o.mixin(new Error,{message:"When using responseType 'image', data URL length cannot exceed 3000000 characters."})),i}var s=new Image;r.allowImageDataAccess&&(e.withCredentials?s.crossOrigin="use-credentials":s.crossOrigin="anonymous");var a=!1,l=new t((function(e){a=!0,s.onload=s.onerror=s.onabort=null,s.src=""})),d=function(e){s.onload=s.onerror=s.onabort=null,a||l.reject(new Error("Unable to load the resource"))};return s.onload=function(){s.onload=s.onerror=s.onabort=null,a||l.resolve(this)},s.onerror=d,s.onabort=d,s.alt="",s.src=e.url,l}(e,i);if(F){if(p&&!g("esri-file-upload")){_=new t((function(){ie.cancel()}));var ie=d.post(e.url,e).then((function(e){_.resolve(e)})).otherwise((function(e){_.reject(e)}));return _.addCallback((function(r){return e.load(r)})),_.addErrback((function(r){return e.error(r)})),_}if(!W&&g("safari")&&(e.url+=(-1===e.url.indexOf("?")?"?":"&")+"_ts="+(new Date).getTime()+x++),y){e.uploadProgress=!0,e.data=e.postData,e.query=e.content,_=new t((function(){se.cancel()}));var se=a.post(e.url,e).then((function(e){_.resolve(e)}),(function(e){_.reject(e)}),(function(e){_.progress({transferType:e.transferType,loaded:e.loaded,total:e.total})}));return _.addCallback((function(r){return e.load(r)})),_.addErrback((function(r){return e.error(r)})),_}return s.post(e)}return s.get(e)}(e=v?v(e):e).jsonp=e.callbackParamName,e.query=e.content,_=new t((function(){ae.cancel()}));var ae=l.get(e.url,e).then((function(e){_.resolve(e)})).otherwise((function(e){_.reject(e)}));return _.addCallback((function(r){return e.load(r)})),_.addErrback((function(r){return e.error(r)})),_}catch(r){return(_=new t).errback(e.error(r)),_}}function E(e){var r=b.corsStatus,o=m.canUseXhr(e,!0);o>-1&&b.corsEnabledServers.splice(o,1);var i=new t;return i.reject({log:!!n.isDebug}),r[y(e)]=i.promise,o}function q(e){var r=b.corsStatus;try{var n=y(e);if(b.corsDetection&&b.useCors&&g("esri-cors")&&e&&-1!==e.toLowerCase().indexOf("/rest/services")&&!m.hasSameOrigin(e,window.location.href)&&!m.canUseXhr(e)){if(r[n]&&!r[n].isCanceled())return r[n];var o=new t(w._dfdCanceller);r[n]=o.promise;var i=s.get({url:e.substring(0,e.toLowerCase().indexOf("/rest/")+"/rest/".length)+"info",content:{f:"json"},failOk:!0,handleAs:"json",headers:{"X-Requested-With":null},timeout:1e3*b.corsDetectionTimeout});return o._pendingDfd=i,i.then((function(r){r?(m.canUseXhr(e)||b.corsEnabledServers.push(n),o.resolve()):o.reject()}),(function(e){o.reject(e)})),o.promise}}catch(e){console.log("esri._detectCors: an unknown error occurred while detecting CORS support")}return O}function R(e){v=e}function L(i,a,l,d){var u=l.form,c=d.disableIdentityLookup,p=d._preLookup,w=!1;if(g("esri-workers")&&!1!==b.useWorkers)if(!0===d.useWorkers||!0===b.useWorkers)w=!0;else if(d.workerOptions){var v=d.workerOptions;(v.callback||v.worker&&v.worker.worker instanceof Worker)&&(w=!0)}var k=u&&g("esri-file-upload")&&u instanceof FormData,x=u&&(u.elements?r.some(u.elements,(function(e){return"file"===e.type})):k),D=-1!==l.url.toLowerCase().indexOf("token=")||l.content&&l.content.token||x&&r.some(u.elements,(function(e){return"token"===e.name}))?1:0;if(!a){i.addCallback((function(e){if((/\/sharing\/rest\/accounts\/self/i.test(l.url)||/\/sharing\/rest\/portals\/self/i.test(l.url))&&!D&&!l._token&&e.user&&e.user.username){b.webTierAuthServers.push(y(l.url));var r=b.corsEnabledServers,n=m.canUseXhr(l.url,!0),t={host:y(l.url),withCredentials:!0};if(-1===n)r.push(t);else{var o=r[n];o instanceof RegExp?(t.host=o,r.splice(n,1,t)):"object"==typeof o?o.withCredentials=!0:r.splice(n,1,t)}}var i=l._credential;if(i){var s,a=f.id.findServerInfo(i.server),d=a&&a.owningSystemUrl;d&&(d=d.replace(/\/?$/,"/sharing"),(s=f.id.findCredential(d,i.userId))&&-1===f.id._getIdenticalSvcIdx(d,s)&&s.resources.splice(0,0,d))}})),i.addBoth((function(e){delete l._credential,!e||g("ie")&&e.nodeType||(e._ssl=l._ssl)}));var O=l.load,q=l.error;O&&i.addCallback((function(e){var r=i._pendingDfd,n=r&&r.ioArgs,t=n&&n.args;return O.call(t,e,n)})),q&&i.addErrback((function(e){var r=i._pendingDfd,n=r&&r.ioArgs,t=n&&n.args;return q.call(t,e,n)}))}if(f.id&&!D&&!l._token&&!f.id._isPublic(l.url)&&(!c||p)){var R=f.id.findCredential(l.url);R&&(l._token=R.token,l._ssl=R.ssl)}function S(e){if(e._pendingDfd=A(l,d,x,k),!e._pendingDfd){e.ioArgs=e._pendingDfd&&e._pendingDfd.ioArgs;var i=new Error("Deferred object is missing");return i.log=!!n.isDebug,e.errback(i),e._pendingDfd=null,e}e._pendingDfd.addCallback((function(r){if(!r)return r;var i=e._pendingDfd&&e._pendingDfd.ioArgs&&e._pendingDfd.ioArgs.xhr;if(!i)return r;var s=i.getResponseHeader("Content-Type");if(s&&-1===(s=s.toLowerCase()).indexOf("text/plain")&&-1===s.indexOf("application/json"))return r;var a;if(r instanceof ArrayBuffer&&r.byteLength<=750)a=new Blob([r]);else{if(!(r instanceof Blob&&r.size<=750))return r;a=r}var l=new t,d=new FileReader;return d.readAsText(a),d.onloadend=function(){var e;if(!d.error)try{var t=JSON.parse(d.result);t.error&&(e=t.error)}catch(i){}if(e){var i=o.mixin(new Error,e);i.log=!!n.isDebug,null==i.httpCode&&(i.httpCode=i.code),l.reject(i)}else l.resolve(r)},l.promise})).addCallback((function(r){e.ioArgs=e._pendingDfd&&e._pendingDfd.ioArgs,d.returnFullResponse&&(r={data:r,_xhr:e.ioArgs&&e.ioArgs.xhr,getHeader:j}),e.callback(r),e._pendingDfd=null})).addErrback((function(n){var t,o,i;if(n&&(t=n.code,o=n.subcode,i=(i=n.messageCode)&&i.toUpperCase()),n&&403==t&&(4==o||n.message&&n.message.toLowerCase().indexOf("ssl")>-1&&-1===n.message.toLowerCase().indexOf("permission"))){if(!l._ssl)return l._ssl=l._sslFromServer=!0,void L(e,!0,l,d)}else if(n&&415==n.status){if(E(l.url),!l._err415)return l._err415=1,void L(e,!0,l,d)}else if(f.id&&-1!==r.indexOf(f.id._errorCodes,t)&&!f.id._isPublic(l.url)&&!c&&(403!=t||-1===r.indexOf(C,i)&&(!h.isDefined(o)||2==o&&l._token)))return e._pendingDfd=f.id.getCredential(l.url,{token:l._token,error:n}),void e._pendingDfd.addCallback((function(r){l._token=r.token,l._credential=r,l._ssl=l._sslFromServer||r.ssl,L(e,!0,l,d)})).addErrback((function(r){e.errback(r),e._pendingDfd=null}));e.ioArgs=e._pendingDfd&&e._pendingDfd.ioArgs,e.isFulfilled()||e.errback(n),e._pendingDfd=null})).then(null,null,(function(r){e.progress(r)}))}return w?d.workerOptions&&d.workerOptions.worker?(_||(_=s),s=d.workerOptions.worker,S(i)):e(["./workers/RequestClient"],(function(e){if(_||(_=s),d.workerOptions){var r=d.workerOptions;s=e.getClient(r.callback,r.cbFunction)}else s=e.getClient();S(i)})):(_&&(s=_,_=null),S(i)),i}function S(e,r){e.url=m.fixUrl(e.url),r=r||{};var n=new t(w._dfdCanceller),o=q(e.url);return n._pendingDfd=o,o.always((function(t){t&&"cancel"===t.dojoType?n.reject(t):L(n,!1,e,r)})),n}return S._makeRequest=A,S._processRequest=L,S._disableCors=E,S._detectCors=q,S.setRequestPreCallback=R,g("extend-esri")&&(f.request=S,f._makeRequest=A,f._processRequest=L,f._disableCors=E,f._detectCors=q,f.setRequestPreCallback=R),S}));