Skip to content

Commit 0556f54

Browse files
ShogunPandamarco-ippolito
authored andcommitted
http: correctly translate HTTP method
PR-URL: #52701 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 234a505 commit 0556f54

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

lib/_http_common.js

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

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

@@ -108,7 +108,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
108108

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

src/node_http_parser.cc

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

13071307
Local<Array> methods = Array::New(env->isolate());
1308+
Local<Array> all_methods = Array::New(env->isolate());
13081309
size_t method_index = -1;
1310+
size_t all_method_index = -1;
13091311
#define V(num, name, string) \
13101312
methods \
13111313
->Set(env->context(), \
@@ -1314,9 +1316,23 @@ void InitializeHttpParser(Local<Object> target,
13141316
.Check();
13151317
HTTP_METHOD_MAP(V)
13161318
#undef V
1319+
#define V(num, name, string) \
1320+
all_methods \
1321+
->Set(env->context(), \
1322+
++all_method_index, \
1323+
FIXED_ONE_BYTE_STRING(env->isolate(), #string)) \
1324+
.Check();
1325+
HTTP_ALL_METHOD_MAP(V)
1326+
#undef V
1327+
13171328
target->Set(env->context(),
13181329
FIXED_ONE_BYTE_STRING(env->isolate(), "methods"),
13191330
methods).Check();
1331+
target
1332+
->Set(env->context(),
1333+
FIXED_ONE_BYTE_STRING(env->isolate(), "allMethods"),
1334+
all_methods)
1335+
.Check();
13201336

13211337
t->Inherit(AsyncWrap::GetConstructorTemplate(env));
13221338
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)