Simplify options checking in Zotero.HTTP.request()

This commit is contained in:
Dan Stillman 2015-03-22 02:36:24 -04:00
parent b437826bd0
commit 842082f818

View File

@ -75,6 +75,8 @@ Zotero.HTTP = new function() {
* code is received (or a code not in options.successCodes if provided). * code is received (or a code not in options.successCodes if provided).
*/ */
this.request = function (method, url, options) { this.request = function (method, url, options) {
options = options || {};
if (url instanceof Components.interfaces.nsIURI) { if (url instanceof Components.interfaces.nsIURI) {
// Don't display password in console // Don't display password in console
var dispURL = this.getDisplayURI(url).spec; var dispURL = this.getDisplayURI(url).spec;
@ -87,7 +89,7 @@ Zotero.HTTP = new function() {
// Don't display API key in console // Don't display API key in console
dispURL = dispURL.replace(/key=[^&]+&?/, "").replace(/\?$/, ""); dispURL = dispURL.replace(/key=[^&]+&?/, "").replace(/\?$/, "");
if(options && options.body) { if (options.body) {
var bodyStart = options.body.substr(0, 1024); var bodyStart = options.body.substr(0, 1024);
// Don't display sync password or session id in console // Don't display sync password or session id in console
bodyStart = bodyStart.replace(/password=[^&]+/, 'password=********'); bodyStart = bodyStart.replace(/password=[^&]+/, 'password=********');
@ -114,13 +116,13 @@ Zotero.HTTP = new function() {
var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(); .createInstance();
// Prevent certificate/authentication dialogs from popping up // Prevent certificate/authentication dialogs from popping up
if (!options || !options.foreground) { if (!options.foreground) {
xmlhttp.mozBackgroundRequest = true; xmlhttp.mozBackgroundRequest = true;
} }
xmlhttp.open(method, url, true); xmlhttp.open(method, url, true);
// Pass the request to a callback // Pass the request to a callback
if (options && options.requestObserver) { if (options.requestObserver) {
options.requestObserver(xmlhttp); options.requestObserver(xmlhttp);
} }
@ -138,24 +140,24 @@ Zotero.HTTP = new function() {
channel.forceAllowThirdPartyCookie = true; channel.forceAllowThirdPartyCookie = true;
// Set charset // Set charset
if (options && options.responseCharset) { if (options.responseCharset) {
channel.contentCharset = responseCharset; channel.contentCharset = responseCharset;
} }
// Disable caching if requested // Disable caching if requested
if(options && options.dontCache) { if (options.dontCache) {
channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE; channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
} }
} }
// Set responseType // Set responseType
if(options && options.responseType) { if (options.responseType) {
xmlhttp.responseType = options.responseType; xmlhttp.responseType = options.responseType;
} }
// Send headers // Send headers
var headers = (options && options.headers) || {}; var headers = (options && options.headers) || {};
if (options && options.body && !headers["Content-Type"]) { if (options.body && !headers["Content-Type"]) {
headers["Content-Type"] = "application/x-www-form-urlencoded"; headers["Content-Type"] = "application/x-www-form-urlencoded";
} }
if (options.debug) { if (options.debug) {
@ -168,11 +170,11 @@ Zotero.HTTP = new function() {
xmlhttp.onloadend = function() { xmlhttp.onloadend = function() {
var status = xmlhttp.status; var status = xmlhttp.status;
if (options && options.successCodes) { if (options.successCodes) {
var success = options.successCodes.indexOf(status) != -1; var success = options.successCodes.indexOf(status) != -1;
} }
// Explicit FALSE means allow any status code // Explicit FALSE means allow any status code
else if (options && options.successCodes === false) { else if (options.successCodes === false) {
var success = true; var success = true;
} }
else if(isFile) { else if(isFile) {
@ -183,7 +185,7 @@ Zotero.HTTP = new function() {
} }
if(success) { if(success) {
if (options && options.debug) { if (options.debug) {
Zotero.debug("HTTP " + method + " " + dispURL Zotero.debug("HTTP " + method + " " + dispURL
+ " succeeded with " + xmlhttp.status); + " succeeded with " + xmlhttp.status);
Zotero.debug(xmlhttp.responseText); Zotero.debug(xmlhttp.responseText);
@ -193,18 +195,18 @@ Zotero.HTTP = new function() {
var msg = "HTTP " + method + " " + dispURL + " failed: " var msg = "HTTP " + method + " " + dispURL + " failed: "
+ "Unexpected status code " + xmlhttp.status; + "Unexpected status code " + xmlhttp.status;
Zotero.debug(msg, 1); Zotero.debug(msg, 1);
if (options && options.debug) { if (options.debug) {
Zotero.debug(xmlhttp.responseText); Zotero.debug(xmlhttp.responseText);
} }
deferred.reject(new Zotero.HTTP.UnexpectedStatusException(xmlhttp, msg)); deferred.reject(new Zotero.HTTP.UnexpectedStatusException(xmlhttp, msg));
} }
}; };
if(options && options.cookieSandbox) { if (options.cookieSandbox) {
options.cookieSandbox.attachToInterfaceRequestor(xmlhttp); options.cookieSandbox.attachToInterfaceRequestor(xmlhttp);
} }
xmlhttp.send((options && options.body) || null); xmlhttp.send(options.body || null);
return deferred.promise; return deferred.promise;
}; };