Skip to content

Commit 99b3d19

Browse files
committed
http: correctly translate HTTP method
1 parent 1728203 commit 99b3d19

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/_http_common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const {
2828
} = primordials;
2929
const { setImmediate } = require('timers');
3030

31-
const { methods, HTTPParser } = internalBinding('http_parser');
31+
const { methods, allMethods, HTTPParser } = internalBinding('http_parser');
3232
const { getOptionValue } = require('internal/options');
3333
const insecureHTTPParser = getOptionValue('--insecure-http-parser');
3434

@@ -109,7 +109,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
109109

110110
if (typeof method === 'number') {
111111
// server only
112-
incoming.method = methods[method];
112+
incoming.method = allMethods[method];
113113
} else {
114114
// client only
115115
incoming.statusCode = statusCode;

src/node_http_parser.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,9 @@ void InitializeHttpParser(Local<Object> target,
13041304
Integer::NewFromUnsigned(env->isolate(), kLenientAll));
13051305

13061306
Local<Array> methods = Array::New(env->isolate());
1307+
Local<Array> all_methods = Array::New(env->isolate());
13071308
size_t method_index = -1;
1309+
size_t all_method_index = -1;
13081310
#define V(num, name, string) \
13091311
methods \
13101312
->Set(env->context(), \
@@ -1313,9 +1315,21 @@ void InitializeHttpParser(Local<Object> target,
13131315
.Check();
13141316
HTTP_METHOD_MAP(V)
13151317
#undef V
1318+
#define V(num, name, string) \
1319+
all_methods \
1320+
->Set(env->context(), \
1321+
++all_method_index, \
1322+
FIXED_ONE_BYTE_STRING(env->isolate(), #string)) \
1323+
.Check();
1324+
HTTP_ALL_METHOD_MAP(V)
1325+
#undef V
1326+
13161327
target->Set(env->context(),
13171328
FIXED_ONE_BYTE_STRING(env->isolate(), "methods"),
13181329
methods).Check();
1330+
target->Set(env->context(),
1331+
FIXED_ONE_BYTE_STRING(env->isolate(), "allMethods"),
1332+
all_methods).Check();
13191333

13201334
t->Inherit(AsyncWrap::GetConstructorTemplate(env));
13211335
SetProtoMethod(isolate, t, "close", Parser::Close);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { strictEqual } = require('assert');
5+
const { createServer, request } = require('http');
6+
7+
const server = createServer(common.mustCall((req, res) => {
8+
strictEqual(req.method, 'QUERY');
9+
res.end('OK');
10+
}));
11+
12+
server.listen(0, common.mustCall(() => {
13+
const req = request({ port: server.address().port, method: 'QUERY' }, common.mustCall((res) => {
14+
strictEqual(res.statusCode, 200);
15+
16+
let buffer = '';
17+
res.setEncoding('utf-8');
18+
19+
res.on('data', (c) => buffer += c);
20+
res.on('end', common.mustCall(() => {
21+
strictEqual(buffer, 'OK');
22+
server.close();
23+
}));
24+
}));
25+
26+
req.end();
27+
}));

0 commit comments

Comments
 (0)