From bb1cbdff269383294f9525b8bed3e3cda03fe82b Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 7 Nov 2017 16:51:29 -0500 Subject: [PATCH] Fix responseCharset parameter in HTTP methods Setting `contentCharset` on the channel doesn't seem to work anymore, so use `overrideMimeType()` instead like we do in the connector. As noted in the comment, we should probably have a `responseContentType` parameter instead, since that's what XHR actually allows. For the moment we just use `text/plain`. --- chrome/content/zotero/xpcom/http.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/chrome/content/zotero/xpcom/http.js b/chrome/content/zotero/xpcom/http.js index 941508bb8..f73cd10b1 100644 --- a/chrome/content/zotero/xpcom/http.js +++ b/chrome/content/zotero/xpcom/http.js @@ -174,8 +174,13 @@ Zotero.HTTP = new function() { channel.forceAllowThirdPartyCookie = true; // Set charset + // + // This is the method used in the connector, but as noted there, this parameter is a + // legacy of XPCOM functionality (where it could be set on the nsIChannel, which + // doesn't seem to work anymore), and we should probably allow responseContentType to + // be set instead if (options.responseCharset) { - channel.contentCharset = responseCharset; + xmlhttp.overrideMimeType(`text/plain; charset=${responseCharset}`); } // Disable caching if requested @@ -342,9 +347,9 @@ Zotero.HTTP = new function() { channel.QueryInterface(Components.interfaces.nsIHttpChannelInternal); channel.forceAllowThirdPartyCookie = true; - // Set charset + // Set charset -- see note in request() above if (responseCharset) { - channel.contentCharset = responseCharset; + xmlhttp.overrideMimeType(`text/plain; charset=${responseCharset}`); } // Set request headers @@ -360,7 +365,7 @@ Zotero.HTTP = new function() { var useMethodjit = Components.utils.methodjit; /** @ignore */ xmlhttp.onreadystatechange = function() { - _stateChange(xmlhttp, onDone, responseCharset); + _stateChange(xmlhttp, onDone); }; if(cookieSandbox) cookieSandbox.attachToInterfaceRequestor(xmlhttp.getInterface(Components.interfaces.nsIInterfaceRequestor)); @@ -414,9 +419,9 @@ Zotero.HTTP = new function() { channel.QueryInterface(Components.interfaces.nsIHttpChannelInternal); channel.forceAllowThirdPartyCookie = true; - // Set charset + // Set charset -- see note in request() above if (responseCharset) { - channel.contentCharset = responseCharset; + xmlhttp.overrideMimeType(`text/plain; charset=${responseCharset}`); } if (headers) { @@ -444,7 +449,7 @@ Zotero.HTTP = new function() { var useMethodjit = Components.utils.methodjit; /** @ignore */ xmlhttp.onreadystatechange = function() { - _stateChange(xmlhttp, onDone, responseCharset); + _stateChange(xmlhttp, onDone); }; if(cookieSandbox) cookieSandbox.attachToInterfaceRequestor(xmlhttp.getInterface(Components.interfaces.nsIInterfaceRequestor)); @@ -988,11 +993,10 @@ Zotero.HTTP = new function() { * * @param {nsIXMLHttpRequest} xmlhttp XMLHttpRequest whose state just changed * @param {Function} [callback] Callback for request completion - * @param {String} [responseCharset] Character set to force on the response * @param {*} [data] Data to be passed back to callback as the second argument * @private */ - function _stateChange(xmlhttp, callback, responseCharset, data) { + function _stateChange(xmlhttp, callback, data) { switch (xmlhttp.readyState){ // Request not yet made case 1: