aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/io/proxy/xip_server.html
blob: 01b7274bb1b606a54b69ef9785ff8b17eedb1d55 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<!--
	/*
		Copyright (c) 2004-2008, The Dojo Foundation
		All Rights Reserved.
	
		Licensed under the Academic Free License version 2.1 or above OR the
		modified BSD license. For more information on Dojo licensing, see:
	
			http://dojotoolkit.org/community/licensing.shtml
	*/
	Pieces taken from Dojo source to make this file stand-alone
-->
<html>
<head>
	<title></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
	<script type="text/javascript" src="isAllowed.js"></script>
	<!--
	BY DEFAULT THIS FILE DOES NOT WORK SO THAT YOU DON'T ACCIDENTALLY EXPOSE
	ALL OF YOUR XHR-ENABLED SERVICES ON YOUR SITE. 
	
	In order for this file to work, you need to uncomment the start and end script tags,
	and you should define a function with the following signature:
	
	function isAllowedRequest(request){
		return false;	
	}
	
	Return true out of the function if you want to allow the cross-domain request.
	
	DON'T DEFINE THIS FUNCTION IN THIS FILE! Define it in a separate file called isAllowed.js
	and include it in this page with a script tag that has a src attribute pointing to the file.
	See the very first script tag in this file for an example. You do not have to place the
	script file in the same directory as this file, just update the path above if you move it
	somewhere else.
	
	Customize the isAllowedRequest function to restrict what types of requests are allowed
	for this server. The request object has the following properties:
	- requestHeaders: an object with the request headers that are to be added to
	                  the XHR request.
	- method: the HTTP method (GET, POST, etc...)
	- uri: The URI for the request.
	- data: The URL-encoded data for the request. For a GET request, this would
	        be the querystring parameters. For a POST request, it wll be the
	        body data.
	        
	See xip_client.html for more info on the xip fragment identifier protocol.	
	-->
	
	<!-- Security protection: uncomment the script tag to enable. -->
	<!-- script type="text/javascript" -->
	// <!--
		//Core XHR handling taken from Dojo IO code.
		dojo = {};
		dojo.hostenv = {};
		// These are in order of decreasing likelihood; this will change in time.
		dojo.hostenv._XMLHTTP_PROGIDS = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
		
		dojo.hostenv.getXmlhttpObject = function(){
				var http = null;
			var last_e = null;
			try{ http = new XMLHttpRequest(); }catch(e){}
				if(!http){
				for(var i=0; i<3; ++i){
					var progid = dojo.hostenv._XMLHTTP_PROGIDS[i];
					try{
						http = new ActiveXObject(progid);
					}catch(e){
						last_e = e;
					}
		
					if(http){
						dojo.hostenv._XMLHTTP_PROGIDS = [progid];  // so faster next time
						break;
					}
				}
		
				/*if(http && !http.toString) {
					http.toString = function() { "[object XMLHttpRequest]"; }
				}*/
			}
		
			if(!http){
				throw "xip_server.html: XMLHTTP not available: " + last_e;
			}
		
			return http;
		}

		dojo.setHeaders = function(http, headers){
			if(headers) {
				for(var header in headers) {
					var headerValue = headers[header];
					http.setRequestHeader(header, headerValue);
				}
			}
		}

	//MSIE has the lowest limit for URLs with fragment identifiers,
	//at around 4K. Choosing a slightly smaller number for good measure.
	xipUrlLimit = 4000;
	xipIdCounter = 1;

	function xipServerInit(){
		xipStateId = "";
		xipCurrentHash = "";
		xipRequestMessage = "";
		xipResponseParts = [];
		xipPartIndex = 0;
	}

	function pollHash(){
		//Can't use location.hash because at least Firefox does a decodeURIComponent on it.
		var urlParts = window.location.href.split("#");
		if(urlParts.length == 2){
			var newHash = urlParts[1];
			if(newHash != xipCurrentHash){
				try{
					messageReceived(newHash);
				}catch(e){
					//Make sure to not keep processing the error hash value.
					xipCurrentHash = newHash;
					throw e;
				}
				xipCurrentHash = newHash;
			}
		}
	}

	function messageReceived(encodedData){
		var msg = unpackMessage(encodedData);
		
		switch(msg.command){
			case "ok":
				sendResponsePart();
				break;
			case "start":
				xipRequestMessage = "";
				xipRequestMessage += msg.message;
				setClientUrl("ok");
				break;
			case "part":
				xipRequestMessage += msg.message;			
				setClientUrl("ok");
				break;
			case "end":
				setClientUrl("ok");
				xipRequestMessage += msg.message;
				sendXhr();
				break;
		}
	}

	function sendResponse(encodedData){
		//Break the message into parts, if necessary.
		xipResponseParts = [];
		var resData = encodedData;
		var urlLength = xipClientUrl.length;
		var partLength = xipUrlLimit - urlLength;
		var resIndex = 0;

		while((resData.length - resIndex) + urlLength > xipUrlLimit){
			var part = resData.substring(resIndex, resIndex + partLength);
			//Safari will do some extra hex escaping unless we keep the original hex
			//escaping complete.
			var percentIndex = part.lastIndexOf("%");
			if(percentIndex == part.length - 1 || percentIndex == part.length - 2){
				part = part.substring(0, percentIndex);
			}
			xipResponseParts.push(part);
			resIndex += part.length;
		}
		xipResponseParts.push(resData.substring(resIndex, resData.length));
		
		xipPartIndex = 0;
		sendResponsePart();
	}
	
	function sendResponsePart(){
		if(xipPartIndex < xipResponseParts.length){
			//Get the message part.
			var partData = xipResponseParts[xipPartIndex];
			
			//Get the command.
			var cmd = "part";
			if(xipPartIndex + 1 == xipResponseParts.length){
				cmd = "end";
			}else if (xipPartIndex == 0){
				cmd = "start";
			}

			setClientUrl(cmd, partData);
			xipPartIndex++;
		}else{
			xipServerInit();
		}
	}

	function setClientUrl(cmd, message){
		var clientUrl = makeClientUrl(cmd, message);
		//Safari won't let us replace across domains.
		if(navigator.userAgent.indexOf("Safari") == -1){
			xipClientWindow.location.replace(clientUrl);
		}else{
			xipClientWindow.location = clientUrl;
		}
	}

	function makeClientUrl(cmd, message){
		var clientUrl = xipClientUrl + "#" + (xipIdCounter++) + ":" + cmd;
		if(message){
			clientUrl += ":" + message;
		}
		return clientUrl
	}

	function xhrDone(xhr){
		/* Need to pull off and return the following data:
			- responseHeaders
			- status
			- statusText
			- responseText
		*/
		var response = {};
	
		if(typeof(xhr.getAllResponseHeaders) != "undefined"){
			var allHeaders = xhr.getAllResponseHeaders();
			if(allHeaders){
				response.responseHeaders = allHeaders;
			}
		}
		
		if(xhr.status == 0 || xhr.status){
			response.status = xhr.status;
		}
		
		if(xhr.statusText){
			response.statusText = xhr.statusText;
		}
		
		if(xhr.responseText){
			response.responseText = xhr.responseText;
		}
	
		//Build a string of the response object.
		var result = "";
		var isFirst = true;
		for (var param in response){
			if(isFirst){
				isFirst = false;
			}else{
				result += "&";
			}
			result += param + "=" + encodeURIComponent(response[param]);
		}
		sendResponse(result);
	}

	function sendXhr(){
		var request = {};
		var nvPairs = xipRequestMessage.split("&");
		var i = 0;
		var nameValue = null;
		for(i = 0; i < nvPairs.length; i++){
			if(nvPairs[i]){
				var nameValue = nvPairs[i].split("=");
				request[decodeURIComponent(nameValue[0])] = decodeURIComponent(nameValue[1]);
			}
		}

		//Split up the request headers, if any.
		var headers = {};
		if(request.requestHeaders){
			nvPairs = request.requestHeaders.split("\r\n");
			for(i = 0; i < nvPairs.length; i++){
				if(nvPairs[i]){
					nameValue = nvPairs[i].split(": ");
					headers[decodeURIComponent(nameValue[0])] = decodeURIComponent(nameValue[1]);
				}
			}

			request.requestHeaders = headers;
		}
		
		if(isAllowedRequest(request)){
		
			//The request is allowed, so set up the XHR object.
			var xhr = dojo.hostenv.getXmlhttpObject();
			
			//Start timer to look for readyState.
			var xhrIntervalId = setInterval(function(){
			
				if(xhr.readyState == 4){
					clearInterval(xhrIntervalId);
					xhrDone(xhr);
				}
			}, 10);

			//Actually start up the XHR request.
			xhr.open(request.method, request.uri, true);
			dojo.setHeaders(xhr, request.requestHeaders);
			
			var content = "";
			if(request.data){
				content = request.data;
			}

			try{
				xhr.send(content);
			}catch(e){
				if(typeof xhr.abort == "function"){
					xhr.abort();
					xhrDone({status: 404, statusText: "xip_server.html error: " + e});
				}
			}
		}
	}

	function unpackMessage(encodedMessage){
		var parts = encodedMessage.split(":");
		var command = parts[1];
		encodedMessage = parts[2] || "";

		var config = null;
		if(command == "init"){
			var configParts = encodedMessage.split("&");
			config = {};
			for(var i = 0; i < configParts.length; i++){
				var nameValue = configParts[i].split("=");
				config[decodeURIComponent(nameValue[0])] = decodeURIComponent(nameValue[1]);
			}
		}
		return {command: command, message: encodedMessage, config: config};
	}

	function onServerLoad(){
		xipServerInit();

		//Decode the init params
		var config = unpackMessage(window.location.href.split("#")[1]).config;

		xipStateId = config.id;
		xipClientUrl = config.client;
		
		//Make sure we don't have a javascript: url, just for good measure.
		if(xipClientUrl.split(":")[0].match(/javascript/i)){
			throw "Invalid client URL";
		}
		if(!xipStateId.match(/^XhrIframeProxy[0-9]+$/)){
			throw "Invalid state ID";
		}
	
		setInterval(pollHash, 10);
		
		var serverUrl = window.location.href.split("#")[0];
		document.getElementById("iframeHolder").innerHTML = '<iframe name="'
			+ xipStateId + '_clientEndPoint'
			+ '" src="javascript:false">'
			+ '</iframe>';
		xipClientWindow = document.getElementsByTagName("iframe")[0];
		xipClientWindow.src = makeClientUrl("init", 'id=' + xipStateId + "&callback=" + encodeURIComponent(config.callback));
		if(xipClientWindow.contentWindow){
			xipClientWindow = xipClientWindow.contentWindow;
		}
	}

	if(typeof(window.addEventListener) == "undefined"){
		window.attachEvent("onload", onServerLoad);
	}else{
		window.addEventListener('load', onServerLoad, false);
	}
	// -->
	<!-- </script> -->
</head>
<body>
	<h4>The Dojo Toolkit -- xip_server.html</h4>

	<p>This file is used for Dojo's XMLHttpRequest Iframe Proxy. This is the the file
	that should go on the server that will actually be doing the XHR request.</p>
	<div id="iframeHolder"></div>
</body>
</html>