Skip to content

Commit

Permalink
http: correctly translate HTTP method
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ShogunPanda authored and targos committed May 8, 2024
1 parent b0e6a6b commit ad9c4bd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const {
} = primordials;
const { setImmediate } = require('timers');

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

Expand Down Expand Up @@ -109,7 +109,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,

if (typeof method === 'number') {
// server only
incoming.method = methods[method];
incoming.method = allMethods[method];
} else {
// client only
incoming.statusCode = statusCode;
Expand Down
16 changes: 16 additions & 0 deletions src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,9 @@ void InitializeHttpParser(Local<Object> target,
Integer::NewFromUnsigned(env->isolate(), kLenientAll));

Local<Array> methods = Array::New(env->isolate());
Local<Array> all_methods = Array::New(env->isolate());
size_t method_index = -1;
size_t all_method_index = -1;
#define V(num, name, string) \
methods \
->Set(env->context(), \
Expand All @@ -1313,9 +1315,23 @@ void InitializeHttpParser(Local<Object> target,
.Check();
HTTP_METHOD_MAP(V)
#undef V
#define V(num, name, string) \
all_methods \
->Set(env->context(), \
++all_method_index, \
FIXED_ONE_BYTE_STRING(env->isolate(), #string)) \
.Check();
HTTP_ALL_METHOD_MAP(V)
#undef V

target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "methods"),
methods).Check();
target
->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "allMethods"),
all_methods)
.Check();

t->Inherit(AsyncWrap::GetConstructorTemplate(env));
SetProtoMethod(isolate, t, "close", Parser::Close);
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-http-server-method.query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const common = require('../common');
const { strictEqual } = require('assert');
const { createServer, request } = require('http');

const server = createServer(common.mustCall((req, res) => {
strictEqual(req.method, 'QUERY');
res.end('OK');
}));

server.listen(0, common.mustCall(() => {
const req = request({ port: server.address().port, method: 'QUERY' }, common.mustCall((res) => {
strictEqual(res.statusCode, 200);

let buffer = '';
res.setEncoding('utf-8');

res.on('data', (c) => buffer += c);
res.on('end', common.mustCall(() => {
strictEqual(buffer, 'OK');
server.close();
}));
}));

req.end();
}));

0 comments on commit ad9c4bd

Please sign in to comment.