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. if (this.userName) {
  29. headers['Authorization'] = 'Basic ' + btoa(this.userName + ':' + this.password);
  30. // xhr.open(method, this.resolveUrl(url), true, this.userName, this.password);
  31. }
  32. xhr.open(method, this.resolveUrl(url), true);
  33. var ii;
  34. for(ii in headers) {
  35. xhr.setRequestHeader(ii, headers[ii]);
  36. }
  37. if (body === undefined) {
  38. xhr.send();
  39. } else {
  40. xhr.send(body);
  41. }
  42. return new Promise(function(fulfill, reject) {
  43. xhr.onreadystatechange = function() {
  44. if (xhr.readyState !== 4) {
  45. return;
  46. }
  47. var resultBody = xhr.response;
  48. if (xhr.status === 207) {
  49. resultBody = self.parseMultiStatus(xhr.responseXML);
  50. }
  51. fulfill({
  52. body: resultBody,
  53. status: xhr.status,
  54. xhr: xhr
  55. });
  56. };
  57. xhr.ontimeout = function() {
  58. reject(new Error('Timeout exceeded'));
  59. };
  60. });
  61. },
  62. _getElementsByTagName: function(node, name, resolver) {
  63. var parts = name.split(':');
  64. var tagName = parts[1];
  65. var namespace = resolver(parts[0]);
  66. if (node.getElementsByTagNameNS) {
  67. return node.getElementsByTagNameNS(namespace, tagName);
  68. }
  69. return node.getElementsByTagName(name);
  70. },
  71. /**
  72. * Parses a multi-status response body.
  73. *
  74. * @param {string} xmlBody
  75. * @param {Array}
  76. */
  77. parseMultiStatus : function(doc) {
  78. var result = [];
  79. var resolver = function(foo) {
  80. var ii;
  81. for(ii in this.xmlNamespaces) {
  82. if (this.xmlNamespaces[ii] === foo) {
  83. return ii;
  84. }
  85. }
  86. }.bind(this);
  87. var responses = this._getElementsByTagName(doc, 'd:response', resolver);
  88. var i;
  89. for (i = 0; i < responses.length; i++) {
  90. var responseNode = responses[i];
  91. var response = {
  92. href : null,
  93. propStat : []
  94. };
  95. var hrefNode = this._getElementsByTagName(responseNode, 'd:href', resolver)[0];
  96. response.href = hrefNode.textContent || hrefNode.text;
  97. var propStatNodes = this._getElementsByTagName(responseNode, 'd:propstat', resolver);
  98. var j = 0;
  99. for (j = 0; j < propStatNodes.length; j++) {
  100. var propStatNode = propStatNodes[j];
  101. var statusNode = this._getElementsByTagName(propStatNode, 'd:status', resolver)[0];
  102. var propStat = {
  103. status : statusNode.textContent || statusNode.text,
  104. properties : []
  105. };
  106. var propNode = this._getElementsByTagName(propStatNode, 'd:prop', resolver)[0];
  107. if (!propNode) {
  108. continue;
  109. }
  110. var k = 0;
  111. for (k = 0; k < propNode.childNodes.length; k++) {
  112. var prop = propNode.childNodes[k];
  113. var value = this._parsePropNode(prop);
  114. propStat.properties['{' + prop.namespaceURI + '}' + (prop.localName || prop.baseName)] = value;
  115. }
  116. response.propStat.push(propStat);
  117. }
  118. result.push(response);
  119. }
  120. return result;
  121. }
  122. });
  123. })(dav);