mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Handle OPTIONS requests in OperationHandler
This commit is contained in:
parent
6f83ac5ead
commit
ad3edcf1a8
@ -15,6 +15,7 @@
|
|||||||
"DELETE"
|
"DELETE"
|
||||||
],
|
],
|
||||||
"options_credentials": true,
|
"options_credentials": true,
|
||||||
|
"options_preflightContinue": true,
|
||||||
"options_exposedHeaders": [
|
"options_exposedHeaders": [
|
||||||
"Accept-Patch",
|
"Accept-Patch",
|
||||||
"ETag",
|
"ETag",
|
||||||
|
@ -5,9 +5,13 @@
|
|||||||
"@id": "urn:solid-server:default:OperationHandler",
|
"@id": "urn:solid-server:default:OperationHandler",
|
||||||
"@type": "WaterfallHandler",
|
"@type": "WaterfallHandler",
|
||||||
"handlers": [
|
"handlers": [
|
||||||
|
{
|
||||||
|
"@type": "OptionsOperationHandler",
|
||||||
|
"resourceSet": { "@id": "urn:solid-server:default:CachedResourceSet" }
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"@type": "GetOperationHandler",
|
"@type": "GetOperationHandler",
|
||||||
"store": { "@id": "urn:solid-server:default:ResourceStore" },
|
"store": { "@id": "urn:solid-server:default:ResourceStore" }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"@type": "PostOperationHandler",
|
"@type": "PostOperationHandler",
|
||||||
@ -23,7 +27,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"@type": "HeadOperationHandler",
|
"@type": "HeadOperationHandler",
|
||||||
"store": { "@id": "urn:solid-server:default:ResourceStore" },
|
"store": { "@id": "urn:solid-server:default:ResourceStore" }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"@type": "PatchOperationHandler",
|
"@type": "PatchOperationHandler",
|
||||||
|
@ -5,7 +5,7 @@ import { isContainerIdentifier } from '../../util/PathUtil';
|
|||||||
import { ModesExtractor } from './ModesExtractor';
|
import { ModesExtractor } from './ModesExtractor';
|
||||||
import { AccessMode } from './Permissions';
|
import { AccessMode } from './Permissions';
|
||||||
|
|
||||||
const READ_METHODS = new Set([ 'GET', 'HEAD' ]);
|
const READ_METHODS = new Set([ 'OPTIONS', 'GET', 'HEAD' ]);
|
||||||
const SUPPORTED_METHODS = new Set([ ...READ_METHODS, 'PUT', 'POST', 'DELETE' ]);
|
const SUPPORTED_METHODS = new Set([ ...READ_METHODS, 'PUT', 'POST', 'DELETE' ]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
36
src/http/ldp/OptionsOperationHandler.ts
Normal file
36
src/http/ldp/OptionsOperationHandler.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import type { ResourceSet } from '../../storage/ResourceSet';
|
||||||
|
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
|
||||||
|
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
|
||||||
|
import { NoContentResponseDescription } from '../output/response/NoContentResponseDescription';
|
||||||
|
import type { ResponseDescription } from '../output/response/ResponseDescription';
|
||||||
|
import type { OperationHandlerInput } from './OperationHandler';
|
||||||
|
import { OperationHandler } from './OperationHandler';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles OPTIONS {@link Operation}s by always returning a 204.
|
||||||
|
*/
|
||||||
|
export class OptionsOperationHandler extends OperationHandler {
|
||||||
|
private readonly resourceSet: ResourceSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses a {@link ResourceSet} to determine the existence of the target resource which impacts the response code.
|
||||||
|
* @param resourceSet - {@link ResourceSet} that knows if the target resource exists or not.
|
||||||
|
*/
|
||||||
|
public constructor(resourceSet: ResourceSet) {
|
||||||
|
super();
|
||||||
|
this.resourceSet = resourceSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async canHandle({ operation }: OperationHandlerInput): Promise<void> {
|
||||||
|
if (operation.method !== 'OPTIONS') {
|
||||||
|
throw new NotImplementedHttpError('This handler only supports OPTIONS operations');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async handle({ operation }: OperationHandlerInput): Promise<ResponseDescription> {
|
||||||
|
if (!await this.resourceSet.hasResource(operation.target)) {
|
||||||
|
throw new NotFoundHttpError();
|
||||||
|
}
|
||||||
|
return new NoContentResponseDescription();
|
||||||
|
}
|
||||||
|
}
|
10
src/http/output/response/NoContentResponseDescription.ts
Normal file
10
src/http/output/response/NoContentResponseDescription.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { ResponseDescription } from './ResponseDescription';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Corresponds to a 204 response.
|
||||||
|
*/
|
||||||
|
export class NoContentResponseDescription extends ResponseDescription {
|
||||||
|
public constructor() {
|
||||||
|
super(204);
|
||||||
|
}
|
||||||
|
}
|
@ -82,6 +82,7 @@ export * from './http/ldp/DeleteOperationHandler';
|
|||||||
export * from './http/ldp/GetOperationHandler';
|
export * from './http/ldp/GetOperationHandler';
|
||||||
export * from './http/ldp/HeadOperationHandler';
|
export * from './http/ldp/HeadOperationHandler';
|
||||||
export * from './http/ldp/OperationHandler';
|
export * from './http/ldp/OperationHandler';
|
||||||
|
export * from './http/ldp/OptionsOperationHandler';
|
||||||
export * from './http/ldp/PatchOperationHandler';
|
export * from './http/ldp/PatchOperationHandler';
|
||||||
export * from './http/ldp/PostOperationHandler';
|
export * from './http/ldp/PostOperationHandler';
|
||||||
export * from './http/ldp/PutOperationHandler';
|
export * from './http/ldp/PutOperationHandler';
|
||||||
@ -103,6 +104,7 @@ export * from './http/output/metadata/WwwAuthMetadataWriter';
|
|||||||
|
|
||||||
// HTTP/Output/Response
|
// HTTP/Output/Response
|
||||||
export * from './http/output/response/CreatedResponseDescription';
|
export * from './http/output/response/CreatedResponseDescription';
|
||||||
|
export * from './http/output/response/NoContentResponseDescription';
|
||||||
export * from './http/output/response/OkResponseDescription';
|
export * from './http/output/response/OkResponseDescription';
|
||||||
export * from './http/output/response/ResetResponseDescription';
|
export * from './http/output/response/ResetResponseDescription';
|
||||||
export * from './http/output/response/ResponseDescription';
|
export * from './http/output/response/ResponseDescription';
|
||||||
|
@ -21,12 +21,13 @@ interface SimpleCorsOptions {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler that sets CORS options on the response.
|
* Handler that sets CORS options on the response.
|
||||||
* In case of an OPTIONS request this handler will close the connection after adding its headers.
|
* In case of an OPTIONS request this handler will close the connection after adding its headers
|
||||||
|
* if `preflightContinue` is set to `false`.
|
||||||
*
|
*
|
||||||
* Solid, §7.1: "A data pod MUST implement the CORS protocol [FETCH] such that, to the extent possible,
|
* Solid, §8.1: "A server MUST implement the CORS protocol [FETCH] such that, to the extent possible,
|
||||||
* the browser allows Solid apps to send any request and combination of request headers to the data pod,
|
* the browser allows Solid apps to send any request and combination of request headers to the server,
|
||||||
* and the Solid app can read any response and response headers received from the data pod."
|
* and the Solid app can read any response and response headers received from the server."
|
||||||
* Full details: https://solid.github.io/specification/protocol#cors-server
|
* Full details: https://solidproject.org/TR/2021/protocol-20211217#cors-server
|
||||||
*/
|
*/
|
||||||
export class CorsHandler extends HttpHandler {
|
export class CorsHandler extends HttpHandler {
|
||||||
private readonly corsHandler: (
|
private readonly corsHandler: (
|
||||||
|
@ -70,7 +70,7 @@ describe('An http server with middleware', (): void => {
|
|||||||
.set('Access-Control-Request-Headers', 'content-type')
|
.set('Access-Control-Request-Headers', 'content-type')
|
||||||
.set('Access-Control-Request-Method', 'POST')
|
.set('Access-Control-Request-Method', 'POST')
|
||||||
.set('Host', 'test.com')
|
.set('Host', 'test.com')
|
||||||
.expect(204);
|
.expect(200);
|
||||||
expect(res.header).toEqual(expect.objectContaining({
|
expect(res.header).toEqual(expect.objectContaining({
|
||||||
'access-control-allow-origin': '*',
|
'access-control-allow-origin': '*',
|
||||||
'access-control-allow-headers': 'content-type',
|
'access-control-allow-headers': 'content-type',
|
||||||
|
@ -44,10 +44,9 @@ const allModes = [ AM.read, AM.append, AM.create, AM.write, AM.delete ];
|
|||||||
// For PUT/PATCH/DELETE we return 205 instead of 200/204
|
// For PUT/PATCH/DELETE we return 205 instead of 200/204
|
||||||
/* eslint-disable no-multi-spaces */
|
/* eslint-disable no-multi-spaces */
|
||||||
const table: [string, string, AM[], AM[] | undefined, string, string, number, number][] = [
|
const table: [string, string, AM[], AM[] | undefined, string, string, number, number][] = [
|
||||||
// We currently handle OPTIONS before authorization
|
[ 'OPTIONS', 'C/R', [], undefined, '', '', 401, 401 ],
|
||||||
// [ 'OPTIONS', 'C/R', [], undefined, '', '', 401, 401 ],
|
[ 'OPTIONS', 'C/R', [], [ AM.read ], '', '', 204, 404 ],
|
||||||
// [ 'OPTIONS', 'C/R', [], [ AM.read ], '', '', 200, 404 ],
|
[ 'OPTIONS', 'C/R', [ AM.read ], undefined, '', '', 204, 404 ],
|
||||||
// [ 'OPTIONS', 'C/R', [ AM.read ], undefined, '', '', 200, 404 ],
|
|
||||||
|
|
||||||
[ 'HEAD', 'C/R', [], undefined, '', '', 401, 401 ],
|
[ 'HEAD', 'C/R', [], undefined, '', '', 401, 401 ],
|
||||||
[ 'HEAD', 'C/R', [], [ AM.read ], '', '', 200, 404 ],
|
[ 'HEAD', 'C/R', [], [ AM.read ], '', '', 200, 404 ],
|
||||||
|
44
test/unit/http/ldp/OptionsOperationHandler.test.ts
Normal file
44
test/unit/http/ldp/OptionsOperationHandler.test.ts
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import { OptionsOperationHandler } from '../../../../src/http/ldp/OptionsOperationHandler';
|
||||||
|
import type { Operation } from '../../../../src/http/Operation';
|
||||||
|
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
|
||||||
|
import { BasicConditions } from '../../../../src/storage/BasicConditions';
|
||||||
|
import type { ResourceSet } from '../../../../src/storage/ResourceSet';
|
||||||
|
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
|
||||||
|
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
|
||||||
|
|
||||||
|
describe('An OptionsOperationHandler', (): void => {
|
||||||
|
let operation: Operation;
|
||||||
|
const conditions = new BasicConditions({});
|
||||||
|
const preferences = {};
|
||||||
|
const body = new BasicRepresentation();
|
||||||
|
let resourceSet: jest.Mocked<ResourceSet>;
|
||||||
|
let handler: OptionsOperationHandler;
|
||||||
|
|
||||||
|
beforeEach(async(): Promise<void> => {
|
||||||
|
operation = { method: 'OPTIONS', target: { path: 'http://test.com/foo' }, preferences, conditions, body };
|
||||||
|
resourceSet = {
|
||||||
|
hasResource: jest.fn().mockResolvedValue(true),
|
||||||
|
};
|
||||||
|
handler = new OptionsOperationHandler(resourceSet);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('only supports Options operations.', async(): Promise<void> => {
|
||||||
|
await expect(handler.canHandle({ operation })).resolves.toBeUndefined();
|
||||||
|
operation.method = 'GET';
|
||||||
|
await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError);
|
||||||
|
operation.method = 'HEAD';
|
||||||
|
await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a 204 response.', async(): Promise<void> => {
|
||||||
|
const result = await handler.handle({ operation });
|
||||||
|
expect(result.statusCode).toBe(204);
|
||||||
|
expect(result.metadata).toBeUndefined();
|
||||||
|
expect(result.data).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a 404 if the target resource does not exist.', async(): Promise<void> => {
|
||||||
|
resourceSet.hasResource.mockResolvedValueOnce(false);
|
||||||
|
await expect(handler.handle({ operation })).rejects.toThrow(NotFoundHttpError);
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user