11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciif (!common.hasCrypto) { 41cb0ef41Sopenharmony_ci common.skip('missing crypto'); 51cb0ef41Sopenharmony_ci} 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 81cb0ef41Sopenharmony_ciconst assert = require('assert'); 91cb0ef41Sopenharmony_ciconst https = require('https'); 101cb0ef41Sopenharmony_ciconst MakeDuplexPair = require('../common/duplexpair'); 111cb0ef41Sopenharmony_ciconst tls = require('tls'); 121cb0ef41Sopenharmony_ciconst { finished } = require('stream'); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst certFixture = { 151cb0ef41Sopenharmony_ci key: fixtures.readKey('agent1-key.pem'), 161cb0ef41Sopenharmony_ci cert: fixtures.readKey('agent1-cert.pem'), 171cb0ef41Sopenharmony_ci ca: fixtures.readKey('ca1-cert.pem'), 181cb0ef41Sopenharmony_ci}; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci// Test that setting the `insecureHTTPParse` option works on a per-stream-basis. 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci// Test 1: The server sends an invalid header. 241cb0ef41Sopenharmony_ci{ 251cb0ef41Sopenharmony_ci const { clientSide, serverSide } = MakeDuplexPair(); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci const req = https.request({ 281cb0ef41Sopenharmony_ci rejectUnauthorized: false, 291cb0ef41Sopenharmony_ci createConnection: common.mustCall(() => clientSide), 301cb0ef41Sopenharmony_ci insecureHTTPParser: true 311cb0ef41Sopenharmony_ci }, common.mustCall((res) => { 321cb0ef41Sopenharmony_ci assert.strictEqual(res.headers.hello, 'foo\x08foo'); 331cb0ef41Sopenharmony_ci res.resume(); // We don’t actually care about contents. 341cb0ef41Sopenharmony_ci res.on('end', common.mustCall()); 351cb0ef41Sopenharmony_ci })); 361cb0ef41Sopenharmony_ci req.end(); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci serverSide.resume(); // Dump the request 391cb0ef41Sopenharmony_ci serverSide.end('HTTP/1.1 200 OK\r\n' + 401cb0ef41Sopenharmony_ci 'Hello: foo\x08foo\r\n' + 411cb0ef41Sopenharmony_ci 'Content-Length: 0\r\n' + 421cb0ef41Sopenharmony_ci '\r\n\r\n'); 431cb0ef41Sopenharmony_ci} 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci// Test 2: The same as Test 1 except without the option, to make sure it fails. 461cb0ef41Sopenharmony_ci{ 471cb0ef41Sopenharmony_ci const { clientSide, serverSide } = MakeDuplexPair(); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci const req = https.request({ 501cb0ef41Sopenharmony_ci rejectUnauthorized: false, 511cb0ef41Sopenharmony_ci createConnection: common.mustCall(() => clientSide) 521cb0ef41Sopenharmony_ci }, common.mustNotCall()); 531cb0ef41Sopenharmony_ci req.end(); 541cb0ef41Sopenharmony_ci req.on('error', common.mustCall()); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci serverSide.resume(); // Dump the request 571cb0ef41Sopenharmony_ci serverSide.end('HTTP/1.1 200 OK\r\n' + 581cb0ef41Sopenharmony_ci 'Hello: foo\x08foo\r\n' + 591cb0ef41Sopenharmony_ci 'Content-Length: 0\r\n' + 601cb0ef41Sopenharmony_ci '\r\n\r\n'); 611cb0ef41Sopenharmony_ci} 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci// Test 3: The client sends an invalid header. 641cb0ef41Sopenharmony_ci{ 651cb0ef41Sopenharmony_ci const testData = 'Hello, World!\n'; 661cb0ef41Sopenharmony_ci const server = https.createServer( 671cb0ef41Sopenharmony_ci { insecureHTTPParser: true, 681cb0ef41Sopenharmony_ci ...certFixture }, 691cb0ef41Sopenharmony_ci common.mustCall((req, res) => { 701cb0ef41Sopenharmony_ci res.statusCode = 200; 711cb0ef41Sopenharmony_ci res.setHeader('Content-Type', 'text/plain'); 721cb0ef41Sopenharmony_ci res.end(testData); 731cb0ef41Sopenharmony_ci })); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci server.on('clientError', common.mustNotCall()); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci server.listen(0, common.mustCall(() => { 781cb0ef41Sopenharmony_ci const client = tls.connect({ 791cb0ef41Sopenharmony_ci port: server.address().port, 801cb0ef41Sopenharmony_ci rejectUnauthorized: false 811cb0ef41Sopenharmony_ci }); 821cb0ef41Sopenharmony_ci client.write( 831cb0ef41Sopenharmony_ci 'GET / HTTP/1.1\r\n' + 841cb0ef41Sopenharmony_ci 'Hello: foo\x08foo\r\n' + 851cb0ef41Sopenharmony_ci '\r\n\r\n'); 861cb0ef41Sopenharmony_ci client.end(); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci client.on('data', () => {}); 891cb0ef41Sopenharmony_ci finished(client, common.mustCall(() => { 901cb0ef41Sopenharmony_ci server.close(); 911cb0ef41Sopenharmony_ci })); 921cb0ef41Sopenharmony_ci })); 931cb0ef41Sopenharmony_ci} 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci// Test 4: The same as Test 3 except without the option, to make sure it fails. 961cb0ef41Sopenharmony_ci{ 971cb0ef41Sopenharmony_ci const server = https.createServer( 981cb0ef41Sopenharmony_ci { ...certFixture }, 991cb0ef41Sopenharmony_ci common.mustNotCall()); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci server.on('clientError', common.mustCall()); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci server.listen(0, common.mustCall(() => { 1041cb0ef41Sopenharmony_ci const client = tls.connect({ 1051cb0ef41Sopenharmony_ci port: server.address().port, 1061cb0ef41Sopenharmony_ci rejectUnauthorized: false 1071cb0ef41Sopenharmony_ci }); 1081cb0ef41Sopenharmony_ci client.write( 1091cb0ef41Sopenharmony_ci 'GET / HTTP/1.1\r\n' + 1101cb0ef41Sopenharmony_ci 'Hello: foo\x08foo\r\n' + 1111cb0ef41Sopenharmony_ci '\r\n\r\n'); 1121cb0ef41Sopenharmony_ci client.end(); 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci client.on('data', () => {}); 1151cb0ef41Sopenharmony_ci finished(client, common.mustCall(() => { 1161cb0ef41Sopenharmony_ci server.close(); 1171cb0ef41Sopenharmony_ci })); 1181cb0ef41Sopenharmony_ci })); 1191cb0ef41Sopenharmony_ci} 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci// Test 5: Invalid argument type 1221cb0ef41Sopenharmony_ci{ 1231cb0ef41Sopenharmony_ci assert.throws( 1241cb0ef41Sopenharmony_ci () => https.request({ insecureHTTPParser: 0 }, common.mustNotCall()), 1251cb0ef41Sopenharmony_ci common.expectsError({ 1261cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 1271cb0ef41Sopenharmony_ci message: 'The "options.insecureHTTPParser" property must be of' + 1281cb0ef41Sopenharmony_ci ' type boolean. Received type number (0)' 1291cb0ef41Sopenharmony_ci }) 1301cb0ef41Sopenharmony_ci ); 1311cb0ef41Sopenharmony_ci} 132