iedavclient.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2015
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. /* global dav */
  11. (function(dav) {
  12. /**
  13. * Override davclient.js methods with IE-compatible logic
  14. */
  15. dav.Client.prototype = _.extend({}, dav.Client.prototype, {
  16. /**
  17. * Performs a HTTP request, and returns a Promise
  18. *
  19. * @param {string} method HTTP method
  20. * @param {string} url Relative or absolute url
  21. * @param {Object} headers HTTP headers as an object.
  22. * @param {string} body HTTP request body.
  23. * @return {Promise}
  24. */
  25. request : function(method, url, headers, body) {
  26. var self = this;
  27. var xhr = this.xhrProvider();
  28. headers = headers || {};
  29. if (this.userName) {
  30. headers['Authorization'] = 'Basic ' + btoa(this.userName + ':' + this.password);
  31. // xhr.open(method, this.resolveUrl(url), true, this.userName, this.password);
  32. }
  33. xhr.open(method, this.resolveUrl(url), true);
  34. var ii;
  35. for(ii in headers) {
  36. xhr.setRequestHeader(ii, headers[ii]);
  37. }
  38. if (body === undefined) {
  39. xhr.send();
  40. } else {
  41. xhr.send(body);
  42. }
  43. return new Promise(function(fulfill, reject) {
  44. xhr.onreadystatechange = function() {
  45. if (xhr.readyState !== 4) {
  46. return;
  47. }
  48. var resultBody = xhr.response;
  49. if (xhr.status === 207) {
  50. resultBody = self.parseMultiStatus(xhr.responseXML);
  51. }
  52. fulfill({
  53. body: resultBody,
  54. status: xhr.status,
  55. xhr: xhr
  56. });
  57. };
  58. xhr.ontimeout = function() {
  59. reject(new Error('Timeout exceeded'));
  60. };
  61. });
  62. },
  63. _getElementsByTagName: function(node, name, resolver) {
  64. var parts = name.split(':');
  65. var tagName = parts[1];
  66. var namespace = resolver(parts[0]);
  67. if (node.getElementsByTagNameNS) {
  68. return node.getElementsByTagNameNS(namespace, tagName);
  69. }
  70. return node.getElementsByTagName(name);
  71. },
  72. /**
  73. * Parses a multi-status response body.
  74. *
  75. * @param {string} xmlBody
  76. * @param {Array}
  77. */
  78. parseMultiStatus : function(doc) {
  79. var result = [];
  80. var resolver = function(foo) {
  81. var ii;
  82. for(ii in this.xmlNamespaces) {
  83. if (this.xmlNamespaces[ii] === foo) {
  84. return ii;
  85. }
  86. }
  87. }.bind(this);
  88. var responses = this._getElementsByTagName(doc, 'd:response', resolver);
  89. var i;
  90. for (i = 0; i < responses.length; i++) {
  91. var responseNode = responses[i];
  92. var response = {
  93. href : null,
  94. propStat : []
  95. };
  96. var hrefNode = this._getElementsByTagName(responseNode, 'd:href', resolver)[0];
  97. response.href = hrefNode.textContent || hrefNode.text;
  98. var propStatNodes = this._getElementsByTagName(responseNode, 'd:propstat', resolver);
  99. var j = 0;
  100. for (j = 0; j < propStatNodes.length; j++) {
  101. var propStatNode = propStatNodes[j];
  102. var statusNode = this._getElementsByTagName(propStatNode, 'd:status', resolver)[0];
  103. var propStat = {
  104. status : statusNode.textContent || statusNode.text,
  105. properties : []
  106. };
  107. var propNode = this._getElementsByTagName(propStatNode, 'd:prop', resolver)[0];
  108. if (!propNode) {
  109. continue;
  110. }
  111. var k = 0;
  112. for (k = 0; k < propNode.childNodes.length; k++) {
  113. var prop = propNode.childNodes[k];
  114. var value = this._parsePropNode(prop);
  115. propStat.properties['{' + prop.namespaceURI + '}' + (prop.localName || prop.baseName)] = value;
  116. }
  117. response.propStat.push(propStat);
  118. }
  119. result.push(response);
  120. }
  121. return result;
  122. }
  123. });
  124. })(dav);