Skip to content

Commit 069636c

Browse files
feat: add HTTP_ALL_METHOD_COUNT for iterating method names (#880)
Embedders had to hardcode the method range, so QUERY never showed up in name caches. Unknown ids now return NULL instead of aborting. Fixes #872
1 parent 515dbe3 commit 069636c

5 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"wasm": "node --import tsx bin/build_wasm.ts",
1919
"clean": "node --import tsx scripts/clean.ts",
2020
"prepare": "npm run clean && npm run build-ts",
21-
"test": "node --import tsx ./test/md-test.ts",
21+
"test": "node --import tsx ./test/md-test.ts && node --import tsx ./test/headers-test.ts",
2222
"lint": "eslint",
2323
"lint-fix": "eslint --fix",
2424
"postversion": "RELEASE=`node -e \"process.stdout.write(require('./package').version)\"` make -B postversion",

‎src/llhttp/c-headers.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class CHeaders {
4141
res += this.buildMap('RTSP_METHOD', constants.METHODS_RTSP);
4242
res += '\n';
4343
res += this.buildMap('HTTP_ALL_METHOD', constants.METHODS);
44+
res += `#define HTTP_ALL_METHOD_COUNT ${this.maxValue(constants.METHODS) + 1}\n`;
4445
res += '\n';
4546
res += this.buildMap('HTTP_STATUS', constants.STATUSES);
4647

@@ -86,4 +87,8 @@ export class CHeaders {
8687

8788
return res;
8889
}
90+
91+
private maxValue(map: IntDict): number {
92+
return Math.max(...Object.values(map));
93+
}
8994
}

‎src/native/api.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ const char* llhttp_method_name(llhttp_method_t method) {
219219
#define HTTP_METHOD_GEN(NUM, NAME, STRING) case HTTP_##NAME: return #STRING;
220220
switch (method) {
221221
HTTP_ALL_METHOD_MAP(HTTP_METHOD_GEN)
222-
default: abort();
222+
default: return NULL;
223223
}
224224
#undef HTTP_METHOD_GEN
225225
}

‎src/native/api.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ const char* llhttp_get_error_pos(const llhttp_t* parser);
212212
LLHTTP_EXPORT
213213
const char* llhttp_errno_name(llhttp_errno_t err);
214214

215-
/* Returns textual name of HTTP method */
215+
/* Returns textual name of HTTP method, or NULL if `method` is unknown.
216+
* Method ids run from 0 up to HTTP_ALL_METHOD_COUNT-1.
217+
*/
216218
LLHTTP_EXPORT
217219
const char* llhttp_method_name(llhttp_method_t method);
218220

‎test/headers-test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import assert from 'node:assert';
2+
import { describe, test } from 'node:test';
3+
4+
import { CHeaders, constants } from '../src/llhttp';
5+
6+
describe('CHeaders method count', () => {
7+
test('HTTP_ALL_METHOD_COUNT is one past the highest method id', () => {
8+
const headers = new CHeaders().build();
9+
const maxId = Math.max(...Object.values(constants.METHODS));
10+
const match = headers.match(/#define HTTP_ALL_METHOD_COUNT (\d+)/);
11+
assert.ok(match);
12+
assert.strictEqual(Number(match[1]), maxId + 1);
13+
});
14+
15+
test('every METHODS id is in [0, HTTP_ALL_METHOD_COUNT)', () => {
16+
const headers = new CHeaders().build();
17+
const match = headers.match(/#define HTTP_ALL_METHOD_COUNT (\d+)/);
18+
assert.ok(match);
19+
const count = Number(match[1]);
20+
for (const id of Object.values(constants.METHODS)) {
21+
assert.ok(id >= 0 && id < count);
22+
}
23+
});
24+
25+
test('HTTP_ALL_METHOD_MAP includes QUERY so a hardcoded 46 would miss it', () => {
26+
const headers = new CHeaders().build();
27+
assert.match(headers, /XX\(46, QUERY, QUERY\)/);
28+
const match = headers.match(/#define HTTP_ALL_METHOD_COUNT (\d+)/);
29+
assert.ok(match);
30+
assert.ok(Number(match[1]) > 46);
31+
});
32+
});

0 commit comments

Comments
 (0)