Closed Bug 1407928 Opened 8 years ago Closed 8 years ago

Get Response Body In Messy Code In Multiple Process Firefox

Categories

(Core :: Networking, defect)

55 Branch
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: bh7578, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 Steps to reproduce: execute the following code in scratchpad(set the Environmnet to 'Browser'): /////// START - DO NOT EDIT var {classes: Cc, interfaces: Ci, results: Cr, Constructor: CC, utils: Cu} = Components; Cu.import('resource://gre/modules/Services.jsm'); var BinaryInputStream = CC('@mozilla.org/binaryinputstream;1', 'nsIBinaryInputStream', 'setInputStream'); var BinaryOutputStream = CC('@mozilla.org/binaryoutputstream;1', 'nsIBinaryOutputStream', 'setOutputStream'); var StorageStream = CC('@mozilla.org/storagestream;1', 'nsIStorageStream', 'init'); let contentTypeWhiteList = ["application/atom+xml", "application/x-csh", "text/", "application/x-texinfo", "application/javascript", "application/json", "application/xv+xml", "application/x-www-form-urlencoded", "application/xml", "application/postscript", "application/rss+xml", "application/xhtml+xml", "application/xcap-diff+xml", "application/xenc+xml", "application/patch-ops-error+xml", "application/resource-lists+xml", "application/rls-services+xml", "application/resource-lists-diff+xml", "application/xslt+xml"]; function isBinary(contentType){ if(!contentType) return true; let temp = contentType.toLowerCase(); return !contentTypeWhiteList.find((e) => { return temp.indexOf(e) >= 0; }); } function TracingListener() { this.receivedChunks = []; // array for incoming data. holds chunks as they come, onStopRequest we join these junks to get the full source this.responseBody; // we'll set this to the this.responseStatusCode; this.url = ''; this.deferredDone = { promise: null, resolve: null, reject: null }; this.deferredDone.promise = new Promise(function(resolve, reject) { this.resolve = resolve; this.reject = reject; }.bind(this.deferredDone)); Object.freeze(this.deferredDone); this.promiseDone = this.deferredDone.promise; } TracingListener.prototype = { onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aCount) { var iStream = new BinaryInputStream(aInputStream) // binaryaInputStream var sStream = new StorageStream(8192, aCount, null); // storageStream // not sure why its 8192 but thats how eveyrone is doing it, we should ask why var oStream = new BinaryOutputStream(sStream.getOutputStream(0)); // binaryOutputStream // Copy received data as they come. var data = iStream.readBytes(aCount); let chunkdata = data; oStream.writeBytes(data, aCount); let channel = aRequest.QueryInterface(Ci.nsIHttpChannel); if(channel && !isBinary(channel.contentType)){ var tempss = new StorageStream(8192, aCount, null); var tempbos = new BinaryOutputStream(tempss.getOutputStream(0)); tempbos.writeBytes(data, aCount); let replacementChar = Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER; let channel = aRequest.QueryInterface(Ci.nsIHttpChannel); let charset = "UTF-8"; // default charset if(channel){ charset = channel.contentCharset || charset; } var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"] .createInstance(Components.interfaces.nsIConverterInputStream); is.init(tempss.newInputStream(0), charset, 8192, replacementChar); var str = {}, finnalStr = ''; chunkData = ''; while(is.readString(8192, str) != 0) { chunkData += str.value; } is.close(); } this.receivedChunks.push(chunkdata); this.originalListener.onDataAvailable(aRequest, aContext, sStream.newInputStream(0), aOffset, aCount); }, onStartRequest: function(aRequest, aContext) { this.originalListener.onStartRequest(aRequest, aContext); }, onStopRequest: function(aRequest, aContext, aStatusCode) { this.responseBody = this.receivedChunks.join(""); delete this.receivedChunks; this.responseStatus = aStatusCode; this.url = aRequest.QueryInterface(Ci.nsIHttpChannel).URI.spec; this.originalListener.onStopRequest(aRequest, aContext, aStatusCode); this.deferredDone.resolve(); }, QueryInterface: function(aIID) { if (aIID.equals(Ci.nsIStreamListener) || aIID.equals(Ci.nsISupports)) { return this; } throw Cr.NS_NOINTERFACE; } }; var httpResponseObserver = { observe: function(aSubject, aTopic, aData) { var newListener = new TracingListener(); aSubject.QueryInterface(Ci.nsITraceableChannel); newListener.originalListener = aSubject.setNewListener(newListener); /////// END - DO NOT EDIT newListener.promiseDone.then( function() { // no error happened console.log('yay response done, url:' + newListener.url + ', body: ', newListener.responseBody); }, function(aReason) { // promise was rejected, right now i didnt set up rejection, but i should listen to on abort or bade status code then reject maybe } ).catch( function(aCatch) { console.error('something went wrong, a typo by dev probably:', aCatch); } ); } }; Services.obs.addObserver(httpResponseObserver, 'http-on-examine-response', false); // Services.obs.removeObserver(httpResponseObserver, 'http-on-examine-response'); // call this when you dont want to listen anymore Actual results: most http response body are displayed as messy code, even it's content type is text/html. Expected results: if the response body's content type is not binary, it should display as string.
Summary: Can't get http response body → Get Response Body In Messy Code In Multiple Process Firefox
I found the above code works well in single process version firefox, however If enable multiple process for firefox, the response is messy code, I guess the body is compressed. How can get the uncompressed response body?
Component: Plug-ins → Networking
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.