Skip to content

Commit 5176dda

Browse files
committed
api: limit HTTP_METHOD_MAP to HTTP methods
RTSP methods shouldn't mix up with HTTP methods in the method map since they are supported only with the RTSP protocol. Node.js uses `HTTP_METHOD_MAP` to generate `http.METHODS` array and this breaks tests in `body-parser` because it assumes that all of these methods are for HTTP requests. PR-URL: nodejs/llhttp#103 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniele Belardi <dwon.dnl@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Beth Griggs <bgriggs@redhat.com>
1 parent 0bf261e commit 5176dda

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/llhttp/c-headers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class CHeaders {
1818

1919
const errorMap = enumToMap(constants.ERROR);
2020
const methodMap = enumToMap(constants.METHODS);
21+
const httpMethodMap = enumToMap(constants.METHODS, constants.METHODS_HTTP);
22+
const rtspMethodMap = enumToMap(constants.METHODS, constants.METHODS_RTSP);
2123

2224
res += this.buildEnum('llhttp_errno', 'HPE', errorMap);
2325
res += '\n';
@@ -39,8 +41,11 @@ export class CHeaders {
3941

4042
res += this.buildMap('HTTP_ERRNO', errorMap);
4143
res += '\n';
42-
res += this.buildMap('HTTP_METHOD', methodMap);
44+
res += this.buildMap('HTTP_METHOD', httpMethodMap);
4345
res += '\n';
46+
res += this.buildMap('RTSP_METHOD', rtspMethodMap);
47+
res += '\n';
48+
res += this.buildMap('HTTP_ALL_METHOD', methodMap);
4449

4550
res += '\n';
4651

src/llhttp/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ export interface IEnumMap {
22
[key: string]: number;
33
}
44

5-
export function enumToMap(obj: any): IEnumMap {
5+
export function enumToMap(obj: any, filter?: ReadonlyArray<number>): IEnumMap {
66
const res: IEnumMap = {};
77

88
Object.keys(obj).forEach((key) => {
99
const value = obj[key];
10-
if (typeof value === 'number') {
11-
res[key] = value;
10+
if (typeof value !== 'number') {
11+
return;
1212
}
13+
if (filter && !filter.includes(value)) {
14+
return;
15+
}
16+
res[key] = value;
1317
});
1418

1519
return res;

src/native/api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const char* llhttp_errno_name(llhttp_errno_t err) {
199199
const char* llhttp_method_name(llhttp_method_t method) {
200200
#define HTTP_METHOD_GEN(NUM, NAME, STRING) case HTTP_##NAME: return #STRING;
201201
switch (method) {
202-
HTTP_METHOD_MAP(HTTP_METHOD_GEN)
202+
HTTP_ALL_METHOD_MAP(HTTP_METHOD_GEN)
203203
default: abort();
204204
}
205205
#undef HTTP_METHOD_GEN

0 commit comments

Comments
 (0)