forked from Esri/arcgis-js-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
25 lines (24 loc) · 8.28 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 © 2016 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.16/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/io/script","dojo/io/iframe","dojo/dom-construct","dojo/io-query","./kernel","./config","./sniff","./lang","./urlUtils","./deferredUtils"],function(e,r,n,t,o,s,i,a,l,d,u,c,f,p,g,h,_){function m(e){return e=new s(e),(e.host+(e.port?":"+e.port:"")).toLowerCase()}function k(e,s,f,_){var m,k=!1,w=!1;g.isDefined(s)&&(o.isObject(s)?(k=!!s.useProxy,w=!!s.usePost,m=s.crossOrigin):k=!!s),e=o.mixin({},e),e._ssl&&(e.url=e.url.replace(/^http:/i,"https:"));var b=e.content,v=e.url,C=f&&e.form,x=O;m=g.isDefined(m)?m:x.useCors,e.load=function(e){var r;return e&&(e.error?(r=o.mixin(new Error,e.error),r.log=n.isDebug):"error"===e.status&&(r=o.mixin(new Error,e),r.log=n.isDebug),r&&!g.isDefined(r.httpCode)&&(r.httpCode=r.code)),r||e},e.error=function(e,r){return r&&r.xhr&&r.xhr.abort(),e instanceof Error||(e=o.mixin(new Error,e)),e.log=n.isDebug,x.errorHandler(e,r),e},e._token&&(e.content=e.content||{},e.content.token=e._token);var y,A=0;b&&v&&(y=u.objectToQuery(b),A=y.length+v.length+1,p("esri-url-encodes-apostrophe")&&(A=y.replace(/'/g,"%27").length+v.length+1)),e.timeout=g.isDefined(e.timeout)?e.timeout:x.timeout,e.handleAs=e.handleAs||"json";try{var S,E,P=m&&h.canUseXhr(e.url)&&!/https?:\/\/[^\/]+\/[^\/]+\/admin\/?(\/.*)?$/i.test(e.url),U=h.hasSameOrigin(e.url,window.location.href)||P,q=w||f||A>x.postLength?!0:!1,L=U||-1===e.handleAs.indexOf("json")||!e.callbackParamName||f?!1:!0,X=h.getProxyRule(e.url)||x.alwaysUseProxy||k||(!L||q)&&!U?!0:!1;if(f&&!p("esri-file-upload")&&!X&&P&&(X=!0),X)if(S=h.getProxyUrl(v,m),E=S.path,S._xo&&(P=!0),!q&&E.length+1+A>x.postLength&&(q=!0),e.url=E+"?"+v,q)e.content=o.mixin(S.query||{},b);else{var R=u.objectToQuery(o.mixin(S.query||{},b));R&&(e.url+="?"+R),e.content=null}if(L&&!q)return!g.isDefined(e.isAsync)&&p("ff")<4&&(e.isAsync=!0),a.get(D?D(e):e);var T=e.headers;if(!P||T&&T.hasOwnProperty("X-Requested-With")||(T=e.headers=T||{},T["X-Requested-With"]=null),f){var W,I,F,N,M,Q=e.callbackParamName||"callback.html",$=e.callbackElementName||"textarea",B=C.elements?C.elements.length:0;if(b=e.content)for(W in b)if(F=b[W],g.isDefined(F)){for(I=null,N=0;B>N;N++)if(M=C.elements[N],M.name===W){I=M;break}I?I.value=F:_?C.append(W,F):C.appendChild(d.create("input",{type:"hidden",name:W,value:F}))}p("esri-file-upload")?(r.forEach(C.elements,function(e){e.name===Q&&C.removeChild(e)}),e.contentType=!1,e.postData=_?C:new FormData(C),delete e.form):(C.enctype="multipart/form-data",p("ie")<9&&(C.encoding="multipart/form-data"),C.method="post",r.some(C.elements,function(e){return e.name===Q})||C.appendChild(d.create("input",{type:"hidden",name:Q,value:$})),(-1!==v.toLowerCase().indexOf("addattachment")||-1!==v.toLowerCase().indexOf("updateattachment"))&&(e.url=v+(-1===v.indexOf("?")?"?":"&")+Q+"="+$,X&&(e.url=E+"?"+e.url)),delete e.content)}if(P&&!e.hasOwnProperty("withCredentials")&&"with-credentials"===O.useCors){var H=X?E:v,z=h.canUseXhr(H,!0),G=z>-1?O.corsEnabledServers[z]:null;if(G&&G.hasOwnProperty("withCredentials"))G.withCredentials&&(e.withCredentials=!0);else if(c.id){var J=c.id.findServerInfo(H);J&&J.webTierAuth&&(e.withCredentials=!0)}}return e=D?D(e):e,q?f&&!p("esri-file-upload")?l.send(e):(!X&&p("safari")&&(e.url+=(-1===e.url.indexOf("?")?"?":"&")+"_ts="+(new Date).getTime()+j++),i.post(e)):i.get(e)}catch(K){var V=new t;return V.errback(e.error(K)),V}}function w(e){var r=O.corsStatus,n=h.canUseXhr(e,!0);return n>-1&&O.corsEnabledServers.splice(n,1),r[m(e)]=1,n}function b(e){var r=O.corsStatus;if(O.corsDetection&&O.useCors)try{var n=m(e);!p("esri-cors")||!e||-1===e.toLowerCase().indexOf("/rest/services")||h.hasSameOrigin(e,window.location.href)||h.canUseXhr(e)||r[n]||(r[n]=-1,i.get({url:e.substring(0,e.toLowerCase().indexOf("/rest/")+"/rest/".length)+"info",content:{f:"json"},failOk:!0,handleAs:"json",headers:{"X-Requested-With":null}}).then(function(t){t?(r[n]=2,h.canUseXhr(e)||O.corsEnabledServers.push(n)):r[n]=1},function(){r[n]=1}))}catch(t){console.log("esri._detectCors: an unknown error occurred while detecting CORS support")}}function v(e){D=e}function C(o,s){function a(e){if(e._pendingDfd=k(o,s,A,j),!e._pendingDfd){e.ioArgs=e._pendingDfd&&e._pendingDfd.ioArgs;var t=new Error("Deferred object is missing");return t.log=n.isDebug,o._usrDfd=null,e.errback(t),e._pendingDfd=null,e}e._pendingDfd.addCallback(function(r){e.ioArgs=e._pendingDfd&&e._pendingDfd.ioArgs,o._usrDfd=null,e.callback(r),e._pendingDfd=null}).addErrback(function(n){var t,i,a;if(n&&(t=n.code,i=n.subcode,a=n.messageCode,a=a&&a.toUpperCase()),n&&403==t&&(4==i||n.message&&n.message.toLowerCase().indexOf("ssl")>-1&&-1===n.message.toLowerCase().indexOf("permission"))){if(!o._ssl)return o._ssl=o._sslFromServer=!0,o._usrDfd=e,void C(o,s)}else if(n&&415==n.status){if(w(o.url),!o._err415)return o._err415=1,o._usrDfd=e,void C(o,s)}else if(c.id&&-1!==r.indexOf(c.id._errorCodes,t)&&!c.id._isPublic(o.url)&&!u&&(403!=t||-1===r.indexOf(y,a)&&(!g.isDefined(i)||2==i&&o._token)))return e._pendingDfd=c.id.getCredential(o.url,{token:o._token,error:n}),void e._pendingDfd.addCallback(function(r){o._token=r.token,o._usrDfd=e,o._credential=r,o._ssl=o._sslFromServer||r.ssl,C(o,s)}).addErrback(function(r){o._usrDfd=null,e.errback(r),e._pendingDfd=null});e.ioArgs=e._pendingDfd&&e._pendingDfd.ioArgs,o._usrDfd=null,e.errback(n),e._pendingDfd=null})}o.url=h.fixUrl(o.url),"file:"!==window.location.protocol&&(o.url=h.getAbsoluteUrl(o.url)),s=s||{};var l,d=o.form,u=s.disableIdentityLookup,f=s._preLookup,v=!1;if(p("esri-workers")&&O.useWorkers!==!1)if(s.useWorkers===!0||O.useWorkers===!0)v=!0;else if(s.workerOptions){var D=s.workerOptions;(D.callback||D.worker&&D.worker.worker instanceof Worker)&&(v=!0)}var j=d&&d.append,A=d&&(d.elements?r.some(d.elements,function(e){return"file"===e.type}):j),S=-1!==o.url.toLowerCase().indexOf("token=")||o.content&&o.content.token||A&&r.some(d.elements,function(e){return"token"===e.name})?1:0;if(b(o.url),o._usrDfd)l=o._usrDfd;else{l=new t(_._dfdCanceller),l.addCallback(function(e){if((/\/sharing\/rest\/accounts\/self/i.test(o.url)||/\/sharing\/rest\/portals\/self/i.test(o.url))&&!S&&!o._token&&e.user&&e.user.username){O.webTierAuthServers.push(m(o.url));var r=O.corsEnabledServers,n=h.canUseXhr(o.url,!0),t={host:m(o.url),withCredentials:!0};if(-1===n)r.push(t);else{var s=r[n];"object"==typeof s?s.withCredentials=!0:r.splice(n,1,t)}}var i=o._credential;if(i){var a,l=c.id.findServerInfo(i.server),d=l&&l.owningSystemUrl;d&&(d=d.replace(/\/?$/,"/sharing"),a=c.id.findCredential(d,i.userId),a&&-1===c.id._getIdenticalSvcIdx(d,a)&&a.resources.splice(0,0,d))}}),l.addBoth(function(e){delete o._credential,!e||p("ie")&&e.nodeType||(e._ssl=o._ssl)});var E=o.load,P=o.error;E&&l.addCallback(function(e){var r=l._pendingDfd,n=r&&r.ioArgs,t=n&&n.args;return E.call(t,e,n)}),P&&l.addErrback(function(e){var r=l._pendingDfd,n=r&&r.ioArgs,t=n&&n.args;return P.call(t,e,n)})}if(!(!c.id||S||o._token||c.id._isPublic(o.url)||u&&!f)){var U=c.id.findCredential(o.url);U&&(o._token=U.token,o._ssl=U.ssl)}return v?s.workerOptions&&s.workerOptions.worker?(x||(x=i),i=s.workerOptions.worker,a(l)):e(["./workers/RequestClient"],function(e){if(x||(x=i),s.workerOptions){var r=s.workerOptions;i=e.getClient(r.callback,r.cbFunction)}else i=e.getClient();a(l)}):(x&&(i=x,x=null),a(l)),l}var D,x=null,O=f.defaults.io,y=["COM_0056","COM_0057"],j=0;return C._request=k,C._disableCors=w,C._detectCors=b,C.setRequestPreCallback=v,p("extend-esri")&&(c.request=C,c._request=k,c._disableCors=w,c._detectCors=b,c.setRequestPreCallback=v),C});