-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLHttpRequest.js
267 lines (222 loc) · 5.83 KB
/
XMLHttpRequest.js
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
/**
* Wrapper for built-in http.js to emulate the browser XMLHttpRequest object.
*
* This can be used with JS designed for browsers to improve reuse of code and
* allow the use of existing libraries.
*
* Usage: include("XMLHttpRequest.js") and use XMLHttpRequest per W3C specs.
*
* @todo SSL Support
* @author Dan DeFelippi <[email protected]>
* @license MIT
*/
exports.XMLHttpRequest = function() {
/**
* Private variables
*/
var self = this;
var http = require('http');
// Holds http.js objects
var client;
var request;
var response;
// Request settings
var settings = {};
// Set some default headers
var defaultHeaders = {
"User-Agent": "node.js",
"Accept": "*/*",
};
var headers = defaultHeaders;
/**
* Constants
*/
this.UNSENT = 0;
this.OPENED = 1;
this.HEADERS_RECEIVED = 2;
this.LOADING = 3;
this.DONE = 4;
/**
* Public vars
*/
// Current state
this.readyState = this.UNSENT;
// default ready state change handler in case one is not set or is set late
this.onreadystatechange = function() {};
// Result & response
this.responseText = "";
this.responseXML = "";
this.status = null;
this.statusText = null;
/**
* Open the connection. Currently supports local server requests.
*
* @param string method Connection method (eg GET, POST)
* @param string url URL for the connection.
* @param boolean async Asynchronous connection. Default is true.
* @param string user Username for basic authentication (optional)
* @param string password Password for basic authentication (optional)
*/
this.open = function(method, url, async, user, password) {
settings = {
"method": method,
"url": url,
"async": async,
"user": user,
"password": password
};
this.abort();
setState(this.OPENED);
};
/**
* Sets a header for the request.
*
* @param string header Header name
* @param string value Header value
*/
this.setRequestHeader = function(header, value) {
headers[header] = value;
};
/**
* Gets a header from the server response.
*
* @param string header Name of header to get.
* @return string Text of the header or null if it doesn't exist.
*/
this.getResponseHeader = function(header) {
if (this.readyState > this.OPENED && response.headers.header) {
return header + ": " + response.headers.header;
}
return null;
};
/**
* Gets all the response headers.
*
* @return string
*/
this.getAllResponseHeaders = function() {
if (this.readyState < this.HEADERS_RECEIVED) {
throw "INVALID_STATE_ERR: Headers have not been received.";
}
var result = "";
for (var i in response.headers) {
result += i + ": " + response.headers[i] + "\r\n";
}
return result.substr(0, result.length - 2);
};
/**
* Sends the request to the server.
*
* @param string data Optional data to send as request body.
*/
this.send = function(data) {
if (this.readyState != this.OPENED) {
throw "INVALID_STATE_ERR: connection must be opened before send() is called";
}
/**
setState(this.OPENED);
* Figure out if a host and/or port were specified.
* Regex borrowed from parseUri and modified. Needs additional optimization.
* @see http://blog.stevenlevithan.com/archives/parseuri
*/
var loc = /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?([^?#]*)/.exec(settings.url);
// Determine the server
switch (loc[1]) {
case 'http':
var host = loc[6];
break;
case undefined:
case '':
var host = "localhost";
break;
case 'https':
throw "SSL is not implemented.";
break;
default:
throw "Protocol not supported.";
}
// Default to port 80. If accessing localhost on another port be sure to
// use http://localhost:port/path
var port = loc[7] ? loc[7] : 80;
// Set the URI, default to /
var uri = loc[8] ? loc[8] : "/";
// Set the Host header or the server may reject the request
headers["Host"] = host;
client = http.createClient(port, host);
client.addListener('error', function (error) { //Error checking
self.status=503;
self.statusText=error;
self.responseText=error.stack;
setState(self.DONE);
//throw error;
})
// Set content length header
if (settings.method == "GET" || settings.method == "HEAD") {
data = null;
} else if (data) {
headers["Content-Length"] = data.length;
if (!headers["Content-Type"]) {
headers["Content-Type"] = "text/plain;charset=UTF-8";
}
}
// Use the correct request method
switch (settings.method) {
case 'GET':
request = client.request("GET",uri, headers);
break;
case 'POST':
request = client.request("POST",uri, headers);
break;
case 'HEAD':
request = client.request("HEAD",uri, headers);
break;
case 'PUT':
request = client.request("PUT",uri, headers);
break;
case 'DELETE':
request = client.request("DELETE",uri, headers);
break;
default:
throw "Request method is unsupported.";
}
// Send data to the server
if (data) {
request.write(data);
}
request.addListener('response', function(resp) {
response = resp;
response.setEncoding("utf8");
setState(this.HEADERS_RECEIVED);
self.status = response.statusCode;
response.addListener("data", function(chunk) {
// Make sure there's some data
if (chunk) {
self.responseText += chunk;
}
setState(self.LOADING);
});
response.addListener("end", function() {
setState(self.DONE);
});
});
request.end();
};
/**
* Aborts a request.
*/
this.abort = function() {
headers = defaultHeaders;
this.readyState = this.UNSENT;
this.responseText = "";
this.responseXML = "";
};
/**
* Changes readyState and calls onreadystatechange.
*
* @param int state New state
*/
var setState = function(state) {
self.readyState = state;
self.onreadystatechange();
}
};