11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciif (!common.hasCrypto) 41cb0ef41Sopenharmony_ci common.skip('missing crypto'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst http2 = require('http2'); 71cb0ef41Sopenharmony_ciconst net = require('net'); 81cb0ef41Sopenharmony_ciconst { Worker, parentPort } = require('worker_threads'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci// Verify that creating a number of invalid HTTP/2 streams will eventually 111cb0ef41Sopenharmony_ci// result in the peer closing the session. 121cb0ef41Sopenharmony_ci// This test uses separate threads for client and server to avoid 131cb0ef41Sopenharmony_ci// the two event loops intermixing, as we are writing in a busy loop here. 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciif (process.env.HAS_STARTED_WORKER) { 161cb0ef41Sopenharmony_ci const server = http2.createServer({ maxSessionInvalidFrames: 100 }); 171cb0ef41Sopenharmony_ci server.on('stream', (stream) => { 181cb0ef41Sopenharmony_ci stream.respond({ 191cb0ef41Sopenharmony_ci 'content-type': 'text/plain', 201cb0ef41Sopenharmony_ci ':status': 200 211cb0ef41Sopenharmony_ci }); 221cb0ef41Sopenharmony_ci stream.end('Hello, world!\n'); 231cb0ef41Sopenharmony_ci }); 241cb0ef41Sopenharmony_ci server.listen(0, () => parentPort.postMessage(server.address().port)); 251cb0ef41Sopenharmony_ci return; 261cb0ef41Sopenharmony_ci} 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciprocess.env.HAS_STARTED_WORKER = 1; 291cb0ef41Sopenharmony_ciconst worker = new Worker(__filename).on('message', common.mustCall((port) => { 301cb0ef41Sopenharmony_ci const h2header = Buffer.alloc(9); 311cb0ef41Sopenharmony_ci const conn = net.connect({ port, allowHalfOpen: true }); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci conn.write('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci h2header[3] = 4; // Send a settings frame. 361cb0ef41Sopenharmony_ci conn.write(Buffer.from(h2header)); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci let inbuf = Buffer.alloc(0); 391cb0ef41Sopenharmony_ci let state = 'settingsHeader'; 401cb0ef41Sopenharmony_ci let settingsFrameLength; 411cb0ef41Sopenharmony_ci conn.on('data', (chunk) => { 421cb0ef41Sopenharmony_ci inbuf = Buffer.concat([inbuf, chunk]); 431cb0ef41Sopenharmony_ci switch (state) { 441cb0ef41Sopenharmony_ci case 'settingsHeader': 451cb0ef41Sopenharmony_ci if (inbuf.length < 9) return; 461cb0ef41Sopenharmony_ci settingsFrameLength = inbuf.readIntBE(0, 3); 471cb0ef41Sopenharmony_ci inbuf = inbuf.slice(9); 481cb0ef41Sopenharmony_ci state = 'readingSettings'; 491cb0ef41Sopenharmony_ci // Fallthrough 501cb0ef41Sopenharmony_ci case 'readingSettings': 511cb0ef41Sopenharmony_ci if (inbuf.length < settingsFrameLength) return; 521cb0ef41Sopenharmony_ci inbuf = inbuf.slice(settingsFrameLength); 531cb0ef41Sopenharmony_ci h2header[3] = 4; // Send a settings ACK. 541cb0ef41Sopenharmony_ci h2header[4] = 1; 551cb0ef41Sopenharmony_ci conn.write(Buffer.from(h2header)); 561cb0ef41Sopenharmony_ci state = 'ignoreInput'; 571cb0ef41Sopenharmony_ci writeRequests(); 581cb0ef41Sopenharmony_ci } 591cb0ef41Sopenharmony_ci }); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci let gotError = false; 621cb0ef41Sopenharmony_ci let streamId = 1; 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci function writeRequests() { 651cb0ef41Sopenharmony_ci for (let i = 1; i < 10 && !gotError; i++) { 661cb0ef41Sopenharmony_ci h2header[3] = 1; // HEADERS 671cb0ef41Sopenharmony_ci h2header[4] = 0x5; // END_HEADERS|END_STREAM 681cb0ef41Sopenharmony_ci h2header.writeIntBE(1, 0, 3); // Length: 1 691cb0ef41Sopenharmony_ci h2header.writeIntBE(streamId, 5, 4); // Stream ID 701cb0ef41Sopenharmony_ci streamId += 2; 711cb0ef41Sopenharmony_ci // 0x88 = :status: 200 721cb0ef41Sopenharmony_ci if (!conn.write(Buffer.concat([h2header, Buffer.from([0x88])]))) { 731cb0ef41Sopenharmony_ci break; 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci } 761cb0ef41Sopenharmony_ci if (!gotError) 771cb0ef41Sopenharmony_ci setImmediate(writeRequests); 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci conn.once('error', common.mustCall(() => { 811cb0ef41Sopenharmony_ci gotError = true; 821cb0ef41Sopenharmony_ci worker.terminate(); 831cb0ef41Sopenharmony_ci conn.destroy(); 841cb0ef41Sopenharmony_ci })); 851cb0ef41Sopenharmony_ci})); 86