Skip to content

Commit

Permalink
browser(firefox): do not fail when decoding large responses (#2130)
Browse files Browse the repository at this point in the history
String.fromCharCode cannot be used to convert very large arrays to strings. Use chunking in this case.
  • Loading branch information
dgozman authored May 7, 2020
1 parent 7a01bb1 commit 98d32c5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion browser_patches/firefox/BUILD_NUMBER
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1090
1091
47 changes: 43 additions & 4 deletions browser_patches/firefox/patches/bootstrap.diff
Original file line number Diff line number Diff line change
Expand Up @@ -1400,17 +1400,16 @@ index 0000000000000000000000000000000000000000..2b1fe7fa712ae210af3ebbccda084041
+
diff --git a/juggler/NetworkObserver.js b/juggler/NetworkObserver.js
new file mode 100644
index 0000000000000000000000000000000000000000..1a55b5498c18d2403eab21fe9149242f286157d4
index 0000000000000000000000000000000000000000..4ed81876c3e176cf07fdeab4ca3fc83874f865a3
--- /dev/null
+++ b/juggler/NetworkObserver.js
@@ -0,0 +1,794 @@
@@ -0,0 +1,833 @@
+"use strict";
+
+const {EventEmitter} = ChromeUtils.import('resource://gre/modules/EventEmitter.jsm');
+const {Helper} = ChromeUtils.import('chrome://juggler/content/Helper.js');
+const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
+const {NetUtil} = ChromeUtils.import('resource://gre/modules/NetUtil.jsm');
+const {CommonUtils} = ChromeUtils.import("resource://services-common/utils.js");
+
+
+const Cc = Components.classes;
Expand Down Expand Up @@ -1978,7 +1977,7 @@ index 0000000000000000000000000000000000000000..1a55b5498c18d2403eab21fe9149242f
+ let result = response.body;
+ if (response.encodings && response.encodings.length) {
+ for (const encoding of response.encodings)
+ result = CommonUtils.convertString(result, encoding, 'uncompressed');
+ result = convertString(result, encoding, 'uncompressed');
+ }
+ return {base64body: btoa(result)};
+ }
Expand Down Expand Up @@ -2171,6 +2170,46 @@ index 0000000000000000000000000000000000000000..1a55b5498c18d2403eab21fe9149242f
+ }
+}
+
+function convertString(s, source, dest) {
+ const is = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
+ Ci.nsIStringInputStream
+ );
+ is.setData(s, s.length);
+ const listener = Cc["@mozilla.org/network/stream-loader;1"].createInstance(
+ Ci.nsIStreamLoader
+ );
+ let result = [];
+ listener.init({
+ onStreamComplete: function onStreamComplete(
+ loader,
+ context,
+ status,
+ length,
+ data
+ ) {
+ const array = Array.from(data);
+ const kChunk = 100000;
+ for (let i = 0; i < length; i += kChunk) {
+ const len = Math.min(kChunk, length - i);
+ const chunk = String.fromCharCode.apply(this, array.slice(i, i + len));
+ result.push(chunk);
+ }
+ },
+ });
+ const converter = Cc["@mozilla.org/streamConverters;1"].getService(
+ Ci.nsIStreamConverterService
+ ).asyncConvertData(
+ source,
+ dest,
+ listener,
+ null
+ );
+ converter.onStartRequest(null, null);
+ converter.onDataAvailable(null, is, 0, s.length);
+ converter.onStopRequest(null, null, null);
+ return result.join('');
+}
+
+const errorMap = {
+ 'aborted': Cr.NS_ERROR_ABORT,
+ 'accessdenied': Cr.NS_ERROR_PORT_ACCESS_NOT_ALLOWED,
Expand Down

0 comments on commit 98d32c5

Please sign in to comment.