Skip to content

Commit

Permalink
http: correctly translate HTTP method
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed May 3, 2024
1 parent 15456e4 commit 71d3c02
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
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
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
@@ -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 71d3c02

Please sign in to comment.