aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--caldav.js28
-rw-r--r--lib/caldav/xhr.js28
-rw-r--r--test/caldav/xhr_test.js63
3 files changed, 86 insertions, 33 deletions
diff --git a/caldav.js b/caldav.js
index 6975f65..7f3d367 100644
--- a/caldav.js
+++ b/caldav.js
@@ -2068,7 +2068,6 @@ function write (chunk) {
} else {
xhr = new this.xhrClass();
}
-
// This hack is in place due to some platform
// bug in gecko when using mozSystem xhr
// the credentials only seem to work as expected
@@ -2083,6 +2082,12 @@ function write (chunk) {
));
}
+ var useMozChunkedText = false;
+ if (this.globalXhrOptions && this.globalXhrOptions.useMozChunkedText) {
+ useMozChunkedText = true;
+ xhr.responseType = 'moz-chunked-text';
+ }
+
for (header in this.headers) {
if (Object.hasOwnProperty.call(this.headers, header)) {
xhr.setRequestHeader(header, this.headers[header]);
@@ -2096,13 +2101,20 @@ function write (chunk) {
if ('onprogress' in xhr) {
hasProgressEvents = true;
var last = 0;
- xhr.onprogress = function onProgress(event) {
- var chunk = xhr.responseText.substr(last, event.loaded);
- last = event.loaded;
- if (this.ondata) {
- this.ondata(chunk);
- }
- }.bind(this);
+
+ if (useMozChunkedText) {
+ xhr.onprogress = function onChunkedProgress(event) {
+ this.ondata(xhr.responseText);
+ }.bind(this);
+ } else {
+ xhr.onprogress = function onProgress(event) {
+ var chunk = xhr.responseText.substr(last, event.loaded);
+ last = event.loaded;
+ if (this.ondata) {
+ this.ondata(chunk);
+ }
+ }.bind(this);
+ }
}
xhr.onreadystatechange = function onReadyStateChange() {
diff --git a/lib/caldav/xhr.js b/lib/caldav/xhr.js
index 230f656..d49186b 100644
--- a/lib/caldav/xhr.js
+++ b/lib/caldav/xhr.js
@@ -86,7 +86,6 @@
} else {
xhr = new this.xhrClass();
}
-
// This hack is in place due to some platform
// bug in gecko when using mozSystem xhr
// the credentials only seem to work as expected
@@ -101,6 +100,12 @@
));
}
+ var useMozChunkedText = false;
+ if (this.globalXhrOptions && this.globalXhrOptions.useMozChunkedText) {
+ useMozChunkedText = true;
+ xhr.responseType = 'moz-chunked-text';
+ }
+
for (header in this.headers) {
if (Object.hasOwnProperty.call(this.headers, header)) {
xhr.setRequestHeader(header, this.headers[header]);
@@ -114,13 +119,20 @@
if ('onprogress' in xhr) {
hasProgressEvents = true;
var last = 0;
- xhr.onprogress = function onProgress(event) {
- var chunk = xhr.responseText.substr(last, event.loaded);
- last = event.loaded;
- if (this.ondata) {
- this.ondata(chunk);
- }
- }.bind(this);
+
+ if (useMozChunkedText) {
+ xhr.onprogress = function onChunkedProgress(event) {
+ this.ondata(xhr.responseText);
+ }.bind(this);
+ } else {
+ xhr.onprogress = function onProgress(event) {
+ var chunk = xhr.responseText.substr(last, event.loaded);
+ last = event.loaded;
+ if (this.ondata) {
+ this.ondata(chunk);
+ }
+ }.bind(this);
+ }
}
xhr.onreadystatechange = function onReadyStateChange() {
diff --git a/test/caldav/xhr_test.js b/test/caldav/xhr_test.js
index aa9b4f2..6130346 100644
--- a/test/caldav/xhr_test.js
+++ b/test/caldav/xhr_test.js
@@ -104,7 +104,7 @@ suite('webacls/xhr', function() {
});
suite('requests real files', function() {
- function request(path) {
+ function request(path, globalOptions) {
path = 'fixtures/' + path;
if (typeof(__dirname) !== 'undefined') {
@@ -113,7 +113,7 @@ suite('webacls/xhr', function() {
path = '/test/caldav/' + path;
}
- return new Xhr({ url: path });
+ return new Xhr({ url: path, globalXhrOptions: globalOptions });
}
test('get', function(done) {
@@ -125,27 +125,56 @@ suite('webacls/xhr', function() {
});
});
- test('.ondata', function(done) {
- var subject = request('long.txt');
- var gotData = '';
+ suite('.ondata', function() {
+ var expected;
+ var file = 'long.txt';
- subject.ondata = function(chunk) {
- gotData += chunk;
- };
+ setup(function(done) {
+ if (expected) {
+ done();
+ }
+
+ var req = request(file);
+ req.send(function(err, xhr) {
+ expected = xhr.responseText;
+ done();
+ });
+ });
- subject.send(function(err, xhr) {
- var data = xhr.responseText;
+ if (this.navigator && navigator.userAgent.indexOf('Mozilla') !== -1) {
+ test('.ondata with chunked', function(done) {
+ var subject = request('long.txt', { useMozChunkedText: true });
+ var gotData = '';
+
+ subject.ondata = function(chunk) {
+ gotData += chunk;
+ };
+
+ var xhr = subject.send(function(err, xhr) {
+ assert.ok(!xhr.responseText);
+ assert.equal(xhr.responseType, 'moz-chunked-text');
+ assert.equal(expected, gotData);
+ done();
+ });
+ });
+ }
- assert.equal(
- data.trim(),
- gotData.trim(),
- 'sends ondata'
- );
+ test('.ondata', function(done) {
+ var subject = request(file);
+ var gotData = '';
- done();
+ subject.ondata = function(chunk) {
+ gotData += chunk;
+ };
+
+ subject.send(function(err, xhr) {
+ assert.equal(expected, gotData);
+ done();
+ });
});
- });
+
+ });
});
});