dial_a_device_node 0.0.153
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +674 -0
- data/README.md +94 -0
- data/Rakefile +1 -0
- data/app/assets/images/denver_summit.jpg +0 -0
- data/app/assets/images/eppendorf_innova_42.jpg +0 -0
- data/app/assets/images/heidolph.jpg +0 -0
- data/app/assets/images/ika_ret_control.jpg +0 -0
- data/app/assets/images/kern.jpg +0 -0
- data/app/assets/images/knf_rc900.jpg +0 -0
- data/app/assets/images/knf_sc920.jpg +0 -0
- data/app/assets/images/knf_simdos_02.jpg +0 -0
- data/app/assets/images/pce_balance.jpg +0 -0
- data/app/assets/images/purebeaglebone.jpg +0 -0
- data/app/assets/images/weathercape.jpg +0 -0
- data/app/assets/javascripts/dial_a_device_node/consolelogger.js +176 -0
- data/app/assets/javascripts/dial_a_device_node/deviceconnection.js +225 -0
- data/app/assets/javascripts/dial_a_device_node/devices/denver_summit.js +153 -0
- data/app/assets/javascripts/dial_a_device_node/devices/denver_summit_SIM.js +111 -0
- data/app/assets/javascripts/dial_a_device_node/devices/denver_summit_UI.js +121 -0
- data/app/assets/javascripts/dial_a_device_node/devices/eppendorf_innova_42.js +128 -0
- data/app/assets/javascripts/dial_a_device_node/devices/eppendorf_innova_42_SIM.js +66 -0
- data/app/assets/javascripts/dial_a_device_node/devices/eppendorf_innova_42_UI.js +46 -0
- data/app/assets/javascripts/dial_a_device_node/devices/heidolph.js +62 -0
- data/app/assets/javascripts/dial_a_device_node/devices/heidolph_SIM.js +24 -0
- data/app/assets/javascripts/dial_a_device_node/devices/heidolph_UI.js +17 -0
- data/app/assets/javascripts/dial_a_device_node/devices/ika_ret_control.js +192 -0
- data/app/assets/javascripts/dial_a_device_node/devices/ika_ret_control_SIM.js +233 -0
- data/app/assets/javascripts/dial_a_device_node/devices/ika_ret_control_UI.js +96 -0
- data/app/assets/javascripts/dial_a_device_node/devices/kern.js +137 -0
- data/app/assets/javascripts/dial_a_device_node/devices/kern_SIM.js +118 -0
- data/app/assets/javascripts/dial_a_device_node/devices/kern_UI.js +121 -0
- data/app/assets/javascripts/dial_a_device_node/devices/knf_rc900.js +289 -0
- data/app/assets/javascripts/dial_a_device_node/devices/knf_rc900_SIM.js +188 -0
- data/app/assets/javascripts/dial_a_device_node/devices/knf_rc900_UI.js +179 -0
- data/app/assets/javascripts/dial_a_device_node/devices/knf_sc920.js +309 -0
- data/app/assets/javascripts/dial_a_device_node/devices/knf_sc920_SIM.js +298 -0
- data/app/assets/javascripts/dial_a_device_node/devices/knf_sc920_UI.js +441 -0
- data/app/assets/javascripts/dial_a_device_node/devices/knf_simdos_02.js +283 -0
- data/app/assets/javascripts/dial_a_device_node/devices/knf_simdos_02_SIM.js +130 -0
- data/app/assets/javascripts/dial_a_device_node/devices/knf_simdos_02_UI.js +188 -0
- data/app/assets/javascripts/dial_a_device_node/devices/legacy_vnc.js +106 -0
- data/app/assets/javascripts/dial_a_device_node/devices/legacy_vnc_SIM.js +32 -0
- data/app/assets/javascripts/dial_a_device_node/devices/legacy_vnc_UI.js +26 -0
- data/app/assets/javascripts/dial_a_device_node/devices/pce_balance.js +111 -0
- data/app/assets/javascripts/dial_a_device_node/devices/pce_balance_SIM.js +80 -0
- data/app/assets/javascripts/dial_a_device_node/devices/pce_balance_UI.js +69 -0
- data/app/assets/javascripts/dial_a_device_node/devices/purebeaglebone.js +286 -0
- data/app/assets/javascripts/dial_a_device_node/devices/purebeaglebone_SIM.js +13 -0
- data/app/assets/javascripts/dial_a_device_node/devices/purebeaglebone_UI.js +256 -0
- data/app/assets/javascripts/dial_a_device_node/devices/weathercape.js +75 -0
- data/app/assets/javascripts/dial_a_device_node/devices/weathercape_SIM.js +33 -0
- data/app/assets/javascripts/dial_a_device_node/devices/weathercape_UI.js +42 -0
- data/app/assets/javascripts/dial_a_device_node/folderwatcher.js +43 -0
- data/app/assets/javascripts/dial_a_device_node/helper/WebSocket-Node-wrapper.js +36 -0
- data/app/assets/javascripts/dial_a_device_node/helper/XMLHttpRequest.js +579 -0
- data/app/assets/javascripts/dial_a_device_node/helper/agent.js +125 -0
- data/app/assets/javascripts/dial_a_device_node/helper/ajax.js +300 -0
- data/app/assets/javascripts/dial_a_device_node/helper/type.js +10 -0
- data/app/assets/javascripts/dial_a_device_node/php/comport.php +49 -0
- data/app/assets/javascripts/dial_a_device_node/php/php_serial.class.php +622 -0
- data/app/assets/javascripts/dial_a_device_node/systemstatus.js +124 -0
- data/app/assets/javascripts/dial_a_device_node/webconnection.js +197 -0
- data/app/assets/javascripts/dial_a_device_node/websocket_rails/abstract_connection.js.coffee +45 -0
- data/app/assets/javascripts/dial_a_device_node/websocket_rails/channel.js.coffee +70 -0
- data/app/assets/javascripts/dial_a_device_node/websocket_rails/event.js.coffee +42 -0
- data/app/assets/javascripts/dial_a_device_node/websocket_rails/http_connection.js.coffee +89 -0
- data/app/assets/javascripts/dial_a_device_node/websocket_rails/websocket_connection.js.coffee +37 -0
- data/app/assets/javascripts/dial_a_device_node/websocket_rails/websocket_rails.js.coffee +164 -0
- data/app/views/devices/ui/_denver_summit.html.erb +52 -0
- data/app/views/devices/ui/_eppendorf_innova_42.html.erb +72 -0
- data/app/views/devices/ui/_heidolph.html.erb +54 -0
- data/app/views/devices/ui/_ika_ret_control.html.erb +81 -0
- data/app/views/devices/ui/_kern.html.erb +52 -0
- data/app/views/devices/ui/_knf_rc900.html.erb +143 -0
- data/app/views/devices/ui/_knf_sc920.html.erb +194 -0
- data/app/views/devices/ui/_knf_simdos_02.html.erb +98 -0
- data/app/views/devices/ui/_pce_balance.html.erb +37 -0
- data/app/views/devices/ui/_purebeaglebone.html.erb +137 -0
- data/app/views/devices/ui/_weathercape.html.erb +313 -0
- data/beaglebonechip.js +57 -0
- data/dial-a-device-web.js +84 -0
- data/dial_a_device_node.gemspec +23 -0
- data/dial_a_device_node.js +419 -0
- data/lib/dial_a_device_node.rb +6 -0
- data/lib/dial_a_device_node/version.rb +3 -0
- data/package.json +35 -0
- data/start.js +40 -0
- metadata +161 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrapper for Worlize WebSocketNode to emulate the browser WebSocket object.
|
|
3
|
+
*/
|
|
4
|
+
var WebSocketClient = require('websocket').client;
|
|
5
|
+
|
|
6
|
+
exports.WebSocket = function (uri) {
|
|
7
|
+
var self = this;
|
|
8
|
+
this.connection = null;
|
|
9
|
+
this.socket = new WebSocketClient();
|
|
10
|
+
this.socket.on('connect', function (connection) {
|
|
11
|
+
self.connection = connection;
|
|
12
|
+
|
|
13
|
+
connection.on('error', function (error) {
|
|
14
|
+
self.onerror();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
connection.on('close', function () {
|
|
18
|
+
self.onclose();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
connection.on('message', function (message) {
|
|
22
|
+
if (message.type === 'utf8') {
|
|
23
|
+
self.onmessage({
|
|
24
|
+
data: message.utf8Data
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// self.onopen();
|
|
30
|
+
});
|
|
31
|
+
this.socket.connect(uri);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
exports.WebSocket.prototype.send = function (data) {
|
|
35
|
+
this.connection.sendUTF(data);
|
|
36
|
+
}
|
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrapper for built-in http.js to emulate the browser XMLHttpRequest object.
|
|
3
|
+
*
|
|
4
|
+
* This can be used with JS designed for browsers to improve reuse of code and
|
|
5
|
+
* allow the use of existing libraries.
|
|
6
|
+
*
|
|
7
|
+
* Usage: include("XMLHttpRequest.js") and use XMLHttpRequest per W3C specs.
|
|
8
|
+
*
|
|
9
|
+
* @author Dan DeFelippi <dan@driverdan.com>
|
|
10
|
+
* @contributor David Ellis <d.f.ellis@ieee.org>
|
|
11
|
+
* @license MIT
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
var Url = require("url")
|
|
15
|
+
, spawn = require("child_process").spawn
|
|
16
|
+
, fs = require('fs');
|
|
17
|
+
|
|
18
|
+
var keepAliveAgent = require('./agent.js');
|
|
19
|
+
|
|
20
|
+
exports.XMLHttpRequest = function() {
|
|
21
|
+
/**
|
|
22
|
+
* Private variables
|
|
23
|
+
*/
|
|
24
|
+
var self = this;
|
|
25
|
+
var http = require('http');
|
|
26
|
+
var https = require('https');
|
|
27
|
+
|
|
28
|
+
// Holds http.js objects
|
|
29
|
+
var client;
|
|
30
|
+
var request;
|
|
31
|
+
var response;
|
|
32
|
+
|
|
33
|
+
// Request settings
|
|
34
|
+
var settings = {};
|
|
35
|
+
|
|
36
|
+
// Disable header blacklist.
|
|
37
|
+
// Not part of XHR specs.
|
|
38
|
+
var disableHeaderCheck = false;
|
|
39
|
+
|
|
40
|
+
// Set some default headers
|
|
41
|
+
var defaultHeaders = {
|
|
42
|
+
"User-Agent": "node-XMLHttpRequest",
|
|
43
|
+
"Accept": "*/*",
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
var headers = defaultHeaders;
|
|
47
|
+
|
|
48
|
+
// These headers are not user setable.
|
|
49
|
+
// The following are allowed but banned in the spec:
|
|
50
|
+
// * user-agent
|
|
51
|
+
var forbiddenRequestHeaders = [
|
|
52
|
+
"accept-charset",
|
|
53
|
+
"accept-encoding",
|
|
54
|
+
"access-control-request-headers",
|
|
55
|
+
"access-control-request-method",
|
|
56
|
+
"connection",
|
|
57
|
+
"content-length",
|
|
58
|
+
"content-transfer-encoding",
|
|
59
|
+
"cookie",
|
|
60
|
+
"cookie2",
|
|
61
|
+
"date",
|
|
62
|
+
"expect",
|
|
63
|
+
"host",
|
|
64
|
+
"keep-alive",
|
|
65
|
+
"origin",
|
|
66
|
+
"referer",
|
|
67
|
+
"te",
|
|
68
|
+
"trailer",
|
|
69
|
+
"transfer-encoding",
|
|
70
|
+
"upgrade",
|
|
71
|
+
"via"
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
// These request methods are not allowed
|
|
75
|
+
var forbiddenRequestMethods = [
|
|
76
|
+
"TRACE",
|
|
77
|
+
"TRACK",
|
|
78
|
+
"CONNECT"
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
// Send flag
|
|
82
|
+
var sendFlag = false;
|
|
83
|
+
// Error flag, used when errors occur or abort is called
|
|
84
|
+
var errorFlag = false;
|
|
85
|
+
|
|
86
|
+
// Event listeners
|
|
87
|
+
var listeners = {};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Constants
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
this.UNSENT = 0;
|
|
94
|
+
this.OPENED = 1;
|
|
95
|
+
this.HEADERS_RECEIVED = 2;
|
|
96
|
+
this.LOADING = 3;
|
|
97
|
+
this.DONE = 4;
|
|
98
|
+
this.NEWDATA = 9;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Public vars
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
// Current state
|
|
105
|
+
this.readyState = this.UNSENT;
|
|
106
|
+
|
|
107
|
+
// default ready state change handler in case one is not set or is set late
|
|
108
|
+
this.onreadystatechange = null;
|
|
109
|
+
|
|
110
|
+
// Result & response
|
|
111
|
+
this.responseText = "";
|
|
112
|
+
this.responseXML = "";
|
|
113
|
+
this.status = null;
|
|
114
|
+
this.statusText = null;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Private methods
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Check if the specified header is allowed.
|
|
122
|
+
*
|
|
123
|
+
* @param string header Header to validate
|
|
124
|
+
* @return boolean False if not allowed, otherwise true
|
|
125
|
+
*/
|
|
126
|
+
var isAllowedHttpHeader = function(header) {
|
|
127
|
+
return disableHeaderCheck || (header && forbiddenRequestHeaders.indexOf(header.toLowerCase()) === -1);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Check if the specified method is allowed.
|
|
132
|
+
*
|
|
133
|
+
* @param string method Request method to validate
|
|
134
|
+
* @return boolean False if not allowed, otherwise true
|
|
135
|
+
*/
|
|
136
|
+
var isAllowedHttpMethod = function(method) {
|
|
137
|
+
return (method && forbiddenRequestMethods.indexOf(method) === -1);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Public methods
|
|
142
|
+
*/
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Open the connection. Currently supports local server requests.
|
|
146
|
+
*
|
|
147
|
+
* @param string method Connection method (eg GET, POST)
|
|
148
|
+
* @param string url URL for the connection.
|
|
149
|
+
* @param boolean async Asynchronous connection. Default is true.
|
|
150
|
+
* @param string user Username for basic authentication (optional)
|
|
151
|
+
* @param string password Password for basic authentication (optional)
|
|
152
|
+
*/
|
|
153
|
+
this.open = function(method, url, async, user, password) {
|
|
154
|
+
this.abort();
|
|
155
|
+
errorFlag = false;
|
|
156
|
+
|
|
157
|
+
// Check for valid request method
|
|
158
|
+
if (!isAllowedHttpMethod(method)) {
|
|
159
|
+
throw "SecurityError: Request method not allowed";
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
settings = {
|
|
164
|
+
"method": method,
|
|
165
|
+
"url": url.toString(),
|
|
166
|
+
"async": (typeof async !== "boolean" ? true : async),
|
|
167
|
+
"user": user || null,
|
|
168
|
+
"password": password || null
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
setState(this.OPENED);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Disables or enables isAllowedHttpHeader() check the request. Enabled by default.
|
|
176
|
+
* This does not conform to the W3C spec.
|
|
177
|
+
*
|
|
178
|
+
* @param boolean state Enable or disable header checking.
|
|
179
|
+
*/
|
|
180
|
+
this.setDisableHeaderCheck = function(state) {
|
|
181
|
+
disableHeaderCheck = state;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Sets a header for the request.
|
|
186
|
+
*
|
|
187
|
+
* @param string header Header name
|
|
188
|
+
* @param string value Header value
|
|
189
|
+
*/
|
|
190
|
+
this.setRequestHeader = function(header, value) {
|
|
191
|
+
if (this.readyState != this.OPENED) {
|
|
192
|
+
throw "INVALID_STATE_ERR: setRequestHeader can only be called when state is OPEN";
|
|
193
|
+
}
|
|
194
|
+
if (!isAllowedHttpHeader(header)) {
|
|
195
|
+
console.warn('Refused to set unsafe header "' + header + '"');
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (sendFlag) {
|
|
199
|
+
throw "INVALID_STATE_ERR: send flag is true";
|
|
200
|
+
}
|
|
201
|
+
headers[header] = value;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Gets a header from the server response.
|
|
206
|
+
*
|
|
207
|
+
* @param string header Name of header to get.
|
|
208
|
+
* @return string Text of the header or null if it doesn't exist.
|
|
209
|
+
*/
|
|
210
|
+
this.getResponseHeader = function(header) {
|
|
211
|
+
if (typeof header === "string"
|
|
212
|
+
&& this.readyState > this.OPENED
|
|
213
|
+
&& response.headers[header.toLowerCase()]
|
|
214
|
+
&& !errorFlag
|
|
215
|
+
) {
|
|
216
|
+
return response.headers[header.toLowerCase()];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return null;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Gets all the response headers.
|
|
224
|
+
*
|
|
225
|
+
* @return string A string with all response headers separated by CR+LF
|
|
226
|
+
*/
|
|
227
|
+
this.getAllResponseHeaders = function() {
|
|
228
|
+
if (this.readyState < this.HEADERS_RECEIVED || errorFlag) {
|
|
229
|
+
return "";
|
|
230
|
+
}
|
|
231
|
+
var result = "";
|
|
232
|
+
|
|
233
|
+
for (var i in response.headers) {
|
|
234
|
+
// Cookie headers are excluded
|
|
235
|
+
if (i !== "set-cookie" && i !== "set-cookie2") {
|
|
236
|
+
result += i + ": " + response.headers[i] + "\r\n";
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return result.substr(0, result.length - 2);
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Gets a request header
|
|
244
|
+
*
|
|
245
|
+
* @param string name Name of header to get
|
|
246
|
+
* @return string Returns the request header or empty string if not set
|
|
247
|
+
*/
|
|
248
|
+
this.getRequestHeader = function(name) {
|
|
249
|
+
// @TODO Make this case insensitive
|
|
250
|
+
if (typeof name === "string" && headers[name]) {
|
|
251
|
+
return headers[name];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return "";
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Sends the request to the server.
|
|
259
|
+
*
|
|
260
|
+
* @param string data Optional data to send as request body.
|
|
261
|
+
*/
|
|
262
|
+
this.send = function(data) {
|
|
263
|
+
if (this.readyState != this.OPENED) {
|
|
264
|
+
throw "INVALID_STATE_ERR: connection must be opened before send() is called";
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (sendFlag) {
|
|
268
|
+
throw "INVALID_STATE_ERR: send has already been called";
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
var ssl = false, local = false;
|
|
272
|
+
var url = Url.parse(settings.url);
|
|
273
|
+
|
|
274
|
+
// Determine the server
|
|
275
|
+
switch (url.protocol) {
|
|
276
|
+
case 'https:':
|
|
277
|
+
ssl = true;
|
|
278
|
+
// SSL & non-SSL both need host, no break here.
|
|
279
|
+
case 'http:':
|
|
280
|
+
var host = url.hostname;
|
|
281
|
+
break;
|
|
282
|
+
|
|
283
|
+
case 'file:':
|
|
284
|
+
local = true;
|
|
285
|
+
break;
|
|
286
|
+
|
|
287
|
+
case undefined:
|
|
288
|
+
case '':
|
|
289
|
+
var host = "localhost";
|
|
290
|
+
break;
|
|
291
|
+
|
|
292
|
+
default:
|
|
293
|
+
throw "Protocol not supported.";
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Load files off the local filesystem (file://)
|
|
297
|
+
if (local) {
|
|
298
|
+
if (settings.method !== "GET") {
|
|
299
|
+
throw "XMLHttpRequest: Only GET method is supported";
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (settings.async) {
|
|
303
|
+
fs.readFile(url.pathname, 'utf8', function(error, data) {
|
|
304
|
+
if (error) {
|
|
305
|
+
self.handleError(error);
|
|
306
|
+
} else {
|
|
307
|
+
self.status = 200;
|
|
308
|
+
self.responseText = data;
|
|
309
|
+
setState(self.DONE);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
} else {
|
|
313
|
+
try {
|
|
314
|
+
this.responseText = fs.readFileSync(url.pathname, 'utf8');
|
|
315
|
+
this.status = 200;
|
|
316
|
+
setState(self.DONE);
|
|
317
|
+
} catch(e) {
|
|
318
|
+
this.handleError(e);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Default to port 80. If accessing localhost on another port be sure
|
|
326
|
+
// to use http://localhost:port/path
|
|
327
|
+
var port = url.port || (ssl ? 443 : 80);
|
|
328
|
+
// Add query string if one is used
|
|
329
|
+
var uri = url.pathname + (url.search ? url.search : '');
|
|
330
|
+
|
|
331
|
+
// Set the Host header or the server may reject the request
|
|
332
|
+
headers["Host"] = host;
|
|
333
|
+
if (!((ssl && port === 443) || port === 80)) {
|
|
334
|
+
headers["Host"] += ':' + url.port;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Set Basic Auth if necessary
|
|
338
|
+
if (settings.user) {
|
|
339
|
+
if (typeof settings.password == "undefined") {
|
|
340
|
+
settings.password = "";
|
|
341
|
+
}
|
|
342
|
+
var authBuf = new Buffer(settings.user + ":" + settings.password);
|
|
343
|
+
headers["Authorization"] = "Basic " + authBuf.toString("base64");
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Set content length header
|
|
347
|
+
if (settings.method === "GET" || settings.method === "HEAD") {
|
|
348
|
+
data = null;
|
|
349
|
+
} else if (data) {
|
|
350
|
+
headers["Content-Length"] = Buffer.byteLength(data);
|
|
351
|
+
|
|
352
|
+
if (!headers["Content-Type"]) {
|
|
353
|
+
headers["Content-Type"] = "text/plain;charset=UTF-8";
|
|
354
|
+
}
|
|
355
|
+
} else if (settings.method === "POST") {
|
|
356
|
+
// For a post with no data set Content-Length: 0.
|
|
357
|
+
// This is required by buggy servers that don't meet the specs.
|
|
358
|
+
headers["Content-Length"] = 0;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
var agent = new keepAliveAgent({ maxSockets: 1 }); // Optionally define more parallel sockets
|
|
362
|
+
|
|
363
|
+
var options = {
|
|
364
|
+
host: host,
|
|
365
|
+
port: port,
|
|
366
|
+
path: uri,
|
|
367
|
+
method: settings.method,
|
|
368
|
+
headers: headers,
|
|
369
|
+
agent: false
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
// Reset error flag
|
|
373
|
+
errorFlag = false;
|
|
374
|
+
|
|
375
|
+
// Handle async requests
|
|
376
|
+
if (settings.async) {
|
|
377
|
+
// Use the proper protocol
|
|
378
|
+
var doRequest = ssl ? https.request : http.request;
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
// Request is being sent, set send flag
|
|
383
|
+
sendFlag = true;
|
|
384
|
+
|
|
385
|
+
// As per spec, this is called here for historical reasons.
|
|
386
|
+
self.dispatchEvent("readystatechange");
|
|
387
|
+
|
|
388
|
+
// Create the request
|
|
389
|
+
request = doRequest(options, function(resp) {
|
|
390
|
+
response = resp;
|
|
391
|
+
response.setEncoding("utf8");
|
|
392
|
+
|
|
393
|
+
setState(self.HEADERS_RECEIVED);
|
|
394
|
+
self.status = response.statusCode;
|
|
395
|
+
|
|
396
|
+
response.on('data', function(chunk) {
|
|
397
|
+
|
|
398
|
+
// Make sure there's some data
|
|
399
|
+
if (chunk) {
|
|
400
|
+
self.responseText += chunk;
|
|
401
|
+
}
|
|
402
|
+
// Don't emit state changes if the connection has been aborted.
|
|
403
|
+
if (sendFlag) {
|
|
404
|
+
setState(self.LOADING);
|
|
405
|
+
if (chunk) setState(self.NEWDATA);
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
response.on('end', function() {
|
|
410
|
+
if (sendFlag) {
|
|
411
|
+
// Discard the 'end' event if the connection has been aborted
|
|
412
|
+
setState(self.DONE);
|
|
413
|
+
sendFlag = false;
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
response.on('error', function(error) {
|
|
418
|
+
self.handleError(error);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
request.socket.destroy();
|
|
422
|
+
|
|
423
|
+
}).on('error', function(error) {
|
|
424
|
+
self.handleError(error);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
// Node 0.4 and later won't accept empty data. Make sure it's needed.
|
|
428
|
+
if (data) {
|
|
429
|
+
request.write(data);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
request.end();
|
|
433
|
+
|
|
434
|
+
self.dispatchEvent("loadstart");
|
|
435
|
+
} else { // Synchronous
|
|
436
|
+
// Create a temporary file for communication with the other Node process
|
|
437
|
+
var syncFile = ".node-xmlhttprequest-sync-" + process.pid;
|
|
438
|
+
fs.writeFileSync(syncFile, "", "utf8");
|
|
439
|
+
// The async request the other Node process executes
|
|
440
|
+
var execString = "var http = require('http'), https = require('https'), fs = require('fs');"
|
|
441
|
+
+ "var doRequest = http" + (ssl ? "s" : "") + ".request;"
|
|
442
|
+
+ "var options = " + JSON.stringify(options) + ";"
|
|
443
|
+
+ "var responseText = '';"
|
|
444
|
+
+ "var req = doRequest(options, function(response) {"
|
|
445
|
+
+ "response.setEncoding('utf8');"
|
|
446
|
+
+ "response.on('data', function(chunk) {"
|
|
447
|
+
+ "responseText += chunk;"
|
|
448
|
+
+ "});"
|
|
449
|
+
+ "response.on('end', function() {"
|
|
450
|
+
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');"
|
|
451
|
+
+ "});"
|
|
452
|
+
+ "response.on('error', function(error) {"
|
|
453
|
+
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
|
|
454
|
+
+ "});"
|
|
455
|
+
+ "}).on('error', function(error) {"
|
|
456
|
+
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
|
|
457
|
+
+ "});"
|
|
458
|
+
+ (data ? "req.write('" + data.replace(/'/g, "\\'") + "');":"")
|
|
459
|
+
+ "req.end();req.socket.destroy();";
|
|
460
|
+
// Start the other Node Process, executing this string
|
|
461
|
+
syncProc = spawn(process.argv[0], ["-e", execString]);
|
|
462
|
+
while((self.responseText = fs.readFileSync(syncFile, 'utf8')) == "") {
|
|
463
|
+
// Wait while the file is empty
|
|
464
|
+
}
|
|
465
|
+
// Kill the child process once the file has data
|
|
466
|
+
syncProc.stdin.end();
|
|
467
|
+
// Remove the temporary file
|
|
468
|
+
fs.unlinkSync(syncFile);
|
|
469
|
+
if (self.responseText.match(/^NODE-XMLHTTPREQUEST-ERROR:/)) {
|
|
470
|
+
// If the file returned an error, handle it
|
|
471
|
+
var errorObj = self.responseText.replace(/^NODE-XMLHTTPREQUEST-ERROR:/, "");
|
|
472
|
+
self.handleError(errorObj);
|
|
473
|
+
} else {
|
|
474
|
+
// If the file returned okay, parse its data and move to the DONE state
|
|
475
|
+
self.status = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:([0-9]*),.*/, "$1");
|
|
476
|
+
self.responseText = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:[0-9]*,(.*)/, "$1");
|
|
477
|
+
setState(self.DONE);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Called when an error is encountered to deal with it.
|
|
484
|
+
*/
|
|
485
|
+
this.handleError = function(error) {
|
|
486
|
+
this.status = 503;
|
|
487
|
+
this.statusText = error;
|
|
488
|
+
this.responseText = error.stack;
|
|
489
|
+
errorFlag = true;
|
|
490
|
+
setState(this.DONE);
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Aborts a request.
|
|
495
|
+
*/
|
|
496
|
+
this.abort = function() {
|
|
497
|
+
if (request) {
|
|
498
|
+
request.abort();
|
|
499
|
+
request.socket.destroy();
|
|
500
|
+
request = null;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
headers = defaultHeaders;
|
|
504
|
+
this.responseText = "";
|
|
505
|
+
this.responseXML = "";
|
|
506
|
+
|
|
507
|
+
errorFlag = true;
|
|
508
|
+
|
|
509
|
+
if (this.readyState !== this.UNSENT
|
|
510
|
+
&& (this.readyState !== this.OPENED || sendFlag)
|
|
511
|
+
&& this.readyState !== this.DONE) {
|
|
512
|
+
sendFlag = false;
|
|
513
|
+
setState(this.DONE);
|
|
514
|
+
}
|
|
515
|
+
this.readyState = this.UNSENT;
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Adds an event listener. Preferred method of binding to events.
|
|
520
|
+
*/
|
|
521
|
+
this.addEventListener = function(event, callback) {
|
|
522
|
+
if (!(event in listeners)) {
|
|
523
|
+
listeners[event] = [];
|
|
524
|
+
}
|
|
525
|
+
// Currently allows duplicate callbacks. Should it?
|
|
526
|
+
listeners[event].push(callback);
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Remove an event callback that has already been bound.
|
|
531
|
+
* Only works on the matching funciton, cannot be a copy.
|
|
532
|
+
*/
|
|
533
|
+
this.removeEventListener = function(event, callback) {
|
|
534
|
+
if (event in listeners) {
|
|
535
|
+
// Filter will return a new array with the callback removed
|
|
536
|
+
listeners[event] = listeners[event].filter(function(ev) {
|
|
537
|
+
return ev !== callback;
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Dispatch any events, including both "on" methods and events attached using addEventListener.
|
|
544
|
+
*/
|
|
545
|
+
this.dispatchEvent = function(event) {
|
|
546
|
+
if (typeof self["on" + event] === "function") {
|
|
547
|
+
self["on" + event]();
|
|
548
|
+
}
|
|
549
|
+
if (event in listeners) {
|
|
550
|
+
for (var i = 0, len = listeners[event].length; i < len; i++) {
|
|
551
|
+
listeners[event][i].call(self);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Changes readyState and calls onreadystatechange.
|
|
558
|
+
*
|
|
559
|
+
* @param int state New state
|
|
560
|
+
*/
|
|
561
|
+
var setState = function(state) {
|
|
562
|
+
if (state == self.NEWDATA) {
|
|
563
|
+
self.dispatchEvent("readystatechange");
|
|
564
|
+
}
|
|
565
|
+
if (self.readyState !== state) {
|
|
566
|
+
self.readyState = state;
|
|
567
|
+
|
|
568
|
+
if (settings.async || self.readyState < self.OPENED || self.readyState === self.DONE) {
|
|
569
|
+
self.dispatchEvent("readystatechange");
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
if (self.readyState === self.DONE && !errorFlag) {
|
|
573
|
+
self.dispatchEvent("load");
|
|
574
|
+
// @TODO figure out InspectorInstrumentation::didLoadXHR(cookie)
|
|
575
|
+
self.dispatchEvent("loadend");
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
};
|