root / trunk / web / dojo / dojox / io / proxy / xip.js @ 11
History | View | Annotate | Download (6.89 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.io.proxy.xip"]){ |
||
9 | dojo._hasResource["dojox.io.proxy.xip"]=true; |
||
10 | dojo.provide("dojox.io.proxy.xip");
|
||
11 | dojo.require("dojo.io.iframe");
|
||
12 | dojo.require("dojox.data.dom");
|
||
13 | dojox.io.proxy.xip={xipClientUrl:((dojo.config||djConfig)["xipClientUrl"])||dojo.moduleUrl("dojox.io.proxy","xip_client.html"),urlLimit:4000,_callbackName:(dojox._scopeName||"dojox")+".io.proxy.xip.fragmentReceived",_state:{},_stateIdCounter:0,_isWebKit:navigator.userAgent.indexOf("WebKit")!=-1,send:function(_1){ |
||
14 | var _2=this.xipClientUrl; |
||
15 | if(_2.split(":")[0].match(/javascript/i)||_1._ifpServerUrl.split(":")[0].match(/javascript/i)){ |
||
16 | return;
|
||
17 | } |
||
18 | var _3=_2.indexOf(":"); |
||
19 | var _4=_2.indexOf("/"); |
||
20 | if(_3==-1||_4<_3){ |
||
21 | var _5=window.location.href;
|
||
22 | if(_4==0){ |
||
23 | _2=_5.substring(0,_5.indexOf("/",9))+_2; |
||
24 | }else{
|
||
25 | _2=_5.substring(0,(_5.lastIndexOf("/")+1))+_2; |
||
26 | } |
||
27 | } |
||
28 | this.fullXipClientUrl=_2;
|
||
29 | if(typeof document.postMessage!="undefined"){ |
||
30 | document.addEventListener("message",dojo.hitch(this,this.fragmentReceivedEvent),false); |
||
31 | } |
||
32 | this.send=this._realSend; |
||
33 | return this._realSend(_1); |
||
34 | },_realSend:function(_6){ |
||
35 | var _7="XhrIframeProxy"+(this._stateIdCounter++); |
||
36 | _6._stateId=_7; |
||
37 | var _8=_6._ifpServerUrl+"#0:init:id="+_7+"&client="+encodeURIComponent(this.fullXipClientUrl)+"&callback="+encodeURIComponent(this._callbackName); |
||
38 | this._state[_7]={facade:_6,stateId:_7,clientFrame:dojo.io.iframe.create(_7,"",_8),isSending:false,serverUrl:_6._ifpServerUrl,requestData:null,responseMessage:"",requestParts:[],idCounter:1,partIndex:0,serverWindow:null}; |
||
39 | return _7;
|
||
40 | },receive:function(_9,_a){ |
||
41 | var _b={};
|
||
42 | var _c=_a.split("&"); |
||
43 | for(var i=0;i<_c.length;i++){ |
||
44 | if(_c[i]){
|
||
45 | var _d=_c[i].split("="); |
||
46 | _b[decodeURIComponent(_d[0])]=decodeURIComponent(_d[1]); |
||
47 | } |
||
48 | } |
||
49 | var _e=this._state[_9]; |
||
50 | var _f=_e.facade;
|
||
51 | _f._setResponseHeaders(_b.responseHeaders); |
||
52 | if(_b.status==0||_b.status){ |
||
53 | _f.status=parseInt(_b.status,10);
|
||
54 | } |
||
55 | if(_b.statusText){
|
||
56 | _f.statusText=_b.statusText; |
||
57 | } |
||
58 | if(_b.responseText){
|
||
59 | _f.responseText=_b.responseText; |
||
60 | var _10=_f.getResponseHeader("Content-Type"); |
||
61 | if(_10){
|
||
62 | var _11=_10.split(";")[0]; |
||
63 | if(_11.indexOf("application/xml")==0||_11.indexOf("text/xml")==0){ |
||
64 | _f.responseXML=dojox.data.dom.createDocument(_b.responseText,_10); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | _f.readyState=4;
|
||
69 | this.destroyState(_9);
|
||
70 | },frameLoaded:function(_12){ |
||
71 | var _13=this._state[_12]; |
||
72 | var _14=_13.facade;
|
||
73 | var _15=[];
|
||
74 | for(var _16 in _14._requestHeaders){ |
||
75 | _15.push(_16+": "+_14._requestHeaders[_16]);
|
||
76 | } |
||
77 | var _17={uri:_14._uri}; |
||
78 | if(_15.length>0){ |
||
79 | _17.requestHeaders=_15.join("\r\n");
|
||
80 | } |
||
81 | if(_14._method){
|
||
82 | _17.method=_14._method; |
||
83 | } |
||
84 | if(_14._bodyData){
|
||
85 | _17.data=_14._bodyData; |
||
86 | } |
||
87 | this.sendRequest(_12,dojo.objectToQuery(_17));
|
||
88 | },destroyState:function(_18){ |
||
89 | var _19=this._state[_18]; |
||
90 | if(_19){
|
||
91 | delete this._state[_18]; |
||
92 | var _1a=_19.clientFrame.parentNode;
|
||
93 | _1a.removeChild(_19.clientFrame); |
||
94 | _19.clientFrame=null;
|
||
95 | _19=null;
|
||
96 | } |
||
97 | },createFacade:function(){ |
||
98 | if(arguments&&arguments[0]&&arguments[0].iframeProxyUrl){ |
||
99 | return new dojox.io.proxy.xip.XhrIframeFacade(arguments[0].iframeProxyUrl); |
||
100 | }else{
|
||
101 | return dojox.io.proxy.xip._xhrObjOld.apply(dojo,arguments); |
||
102 | } |
||
103 | },sendRequest:function(_1b,_1c){ |
||
104 | var _1d=this._state[_1b]; |
||
105 | if(!_1d.isSending){
|
||
106 | _1d.isSending=true;
|
||
107 | _1d.requestData=_1c||"";
|
||
108 | _1d.serverWindow=frames[_1d.stateId]; |
||
109 | if(!_1d.serverWindow){
|
||
110 | _1d.serverWindow=document.getElementById(_1d.stateId).contentWindow; |
||
111 | } |
||
112 | if(typeof document.postMessage=="undefined"){ |
||
113 | if(_1d.serverWindow.contentWindow){
|
||
114 | _1d.serverWindow=_1d.serverWindow.contentWindow; |
||
115 | } |
||
116 | } |
||
117 | this.sendRequestStart(_1b);
|
||
118 | } |
||
119 | },sendRequestStart:function(_1e){ |
||
120 | var _1f=this._state[_1e]; |
||
121 | _1f.requestParts=[]; |
||
122 | var _20=_1f.requestData;
|
||
123 | var _21=_1f.serverUrl.length;
|
||
124 | var _22=this.urlLimit-_21; |
||
125 | var _23=0; |
||
126 | while((_20.length-_23)+_21>this.urlLimit){ |
||
127 | var _24=_20.substring(_23,_23+_22);
|
||
128 | var _25=_24.lastIndexOf("%"); |
||
129 | if(_25==_24.length-1||_25==_24.length-2){ |
||
130 | _24=_24.substring(0,_25);
|
||
131 | } |
||
132 | _1f.requestParts.push(_24); |
||
133 | _23+=_24.length; |
||
134 | } |
||
135 | _1f.requestParts.push(_20.substring(_23,_20.length)); |
||
136 | _1f.partIndex=0;
|
||
137 | this.sendRequestPart(_1e);
|
||
138 | },sendRequestPart:function(_26){ |
||
139 | var _27=this._state[_26]; |
||
140 | if(_27.partIndex<_27.requestParts.length){
|
||
141 | var _28=_27.requestParts[_27.partIndex];
|
||
142 | var cmd="part"; |
||
143 | if(_27.partIndex+1==_27.requestParts.length){ |
||
144 | cmd="end";
|
||
145 | }else{
|
||
146 | if(_27.partIndex==0){ |
||
147 | cmd="start";
|
||
148 | } |
||
149 | } |
||
150 | this.setServerUrl(_26,cmd,_28);
|
||
151 | _27.partIndex++; |
||
152 | } |
||
153 | },setServerUrl:function(_29,cmd,_2a){ |
||
154 | var _2b=this.makeServerUrl(_29,cmd,_2a); |
||
155 | var _2c=this._state[_29]; |
||
156 | if(this._isWebKit){ |
||
157 | _2c.serverWindow.location=_2b; |
||
158 | }else{
|
||
159 | _2c.serverWindow.location.replace(_2b); |
||
160 | } |
||
161 | },makeServerUrl:function(_2d,cmd,_2e){ |
||
162 | var _2f=this._state[_2d]; |
||
163 | var _30=_2f.serverUrl+"#"+(_2f.idCounter++)+":"+cmd; |
||
164 | if(_2e){
|
||
165 | _30+=":"+_2e;
|
||
166 | } |
||
167 | return _30;
|
||
168 | },fragmentReceivedEvent:function(evt){ |
||
169 | if(evt.uri.split("#")[0]==this.fullXipClientUrl){ |
||
170 | this.fragmentReceived(evt.data);
|
||
171 | } |
||
172 | },fragmentReceived:function(_31){ |
||
173 | var _32=_31.indexOf("#"); |
||
174 | var _33=_31.substring(0,_32); |
||
175 | var _34=_31.substring(_32+1,_31.length); |
||
176 | var msg=this.unpackMessage(_34); |
||
177 | var _35=this._state[_33]; |
||
178 | switch(msg.command){
|
||
179 | case "loaded": |
||
180 | this.frameLoaded(_33);
|
||
181 | break;
|
||
182 | case "ok": |
||
183 | this.sendRequestPart(_33);
|
||
184 | break;
|
||
185 | case "start": |
||
186 | _35.responseMessage=""+msg.message;
|
||
187 | this.setServerUrl(_33,"ok"); |
||
188 | break;
|
||
189 | case "part": |
||
190 | _35.responseMessage+=msg.message; |
||
191 | this.setServerUrl(_33,"ok"); |
||
192 | break;
|
||
193 | case "end": |
||
194 | this.setServerUrl(_33,"ok"); |
||
195 | _35.responseMessage+=msg.message; |
||
196 | this.receive(_33,_35.responseMessage);
|
||
197 | break;
|
||
198 | } |
||
199 | },unpackMessage:function(_36){ |
||
200 | var _37=_36.split(":"); |
||
201 | var _38=_37[1]; |
||
202 | _36=_37[2]||""; |
||
203 | var _39=null; |
||
204 | if(_38=="init"){ |
||
205 | var _3a=_36.split("&"); |
||
206 | _39={}; |
||
207 | for(var i=0;i<_3a.length;i++){ |
||
208 | var _3b=_3a[i].split("="); |
||
209 | _39[decodeURIComponent(_3b[0])]=decodeURIComponent(_3b[1]); |
||
210 | } |
||
211 | } |
||
212 | return {command:_38,message:_36,config:_39}; |
||
213 | }}; |
||
214 | dojox.io.proxy.xip._xhrObjOld=dojo._xhrObj; |
||
215 | dojo._xhrObj=dojox.io.proxy.xip.createFacade; |
||
216 | dojox.io.proxy.xip.XhrIframeFacade=function(_3c){ |
||
217 | this._requestHeaders={};
|
||
218 | this._allResponseHeaders=null; |
||
219 | this._responseHeaders={};
|
||
220 | this._method=null; |
||
221 | this._uri=null; |
||
222 | this._bodyData=null; |
||
223 | this.responseText=null; |
||
224 | this.responseXML=null; |
||
225 | this.status=null; |
||
226 | this.statusText=null; |
||
227 | this.readyState=0; |
||
228 | this._ifpServerUrl=_3c;
|
||
229 | this._stateId=null; |
||
230 | }; |
||
231 | dojo.extend(dojox.io.proxy.xip.XhrIframeFacade,{open:function(_3d,uri){ |
||
232 | this._method=_3d;
|
||
233 | this._uri=uri;
|
||
234 | this.readyState=1; |
||
235 | },setRequestHeader:function(_3e,_3f){ |
||
236 | this._requestHeaders[_3e]=_3f;
|
||
237 | },send:function(_40){ |
||
238 | this._bodyData=_40;
|
||
239 | this._stateId=dojox.io.proxy.xip.send(this); |
||
240 | this.readyState=2; |
||
241 | },abort:function(){ |
||
242 | dojox.io.proxy.xip.destroyState(this._stateId);
|
||
243 | },getAllResponseHeaders:function(){ |
||
244 | return this._allResponseHeaders; |
||
245 | },getResponseHeader:function(_41){ |
||
246 | return this._responseHeaders[_41]; |
||
247 | },_setResponseHeaders:function(_42){ |
||
248 | if(_42){
|
||
249 | this._allResponseHeaders=_42;
|
||
250 | _42=_42.replace(/\r/g,""); |
||
251 | var _43=_42.split("\n"); |
||
252 | for(var i=0;i<_43.length;i++){ |
||
253 | if(_43[i]){
|
||
254 | var _44=_43[i].split(": "); |
||
255 | this._responseHeaders[_44[0]]=_44[1]; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | }}); |
||
260 | } |