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 Jul 1, 2017
1 parent 5da6423 commit c38572e
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/http-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,25 +408,45 @@ 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') {
if (self.authorization_header_supported === undefined) {
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 (err.toString().indexOf("Error: CORS request rejected") != -1 &&
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;
});
} else {
throw err;
}
});
}

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 c38572e

Please sign in to comment.