Skip to content

Commit

Permalink
Implement failover when server does not allow setting the Authorized-…
Browse files Browse the repository at this point in the history
…header (CORS)
  • Loading branch information
krombel committed Jun 30, 2017
1 parent 5da6423 commit 80fd8d4
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/http-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,25 +408,42 @@ module.exports.MatrixHttpApi.prototype = {
}
}

const request_promise = this.request(
callback, method, path, queryParams, data, opts,
);

const self = this;
request_promise.catch(function(err) {
if (err.errcode == 'M_UNKNOWN_TOKEN') {
let requestPromise = null;
if (this.authorization_header_supported === undefined) {
requestPromise = this.request(
callback, method, path, queryParams, data, opts,
).then((ret) => {
self.authorization_header_supported = true;
return ret;
}).catch((err) => {
if (self.authorization_header_supported === undefined) {
self.authorization_header_supported = false;
delete opts.headers.Authorization;
return self.authedRequest(
callback, method, path, queryParams, data, opts);
callback, method, path, queryParams, data, opts,
).then((ret) => {
return ret;
});
}
});
}

if (requestPromise == null) {
requestPromise = this.request(
callback, method, path, queryParams, data, opts,
);
}

requestPromise.catch(function(err) {
if (err.errcode == 'M_UNKNOWN_TOKEN') {
self.event_emitter.emit("Session.logged_out");
}
});

// return the original promise, otherwise tests break due to it having to
// go around the event loop one more time to process the result of the request
return request_promise;
return requestPromise;
},

/**
Expand Down

0 comments on commit 80fd8d4

Please sign in to comment.