fix: Let Representations always have a body

This is relevant when the request has a content-type
but no data.
This commit is contained in:
Joachim Van Herwegen
2021-10-11 15:21:40 +02:00
parent f1ef2ced03
commit 5613ff9e71
36 changed files with 95 additions and 63 deletions

View File

@@ -29,7 +29,7 @@ export interface Operation {
*/
permissionSet?: PermissionSet;
/**
* Optional representation of the body.
* Representation of the body and metadata headers.
*/
body?: Representation;
body: Representation;
}

View File

@@ -19,4 +19,4 @@ export interface BodyParserArgs {
* Parses the body of an incoming {@link HttpRequest} and converts it to a {@link Representation}.
*/
export abstract class BodyParser extends
AsyncHandler<BodyParserArgs, Representation | undefined> {}
AsyncHandler<BodyParserArgs, Representation> {}

View File

@@ -13,7 +13,7 @@ export class RawBodyParser extends BodyParser {
// Note that the only reason this is a union is in case the body is empty.
// If this check gets moved away from the BodyParsers this union could be removed
public async handle({ request, metadata }: BodyParserArgs): Promise<Representation | undefined> {
public async handle({ request, metadata }: BodyParserArgs): Promise<Representation> {
const {
'content-type': contentType,
'content-length': contentLength,
@@ -26,7 +26,7 @@ export class RawBodyParser extends BodyParser {
// some still provide a Content-Length of 0 (but without Content-Type).
if ((!contentLength || (/^0+$/u.test(contentLength) && !contentType)) && !transferEncoding) {
this.logger.debug('HTTP request does not have a body, or its empty body is missing a Content-Type header');
return;
return new BasicRepresentation([], metadata);
}
// While RFC7231 allows treating a body without content type as an octet stream,

View File

@@ -38,6 +38,7 @@ export class SparqlUpdateBodyParser extends BodyParser {
binary: true,
data: guardedStreamFrom(sparql),
metadata,
isEmpty: false,
};
}
}

View File

@@ -32,9 +32,9 @@ export class PatchOperationHandler extends OperationHandler {
// Solid, §2.1: "A Solid server MUST reject PUT, POST and PATCH requests
// without the Content-Type header with a status code of 400."
// https://solid.github.io/specification/protocol#http-server
if (!operation.body?.metadata.contentType) {
this.logger.warn('No Content-Type header specified on PATCH request');
throw new BadRequestHttpError('No Content-Type header specified on PATCH request');
if (!operation.body.metadata.contentType) {
this.logger.warn('PATCH requests require the Content-Type header to be set');
throw new BadRequestHttpError('PATCH requests require the Content-Type header to be set');
}
await this.store.modifyResource(operation.target, operation.body as Patch, operation.conditions);
return new ResetResponseDescription();

View File

@@ -31,9 +31,9 @@ export class PostOperationHandler extends OperationHandler {
// Solid, §2.1: "A Solid server MUST reject PUT, POST and PATCH requests
// without the Content-Type header with a status code of 400."
// https://solid.github.io/specification/protocol#http-server
if (!operation.body?.metadata.contentType) {
this.logger.warn('No Content-Type header specified on POST request');
throw new BadRequestHttpError('No Content-Type header specified on POST request');
if (!operation.body.metadata.contentType) {
this.logger.warn('POST requests require the Content-Type header to be set');
throw new BadRequestHttpError('POST requests require the Content-Type header to be set');
}
const identifier = await this.store.addResource(operation.target, operation.body, operation.conditions);
return new CreatedResponseDescription(identifier);

View File

@@ -31,9 +31,9 @@ export class PutOperationHandler extends OperationHandler {
// Solid, §2.1: "A Solid server MUST reject PUT, POST and PATCH requests
// without the Content-Type header with a status code of 400."
// https://solid.github.io/specification/protocol#http-server
if (!operation.body?.metadata.contentType) {
this.logger.warn('No Content-Type header specified on PUT request');
throw new BadRequestHttpError('No Content-Type header specified on PUT request');
if (!operation.body.metadata.contentType) {
this.logger.warn('PUT requests require the Content-Type header to be set');
throw new BadRequestHttpError('PUT requests require the Content-Type header to be set');
}
await this.store.setRepresentation(operation.target, operation.body, operation.conditions);
return new ResetResponseDescription();

View File

@@ -24,6 +24,11 @@ export class BasicRepresentation implements Representation {
public readonly metadata: RepresentationMetadata;
public readonly binary: boolean;
/**
* An empty Representation
*/
public constructor();
/**
* @param data - The representation data
* @param metadata - The representation metadata
@@ -86,13 +91,15 @@ export class BasicRepresentation implements Representation {
);
public constructor(
data: Readable | any[] | string,
metadata: RepresentationMetadata | MetadataRecord | MetadataIdentifier | string,
data?: Readable | any[] | string,
metadata?: RepresentationMetadata | MetadataRecord | MetadataIdentifier | string,
metadataRest?: MetadataRecord | string | boolean,
binary?: boolean,
) {
if (typeof data === 'string' || Array.isArray(data)) {
data = guardedStreamFrom(data);
} else if (!data) {
data = guardedStreamFrom([]);
}
this.data = guardStream(data);
@@ -110,4 +117,11 @@ export class BasicRepresentation implements Representation {
}
this.binary = binary;
}
/**
* Data should only be interpreted if there is a content type.
*/
public get isEmpty(): boolean {
return !this.metadata.contentType;
}
}

View File

@@ -19,4 +19,10 @@ export interface Representation {
* (as opposed to complex objects).
*/
binary: boolean;
/**
* Whether the data stream is empty.
* This being true does not imply that the data stream has a length of more than 0,
* only that it is a possibility and should be read to be sure.
*/
isEmpty: boolean;
}