Skip to content

Commit 82012b7

Browse files
committed
Clean up style for redirects
1 parent 8c6d6a3 commit 82012b7

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed

lib/XMLHttpRequest.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ exports.XMLHttpRequest = function() {
176176
*/
177177
this.setDisableHeaderCheck = function(state) {
178178
disableHeaderCheck = state;
179-
}
179+
};
180180

181181
/**
182182
* Sets a header for the request.
@@ -249,7 +249,7 @@ exports.XMLHttpRequest = function() {
249249
}
250250

251251
return "";
252-
}
252+
};
253253

254254
/**
255255
* Sends the request to the server.
@@ -378,29 +378,35 @@ exports.XMLHttpRequest = function() {
378378
// As per spec, this is called here for historical reasons.
379379
self.dispatchEvent("readystatechange");
380380

381-
// Create the request
382-
381+
// Handler for the response
383382
function responseHandler(resp) {
384-
383+
// Set response var to the response we got back
384+
// This is so it remains accessable outside this scope
385385
response = resp;
386-
387-
if(response.statusCode === 302 || response.statusCode === 307 || response.statusCode === 303){
388-
settings.url = response.headers['location']
386+
// Check for redirect
387+
// @TODO Prevent looped redirects
388+
if (response.statusCode === 302 || response.statusCode === 303 || response.statusCode === 307) {
389+
// Change URL to the redirect location
390+
settings.url = response.headers.location;
389391
var url = Url.parse(settings.url);
390-
host = url.hostname
392+
// Set host var in case it's used later
393+
host = url.hostname;
394+
// Options for the new request
391395
var newOptions = {
392396
hostname: url.hostname,
393397
port: url.port,
394398
path: url.path,
395399
method: response.statusCode === 303 ? 'GET' : settings.method,
396400
headers: headers
397-
}
401+
};
398402

403+
// Issue the new request
399404
request = doRequest(newOptions, responseHandler).on('error', errorHandler);
400-
request.end()
401-
return
405+
request.end();
406+
// @TODO Check if an XHR event needs to be fired here
407+
return;
402408
}
403-
409+
404410
response.setEncoding("utf8");
405411

406412
setState(self.HEADERS_RECEIVED);
@@ -429,10 +435,13 @@ exports.XMLHttpRequest = function() {
429435
self.handleError(error);
430436
});
431437
}
438+
439+
// Error handler for the request
432440
function errorHandler(error) {
433441
self.handleError(error);
434442
}
435443

444+
// Create the request
436445
request = doRequest(options, responseHandler).on('error', errorHandler);
437446

438447
// Node 0.4 and later won't accept empty data. Make sure it's needed.

tests/test-redirect-302.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ var sys = require("util")
66

77
// Test server
88
var server = http.createServer(function (req, res) {
9-
10-
if(req.url === '/redirectingResource'){
11-
res.writeHead(302, {'Location': 'http://localhost:8000/'})
12-
res.end()
13-
return
9+
if (req.url === '/redirectingResource') {
10+
res.writeHead(302, {'Location': 'http://localhost:8000/'});
11+
res.end();
12+
return;
1413
}
1514

1615
var body = "Hello World";
@@ -28,8 +27,8 @@ var server = http.createServer(function (req, res) {
2827

2928
xhr.onreadystatechange = function() {
3029
if (this.readyState == 4) {
31-
assert.equal(xhr.getRequestHeader('Location'), '')
32-
assert.equal(xhr.responseText, "Hello World")
30+
assert.equal(xhr.getRequestHeader('Location'), '');
31+
assert.equal(xhr.responseText, "Hello World");
3332
sys.puts("done");
3433
}
3534
};

tests/test-redirect-303.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ var sys = require("util")
66

77
// Test server
88
var server = http.createServer(function (req, res) {
9-
10-
if(req.url === '/redirectingResource'){
11-
res.writeHead(303, {'Location': 'http://localhost:8000/'})
12-
res.end()
13-
return
9+
if (req.url === '/redirectingResource') {
10+
res.writeHead(303, {'Location': 'http://localhost:8000/'});
11+
res.end();
12+
return;
1413
}
1514

1615
var body = "Hello World";
@@ -28,8 +27,8 @@ var server = http.createServer(function (req, res) {
2827

2928
xhr.onreadystatechange = function() {
3029
if (this.readyState == 4) {
31-
assert.equal(xhr.getRequestHeader('Location'), '')
32-
assert.equal(xhr.responseText, "Hello World")
30+
assert.equal(xhr.getRequestHeader('Location'), '');
31+
assert.equal(xhr.responseText, "Hello World");
3332
sys.puts("done");
3433
}
3534
};

tests/test-redirect-307.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ var sys = require("util")
66

77
// Test server
88
var server = http.createServer(function (req, res) {
9-
10-
if(req.url === '/redirectingResource'){
11-
res.writeHead(307, {'Location': 'http://localhost:8000/'})
12-
res.end()
13-
return
9+
if (req.url === '/redirectingResource') {
10+
res.writeHead(307, {'Location': 'http://localhost:8000/'});
11+
res.end();
12+
return;
1413
}
15-
16-
assert.equal(req.method, 'POST')
14+
15+
assert.equal(req.method, 'POST');
1716

1817
var body = "Hello World";
1918
res.writeHead(200, {
@@ -30,8 +29,8 @@ var server = http.createServer(function (req, res) {
3029

3130
xhr.onreadystatechange = function() {
3231
if (this.readyState == 4) {
33-
assert.equal(xhr.getRequestHeader('Location'), '')
34-
assert.equal(xhr.responseText, "Hello World")
32+
assert.equal(xhr.getRequestHeader('Location'), '');
33+
assert.equal(xhr.responseText, "Hello World");
3534
sys.puts("done");
3635
}
3736
};

0 commit comments

Comments
 (0)