From 3ad507a439b9bd6b4e221a0ab18cf400cfd6ea91 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Tue, 20 Apr 2021 12:17:36 -0700 Subject: [PATCH] c-headers: do not list PRI as a valid HTTP method PRI gets special treatment (parse preamble and error) and even though it is parsed and allowed after `HTTP/1.x` - it is not technically a method that could appear in the request parsed with llhttp. Remove the method from the list of HTTP methods in C headers to prevent confusion and breakage of `body-parser`. --- src/llhttp/c-headers.ts | 4 +++- src/llhttp/utils.ts | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/llhttp/c-headers.ts b/src/llhttp/c-headers.ts index 8aa3444b..717ed6e3 100644 --- a/src/llhttp/c-headers.ts +++ b/src/llhttp/c-headers.ts @@ -18,7 +18,9 @@ export class CHeaders { const errorMap = enumToMap(constants.ERROR); const methodMap = enumToMap(constants.METHODS); - const httpMethodMap = enumToMap(constants.METHODS, constants.METHODS_HTTP); + const httpMethodMap = enumToMap(constants.METHODS, constants.METHODS_HTTP, [ + constants.METHODS.PRI, + ]); const rtspMethodMap = enumToMap(constants.METHODS, constants.METHODS_RTSP); res += this.buildEnum('llhttp_errno', 'HPE', errorMap); diff --git a/src/llhttp/utils.ts b/src/llhttp/utils.ts index 950d6228..cb4605b1 100644 --- a/src/llhttp/utils.ts +++ b/src/llhttp/utils.ts @@ -2,7 +2,11 @@ export interface IEnumMap { [key: string]: number; } -export function enumToMap(obj: any, filter?: ReadonlyArray): IEnumMap { +export function enumToMap( + obj: any, + filter?: ReadonlyArray, + exceptions?: ReadonlyArray, +): IEnumMap { const res: IEnumMap = {}; Object.keys(obj).forEach((key) => { @@ -13,6 +17,9 @@ export function enumToMap(obj: any, filter?: ReadonlyArray): IEnumMap { if (filter && !filter.includes(value)) { return; } + if (exceptions && exceptions.includes(value)) { + return; + } res[key] = value; });