1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Fix WebDAV by requesting new ETag. (#3230)

* Fix WebDAV by requesting new ETag conditionally

For me. this was saving only the first time and subsequently failing.
Having revised the requests, I noticed it didn't get a new ETag after saving.
Seems not all WebDAV implementations return a new ETag in PUT requests.

In my WebDAV service (WsgiDAV) - ETag is only served from a HEAD
request.

So if no ETag is found with PUT - we request one with HEAD.

This patch fixes error handling and should also work with servers that
provide ETag directly upon PUT.

* Add tweak from PMario
This commit is contained in:
Sebastian Silva 2018-05-04 05:48:38 -05:00 committed by Jeremy Ruston
parent 38baa70bc6
commit 46e8e4343a

View File

@ -15,6 +15,24 @@ to the current URL, such as a WebDAV server.
/*global $tw: false */ /*global $tw: false */
"use strict"; "use strict";
/*
Retrieve ETag if available
*/
var RetrieveETag = function(self) {
var headers = { "Accept": "*/*;charset=UTF-8" };
$tw.utils.httpRequest({
url: self.uri(),
type: "HEAD",
headers: headers,
callback: function(err, data, xhr) {
if(!err) {
self.etag = xhr.getResponseHeader("ETag").replace(/^W\//,"");
}
}
});
};
/* /*
Select the appropriate saver module and set it up Select the appropriate saver module and set it up
*/ */
@ -34,16 +52,7 @@ var PutSaver = function(wiki) {
} }
} }
}); });
// Retrieve ETag if available RetrieveETag(this);
$tw.utils.httpRequest({
url: uri,
type: "HEAD",
callback: function(err, data, xhr) {
if(!err) {
self.etag = xhr.getResponseHeader("ETag");
}
}
});
}; };
PutSaver.prototype.uri = function() { PutSaver.prototype.uri = function() {
@ -69,15 +78,20 @@ PutSaver.prototype.save = function(text, method, callback) {
data: text, data: text,
callback: function(err, data, xhr) { callback: function(err, data, xhr) {
if(err) { if(err) {
callback(err); // response is textual: "XMLHttpRequest error code: 412"
} else if(xhr.status === 200 || xhr.status === 201) { const status = Number(err.substring(err.indexOf(':') + 2, err.length))
self.etag = xhr.getResponseHeader("ETag"); if(status === 412) { // edit conflict
callback(null); // success var message = $tw.language.getString("Error/EditConflict");
} else if(xhr.status === 412) { // edit conflict callback(message);
var message = $tw.language.getString("Error/EditConflict"); } else {
callback(message); callback(err); // fail
}
} else { } else {
callback(xhr.responseText); // fail self.etag = xhr.getResponseHeader("ETag");
if (self.etag == null) {
RetrieveETag(self);
}
callback(null); // success
} }
} }
}); });