Skip to content

Commit ad9c4bd

Browse files
ShogunPandatargos
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 b0e6a6b commit ad9c4bd

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

lib/_http_common.js

+2-2
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

+16
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,23 @@ 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
1331+
->Set(env->context(),
1332+
FIXED_ONE_BYTE_STRING(env->isolate(), "allMethods"),
1333+
all_methods)
1334+
.Check();
13191335

13201336
t->Inherit(AsyncWrap::GetConstructorTemplate(env));
13211337
SetProtoMethod(isolate, t, "close", Parser::Close);
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)