Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.render.name = dojo.hostenv.name_ = "dashboard";
12
dojo.hostenv.println = function (message) {
13
	return alert(message);
14
};
15
dojo.hostenv.getXmlhttpObject = function (kwArgs) {
16
	if (widget.system && kwArgs) {
17
		if ((kwArgs.contentType && kwArgs.contentType.indexOf("text/") != 0) || (kwArgs.headers && kwArgs.headers["content-type"] && kwArgs.headers["content-type"].indexOf("text/") != 0)) {
18
			var curl = new dojo.hostenv.CurlRequest;
19
			curl._save = true;
20
			return curl;
21
		} else {
22
			if (kwArgs.method && kwArgs.method.toUpperCase() == "HEAD") {
23
				return new dojo.hostenv.CurlRequest;
24
			} else {
25
				if (kwArgs.headers && kwArgs.header.referer) {
26
					return new dojo.hostenv.CurlRequest;
27
				}
28
			}
29
		}
30
	}
31
	return new XMLHttpRequest;
32
};
33
dojo.hostenv.CurlRequest = function () {
34
	this.onreadystatechange = null;
35
	this.readyState = 0;
36
	this.responseText = "";
37
	this.responseXML = null;
38
	this.status = 0;
39
	this.statusText = "";
40
	this._method = "";
41
	this._url = "";
42
	this._async = true;
43
	this._referrer = "";
44
	this._headers = [];
45
	this._save = false;
46
	this._responseHeader = "";
47
	this._responseHeaders = {};
48
	this._fileName = "";
49
	this._username = "";
50
	this._password = "";
51
};
52
dojo.hostenv.CurlRequest.prototype.open = function (method, url, async, username, password) {
53
	this._method = method;
54
	this._url = url;
55
	if (async) {
56
		this._async = async;
57
	}
58
	if (username) {
59
		this._username = username;
60
	}
61
	if (password) {
62
		this._password = password;
63
	}
64
};
65
dojo.hostenv.CurlRequest.prototype.setRequestHeader = function (label, value) {
66
	switch (label) {
67
	  case "Referer":
68
		this._referrer = value;
69
		break;
70
	  case "content-type":
71
		break;
72
	  default:
73
		this._headers.push(label + "=" + value);
74
		break;
75
	}
76
};
77
dojo.hostenv.CurlRequest.prototype.getAllResponseHeaders = function () {
78
	return this._responseHeader;
79
};
80
dojo.hostenv.CurlRequest.prototype.getResponseHeader = function (headerLabel) {
81
	return this._responseHeaders[headerLabel];
82
};
83
dojo.hostenv.CurlRequest.prototype.send = function (content) {
84
	this.readyState = 1;
85
	if (this.onreadystatechange) {
86
		this.onreadystatechange.call(this);
87
	}
88
	var query = {sS:""};
89
	if (this._referrer) {
90
		query.e = this._referrer;
91
	}
92
	if (this._headers.length) {
93
		query.H = this._headers.join("&");
94
	}
95
	if (this._username) {
96
		if (this._password) {
97
			query.u = this._username + ":" + this._password;
98
		} else {
99
			query.u = this._username;
100
		}
101
	}
102
	if (content) {
103
		query.d = this.content;
104
		if (this._method != "POST") {
105
			query.G = "";
106
		}
107
	}
108
	if (this._method == "HEAD") {
109
		query.I = "";
110
	} else {
111
		if (this._save) {
112
			query.I = "";
113
		} else {
114
			query.i = "";
115
		}
116
	}
117
	var system = widget.system(dojo.hostenv.CurlRequest._formatCall(query, this._url), null);
118
	this.readyState = 2;
119
	if (this.onreadystatechange) {
120
		this.onreadystatechange.call(this);
121
	}
122
	if (system.errorString) {
123
		this.responseText = system.errorString;
124
		this.status = 0;
125
	} else {
126
		if (this._save) {
127
			this._responseHeader = system.outputString;
128
		} else {
129
			var split = system.outputString.replace(/\r/g, "").split("\n\n", 2);
130
			this._responseHeader = split[0];
131
			this.responseText = split[1];
132
		}
133
		split = this._responseHeader.split("\n");
134
		this.statusText = split.shift();
135
		this.status = this.statusText.split(" ")[1];
136
		for (var i = 0, header; header = split[i]; i++) {
137
			var header_split = header.split(": ", 2);
138
			this._responseHeaders[header_split[0]] = header_split[1];
139
		}
140
		if (this._save) {
141
			widget.system("/bin/mkdir cache", null);
142
			this._fileName = this._url.split("/").pop().replace(/\W/g, "");
143
			this._fileName += "." + this._responseHeaders["Content-Type"].replace(/[\r\n]/g, "").split("/").pop();
144
			delete query.I;
145
			query.o = "cache/" + this._fileName;
146
			system = widget.system(dojo.hostenv.CurlRequest._formatCall(query, this._url), null);
147
			if (!system.errorString) {
148
				this.responseText = "cache/" + this._fileName;
149
			}
150
		} else {
151
			if (this._method == "HEAD") {
152
				this.responseText = this._responseHeader;
153
			}
154
		}
155
	}
156
	this.readyState = 4;
157
	if (this.onreadystatechange) {
158
		this.onreadystatechange.call(this);
159
	}
160
};
161
dojo.hostenv.CurlRequest._formatCall = function (query, url) {
162
	var call = ["/usr/bin/curl"];
163
	for (var key in query) {
164
		if (query[key] != "") {
165
			call.push("-" + key + " '" + query[key].replace(/'/g, "'") + "'");
166
		} else {
167
			call.push("-" + key);
168
		}
169
	}
170
	call.push("'" + url.replace(/'/g, "'") + "'");
171
	return call.join(" ");
172
};
173
dojo.hostenv.exit = function () {
174
	if (widget.system) {
175
		widget.system("/bin/rm -rf cache/*", null);
176
	}
177
};
178