11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciif (!common.hasCrypto) {
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ci}
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst https = require('https');
111cb0ef41Sopenharmony_ciconst http = require('http');
121cb0ef41Sopenharmony_ciconst tls = require('tls');
131cb0ef41Sopenharmony_ciconst MakeDuplexPair = require('../common/duplexpair');
141cb0ef41Sopenharmony_ciconst { finished } = require('stream');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst certFixture = {
171cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
181cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem'),
191cb0ef41Sopenharmony_ci  ca: fixtures.readKey('ca1-cert.pem'),
201cb0ef41Sopenharmony_ci};
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// Test that setting the `maxHeaderSize` option works on a per-stream-basis.
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// Test 1: The server sends larger headers than what would otherwise be allowed.
261cb0ef41Sopenharmony_ci{
271cb0ef41Sopenharmony_ci  const { clientSide, serverSide } = MakeDuplexPair();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  const req = https.request({
301cb0ef41Sopenharmony_ci    createConnection: common.mustCall(() => clientSide),
311cb0ef41Sopenharmony_ci    maxHeaderSize: http.maxHeaderSize * 4
321cb0ef41Sopenharmony_ci  }, common.mustCall((res) => {
331cb0ef41Sopenharmony_ci    assert.strictEqual(res.headers.hello, 'A'.repeat(http.maxHeaderSize * 3));
341cb0ef41Sopenharmony_ci    res.resume();  // We don’t actually care about contents.
351cb0ef41Sopenharmony_ci    res.on('end', common.mustCall());
361cb0ef41Sopenharmony_ci  }));
371cb0ef41Sopenharmony_ci  req.end();
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  serverSide.resume();  // Dump the request
401cb0ef41Sopenharmony_ci  serverSide.end('HTTP/1.1 200 OK\r\n' +
411cb0ef41Sopenharmony_ci                 'Hello: ' + 'A'.repeat(http.maxHeaderSize * 3) + '\r\n' +
421cb0ef41Sopenharmony_ci                 'Content-Length: 0\r\n' +
431cb0ef41Sopenharmony_ci                 '\r\n\r\n');
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci// Test 2: The same as Test 1 except without the option, to make sure it fails.
471cb0ef41Sopenharmony_ci{
481cb0ef41Sopenharmony_ci  const { clientSide, serverSide } = MakeDuplexPair();
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  const req = https.request({
511cb0ef41Sopenharmony_ci    createConnection: common.mustCall(() => clientSide)
521cb0ef41Sopenharmony_ci  }, common.mustNotCall());
531cb0ef41Sopenharmony_ci  req.end();
541cb0ef41Sopenharmony_ci  req.on('error', common.mustCall());
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  serverSide.resume();  // Dump the request
571cb0ef41Sopenharmony_ci  serverSide.end('HTTP/1.1 200 OK\r\n' +
581cb0ef41Sopenharmony_ci                 'Hello: ' + 'A'.repeat(http.maxHeaderSize * 3) + '\r\n' +
591cb0ef41Sopenharmony_ci                 'Content-Length: 0\r\n' +
601cb0ef41Sopenharmony_ci                 '\r\n\r\n');
611cb0ef41Sopenharmony_ci}
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci// Test 3: The client sends larger headers than what would otherwise be allowed.
641cb0ef41Sopenharmony_ci{
651cb0ef41Sopenharmony_ci  const testData = 'Hello, World!\n';
661cb0ef41Sopenharmony_ci  const server = https.createServer(
671cb0ef41Sopenharmony_ci    { maxHeaderSize: http.maxHeaderSize * 4,
681cb0ef41Sopenharmony_ci      ...certFixture },
691cb0ef41Sopenharmony_ci    common.mustCall((req, res) => {
701cb0ef41Sopenharmony_ci      res.statusCode = 200;
711cb0ef41Sopenharmony_ci      res.setHeader('Content-Type', 'text/plain');
721cb0ef41Sopenharmony_ci      res.end(testData);
731cb0ef41Sopenharmony_ci    }));
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  server.on('clientError', common.mustNotCall());
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
781cb0ef41Sopenharmony_ci    const client = tls.connect({
791cb0ef41Sopenharmony_ci      port: server.address().port,
801cb0ef41Sopenharmony_ci      rejectUnauthorized: false
811cb0ef41Sopenharmony_ci    });
821cb0ef41Sopenharmony_ci    client.write(
831cb0ef41Sopenharmony_ci      'GET / HTTP/1.1\r\n' +
841cb0ef41Sopenharmony_ci      'Hello: ' + 'A'.repeat(http.maxHeaderSize * 3) + '\r\n' +
851cb0ef41Sopenharmony_ci      '\r\n\r\n');
861cb0ef41Sopenharmony_ci    client.end();
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci    client.on('data', () => {});
891cb0ef41Sopenharmony_ci    finished(client, common.mustCall(() => {
901cb0ef41Sopenharmony_ci      server.close();
911cb0ef41Sopenharmony_ci    }));
921cb0ef41Sopenharmony_ci  }));
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci// Test 4: The same as Test 3 except without the option, to make sure it fails.
961cb0ef41Sopenharmony_ci{
971cb0ef41Sopenharmony_ci  const server = https.createServer({ ...certFixture }, common.mustNotCall());
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  // clientError may be emitted multiple times when header is larger than
1001cb0ef41Sopenharmony_ci  // maxHeaderSize.
1011cb0ef41Sopenharmony_ci  server.on('clientError', common.mustCallAtLeast(1));
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
1041cb0ef41Sopenharmony_ci    const client = tls.connect({
1051cb0ef41Sopenharmony_ci      port: server.address().port,
1061cb0ef41Sopenharmony_ci      rejectUnauthorized: false
1071cb0ef41Sopenharmony_ci    });
1081cb0ef41Sopenharmony_ci    client.write(
1091cb0ef41Sopenharmony_ci      'GET / HTTP/1.1\r\n' +
1101cb0ef41Sopenharmony_ci      'Hello: ' + 'A'.repeat(http.maxHeaderSize * 3) + '\r\n' +
1111cb0ef41Sopenharmony_ci      '\r\n\r\n');
1121cb0ef41Sopenharmony_ci    client.end();
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci    client.on('data', () => {});
1151cb0ef41Sopenharmony_ci    finished(client, common.mustCall(() => {
1161cb0ef41Sopenharmony_ci      server.close();
1171cb0ef41Sopenharmony_ci    }));
1181cb0ef41Sopenharmony_ci  }));
1191cb0ef41Sopenharmony_ci}
120