Skip to content

Commit 9623f48

Browse files
ShogunPandamarco-ippolito
authored andcommitted
http: correctly translate HTTP method
PR-URL: #52701 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 94f54e6 commit 9623f48

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
@@ -1311,7 +1311,9 @@ void InitializeHttpParser(Local<Object> target,
13111311
Integer::NewFromUnsigned(env->isolate(), kLenientAll));
13121312

13131313
Local<Array> methods = Array::New(env->isolate());
1314+
Local<Array> all_methods = Array::New(env->isolate());
13141315
size_t method_index = -1;
1316+
size_t all_method_index = -1;
13151317
#define V(num, name, string) \
13161318
methods \
13171319
->Set(env->context(), \
@@ -1320,9 +1322,23 @@ void InitializeHttpParser(Local<Object> target,
13201322
.Check();
13211323
HTTP_METHOD_MAP(V)
13221324
#undef V
1325+
#define V(num, name, string) \
1326+
all_methods \
1327+
->Set(env->context(), \
1328+
++all_method_index, \
1329+
FIXED_ONE_BYTE_STRING(env->isolate(), #string)) \
1330+
.Check();
1331+
HTTP_ALL_METHOD_MAP(V)
1332+
#undef V
1333+
13231334
target->Set(env->context(),
13241335
FIXED_ONE_BYTE_STRING(env->isolate(), "methods"),
13251336
methods).Check();
1337+
target
1338+
->Set(env->context(),
1339+
FIXED_ONE_BYTE_STRING(env->isolate(), "allMethods"),
1340+
all_methods)
1341+
.Check();
13261342

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