11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst { createServer } = require('http'); 61cb0ef41Sopenharmony_ciconst { connect } = require('net'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci// This test validates that the server returns 408 91cb0ef41Sopenharmony_ci// after server.requestTimeout if the client 101cb0ef41Sopenharmony_ci// does not complete a request when using pipelining. 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst headersTimeout = common.platformTimeout(2000); 131cb0ef41Sopenharmony_ciconst server = createServer({ 141cb0ef41Sopenharmony_ci headersTimeout, 151cb0ef41Sopenharmony_ci requestTimeout: 0, 161cb0ef41Sopenharmony_ci keepAliveTimeout: 0, 171cb0ef41Sopenharmony_ci connectionsCheckingInterval: headersTimeout / 4, 181cb0ef41Sopenharmony_ci}, common.mustCallAtLeast((req, res) => { 191cb0ef41Sopenharmony_ci res.writeHead(200, { 'Content-Type': 'text/plain' }); 201cb0ef41Sopenharmony_ci res.end(); 211cb0ef41Sopenharmony_ci})); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci// 0 seconds is the default 241cb0ef41Sopenharmony_ciassert.strictEqual(server.headersTimeout, headersTimeout); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci// Make sure requestTimeout and keepAliveTimeout 271cb0ef41Sopenharmony_ci// are big enough for the headersTimeout. 281cb0ef41Sopenharmony_ciserver.requestTimeout = 0; 291cb0ef41Sopenharmony_ciserver.keepAliveTimeout = 0; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => { 321cb0ef41Sopenharmony_ci const client = connect(server.address().port); 331cb0ef41Sopenharmony_ci let second = false; 341cb0ef41Sopenharmony_ci let response = ''; 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci client.setEncoding('utf8'); 371cb0ef41Sopenharmony_ci client.on('data', common.mustCallAtLeast((chunk) => { 381cb0ef41Sopenharmony_ci response += chunk; 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci // First response has ended 411cb0ef41Sopenharmony_ci if (!second && response.endsWith('\r\n\r\n')) { 421cb0ef41Sopenharmony_ci assert.strictEqual( 431cb0ef41Sopenharmony_ci response.split('\r\n')[0], 441cb0ef41Sopenharmony_ci 'HTTP/1.1 200 OK' 451cb0ef41Sopenharmony_ci ); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci response = ''; 481cb0ef41Sopenharmony_ci second = true; 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci }, 1)); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci const errOrEnd = common.mustCall(function(err) { 531cb0ef41Sopenharmony_ci if (!second) { 541cb0ef41Sopenharmony_ci return; 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci assert.strictEqual( 581cb0ef41Sopenharmony_ci response, 591cb0ef41Sopenharmony_ci 'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n' 601cb0ef41Sopenharmony_ci ); 611cb0ef41Sopenharmony_ci server.close(); 621cb0ef41Sopenharmony_ci }); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci client.on('error', errOrEnd); 651cb0ef41Sopenharmony_ci client.on('end', errOrEnd); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci // Send two requests using pipelining. Delay before finishing the second one 681cb0ef41Sopenharmony_ci client.resume(); 691cb0ef41Sopenharmony_ci client.write('GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\nGET / HTTP/1.1\r\nConnection: '); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci // Complete the request 721cb0ef41Sopenharmony_ci setTimeout(() => { 731cb0ef41Sopenharmony_ci client.write('close\r\n\r\n'); 741cb0ef41Sopenharmony_ci }, headersTimeout * 1.5).unref(); 751cb0ef41Sopenharmony_ci})); 76