Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work-around for Moz bug #608735 #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var Request = module.exports = function (xhr, params) {
});
}

var res = new Response;
var res = new Response(xhr);
res.on('ready', function () {
self.emit('response', res);
});
Expand Down
50 changes: 29 additions & 21 deletions lib/response.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var EventEmitter = require('events').EventEmitter;

var Response = module.exports = function (res) {
var Response = module.exports = function (xhr) {
this.xhr = xhr;
this.offset = 0;
};

Expand All @@ -11,8 +12,8 @@ var capable = {
status2 : true
};

function parseHeaders (res) {
var lines = res.getAllResponseHeaders().split(/\r?\n/);
function parseHeaders (xhr) {
var lines = xhr.getAllResponseHeaders().split(/\r?\n/);
var headers = {};
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
Expand Down Expand Up @@ -43,14 +44,20 @@ function parseHeaders (res) {
}

Response.prototype.getHeader = function (key) {
return this.headers[key.toLowerCase()];
var header = this.headers[key.toLowerCase()];

// Work around Mozilla bug #608735 [https://bugzil.la/608735], which causes
// getAllResponseHeaders() to return {} if the response is a CORS request.
// xhr.getHeader still works correctly.
return header || this.xhr.getResponseHeader(key);
};

Response.prototype.handle = function (res) {
if (res.readyState === 2 && capable.status2) {
Response.prototype.handle = function () {
var xhr = this.xhr;
if (xhr.readyState === 2 && capable.status2) {
try {
this.statusCode = res.status;
this.headers = parseHeaders(res);
this.statusCode = xhr.status;
this.headers = parseHeaders(xhr);
}
catch (err) {
capable.status2 = false;
Expand All @@ -60,40 +67,41 @@ Response.prototype.handle = function (res) {
this.emit('ready');
}
}
else if (capable.streaming && res.readyState === 3) {
else if (capable.streaming && xhr.readyState === 3) {
try {
if (!this.statusCode) {
this.statusCode = res.status;
this.headers = parseHeaders(res);
this.statusCode = xhr.status;
this.headers = parseHeaders(xhr);
this.emit('ready');
}
}
catch (err) {}

try {
this.write(res);
this.write();
}
catch (err) {
capable.streaming = false;
}
}
else if (res.readyState === 4) {
else if (xhr.readyState === 4) {
if (!this.statusCode) {
this.statusCode = res.status;
this.statusCode = xhr.status;
this.emit('ready');
}
this.write(res);
this.write();

if (res.error) {
this.emit('error', res.responseText);
if (xhr.error) {
this.emit('error', xhr.responseText);
}
else this.emit('end');
}
};

Response.prototype.write = function (res) {
if (res.responseText.length > this.offset) {
this.emit('data', res.responseText.slice(this.offset));
this.offset = res.responseText.length;
Response.prototype.write = function () {
var xhr = this.xhr;
if (xhr.responseText.length > this.offset) {
this.emit('data', xhr.responseText.slice(this.offset));
this.offset = xhr.responseText.length;
}
};