feat(federation): Automatically try to reach the HTTP version of an URL if HTTPS fails

This commit is contained in:
Jesse Wierzbinski 2024-07-24 18:20:01 +02:00
parent 2313bcbbcc
commit 021a3485f5
No known key found for this signature in database

View file

@ -58,7 +58,7 @@ export class ResponseError<
export class FederationRequester { export class FederationRequester {
constructor( constructor(
private serverUrl: URL, private serverUrl: URL,
private signatureConstructor: SignatureConstructor, private signatureConstructor?: SignatureConstructor,
public globalCatch: (error: ResponseError) => void = () => { public globalCatch: (error: ResponseError) => void = () => {
// Do nothing by default // Do nothing by default
}, },
@ -125,7 +125,16 @@ export class FederationRequester {
private async request<ReturnType>( private async request<ReturnType>(
request: Request, request: Request,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
const result = await fetch(request); const result = await fetch(request).catch((e) => {
// If using https, try and use http
const url = new URL(request.url);
if (url.protocol === "https") {
url.protocol = "http";
return fetch(url, request);
}
throw e;
});
const isJson = result.headers.get("Content-Type")?.includes("json"); const isJson = result.headers.get("Content-Type")?.includes("json");
if (!result.ok) { if (!result.ok) {
@ -180,7 +189,9 @@ export class FederationRequester {
...extra, ...extra,
}); });
return (await this.signatureConstructor.sign(request)).request; return this.signatureConstructor
? (await this.signatureConstructor.sign(request)).request
: request;
} }
public async get<ReturnType>( public async get<ReturnType>(