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