11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst http = require('http'); 61cb0ef41Sopenharmony_ciconst net = require('net'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst server = http.createServer(common.mustCall((req, res) => { 91cb0ef41Sopenharmony_ci res.end(); 101cb0ef41Sopenharmony_ci}, 2)); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciserver.keepAliveTimeout = common.platformTimeout(100); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => { 151cb0ef41Sopenharmony_ci const port = server.address().port; 161cb0ef41Sopenharmony_ci const socket = net.connect({ port }, common.mustCall(() => { 171cb0ef41Sopenharmony_ci request(common.mustCall(() => { 181cb0ef41Sopenharmony_ci // Make a second request on the same socket, after the keep-alive timeout 191cb0ef41Sopenharmony_ci // has been set on the server side. 201cb0ef41Sopenharmony_ci request(common.mustCall()); 211cb0ef41Sopenharmony_ci })); 221cb0ef41Sopenharmony_ci })); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci server.on('timeout', common.mustCall(() => { 251cb0ef41Sopenharmony_ci socket.end(); 261cb0ef41Sopenharmony_ci server.close(); 271cb0ef41Sopenharmony_ci })); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci function request(callback) { 301cb0ef41Sopenharmony_ci socket.setEncoding('utf8'); 311cb0ef41Sopenharmony_ci socket.on('data', onData); 321cb0ef41Sopenharmony_ci let response = ''; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci // Simulate a client that sends headers slowly (with a period of inactivity 351cb0ef41Sopenharmony_ci // that is longer than the keep-alive timeout). 361cb0ef41Sopenharmony_ci socket.write('GET / HTTP/1.1\r\n' + 371cb0ef41Sopenharmony_ci `Host: localhost:${port}\r\n`); 381cb0ef41Sopenharmony_ci setTimeout(() => { 391cb0ef41Sopenharmony_ci socket.write('Connection: keep-alive\r\n' + 401cb0ef41Sopenharmony_ci '\r\n'); 411cb0ef41Sopenharmony_ci }, common.platformTimeout(300)); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci function onData(chunk) { 441cb0ef41Sopenharmony_ci response += chunk; 451cb0ef41Sopenharmony_ci if (chunk.includes('\r\n')) { 461cb0ef41Sopenharmony_ci socket.removeListener('data', onData); 471cb0ef41Sopenharmony_ci onHeaders(); 481cb0ef41Sopenharmony_ci } 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci function onHeaders() { 521cb0ef41Sopenharmony_ci assert.ok(response.includes('HTTP/1.1 200 OK\r\n')); 531cb0ef41Sopenharmony_ci assert.ok(response.includes('Connection: keep-alive\r\n')); 541cb0ef41Sopenharmony_ci callback(); 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci})); 58