11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciif (!common.hasCrypto) { 61cb0ef41Sopenharmony_ci common.skip('missing crypto'); 71cb0ef41Sopenharmony_ci} 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst { createServer } = require('https'); 101cb0ef41Sopenharmony_ciconst { connect } = require('tls'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst options = { 151cb0ef41Sopenharmony_ci key: fixtures.readKey('agent1-key.pem'), 161cb0ef41Sopenharmony_ci cert: fixtures.readKey('agent1-cert.pem') 171cb0ef41Sopenharmony_ci}; 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cilet connections = 0; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciconst server = createServer(options, common.mustCall(function(req, res) { 221cb0ef41Sopenharmony_ci res.writeHead(200, { Connection: 'keep-alive' }); 231cb0ef41Sopenharmony_ci res.end(); 241cb0ef41Sopenharmony_ci}), { 251cb0ef41Sopenharmony_ci headersTimeout: 0, 261cb0ef41Sopenharmony_ci keepAliveTimeout: 0, 271cb0ef41Sopenharmony_ci requestTimeout: common.platformTimeout(60000), 281cb0ef41Sopenharmony_ci}); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciserver.on('connection', function() { 311cb0ef41Sopenharmony_ci connections++; 321cb0ef41Sopenharmony_ci}); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciserver.listen(0, function() { 351cb0ef41Sopenharmony_ci const port = server.address().port; 361cb0ef41Sopenharmony_ci let client1Closed = false; 371cb0ef41Sopenharmony_ci let client2Closed = false; 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci // Create a first request but never finish it 401cb0ef41Sopenharmony_ci const client1 = connect({ port, rejectUnauthorized: false }); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci client1.on('connect', common.mustCall(() => { 431cb0ef41Sopenharmony_ci // Create a second request, let it finish but leave the connection opened using HTTP keep-alive 441cb0ef41Sopenharmony_ci const client2 = connect({ port, rejectUnauthorized: false }); 451cb0ef41Sopenharmony_ci let response = ''; 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci client2.setEncoding('utf8'); 481cb0ef41Sopenharmony_ci client2.on('data', common.mustCall((chunk) => { 491cb0ef41Sopenharmony_ci response += chunk; 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci if (response.endsWith('0\r\n\r\n')) { 521cb0ef41Sopenharmony_ci assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive')); 531cb0ef41Sopenharmony_ci assert.strictEqual(connections, 2); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci server.close(common.mustCall()); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci // Check that only the idle connection got closed 581cb0ef41Sopenharmony_ci setTimeout(common.mustCall(() => { 591cb0ef41Sopenharmony_ci assert(!client1Closed); 601cb0ef41Sopenharmony_ci assert(client2Closed); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci server.closeAllConnections(); 631cb0ef41Sopenharmony_ci server.close(common.mustCall()); 641cb0ef41Sopenharmony_ci }), common.platformTimeout(500)).unref(); 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci })); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci client2.on('close', common.mustCall(() => { 691cb0ef41Sopenharmony_ci client2Closed = true; 701cb0ef41Sopenharmony_ci })); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci client2.write('GET / HTTP/1.1\r\n\r\n'); 731cb0ef41Sopenharmony_ci })); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci client1.on('close', common.mustCall(() => { 761cb0ef41Sopenharmony_ci client1Closed = true; 771cb0ef41Sopenharmony_ci })); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci client1.on('error', () => {}); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci client1.write('GET / HTTP/1.1'); 821cb0ef41Sopenharmony_ci}); 83