1// Flags: --expose-internals 2 3'use strict'; 4const { expectsError, mustCall } = require('../common'); 5const assert = require('assert'); 6const { createServer, maxHeaderSize } = require('http'); 7const { createConnection } = require('net'); 8 9const CRLF = '\r\n'; 10const DUMMY_HEADER_NAME = 'Cookie: '; 11const DUMMY_HEADER_VALUE = 'a'.repeat( 12 // Plus one is to make it 1 byte too big 13 maxHeaderSize - DUMMY_HEADER_NAME.length + 2 14); 15const PAYLOAD_GET = 'GET /blah HTTP/1.1'; 16const PAYLOAD = PAYLOAD_GET + CRLF + 17 DUMMY_HEADER_NAME + DUMMY_HEADER_VALUE + CRLF.repeat(2); 18 19const server = createServer(); 20 21server.on('connection', mustCall((socket) => { 22 socket.on('error', expectsError({ 23 name: 'Error', 24 message: 'Parse Error: Header overflow', 25 code: 'HPE_HEADER_OVERFLOW', 26 bytesParsed: maxHeaderSize + PAYLOAD_GET.length + (CRLF.length * 2) + 1, 27 rawPacket: Buffer.from(PAYLOAD) 28 })); 29})); 30 31server.listen(0, mustCall(() => { 32 const c = createConnection(server.address().port); 33 let received = ''; 34 35 c.on('connect', mustCall(() => { 36 c.write(PAYLOAD); 37 })); 38 c.on('data', mustCall((data) => { 39 received += data.toString(); 40 })); 41 c.on('end', mustCall(() => { 42 assert.strictEqual( 43 received, 44 'HTTP/1.1 431 Request Header Fields Too Large\r\n' + 45 'Connection: close\r\n\r\n' 46 ); 47 c.end(); 48 })); 49 c.on('close', mustCall(() => server.close())); 50})); 51