node/test/parallel/test-http-header-overflow.js
Yann Hamon c957b05177
http: send connection: close when closing conn
HTTP/1.1 mandates connections which do not support keep-alive and
close the connection send the connection: close header, see
https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.10

This page also provides more information:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection

I understand that HTTP/1.1 defaults to keep-alive - and that the
Connection: close header is required when closing a connection.

This adds the Connection: close header in the 400 and 414
responses sent on client errors.

PR-URL: https://github.com/nodejs/node/pull/26467
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2019-03-09 00:23:48 +01:00

49 lines
1.3 KiB
JavaScript

'use strict';
const assert = require('assert');
const { createServer, maxHeaderSize } = require('http');
const { createConnection } = require('net');
const { expectsError, mustCall } = require('../common');
const CRLF = '\r\n';
const DUMMY_HEADER_NAME = 'Cookie: ';
const DUMMY_HEADER_VALUE = 'a'.repeat(
// plus one is to make it 1 byte too big
maxHeaderSize - DUMMY_HEADER_NAME.length - (2 * CRLF.length) + 1
);
const PAYLOAD_GET = 'GET /blah HTTP/1.1';
const PAYLOAD = PAYLOAD_GET + CRLF +
DUMMY_HEADER_NAME + DUMMY_HEADER_VALUE + CRLF.repeat(2);
const server = createServer();
server.on('connection', mustCall((socket) => {
socket.on('error', expectsError({
type: Error,
message: 'Parse Error',
code: 'HPE_HEADER_OVERFLOW',
bytesParsed: maxHeaderSize + PAYLOAD_GET.length,
rawPacket: Buffer.from(PAYLOAD)
}));
}));
server.listen(0, mustCall(() => {
const c = createConnection(server.address().port);
let received = '';
c.on('connect', mustCall(() => {
c.write(PAYLOAD);
}));
c.on('data', mustCall((data) => {
received += data.toString();
}));
c.on('end', mustCall(() => {
assert.strictEqual(
received,
'HTTP/1.1 431 Request Header Fields Too Large\r\n' +
'Connection: close\r\n\r\n'
);
c.end();
}));
c.on('close', mustCall(() => server.close()));
}));