Use foreground requests for manual sync and error reporting

Adds a 'foreground' flag to Zotero.HTTP.promise() options

Also, can now pass successCodes: false to always resolve the promise and never
throw UnexpectedStatusException
This commit is contained in:
Dan Stillman 2013-05-01 15:30:39 -04:00
parent e09295ee76
commit 2a7a604f28
4 changed files with 62 additions and 38 deletions

View file

@ -53,8 +53,13 @@
body += key + '=' + encodeURIComponent(parts[key]) + '&';
}
body = body.substr(0, body.length - 1);
Zotero.HTTP.doPost("https://repo.zotero.org/repo/report", body,
_sendErrorReportCallback);
var url = 'https://repo.zotero.org/repo/report';
Zotero.HTTP.promise('POST', url,
{ body: body, successCodes: false, foreground: true })
.then(function (xmlhttp) {
_sendErrorReportCallback(xmlhttp);
})
.done();
});
}

View file

@ -333,7 +333,11 @@ Zotero_Preferences.Sync = {
// TODO: better way of checking for an active session?
if (Zotero.Sync.Server.sessionIDComponent == 'sessionid=') {
Zotero.Sync.Server.login(callback);
Zotero.Sync.Server.login()
.then(function () {
callback();
})
.done();
}
else {
callback();

View file

@ -53,11 +53,12 @@ Zotero.HTTP = new function() {
* <li>cookieSandbox - The sandbox from which cookies should be taken</li>
* <li>debug - Log response text and status code</li>
* <li>dontCache - If set, specifies that the request should not be fulfilled from the cache</li>
* <li>foreground - Make a foreground request, showing certificate/authentication dialogs if necessary</li>
* <li>headers - HTTP headers to include in the request</li>
* <li>requestObserver - Callback to receive XMLHttpRequest after open()</li>
* <li>responseType - The type of the response. See XHR 2 documentation for legal values</li>
* <li>responseCharset - The charset the response should be interpreted as</li>
* <li>successCodes - HTTP status codes that are considered successful</li>
* <li>successCodes - HTTP status codes that are considered successful, or FALSE to allow all</li>
* </ul>
* @param {Zotero.CookieSandbox} [cookieSandbox] Cookie sandbox object
* @return {Promise} A promise resolved with the XMLHttpRequest object if the request
@ -105,7 +106,9 @@ Zotero.HTTP = new function() {
var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance();
// Prevent certificate/authentication dialogs from popping up
xmlhttp.mozBackgroundRequest = true;
if (!options || !options.foreground) {
xmlhttp.mozBackgroundRequest = true;
}
xmlhttp.open(method, url, true);
// Pass the request to a callback
@ -157,6 +160,10 @@ Zotero.HTTP = new function() {
if (options && options.successCodes) {
var success = options.successCodes.indexOf(status) != -1;
}
// Explicit FALSE means allow any status code
else if (options && options.successCodes === false) {
var success = true;
}
else if(isFile) {
var success = status == 200 || status == 0;
}

View file

@ -1345,7 +1345,7 @@ Zotero.Sync.Server = new function () {
}
};
function login(callback) {
function login() {
var url = _serverURL + "login";
var username = Zotero.Sync.Server.username;
@ -1368,7 +1368,9 @@ Zotero.Sync.Server = new function () {
Zotero.Sync.Runner.setSyncStatus(Zotero.getString('sync.status.loggingIn'));
Zotero.HTTP.doPost(url, body, function (xmlhttp) {
return Zotero.HTTP.promise("POST", url,
{ body: body, successCodes: false, foreground: !Zotero.Sync.Runner.background })
.then(function (xmlhttp) {
_checkResponse(xmlhttp, true);
var response = xmlhttp.responseXML.childNodes[0];
@ -1396,10 +1398,6 @@ Zotero.Sync.Server = new function () {
//Zotero.debug('Got session ID ' + _sessionID + ' from server');
if (callback) {
callback();
}
});
}
@ -1416,9 +1414,11 @@ Zotero.Sync.Server = new function () {
if (!_sessionID) {
Zotero.debug("Session ID not available -- logging in");
Zotero.Sync.Server.login(function () {
Zotero.Sync.Server.login()
.then(function () {
Zotero.Sync.Server.sync(_callbacks);
});
})
.done();
return;
}
@ -1472,9 +1472,11 @@ Zotero.Sync.Server = new function () {
Zotero.debug("Invalid session ID -- logging in");
_sessionID = false;
_syncInProgress = false;
Zotero.Sync.Server.login(function () {
Zotero.Sync.Server.login()
.then(function () {
Zotero.Sync.Server.sync(_callbacks);
});
})
.done();
return;
}
@ -1800,9 +1802,11 @@ Zotero.Sync.Server = new function () {
function clear(callback) {
if (!_sessionID) {
Zotero.debug("Session ID not available -- logging in");
Zotero.Sync.Server.login(function () {
Zotero.Sync.Server.login()
.then(function () {
Zotero.Sync.Server.clear(callback);
});
})
.done();
return;
}
@ -1814,9 +1818,11 @@ Zotero.Sync.Server = new function () {
if (_invalidSession(xmlhttp)) {
Zotero.debug("Invalid session ID -- logging in");
_sessionID = false;
Zotero.Sync.Server.login(function () {
Zotero.Sync.Server.login()
.then(function () {
Zotero.Sync.Server.clear(callback);
});
})
.done();
return;
}
@ -1896,27 +1902,29 @@ Zotero.Sync.Server = new function () {
if (!xmlhttp.responseText) {
var channel = xmlhttp.channel;
// Check SSL cert
var secInfo = channel.securityInfo;
if (secInfo instanceof Ci.nsITransportSecurityInfo) {
secInfo.QueryInterface(Ci.nsITransportSecurityInfo);
if ((secInfo.securityState & Ci.nsIWebProgressListener.STATE_IS_INSECURE) == Ci.nsIWebProgressListener.STATE_IS_INSECURE) {
var url = channel.name;
var ios = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService);
try {
var uri = ios.newURI(url, null, null);
var host = uri.host;
if (channel) {
var secInfo = channel.securityInfo;
if (secInfo instanceof Ci.nsITransportSecurityInfo) {
secInfo.QueryInterface(Ci.nsITransportSecurityInfo);
if ((secInfo.securityState & Ci.nsIWebProgressListener.STATE_IS_INSECURE) == Ci.nsIWebProgressListener.STATE_IS_INSECURE) {
var url = channel.name;
var ios = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService);
try {
var uri = ios.newURI(url, null, null);
var host = uri.host;
}
catch (e) {
Zotero.debug(e);
}
var kbURL = 'http://zotero.org/support/kb/ssl_certificate_error';
_error(Zotero.getString('sync.storage.error.webdav.sslCertificateError', host) + "\n\n"
+ Zotero.getString('general.seeForMoreInformation', kbURL),
false, noReloadOnFailure);
}
catch (e) {
Zotero.debug(e);
else if ((secInfo.securityState & Ci.nsIWebProgressListener.STATE_IS_BROKEN) == Ci.nsIWebProgressListener.STATE_IS_BROKEN) {
_error(Zotero.getString('sync.error.sslConnectionError'), false, noReloadOnFailure);
}
var kbURL = 'http://zotero.org/support/kb/ssl_certificate_error';
_error(Zotero.getString('sync.storage.error.webdav.sslCertificateError', host) + "\n\n"
+ Zotero.getString('general.seeForMoreInformation', kbURL),
false, noReloadOnFailure);
}
else if ((secInfo.securityState & Ci.nsIWebProgressListener.STATE_IS_BROKEN) == Ci.nsIWebProgressListener.STATE_IS_BROKEN) {
_error(Zotero.getString('sync.error.sslConnectionError'), false, noReloadOnFailure);
}
}
if (xmlhttp.status === 0) {