mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Replace RedirectAllHttpHandler usage with RedirectingHttpHandler
This commit is contained in:
@@ -269,7 +269,6 @@ export * from './server/HttpResponse';
|
||||
export * from './server/HttpServerFactory';
|
||||
export * from './server/OperationHttpHandler';
|
||||
export * from './server/ParsingHttpHandler';
|
||||
export * from './server/RedirectingHttpHandler';
|
||||
export * from './server/WebSocketHandler';
|
||||
export * from './server/WebSocketServerFactory';
|
||||
|
||||
@@ -280,7 +279,7 @@ export * from './server/middleware/StaticAssetHandler';
|
||||
export * from './server/middleware/WebSocketAdvertiser';
|
||||
|
||||
// Server/Util
|
||||
export * from './server/util/RedirectAllHttpHandler';
|
||||
export * from './server/util/RedirectingHttpHandler';
|
||||
export * from './server/util/RouterHandler';
|
||||
|
||||
// Storage/Accessors
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
import type { TargetExtractor } from '../../http/input/identifier/TargetExtractor';
|
||||
import { RedirectResponseDescription } from '../../http/output/response/RedirectResponseDescription';
|
||||
import type { ResponseWriter } from '../../http/output/ResponseWriter';
|
||||
import { FoundHttpError } from '../../util/errors/FoundHttpError';
|
||||
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
|
||||
import { getRelativeUrl, joinUrl } from '../../util/PathUtil';
|
||||
import type { HttpHandlerInput } from '../HttpHandler';
|
||||
import { HttpHandler } from '../HttpHandler';
|
||||
|
||||
export interface RedirectAllHttpHandlerArgs {
|
||||
baseUrl: string;
|
||||
target: string;
|
||||
targetExtractor: TargetExtractor;
|
||||
responseWriter: ResponseWriter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will redirect all incoming requests to the given target.
|
||||
* In case the incoming request already has the correct target,
|
||||
* the `canHandle` call will reject the input.
|
||||
*/
|
||||
export class RedirectAllHttpHandler extends HttpHandler {
|
||||
private readonly baseUrl: string;
|
||||
private readonly target: string;
|
||||
private readonly targetExtractor: TargetExtractor;
|
||||
private readonly responseWriter: ResponseWriter;
|
||||
|
||||
public constructor(args: RedirectAllHttpHandlerArgs) {
|
||||
super();
|
||||
this.baseUrl = args.baseUrl;
|
||||
this.target = args.target;
|
||||
this.targetExtractor = args.targetExtractor;
|
||||
this.responseWriter = args.responseWriter;
|
||||
}
|
||||
|
||||
public async canHandle({ request }: HttpHandlerInput): Promise<void> {
|
||||
const target = await getRelativeUrl(this.baseUrl, request, this.targetExtractor);
|
||||
if (target === this.target) {
|
||||
throw new NotImplementedHttpError('Target is already correct.');
|
||||
}
|
||||
}
|
||||
|
||||
public async handle({ response }: HttpHandlerInput): Promise<void> {
|
||||
const result = new RedirectResponseDescription(new FoundHttpError(joinUrl(this.baseUrl, this.target)));
|
||||
await this.responseWriter.handleSafe({ response, result });
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
import type { TargetExtractor } from '../http/input/identifier/TargetExtractor';
|
||||
import { RedirectResponseDescription } from '../http/output/response/RedirectResponseDescription';
|
||||
import type { ResponseWriter } from '../http/output/ResponseWriter';
|
||||
import { getLoggerFor } from '../logging/LogUtil';
|
||||
import { FoundHttpError } from '../util/errors/FoundHttpError';
|
||||
import { MovedPermanentlyHttpError } from '../util/errors/MovedPermanentlyHttpError';
|
||||
import { NotImplementedHttpError } from '../util/errors/NotImplementedHttpError';
|
||||
import { PermanentRedirectHttpError } from '../util/errors/PermanentRedirectHttpError';
|
||||
import type { RedirectHttpError } from '../util/errors/RedirectHttpError';
|
||||
import { SeeOtherHttpError } from '../util/errors/SeeOtherHttpError';
|
||||
import { TemporaryRedirectHttpError } from '../util/errors/TemporaryRedirectHttpError';
|
||||
import { getRelativeUrl, joinUrl } from '../util/PathUtil';
|
||||
import type { HttpHandlerInput } from './HttpHandler';
|
||||
import { HttpHandler } from './HttpHandler';
|
||||
import type { HttpRequest } from './HttpRequest';
|
||||
import type { TargetExtractor } from '../../http/input/identifier/TargetExtractor';
|
||||
import { RedirectResponseDescription } from '../../http/output/response/RedirectResponseDescription';
|
||||
import type { ResponseWriter } from '../../http/output/ResponseWriter';
|
||||
import { getLoggerFor } from '../../logging/LogUtil';
|
||||
import { FoundHttpError } from '../../util/errors/FoundHttpError';
|
||||
import { MovedPermanentlyHttpError } from '../../util/errors/MovedPermanentlyHttpError';
|
||||
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
|
||||
import { PermanentRedirectHttpError } from '../../util/errors/PermanentRedirectHttpError';
|
||||
import type { RedirectHttpError } from '../../util/errors/RedirectHttpError';
|
||||
import { SeeOtherHttpError } from '../../util/errors/SeeOtherHttpError';
|
||||
import { TemporaryRedirectHttpError } from '../../util/errors/TemporaryRedirectHttpError';
|
||||
import { getRelativeUrl, joinUrl } from '../../util/PathUtil';
|
||||
import type { HttpHandlerInput } from '../HttpHandler';
|
||||
import { HttpHandler } from '../HttpHandler';
|
||||
import type { HttpRequest } from '../HttpRequest';
|
||||
|
||||
const redirectErrorFactories: Record<301 | 302 | 303 | 307 | 308, (location: string) => RedirectHttpError> = {
|
||||
301: (location: string): RedirectHttpError => new MovedPermanentlyHttpError(location),
|
||||
Reference in New Issue
Block a user