File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -212,7 +212,9 @@ const char* llhttp_get_error_pos(const llhttp_t* parser);
212212LLHTTP_EXPORT
213213const 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+ */
216218LLHTTP_EXPORT
217219const char * llhttp_method_name (llhttp_method_t method );
218220
Original file line number Diff line number Diff line change 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 ( / # d e f i n e H T T P _ A L L _ M E T H O D _ C O U N T ( \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 ( / # d e f i n e H T T P _ A L L _ M E T H O D _ C O U N T ( \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 , / X X \( 4 6 , Q U E R Y , Q U E R Y \) / ) ;
28+ const match = headers . match ( / # d e f i n e H T T P _ A L L _ M E T H O D _ C O U N T ( \d + ) / ) ;
29+ assert . ok ( match ) ;
30+ assert . ok ( Number ( match [ 1 ] ) > 46 ) ;
31+ } ) ;
32+ } ) ;
You can’t perform that action at this time.
0 commit comments