2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.io.proxy.xip"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.io.proxy.xip"] = true;
|
|
|
3 |
dojo.provide("dojox.io.proxy.xip");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojo.io.iframe");
|
|
|
6 |
dojo.require("dojox.data.dom");
|
|
|
7 |
|
|
|
8 |
dojox.io.proxy.xip = {
|
|
|
9 |
//summary: Object that implements the iframe handling for XMLHttpRequest
|
|
|
10 |
//IFrame Proxying.
|
|
|
11 |
//description: Do not use this object directly. See the Dojo Book page
|
|
|
12 |
//on XMLHttpRequest IFrame Proxying:
|
|
|
13 |
//http://dojotoolkit.org/book/dojo-book-0-4/part-5-connecting-pieces/i-o/cross-domain-xmlhttprequest-using-iframe-proxy
|
|
|
14 |
//Usage of XHR IFrame Proxying does not work from local disk in Safari.
|
|
|
15 |
|
|
|
16 |
xipClientUrl: djConfig["xipClientUrl"] || dojo.moduleUrl("dojox.io.proxy", "xip_client.html"),
|
|
|
17 |
|
|
|
18 |
_state: {},
|
|
|
19 |
_stateIdCounter: 0,
|
|
|
20 |
|
|
|
21 |
needFrameRecursion: function(){
|
|
|
22 |
return (dojo.isIE >= 7);
|
|
|
23 |
},
|
|
|
24 |
|
|
|
25 |
send: function(facade){
|
|
|
26 |
var stateId = "XhrIframeProxy" + (this._stateIdCounter++);
|
|
|
27 |
facade._stateId = stateId;
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
var frameUrl = this.xipClientUrl + "#0:init:id=" + stateId + "&server="
|
|
|
31 |
+ encodeURIComponent(facade._ifpServerUrl) + "&fr=false";
|
|
|
32 |
if(this.needFrameRecursion()){
|
|
|
33 |
//IE7 hack. Need to load server URL, and have that load the xip_client.html.
|
|
|
34 |
//Also, this server URL needs to different from the one eventually loaded by xip_client.html
|
|
|
35 |
//Otherwise, IE7 will not load it. Funky.
|
|
|
36 |
var fullClientUrl = window.location.href.split("#")[0].split("?")[0];
|
|
|
37 |
if((this.xipClientUrl + "").charAt(0) == "/"){
|
|
|
38 |
var endIndex = fullClientUrl.indexOf("://");
|
|
|
39 |
endIndex = fullClientUrl.indexOf("/", endIndex + 3);
|
|
|
40 |
fullClientUrl = fullClientUrl.substring(0, endIndex);
|
|
|
41 |
}else{
|
|
|
42 |
fullClientUrl = fullClientUrl.substring(0, fullClientUrl.lastIndexOf("/") + 1);
|
|
|
43 |
}
|
|
|
44 |
fullClientUrl += this.xipClientUrl;
|
|
|
45 |
|
|
|
46 |
var serverUrl = facade._ifpServerUrl
|
|
|
47 |
+ (facade._ifpServerUrl.indexOf("?") == -1 ? "?" : "&") + "dojo.fr=1";
|
|
|
48 |
|
|
|
49 |
frameUrl = serverUrl + "#0:init:id=" + stateId + "&client="
|
|
|
50 |
+ encodeURIComponent(fullClientUrl) + "&fr=" + this.needFrameRecursion(); //fr is for Frame Recursion
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
this._state[stateId] = {
|
|
|
54 |
facade: facade,
|
|
|
55 |
stateId: stateId,
|
|
|
56 |
clientFrame: dojo.io.iframe.create(stateId, "", frameUrl)
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
return stateId;
|
|
|
60 |
},
|
|
|
61 |
|
|
|
62 |
receive: function(/*String*/stateId, /*String*/urlEncodedData){
|
|
|
63 |
/* urlEncodedData should have the following params:
|
|
|
64 |
- responseHeaders
|
|
|
65 |
- status
|
|
|
66 |
- statusText
|
|
|
67 |
- responseText
|
|
|
68 |
*/
|
|
|
69 |
//Decode response data.
|
|
|
70 |
var response = {};
|
|
|
71 |
var nvPairs = urlEncodedData.split("&");
|
|
|
72 |
for(var i = 0; i < nvPairs.length; i++){
|
|
|
73 |
if(nvPairs[i]){
|
|
|
74 |
var nameValue = nvPairs[i].split("=");
|
|
|
75 |
response[decodeURIComponent(nameValue[0])] = decodeURIComponent(nameValue[1]);
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
//Set data on facade object.
|
|
|
80 |
var state = this._state[stateId];
|
|
|
81 |
var facade = state.facade;
|
|
|
82 |
|
|
|
83 |
facade._setResponseHeaders(response.responseHeaders);
|
|
|
84 |
if(response.status == 0 || response.status){
|
|
|
85 |
facade.status = parseInt(response.status, 10);
|
|
|
86 |
}
|
|
|
87 |
if(response.statusText){
|
|
|
88 |
facade.statusText = response.statusText;
|
|
|
89 |
}
|
|
|
90 |
if(response.responseText){
|
|
|
91 |
facade.responseText = response.responseText;
|
|
|
92 |
|
|
|
93 |
//Fix responseXML.
|
|
|
94 |
var contentType = facade.getResponseHeader("Content-Type");
|
|
|
95 |
if(contentType && (contentType == "application/xml" || contentType == "text/xml")){
|
|
|
96 |
facade.responseXML = dojox.data.dom.createDocument(response.responseText, contentType);
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
facade.readyState = 4;
|
|
|
100 |
|
|
|
101 |
this.destroyState(stateId);
|
|
|
102 |
},
|
|
|
103 |
|
|
|
104 |
clientFrameLoaded: function(/*String*/stateId){
|
|
|
105 |
var state = this._state[stateId];
|
|
|
106 |
var facade = state.facade;
|
|
|
107 |
|
|
|
108 |
if(this.needFrameRecursion()){
|
|
|
109 |
var clientWindow = window.open("", state.stateId + "_clientEndPoint");
|
|
|
110 |
}else{
|
|
|
111 |
var clientWindow = state.clientFrame.contentWindow;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
var reqHeaders = [];
|
|
|
115 |
for(var param in facade._requestHeaders){
|
|
|
116 |
reqHeaders.push(param + ": " + facade._requestHeaders[param]);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
var requestData = {
|
|
|
120 |
uri: facade._uri
|
|
|
121 |
};
|
|
|
122 |
if(reqHeaders.length > 0){
|
|
|
123 |
requestData.requestHeaders = reqHeaders.join("\r\n");
|
|
|
124 |
}
|
|
|
125 |
if(facade._method){
|
|
|
126 |
requestData.method = facade._method;
|
|
|
127 |
}
|
|
|
128 |
if(facade._bodyData){
|
|
|
129 |
requestData.data = facade._bodyData;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
clientWindow.send(dojo.objectToQuery(requestData));
|
|
|
133 |
},
|
|
|
134 |
|
|
|
135 |
destroyState: function(/*String*/stateId){
|
|
|
136 |
var state = this._state[stateId];
|
|
|
137 |
if(state){
|
|
|
138 |
delete this._state[stateId];
|
|
|
139 |
var parentNode = state.clientFrame.parentNode;
|
|
|
140 |
parentNode.removeChild(state.clientFrame);
|
|
|
141 |
state.clientFrame = null;
|
|
|
142 |
state = null;
|
|
|
143 |
}
|
|
|
144 |
},
|
|
|
145 |
|
|
|
146 |
createFacade: function(){
|
|
|
147 |
if(arguments && arguments[0] && arguments[0].iframeProxyUrl){
|
|
|
148 |
return new dojox.io.proxy.xip.XhrIframeFacade(arguments[0].iframeProxyUrl);
|
|
|
149 |
}else{
|
|
|
150 |
return dojox.io.proxy.xip._xhrObjOld.apply(dojo, arguments);
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
//Replace the normal XHR factory with the proxy one.
|
|
|
156 |
dojox.io.proxy.xip._xhrObjOld = dojo._xhrObj;
|
|
|
157 |
dojo._xhrObj = dojox.io.proxy.xip.createFacade;
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
Using this a reference: http://www.w3.org/TR/XMLHttpRequest/
|
|
|
161 |
|
|
|
162 |
Does not implement the onreadystate callback since dojo.xhr* does
|
|
|
163 |
not use it.
|
|
|
164 |
*/
|
|
|
165 |
dojox.io.proxy.xip.XhrIframeFacade = function(ifpServerUrl){
|
|
|
166 |
//summary: XMLHttpRequest facade object used by dojox.io.proxy.xip.
|
|
|
167 |
|
|
|
168 |
//description: Do not use this object directly. See the Dojo Book page
|
|
|
169 |
//on XMLHttpRequest IFrame Proxying:
|
|
|
170 |
//http://dojotoolkit.org/book/dojo-book-0-4/part-5-connecting-pieces/i-o/cross-domain-xmlhttprequest-using-iframe-proxy
|
|
|
171 |
this._requestHeaders = {};
|
|
|
172 |
this._allResponseHeaders = null;
|
|
|
173 |
this._responseHeaders = {};
|
|
|
174 |
this._method = null;
|
|
|
175 |
this._uri = null;
|
|
|
176 |
this._bodyData = null;
|
|
|
177 |
this.responseText = null;
|
|
|
178 |
this.responseXML = null;
|
|
|
179 |
this.status = null;
|
|
|
180 |
this.statusText = null;
|
|
|
181 |
this.readyState = 0;
|
|
|
182 |
|
|
|
183 |
this._ifpServerUrl = ifpServerUrl;
|
|
|
184 |
this._stateId = null;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
dojo.extend(dojox.io.proxy.xip.XhrIframeFacade, {
|
|
|
188 |
//The open method does not properly reset since Dojo does not reuse XHR objects.
|
|
|
189 |
open: function(/*String*/method, /*String*/uri){
|
|
|
190 |
this._method = method;
|
|
|
191 |
this._uri = uri;
|
|
|
192 |
|
|
|
193 |
this.readyState = 1;
|
|
|
194 |
},
|
|
|
195 |
|
|
|
196 |
setRequestHeader: function(/*String*/header, /*String*/value){
|
|
|
197 |
this._requestHeaders[header] = value;
|
|
|
198 |
},
|
|
|
199 |
|
|
|
200 |
send: function(/*String*/stringData){
|
|
|
201 |
this._bodyData = stringData;
|
|
|
202 |
|
|
|
203 |
this._stateId = dojox.io.proxy.xip.send(this);
|
|
|
204 |
|
|
|
205 |
this.readyState = 2;
|
|
|
206 |
},
|
|
|
207 |
abort: function(){
|
|
|
208 |
dojox.io.proxy.xip.destroyState(this._stateId);
|
|
|
209 |
},
|
|
|
210 |
|
|
|
211 |
getAllResponseHeaders: function(){
|
|
|
212 |
return this._allResponseHeaders; //String
|
|
|
213 |
},
|
|
|
214 |
|
|
|
215 |
getResponseHeader: function(/*String*/header){
|
|
|
216 |
return this._responseHeaders[header]; //String
|
|
|
217 |
},
|
|
|
218 |
|
|
|
219 |
_setResponseHeaders: function(/*String*/allHeaders){
|
|
|
220 |
if(allHeaders){
|
|
|
221 |
this._allResponseHeaders = allHeaders;
|
|
|
222 |
|
|
|
223 |
//Make sure ther are now CR characters in the headers.
|
|
|
224 |
allHeaders = allHeaders.replace(/\r/g, "");
|
|
|
225 |
var nvPairs = allHeaders.split("\n");
|
|
|
226 |
for(var i = 0; i < nvPairs.length; i++){
|
|
|
227 |
if(nvPairs[i]){
|
|
|
228 |
var nameValue = nvPairs[i].split(": ");
|
|
|
229 |
this._responseHeaders[nameValue[0]] = nameValue[1];
|
|
|
230 |
}
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
});
|
|
|
235 |
|
|
|
236 |
}
|