2015-11-02 12:19:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global dav */
|
|
|
|
(function(dav) {
|
|
|
|
|
|
|
|
/**
|
2015-11-19 16:20:06 +00:00
|
|
|
* Override davclient.js methods with IE-compatible logic
|
2015-11-02 12:19:30 +00:00
|
|
|
*/
|
|
|
|
dav.Client.prototype = _.extend({}, dav.Client.prototype, {
|
|
|
|
|
|
|
|
/**
|
2016-02-05 13:40:27 +00:00
|
|
|
* Performs a HTTP request, and returns a Promise
|
2015-11-02 12:19:30 +00:00
|
|
|
*
|
2016-02-05 13:40:27 +00:00
|
|
|
* @param {string} method HTTP method
|
|
|
|
* @param {string} url Relative or absolute url
|
|
|
|
* @param {Object} headers HTTP headers as an object.
|
|
|
|
* @param {string} body HTTP request body.
|
2015-11-02 12:19:30 +00:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-02-05 13:40:27 +00:00
|
|
|
request : function(method, url, headers, body) {
|
2015-11-02 12:19:30 +00:00
|
|
|
|
2016-02-05 13:40:27 +00:00
|
|
|
var self = this;
|
|
|
|
var xhr = this.xhrProvider();
|
2016-06-22 14:58:06 +00:00
|
|
|
headers = headers || {};
|
2016-02-05 13:40:27 +00:00
|
|
|
|
|
|
|
if (this.userName) {
|
2017-05-01 03:45:41 +00:00
|
|
|
headers.Authorization = 'Basic ' + btoa(this.userName + ':' + this.password);
|
2016-02-05 13:40:27 +00:00
|
|
|
// xhr.open(method, this.resolveUrl(url), true, this.userName, this.password);
|
|
|
|
}
|
|
|
|
xhr.open(method, this.resolveUrl(url), true);
|
|
|
|
var ii;
|
|
|
|
for(ii in headers) {
|
|
|
|
xhr.setRequestHeader(ii, headers[ii]);
|
2015-11-02 12:19:30 +00:00
|
|
|
}
|
2016-03-10 12:06:02 +00:00
|
|
|
|
|
|
|
if (body === undefined) {
|
|
|
|
xhr.send();
|
|
|
|
} else {
|
|
|
|
xhr.send(body);
|
|
|
|
}
|
2015-11-02 12:19:30 +00:00
|
|
|
|
2016-02-05 13:40:27 +00:00
|
|
|
return new Promise(function(fulfill, reject) {
|
2015-11-02 12:19:30 +00:00
|
|
|
|
2016-02-05 13:40:27 +00:00
|
|
|
xhr.onreadystatechange = function() {
|
2015-11-02 12:19:30 +00:00
|
|
|
|
2016-02-05 13:40:27 +00:00
|
|
|
if (xhr.readyState !== 4) {
|
|
|
|
return;
|
|
|
|
}
|
2015-11-02 12:19:30 +00:00
|
|
|
|
2016-02-05 13:40:27 +00:00
|
|
|
var resultBody = xhr.response;
|
|
|
|
if (xhr.status === 207) {
|
|
|
|
resultBody = self.parseMultiStatus(xhr.responseXML);
|
2015-11-02 12:19:30 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 13:40:27 +00:00
|
|
|
fulfill({
|
|
|
|
body: resultBody,
|
|
|
|
status: xhr.status,
|
|
|
|
xhr: xhr
|
|
|
|
});
|
2015-11-02 12:19:30 +00:00
|
|
|
|
2016-02-05 13:40:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.ontimeout = function() {
|
|
|
|
|
|
|
|
reject(new Error('Timeout exceeded'));
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
2015-11-02 12:19:30 +00:00
|
|
|
|
2016-02-05 13:40:27 +00:00
|
|
|
},
|
2015-11-02 12:19:30 +00:00
|
|
|
|
|
|
|
_getElementsByTagName: function(node, name, resolver) {
|
|
|
|
var parts = name.split(':');
|
|
|
|
var tagName = parts[1];
|
|
|
|
var namespace = resolver(parts[0]);
|
2019-04-15 11:04:51 +00:00
|
|
|
// make sure we can get elements
|
|
|
|
if (typeof node === 'string') {
|
|
|
|
var parser = new DOMParser()
|
|
|
|
node = parser.parseFromString(node, 'text/xml')
|
|
|
|
}
|
2015-11-02 12:19:30 +00:00
|
|
|
if (node.getElementsByTagNameNS) {
|
|
|
|
return node.getElementsByTagNameNS(namespace, tagName);
|
|
|
|
}
|
|
|
|
return node.getElementsByTagName(name);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a multi-status response body.
|
|
|
|
*
|
|
|
|
* @param {string} xmlBody
|
|
|
|
* @param {Array}
|
|
|
|
*/
|
|
|
|
parseMultiStatus : function(doc) {
|
|
|
|
var result = [];
|
|
|
|
var resolver = function(foo) {
|
|
|
|
var ii;
|
|
|
|
for(ii in this.xmlNamespaces) {
|
|
|
|
if (this.xmlNamespaces[ii] === foo) {
|
|
|
|
return ii;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
var responses = this._getElementsByTagName(doc, 'd:response', resolver);
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < responses.length; i++) {
|
|
|
|
var responseNode = responses[i];
|
|
|
|
var response = {
|
|
|
|
href : null,
|
|
|
|
propStat : []
|
|
|
|
};
|
|
|
|
|
|
|
|
var hrefNode = this._getElementsByTagName(responseNode, 'd:href', resolver)[0];
|
|
|
|
|
|
|
|
response.href = hrefNode.textContent || hrefNode.text;
|
|
|
|
|
|
|
|
var propStatNodes = this._getElementsByTagName(responseNode, 'd:propstat', resolver);
|
|
|
|
var j = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < propStatNodes.length; j++) {
|
|
|
|
var propStatNode = propStatNodes[j];
|
|
|
|
var statusNode = this._getElementsByTagName(propStatNode, 'd:status', resolver)[0];
|
|
|
|
|
|
|
|
var propStat = {
|
|
|
|
status : statusNode.textContent || statusNode.text,
|
|
|
|
properties : []
|
|
|
|
};
|
|
|
|
|
|
|
|
var propNode = this._getElementsByTagName(propStatNode, 'd:prop', resolver)[0];
|
|
|
|
if (!propNode) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var k = 0;
|
|
|
|
for (k = 0; k < propNode.childNodes.length; k++) {
|
|
|
|
var prop = propNode.childNodes[k];
|
2015-11-24 10:59:00 +00:00
|
|
|
var value = this._parsePropNode(prop);
|
2015-11-02 12:19:30 +00:00
|
|
|
propStat.properties['{' + prop.namespaceURI + '}' + (prop.localName || prop.baseName)] = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
response.propStat.push(propStat);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
})(dav);
|
|
|
|
|