2017-03-10 10:06:03 +03:00
|
|
|
/*
|
|
|
|
* vim: expandtab shiftwidth=4 softtabstop=4
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global dav */
|
2015-11-02 15:19:30 +03:00
|
|
|
if (typeof dav == 'undefined') { dav = {}; };
|
|
|
|
|
2015-12-01 23:01:12 +03:00
|
|
|
dav._XML_CHAR_MAP = {
|
2016-01-28 16:24:12 +03:00
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'&': '&',
|
|
|
|
'"': '"',
|
|
|
|
"'": '''
|
2015-12-01 23:01:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
dav._escapeXml = function(s) {
|
2016-01-28 16:24:12 +03:00
|
|
|
return s.replace(/[<>&"']/g, function (ch) {
|
|
|
|
return dav._XML_CHAR_MAP[ch];
|
|
|
|
});
|
2015-12-01 23:01:12 +03:00
|
|
|
};
|
|
|
|
|
2015-11-02 15:19:30 +03:00
|
|
|
dav.Client = function(options) {
|
|
|
|
var i;
|
|
|
|
for(i in options) {
|
|
|
|
this[i] = options[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
dav.Client.prototype = {
|
|
|
|
|
|
|
|
baseUrl : null,
|
|
|
|
|
|
|
|
userName : null,
|
|
|
|
|
|
|
|
password : null,
|
|
|
|
|
|
|
|
|
|
|
|
xmlNamespaces : {
|
|
|
|
'DAV:' : 'd'
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a propFind request.
|
|
|
|
*
|
|
|
|
* @param {string} url Url to do the propfind request on
|
|
|
|
* @param {Array} properties List of properties to retrieve.
|
2016-02-16 19:28:24 +03:00
|
|
|
* @param {Object} [headers] headers
|
2015-11-02 15:19:30 +03:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-02-16 19:28:24 +03:00
|
|
|
propFind : function(url, properties, depth, headers) {
|
2015-11-02 15:19:30 +03:00
|
|
|
|
|
|
|
if(typeof depth == "undefined") {
|
|
|
|
depth = 0;
|
|
|
|
}
|
|
|
|
|
2016-02-16 19:28:24 +03:00
|
|
|
headers = headers || {};
|
|
|
|
|
|
|
|
headers['Depth'] = depth;
|
|
|
|
headers['Content-Type'] = 'application/xml; charset=utf-8';
|
2015-11-02 15:19:30 +03:00
|
|
|
|
|
|
|
var body =
|
|
|
|
'<?xml version="1.0"?>\n' +
|
|
|
|
'<d:propfind ';
|
|
|
|
var namespace;
|
|
|
|
for (namespace in this.xmlNamespaces) {
|
|
|
|
body += ' xmlns:' + this.xmlNamespaces[namespace] + '="' + namespace + '"';
|
|
|
|
}
|
|
|
|
body += '>\n' +
|
|
|
|
' <d:prop>\n';
|
|
|
|
|
|
|
|
for(var ii in properties) {
|
|
|
|
|
|
|
|
var property = this.parseClarkNotation(properties[ii]);
|
|
|
|
if (this.xmlNamespaces[property.namespace]) {
|
|
|
|
body+=' <' + this.xmlNamespaces[property.namespace] + ':' + property.name + ' />\n';
|
|
|
|
} else {
|
|
|
|
body+=' <x:' + property.name + ' xmlns:x="' + property.namespace + '" />\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
body+=' </d:prop>\n';
|
|
|
|
body+='</d:propfind>';
|
|
|
|
|
|
|
|
return this.request('PROPFIND', url, headers, body).then(
|
|
|
|
function(result) {
|
|
|
|
|
|
|
|
if (depth===0) {
|
|
|
|
return {
|
|
|
|
status: result.status,
|
2016-01-28 16:24:12 +03:00
|
|
|
body: result.body[0],
|
2015-11-02 15:19:30 +03:00
|
|
|
xhr: result.xhr
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
status: result.status,
|
2016-01-28 16:24:12 +03:00
|
|
|
body: result.body,
|
2015-11-02 15:19:30 +03:00
|
|
|
xhr: result.xhr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-03-10 10:06:03 +03:00
|
|
|
/**
|
|
|
|
* Renders a "d:set" block for the given properties.
|
|
|
|
*
|
|
|
|
* @param {Object.<String,String>} properties
|
|
|
|
* @return {String} XML "<d:set>" block
|
|
|
|
*/
|
|
|
|
_renderPropSet: function(properties) {
|
|
|
|
var body = ' <d:set>\n' +
|
|
|
|
' <d:prop>\n';
|
|
|
|
|
|
|
|
for(var ii in properties) {
|
|
|
|
var property = this.parseClarkNotation(ii);
|
|
|
|
var propName;
|
|
|
|
var propValue = properties[ii];
|
|
|
|
if (this.xmlNamespaces[property.namespace]) {
|
|
|
|
propName = this.xmlNamespaces[property.namespace] + ':' + property.name;
|
|
|
|
} else {
|
|
|
|
propName = 'x:' + property.name + ' xmlns:x="' + property.namespace + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: hard-coded for now until we allow properties to
|
|
|
|
// specify whether to be escaped or not
|
|
|
|
if (propName !== 'd:resourcetype') {
|
|
|
|
propValue = dav._escapeXml(propValue);
|
|
|
|
}
|
|
|
|
body += ' <' + propName + '>' + propValue + '</' + propName + '>\n';
|
|
|
|
}
|
|
|
|
body +=' </d:prop>\n';
|
|
|
|
body +=' </d:set>\n';
|
|
|
|
return body;
|
|
|
|
},
|
|
|
|
|
2015-12-01 23:01:12 +03:00
|
|
|
/**
|
|
|
|
* Generates a propPatch request.
|
|
|
|
*
|
|
|
|
* @param {string} url Url to do the proppatch request on
|
2017-03-10 10:06:03 +03:00
|
|
|
* @param {Object.<String,String>} properties List of properties to store.
|
2016-02-16 19:28:24 +03:00
|
|
|
* @param {Object} [headers] headers
|
2015-12-01 23:01:12 +03:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
propPatch : function(url, properties, headers) {
|
|
|
|
headers = headers || {};
|
|
|
|
|
|
|
|
headers['Content-Type'] = 'application/xml; charset=utf-8';
|
|
|
|
|
|
|
|
var body =
|
|
|
|
'<?xml version="1.0"?>\n' +
|
|
|
|
'<d:propertyupdate ';
|
|
|
|
var namespace;
|
|
|
|
for (namespace in this.xmlNamespaces) {
|
|
|
|
body += ' xmlns:' + this.xmlNamespaces[namespace] + '="' + namespace + '"';
|
|
|
|
}
|
2017-03-10 10:06:03 +03:00
|
|
|
body += '>\n' + this._renderPropSet(properties);
|
|
|
|
body += '</d:propertyupdate>';
|
2015-12-01 23:01:12 +03:00
|
|
|
|
2017-03-10 10:06:03 +03:00
|
|
|
return this.request('PROPPATCH', url, headers, body).then(
|
|
|
|
function(result) {
|
|
|
|
return {
|
|
|
|
status: result.status,
|
|
|
|
body: result.body,
|
|
|
|
xhr: result.xhr
|
|
|
|
};
|
|
|
|
}.bind(this)
|
|
|
|
);
|
2015-12-01 23:01:12 +03:00
|
|
|
|
2017-03-10 10:06:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a MKCOL request.
|
|
|
|
* If attributes are given, it will use an extended MKCOL request.
|
|
|
|
*
|
|
|
|
* @param {string} url Url to do the proppatch request on
|
|
|
|
* @param {Object.<String,String>} [properties] list of properties to store.
|
|
|
|
* @param {Object} [headers] headers
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
mkcol : function(url, properties, headers) {
|
|
|
|
var body = '';
|
|
|
|
headers = headers || {};
|
|
|
|
headers['Content-Type'] = 'application/xml; charset=utf-8';
|
|
|
|
|
|
|
|
if (properties) {
|
|
|
|
body =
|
|
|
|
'<?xml version="1.0"?>\n' +
|
|
|
|
'<d:mkcol';
|
|
|
|
var namespace;
|
|
|
|
for (namespace in this.xmlNamespaces) {
|
|
|
|
body += ' xmlns:' + this.xmlNamespaces[namespace] + '="' + namespace + '"';
|
2015-12-01 23:01:12 +03:00
|
|
|
}
|
2017-03-10 10:06:03 +03:00
|
|
|
body += '>\n' + this._renderPropSet(properties);
|
|
|
|
body +='</d:mkcol>';
|
2015-12-01 23:01:12 +03:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:06:03 +03:00
|
|
|
return this.request('MKCOL', url, headers, body).then(
|
2015-12-01 23:01:12 +03:00
|
|
|
function(result) {
|
|
|
|
return {
|
|
|
|
status: result.status,
|
|
|
|
body: result.body,
|
|
|
|
xhr: result.xhr
|
|
|
|
};
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-11-02 15:19:30 +03:00
|
|
|
/**
|
|
|
|
* Performs a HTTP request, and returns a Promise
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
request : function(method, url, headers, body) {
|
|
|
|
|
2016-01-28 16:24:12 +03:00
|
|
|
var self = this;
|
2015-11-02 15:19:30 +03:00
|
|
|
var xhr = this.xhrProvider();
|
2016-02-24 12:09:54 +03:00
|
|
|
headers = headers || {};
|
|
|
|
|
2015-11-02 15:19:30 +03:00
|
|
|
if (this.userName) {
|
|
|
|
headers['Authorization'] = 'Basic ' + btoa(this.userName + ':' + this.password);
|
|
|
|
// 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]);
|
|
|
|
}
|
2016-02-10 15:52:54 +03:00
|
|
|
|
|
|
|
// Work around for edge
|
|
|
|
if (body === undefined) {
|
|
|
|
xhr.send();
|
|
|
|
} else {
|
|
|
|
xhr.send(body);
|
|
|
|
}
|
2015-11-02 15:19:30 +03:00
|
|
|
|
|
|
|
return new Promise(function(fulfill, reject) {
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
|
|
|
|
if (xhr.readyState !== 4) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-28 16:24:12 +03:00
|
|
|
var resultBody = xhr.response;
|
|
|
|
if (xhr.status === 207) {
|
|
|
|
resultBody = self.parseMultiStatus(xhr.response);
|
|
|
|
}
|
|
|
|
|
2015-11-02 15:19:30 +03:00
|
|
|
fulfill({
|
2016-01-28 16:24:12 +03:00
|
|
|
body: resultBody,
|
2015-11-02 15:19:30 +03:00
|
|
|
status: xhr.status,
|
|
|
|
xhr: xhr
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.ontimeout = function() {
|
|
|
|
|
|
|
|
reject(new Error('Timeout exceeded'));
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an XMLHttpRequest object.
|
|
|
|
*
|
|
|
|
* This is in its own method, so it can be easily overridden.
|
|
|
|
*
|
|
|
|
* @return {XMLHttpRequest}
|
|
|
|
*/
|
|
|
|
xhrProvider : function() {
|
|
|
|
|
|
|
|
return new XMLHttpRequest();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-11-24 13:59:00 +03:00
|
|
|
/**
|
|
|
|
* Parses a property node.
|
|
|
|
*
|
|
|
|
* Either returns a string if the node only contains text, or returns an
|
|
|
|
* array of non-text subnodes.
|
|
|
|
*
|
|
|
|
* @param {Object} propNode node to parse
|
|
|
|
* @return {string|Array} text content as string or array of subnodes, excluding text nodes
|
|
|
|
*/
|
|
|
|
_parsePropNode: function(propNode) {
|
|
|
|
var content = null;
|
|
|
|
if (propNode.childNodes && propNode.childNodes.length > 0) {
|
|
|
|
var subNodes = [];
|
|
|
|
// filter out text nodes
|
|
|
|
for (var j = 0; j < propNode.childNodes.length; j++) {
|
|
|
|
var node = propNode.childNodes[j];
|
|
|
|
if (node.nodeType === 1) {
|
|
|
|
subNodes.push(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (subNodes.length) {
|
|
|
|
content = subNodes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 13:52:55 +03:00
|
|
|
return content || propNode.textContent || propNode.text || '';
|
2015-11-24 13:59:00 +03:00
|
|
|
},
|
2015-11-02 15:19:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a multi-status response body.
|
|
|
|
*
|
|
|
|
* @param {string} xmlBody
|
|
|
|
* @param {Array}
|
|
|
|
*/
|
|
|
|
parseMultiStatus : function(xmlBody) {
|
|
|
|
|
|
|
|
var parser = new DOMParser();
|
|
|
|
var doc = parser.parseFromString(xmlBody, "application/xml");
|
|
|
|
|
|
|
|
var resolver = function(foo) {
|
|
|
|
var ii;
|
|
|
|
for(ii in this.xmlNamespaces) {
|
|
|
|
if (this.xmlNamespaces[ii] === foo) {
|
|
|
|
return ii;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
|
2015-11-19 17:22:52 +03:00
|
|
|
var responseIterator = doc.evaluate('/d:multistatus/d:response', doc, resolver, XPathResult.ANY_TYPE, null);
|
2015-11-02 15:19:30 +03:00
|
|
|
|
|
|
|
var result = [];
|
|
|
|
var responseNode = responseIterator.iterateNext();
|
|
|
|
|
|
|
|
while(responseNode) {
|
|
|
|
|
|
|
|
var response = {
|
|
|
|
href : null,
|
|
|
|
propStat : []
|
|
|
|
};
|
|
|
|
|
2015-11-19 17:22:52 +03:00
|
|
|
response.href = doc.evaluate('string(d:href)', responseNode, resolver, XPathResult.ANY_TYPE, null).stringValue;
|
2015-11-02 15:19:30 +03:00
|
|
|
|
2015-11-19 17:22:52 +03:00
|
|
|
var propStatIterator = doc.evaluate('d:propstat', responseNode, resolver, XPathResult.ANY_TYPE, null);
|
2015-11-02 15:19:30 +03:00
|
|
|
var propStatNode = propStatIterator.iterateNext();
|
|
|
|
|
|
|
|
while(propStatNode) {
|
|
|
|
var propStat = {
|
2015-11-19 17:22:52 +03:00
|
|
|
status : doc.evaluate('string(d:status)', propStatNode, resolver, XPathResult.ANY_TYPE, null).stringValue,
|
2017-03-10 10:06:03 +03:00
|
|
|
properties : {},
|
2015-11-02 15:19:30 +03:00
|
|
|
};
|
|
|
|
|
2015-11-19 17:22:52 +03:00
|
|
|
var propIterator = doc.evaluate('d:prop/*', propStatNode, resolver, XPathResult.ANY_TYPE, null);
|
2015-11-02 15:19:30 +03:00
|
|
|
|
|
|
|
var propNode = propIterator.iterateNext();
|
|
|
|
while(propNode) {
|
2015-11-24 13:59:00 +03:00
|
|
|
var content = this._parsePropNode(propNode);
|
2015-11-02 15:19:30 +03:00
|
|
|
propStat.properties['{' + propNode.namespaceURI + '}' + propNode.localName] = content;
|
|
|
|
propNode = propIterator.iterateNext();
|
|
|
|
|
|
|
|
}
|
|
|
|
response.propStat.push(propStat);
|
|
|
|
propStatNode = propStatIterator.iterateNext();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push(response);
|
|
|
|
responseNode = responseIterator.iterateNext();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a relative url, and maps it to an absolute url, using the baseUrl
|
|
|
|
*
|
|
|
|
* @param {string} url
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
resolveUrl : function(url) {
|
|
|
|
|
|
|
|
// Note: this is rudamentary.. not sure yet if it handles every case.
|
|
|
|
if (/^https?:\/\//i.test(url)) {
|
|
|
|
// absolute
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
var baseParts = this.parseUrl(this.baseUrl);
|
|
|
|
if (url.charAt('/')) {
|
|
|
|
// Url starts with a slash
|
|
|
|
return baseParts.root + url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Url does not start with a slash, we need grab the base url right up until the last slash.
|
|
|
|
var newUrl = baseParts.root + '/';
|
|
|
|
if (baseParts.path.lastIndexOf('/')!==-1) {
|
|
|
|
newUrl = newUrl = baseParts.path.subString(0, baseParts.path.lastIndexOf('/')) + '/';
|
|
|
|
}
|
|
|
|
newUrl+=url;
|
|
|
|
return url;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a url and returns its individual components.
|
|
|
|
*
|
|
|
|
* @param {String} url
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
parseUrl : function(url) {
|
|
|
|
|
|
|
|
var parts = url.match(/^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/);
|
|
|
|
var result = {
|
|
|
|
url : parts[0],
|
|
|
|
scheme : parts[1],
|
|
|
|
host : parts[3],
|
|
|
|
port : parts[4],
|
|
|
|
path : parts[5],
|
|
|
|
query : parts[6],
|
|
|
|
fragment : parts[7],
|
|
|
|
};
|
|
|
|
result.root =
|
|
|
|
result.scheme + '://' +
|
|
|
|
result.host +
|
|
|
|
(result.port ? ':' + result.port : '');
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
parseClarkNotation : function(propertyName) {
|
|
|
|
|
|
|
|
var result = propertyName.match(/^{([^}]+)}(.*)$/);
|
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name : result[2],
|
|
|
|
namespace : result[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|