don't fail login on missing cors on well-known

This commit is contained in:
Bruno Windels 2021-08-23 19:53:33 +02:00
parent c0d3c950b0
commit 59605a2a57

View File

@ -32,28 +32,35 @@ function getRetryHomeserver(homeserver) {
} }
} }
export async function lookupHomeserver(homeserver, request) { async function getWellKnownResponse(homeserver, request) {
homeserver = normalizeHomeserver(homeserver);
const requestOptions = {format: "json", timeout: 30000, method: "GET"}; const requestOptions = {format: "json", timeout: 30000, method: "GET"};
let wellKnownResponse = null; let wellKnownResponse = null;
while (!wellKnownResponse) { while (!wellKnownResponse) {
try { try {
const wellKnownUrl = `${homeserver}/.well-known/matrix/client`; const wellKnownUrl = `${homeserver}/.well-known/matrix/client`;
wellKnownResponse = await request(wellKnownUrl, requestOptions).response(); return await request(wellKnownUrl, requestOptions).response();
} catch (err) { } catch (err) {
if (err.name === "ConnectionError") { if (err.name === "ConnectionError") {
const retryHS = getRetryHomeserver(homeserver); const retryHS = getRetryHomeserver(homeserver);
if (retryHS) { if (retryHS) {
homeserver = retryHS; homeserver = retryHS;
} else { } else {
throw err; // don't fail lookup on a ConnectionError,
// there might be a missing CORS header on a 404 response or something,
// which won't be a problem necessarily with homeserver requests later on ...
return null;
} }
} else { } else {
throw err; throw err;
} }
} }
} }
if (wellKnownResponse.status === 200) { }
export async function lookupHomeserver(homeserver, request) {
homeserver = normalizeHomeserver(homeserver);
const wellKnownResponse = await getWellKnownResponse(homeserver, request);
if (wellKnownResponse && wellKnownResponse.status === 200) {
const {body} = wellKnownResponse; const {body} = wellKnownResponse;
const wellKnownHomeserver = body["m.homeserver"]?.["base_url"]; const wellKnownHomeserver = body["m.homeserver"]?.["base_url"];
if (typeof wellKnownHomeserver === "string") { if (typeof wellKnownHomeserver === "string") {