Skip to content

Commit 1f285ba

Browse files
http: added test and fixed behaviour
1 parent 91399ac commit 1f285ba

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

lib/_http_server.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,18 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
10171017

10181018
let handled = false;
10191019

1020+
10201021
if (req.httpVersionMajor === 1 && req.httpVersionMinor === 1) {
1022+
1023+
// From RFC 7230 5.4 https://datatracker.ietf.org/doc/html/rfc7230#section-5.4
1024+
// A server MUST respond with a 400 (Bad Request) status code to any
1025+
// HTTP/1.1 request message that lacks a Host header field
1026+
if (req.headers.host === undefined) {
1027+
res.writeHead(400);
1028+
res.end();
1029+
return 0;
1030+
}
1031+
10211032
const isRequestsLimitSet = (
10221033
typeof server.maxRequestsPerSocket === 'number' &&
10231034
server.maxRequestsPerSocket > 0
@@ -1038,15 +1049,6 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
10381049
} else if (req.headers.expect !== undefined) {
10391050
handled = true;
10401051

1041-
// From RFC 7230 5.4 https://datatracker.ietf.org/doc/html/rfc7230#section-5.4
1042-
// A server MUST respond with a 400 (Bad Request) status code to any
1043-
// HTTP/1.1 request message that lacks a Host header field
1044-
if (req.headers.host === undefined) {
1045-
res.writeHead(400);
1046-
res.end();
1047-
return 0;
1048-
}
1049-
10501052
if (RegExpPrototypeExec(continueExpression, req.headers.expect) !== null) {
10511053
res._expect_continue = true;
10521054
if (server.listenerCount('checkContinue') > 0) {

test/parallel/test-http-max-headers-count.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ server.listen(0, function() {
5959
const maxAndExpected = [ // for client
6060
[20, 20],
6161
[1200, 104],
62-
[0, N + 4], // Connection, Date and Transfer-Encoding
62+
[0, N + 4], // Host and Connection
6363
];
6464
doRequest();
6565

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
6+
const server = http.createServer(common.mustNotCall((req, res) => {
7+
res.writeHead(200);
8+
res.end();
9+
}));
10+
11+
// From RFC 7230 5.4 https://datatracker.ietf.org/doc/html/rfc7230#section-5.4
12+
// A server MUST respond with a 400 (Bad Request) status code to any
13+
// HTTP/1.1 request message that lacks a Host header field
14+
server.listen(0, common.mustCall(() => {
15+
http.get({ port: server.address().port, headers: [] }, (res) => {
16+
assert.strictEqual(res.statusCode, 400);
17+
res.resume().on('end', common.mustCall(() => {
18+
server.close();
19+
}));
20+
});
21+
}));

test/parallel/test-https-max-headers-count.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ server.listen(0, common.mustCall(() => {
4848
const maxAndExpected = [ // for client
4949
[20, 20],
5050
[1200, 104],
51-
[0, N + 4], // Connection, Date and Transfer-Encoding
51+
[0, N + 4], // Host and Connection
5252
];
5353
const doRequest = common.mustCall(() => {
5454
const max = maxAndExpected[responses][0];

0 commit comments

Comments
 (0)