11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciif (!common.hasCrypto) 41cb0ef41Sopenharmony_ci common.skip('missing crypto'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst net = require('net'); 81cb0ef41Sopenharmony_ciconst http = require('http'); 91cb0ef41Sopenharmony_ciconst http2 = require('http2'); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci// Example test for HTTP/1 vs HTTP/2 protocol autoselection. 121cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/34532 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst h1Server = http.createServer(common.mustCall((req, res) => { 151cb0ef41Sopenharmony_ci res.end('HTTP/1 Response'); 161cb0ef41Sopenharmony_ci})); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciconst h2Server = http2.createServer(common.mustCall((req, res) => { 191cb0ef41Sopenharmony_ci res.end('HTTP/2 Response'); 201cb0ef41Sopenharmony_ci})); 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciconst rawServer = net.createServer(common.mustCall(function listener(socket) { 231cb0ef41Sopenharmony_ci const data = socket.read(3); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci if (!data) { // Repeat until data is available 261cb0ef41Sopenharmony_ci socket.once('readable', () => listener(socket)); 271cb0ef41Sopenharmony_ci return; 281cb0ef41Sopenharmony_ci } 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci // Put the data back, so the real server can handle it: 311cb0ef41Sopenharmony_ci socket.unshift(data); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci if (data.toString('ascii') === 'PRI') { // Very dumb preface check 341cb0ef41Sopenharmony_ci h2Server.emit('connection', socket); 351cb0ef41Sopenharmony_ci } else { 361cb0ef41Sopenharmony_ci h1Server.emit('connection', socket); 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci}, 2)); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_cirawServer.listen(common.mustCall(() => { 411cb0ef41Sopenharmony_ci const { port } = rawServer.address(); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci let done = 0; 441cb0ef41Sopenharmony_ci { 451cb0ef41Sopenharmony_ci // HTTP/2 Request 461cb0ef41Sopenharmony_ci const client = http2.connect(`http://localhost:${port}`); 471cb0ef41Sopenharmony_ci const req = client.request({ ':path': '/' }); 481cb0ef41Sopenharmony_ci req.end(); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci let content = ''; 511cb0ef41Sopenharmony_ci req.setEncoding('utf8'); 521cb0ef41Sopenharmony_ci req.on('data', (chunk) => content += chunk); 531cb0ef41Sopenharmony_ci req.on('end', common.mustCall(() => { 541cb0ef41Sopenharmony_ci assert.strictEqual(content, 'HTTP/2 Response'); 551cb0ef41Sopenharmony_ci if (++done === 2) rawServer.close(); 561cb0ef41Sopenharmony_ci client.close(); 571cb0ef41Sopenharmony_ci })); 581cb0ef41Sopenharmony_ci } 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci { 611cb0ef41Sopenharmony_ci // HTTP/1 Request 621cb0ef41Sopenharmony_ci http.get(`http://localhost:${port}`, common.mustCall((res) => { 631cb0ef41Sopenharmony_ci let content = ''; 641cb0ef41Sopenharmony_ci res.setEncoding('utf8'); 651cb0ef41Sopenharmony_ci res.on('data', (chunk) => content += chunk); 661cb0ef41Sopenharmony_ci res.on('end', common.mustCall(() => { 671cb0ef41Sopenharmony_ci assert.strictEqual(content, 'HTTP/1 Response'); 681cb0ef41Sopenharmony_ci if (++done === 2) rawServer.close(); 691cb0ef41Sopenharmony_ci })); 701cb0ef41Sopenharmony_ci })); 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci})); 73