From 2d75cb83afe3dd8cecf1faef43c7a9c9b7324bfb Mon Sep 17 00:00:00 2001 From: FND Date: Sat, 15 Oct 2016 17:03:50 +0200 Subject: [PATCH] putSaver: detect edit conflicts to prevent clobbering, if possible if the server supplies an ETag, we send it back when saving, allowing the server to detect edit conflicts and respond with 412 (cf. https://www.w3.org/1999/04/Editing/) caveats: * this only kicks in after the first save, as we don't have access to the ETag when first loading the document * there's no recovery mechanism (e.g. resetting `this.etag` in order to force clobbering), other than manually reloading the document --- core/modules/savers/put.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/modules/savers/put.js b/core/modules/savers/put.js index 39f1ca18c..2fdb77fae 100644 --- a/core/modules/savers/put.js +++ b/core/modules/savers/put.js @@ -27,7 +27,7 @@ var PutSaver = function(wiki) { req.open("OPTIONS",encodeURI(document.location.protocol + "//" + document.location.hostname + ":" + document.location.port + document.location.pathname)); req.onload = function() { // Check DAV header http://www.webdav.org/specs/rfc2518.html#rfc.section.9.1 - self.serverAcceptsPuts = (this.status === 200 && !!this.getResponseHeader('dav')); + self.serverAcceptsPuts = (this.status === 200 && !!this.getResponseHeader("dav")); }; req.send(); }; @@ -37,19 +37,25 @@ PutSaver.prototype.save = function(text,method,callback) { return false; } var req = new XMLHttpRequest(); - // TODO: store/check ETags if supported by server, to protect against overwrites + // TODO: in case of edit conflict // Prompt: Do you want to save over this? Y/N // Merging would be ideal, and may be possible using future generic merge flow req.onload = function() { if (this.status === 200 || this.status === 201) { + this.etag = this.getResponseHeader("ETag"); callback(null); // success } + else if (this.status === 412) { // edit conflict + callback("File changed on server"); else { callback(this.responseText); // fail } }; req.open("PUT", encodeURI(window.location.href)); req.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); + if (this.etag) { + req.setRequestHeader("If-Match", this.etag); + } req.send(text); return true; };