mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add simple preference parser
This commit is contained in:
42
src/ldp/http/SimplePreferenceParser.ts
Normal file
42
src/ldp/http/SimplePreferenceParser.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { HttpRequest } from '../../server/HttpRequest';
|
||||
import { PreferenceParser } from './PreferenceParser';
|
||||
import { RepresentationPreference } from '../representation/RepresentationPreference';
|
||||
import { RepresentationPreferences } from '../representation/RepresentationPreferences';
|
||||
|
||||
export class SimplePreferenceParser extends PreferenceParser {
|
||||
public constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public async canHandle(): Promise<void> {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public async handle(input: HttpRequest): Promise<RepresentationPreferences> {
|
||||
const type = this.parseHeader(input.headers.accept);
|
||||
const charset = this.parseHeader(input.headers['accept-charset'] as string);
|
||||
const language = this.parseHeader(input.headers['accept-language']);
|
||||
|
||||
// Datetime can have commas so requires separate rules
|
||||
let datetime;
|
||||
if (input.headers['accept-datetime']) {
|
||||
datetime = [{ value: input.headers['accept-datetime'] as string }];
|
||||
}
|
||||
|
||||
return { type, charset, datetime, language };
|
||||
}
|
||||
|
||||
private parseHeader(header: string): RepresentationPreference[] {
|
||||
if (!header) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return header.split(',').map((preference): RepresentationPreference => {
|
||||
const parts = preference.split(';');
|
||||
if (parts.length === 1) {
|
||||
return { value: parts[0].trim() };
|
||||
}
|
||||
return { value: parts[0].trim(), weight: parseFloat(parts[1].trim().slice('q='.length)) };
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user