Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

added support for 302, 303, 307 redirects #40

Merged
merged 4 commits into from

2 participants

@lfdoherty

I'm not sure I'm updating the internal state correctly - but it works for simple cases at least. XHR requests are automatically/transparently redirected by the browser so I needed this for compatibility node-side.

Thanks for the great module!

Liam

@driverdan driverdan merged commit aa3fbcf into from
@driverdan
Owner

Thanks for the pull request. I know it's late but I finally merged your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 14, 2012
  1. @lfdoherty
  2. @lfdoherty

    cleaned up whitespace

    lfdoherty authored
  3. @lfdoherty

    oops

    lfdoherty authored
Commits on Sep 20, 2012
  1. @lfdoherty
This page is out of date. Refresh to see the latest.
View
29 lib/XMLHttpRequest.js
@@ -378,8 +378,28 @@ exports.XMLHttpRequest = function() {
self.dispatchEvent("readystatechange");
// Create the request
- request = doRequest(options, function(resp) {
+
+ function responseHandler(resp) {
+
response = resp;
+
+ if(response.statusCode === 302 || response.statusCode === 307 || response.statusCode === 303){
+ settings.url = response.headers['location']
+ var url = Url.parse(settings.url);
+ host = url.hostname
+ var newOptions = {
+ hostname: url.hostname,
+ port: url.port,
+ path: url.path,
+ method: response.statusCode === 303 ? 'GET' : settings.method,
+ headers: headers
+ }
+
+ request = doRequest(newOptions, responseHandler).on('error', errorHandler);
+ request.end()
+ return
+ }
+
response.setEncoding("utf8");
setState(self.HEADERS_RECEIVED);
@@ -407,9 +427,12 @@ exports.XMLHttpRequest = function() {
response.on('error', function(error) {
self.handleError(error);
});
- }).on('error', function(error) {
+ }
+ function errorHandler(error) {
self.handleError(error);
- });
+ }
+
+ request = doRequest(options, responseHandler).on('error', errorHandler);
// Node 0.4 and later won't accept empty data. Make sure it's needed.
if (data) {
View
42 tests/test-redirect-302.js
@@ -0,0 +1,42 @@
+var sys = require("util")
+ , assert = require("assert")
+ , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
+ , xhr = new XMLHttpRequest()
+ , http = require("http");
+
+// Test server
+var server = http.createServer(function (req, res) {
+
+ if(req.url === '/redirectingResource'){
+ res.writeHead(302, {'Location': 'http://localhost:8000/'})
+ res.end()
+ return
+ }
+
+ var body = "Hello World";
+ res.writeHead(200, {
+ "Content-Type": "text/plain",
+ "Content-Length": Buffer.byteLength(body),
+ "Date": "Thu, 30 Aug 2012 18:17:53 GMT",
+ "Connection": "close"
+ });
+ res.write("Hello World");
+ res.end();
+
+ this.close();
+}).listen(8000);
+
+xhr.onreadystatechange = function() {
+ if (this.readyState == 4) {
+ assert.equal(xhr.getRequestHeader('Location'), '')
+ assert.equal(xhr.responseText, "Hello World")
+ sys.puts("done");
+ }
+};
+
+try {
+ xhr.open("GET", "http://localhost:8000/redirectingResource");
+ xhr.send();
+} catch(e) {
+ console.log("ERROR: Exception raised", e);
+}
View
42 tests/test-redirect-303.js
@@ -0,0 +1,42 @@
+var sys = require("util")
+ , assert = require("assert")
+ , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
+ , xhr = new XMLHttpRequest()
+ , http = require("http");
+
+// Test server
+var server = http.createServer(function (req, res) {
+
+ if(req.url === '/redirectingResource'){
+ res.writeHead(303, {'Location': 'http://localhost:8000/'})
+ res.end()
+ return
+ }
+
+ var body = "Hello World";
+ res.writeHead(200, {
+ "Content-Type": "text/plain",
+ "Content-Length": Buffer.byteLength(body),
+ "Date": "Thu, 30 Aug 2012 18:17:53 GMT",
+ "Connection": "close"
+ });
+ res.write("Hello World");
+ res.end();
+
+ this.close();
+}).listen(8000);
+
+xhr.onreadystatechange = function() {
+ if (this.readyState == 4) {
+ assert.equal(xhr.getRequestHeader('Location'), '')
+ assert.equal(xhr.responseText, "Hello World")
+ sys.puts("done");
+ }
+};
+
+try {
+ xhr.open("POST", "http://localhost:8000/redirectingResource");
+ xhr.send();
+} catch(e) {
+ console.log("ERROR: Exception raised", e);
+}
View
44 tests/test-redirect-307.js
@@ -0,0 +1,44 @@
+var sys = require("util")
+ , assert = require("assert")
+ , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
+ , xhr = new XMLHttpRequest()
+ , http = require("http");
+
+// Test server
+var server = http.createServer(function (req, res) {
+
+ if(req.url === '/redirectingResource'){
+ res.writeHead(307, {'Location': 'http://localhost:8000/'})
+ res.end()
+ return
+ }
+
+ assert.equal(req.method, 'POST')
+
+ var body = "Hello World";
+ res.writeHead(200, {
+ "Content-Type": "text/plain",
+ "Content-Length": Buffer.byteLength(body),
+ "Date": "Thu, 30 Aug 2012 18:17:53 GMT",
+ "Connection": "close"
+ });
+ res.write("Hello World");
+ res.end();
+
+ this.close();
+}).listen(8000);
+
+xhr.onreadystatechange = function() {
+ if (this.readyState == 4) {
+ assert.equal(xhr.getRequestHeader('Location'), '')
+ assert.equal(xhr.responseText, "Hello World")
+ sys.puts("done");
+ }
+};
+
+try {
+ xhr.open("POST", "http://localhost:8000/redirectingResource");
+ xhr.send();
+} catch(e) {
+ console.log("ERROR: Exception raised", e);
+}
Something went wrong with that request. Please try again.