Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"wasm": "node --import tsx bin/build_wasm.ts",
"clean": "node --import tsx scripts/clean.ts",
"prepare": "npm run clean && npm run build-ts",
"test": "node --import tsx ./test/md-test.ts",
"test": "node --import tsx ./test/md-test.ts && node --import tsx ./test/headers-test.ts",
"lint": "eslint",
"lint-fix": "eslint --fix",
"postversion": "RELEASE=`node -e \"process.stdout.write(require('./package').version)\"` make -B postversion",
Expand Down
5 changes: 5 additions & 0 deletions src/llhttp/c-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class CHeaders {
res += this.buildMap('RTSP_METHOD', constants.METHODS_RTSP);
res += '\n';
res += this.buildMap('HTTP_ALL_METHOD', constants.METHODS);
res += `#define HTTP_ALL_METHOD_COUNT ${this.maxValue(constants.METHODS) + 1}\n`;
res += '\n';
res += this.buildMap('HTTP_STATUS', constants.STATUSES);

Expand Down Expand Up @@ -86,4 +87,8 @@ export class CHeaders {

return res;
}

private maxValue(map: IntDict): number {
return Math.max(...Object.values(map));
}
}
2 changes: 1 addition & 1 deletion src/native/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ const char* llhttp_method_name(llhttp_method_t method) {
#define HTTP_METHOD_GEN(NUM, NAME, STRING) case HTTP_##NAME: return #STRING;
switch (method) {
HTTP_ALL_METHOD_MAP(HTTP_METHOD_GEN)
default: abort();
default: return NULL;
}
#undef HTTP_METHOD_GEN
}
Expand Down
4 changes: 3 additions & 1 deletion src/native/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ const char* llhttp_get_error_pos(const llhttp_t* parser);
LLHTTP_EXPORT
const char* llhttp_errno_name(llhttp_errno_t err);

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

Expand Down
32 changes: 32 additions & 0 deletions test/headers-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import assert from 'node:assert';
import { describe, test } from 'node:test';

import { CHeaders, constants } from '../src/llhttp';

describe('CHeaders method count', () => {
test('HTTP_ALL_METHOD_COUNT is one past the highest method id', () => {
const headers = new CHeaders().build();
const maxId = Math.max(...Object.values(constants.METHODS));
const match = headers.match(/#define HTTP_ALL_METHOD_COUNT (\d+)/);
assert.ok(match);
assert.strictEqual(Number(match[1]), maxId + 1);
});

test('every METHODS id is in [0, HTTP_ALL_METHOD_COUNT)', () => {
const headers = new CHeaders().build();
const match = headers.match(/#define HTTP_ALL_METHOD_COUNT (\d+)/);
assert.ok(match);
const count = Number(match[1]);
for (const id of Object.values(constants.METHODS)) {
assert.ok(id >= 0 && id < count);
}
});

test('HTTP_ALL_METHOD_MAP includes QUERY so a hardcoded 46 would miss it', () => {
const headers = new CHeaders().build();
assert.match(headers, /XX\(46, QUERY, QUERY\)/);
const match = headers.match(/#define HTTP_ALL_METHOD_COUNT (\d+)/);
assert.ok(match);
assert.ok(Number(match[1]) > 46);
});
});