summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/io/xhrWindowNamePlugin.js
blob: 729f36e525ed2ac7c9cbde56ff8957ac1ce06c19 (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
//>>built
define("dojox/io/xhrWindowNamePlugin", [
	"dojo/_base/kernel",
	"dojo/_base/json",
	"dojo/_base/xhr",
	"dojox/io/xhrPlugins",
	"dojox/io/windowName",
	"dojox/io/httpParse",
	"dojox/secure/capability"
], function(dojo, json, xhr, xhrPlugins, windowName, httpParse, capability){
dojo.getObject("io.xhrWindowNamePlugin", true, dojox);

dojox.io.xhrWindowNamePlugin = function(/*String*/url, /*Function?*/httpAdapter, /*Boolean?*/trusted){
	// summary:
	//		Adds the windowName transport as an XHR plugin for the given site. See
	//		dojox.io.windowName for more information on the transport.
	//	url:
	//		Url prefix of the site which can handle windowName requests.
	// 	httpAdapter: This allows for adapting HTTP requests that could not otherwise be
	// 		sent with window.name, so you can use a convention for headers and PUT/DELETE methods.
	xhrPlugins.register(
		"windowName",
		function(method,args){
			 return args.sync !== true &&
				(method == "GET" || method == "POST" || httpAdapter) &&
				(args.url.substring(0,url.length) == url);
		},
		function(method,args,hasBody){
			var send = windowName.send;
			var load = args.load;
			args.load = undefined; //we don't want send to set this callback
			var dfd = (httpAdapter ? httpAdapter(send, true) : send)(method, args, hasBody); // use the windowName transport
			dfd.addCallback(function(result){
				var ioArgs = dfd.ioArgs;
				ioArgs.xhr = {
					getResponseHeader: function(name){
						// convert the hash to an object to act like response headers
						return dojo.queryToObject(ioArgs.hash.match(/[^#]*$/)[0])[name];
					}
				}
				// use the XHR content handlers for handling
				if(ioArgs.handleAs == 'json'){
					// use a secure json verifier, using object capability validator for now
					if(!trusted){
						capability.validate(result,["Date"],{});
					}
					return dojo.fromJson(result);
				}
				return dojo._contentHandlers[ioArgs.handleAs || "text"]({responseText:result});
			});
			args.load = load;
			if(load){
 				dfd.addCallback(load);
 			}
			return dfd;
		}
	);
};

return dojox.io.xhrWindowNamePlugin;
});