diff --git a/src/http-api.js b/src/http-api.js index 68da4643129..16f5f41108b 100644 --- a/src/http-api.js +++ b/src/http-api.js @@ -422,18 +422,44 @@ module.exports.MatrixHttpApi.prototype = { const self = this; + if (this.authorization_header_supported === undefined) { + const defer = q.defer(); + const returnPromise = defer.promise; + returnPromise.abort = requestPromise.abort; + + requestPromise.then((resp) => { + self.authorization_header_supported = true; + defer.resolve(resp); + }, (err) => { + if (err.errcode == 'M_MISSING_TOKEN' || + err.toString().indexOf("Error: CORS request rejected") != -1) { + self.authorization_header_supported = false; + queryParams.access_token = opts.headers.Authorization.substr(7); + delete opts.headers.Authorization; + const secondPromise = self.request( + callback, method, path, queryParams, data, opts, + ); + returnPromise.abort = secondPromise.abort; + secondPromise.then((resp) => { + defer.resolve(resp); + }, (err) => { + if (err.errcode == 'M_UNKNOWN_TOKEN') { + self.event_emitter.emit("Session.logged_out"); + } + defer.reject(err); + }); + } else if (err.errcode == 'M_UNKNOWN_TOKEN') { + self.event_emitter.emit("Session.logged_out"); + defer.reject(err); + } else { + defer.reject(err); + } + }); + + return returnPromise; + } + requestPromise.catch(function(err) { - if (err.errcode == 'M_MISSING_TOKEN' || - err.toString().indexOf("Error: CORS request rejected") != -1) { - self.authorization_header_supported = false; - queryParams.access_token = opts.headers.Authorization.substr(7); - delete opts.headers.Authorization; - const secondPromise = self.request( - callback, method, path, queryParams, data, opts, - ); - requestPromise.abort = secondPromise.abort; - return secondPromise; - } if (err.errcode == 'M_UNKNOWN_TOKEN') { self.event_emitter.emit("Session.logged_out"); }