11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst fixtures = require('../../test/common/fixtures'); 41cb0ef41Sopenharmony_ciconst https = require('https'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst options = { 71cb0ef41Sopenharmony_ci key: fixtures.readKey('rsa_private.pem'), 81cb0ef41Sopenharmony_ci cert: fixtures.readKey('rsa_cert.crt') 91cb0ef41Sopenharmony_ci}; 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst storedBytes = Object.create(null); 121cb0ef41Sopenharmony_ciconst storedBuffer = Object.create(null); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cimodule.exports = https.createServer(options, (req, res) => { 151cb0ef41Sopenharmony_ci // URL format: /<type>/<length>/<chunks>/chunkedEnc 161cb0ef41Sopenharmony_ci const params = req.url.split('/'); 171cb0ef41Sopenharmony_ci const command = params[1]; 181cb0ef41Sopenharmony_ci let body = ''; 191cb0ef41Sopenharmony_ci const arg = params[2]; 201cb0ef41Sopenharmony_ci const n_chunks = parseInt(params[3], 10); 211cb0ef41Sopenharmony_ci const chunkedEnc = params.length >= 5 && params[4] === '0' ? false : true; 221cb0ef41Sopenharmony_ci let status = 200; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci let n, i; 251cb0ef41Sopenharmony_ci if (command === 'bytes') { 261cb0ef41Sopenharmony_ci n = ~~arg; 271cb0ef41Sopenharmony_ci if (n <= 0) 281cb0ef41Sopenharmony_ci throw new Error('bytes called with n <= 0'); 291cb0ef41Sopenharmony_ci if (storedBytes[n] === undefined) { 301cb0ef41Sopenharmony_ci storedBytes[n] = 'C'.repeat(n); 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci body = storedBytes[n]; 331cb0ef41Sopenharmony_ci } else if (command === 'buffer') { 341cb0ef41Sopenharmony_ci n = ~~arg; 351cb0ef41Sopenharmony_ci if (n <= 0) 361cb0ef41Sopenharmony_ci throw new Error('buffer called with n <= 0'); 371cb0ef41Sopenharmony_ci if (storedBuffer[n] === undefined) { 381cb0ef41Sopenharmony_ci storedBuffer[n] = Buffer.allocUnsafe(n); 391cb0ef41Sopenharmony_ci for (i = 0; i < n; i++) { 401cb0ef41Sopenharmony_ci storedBuffer[n][i] = 'C'.charCodeAt(0); 411cb0ef41Sopenharmony_ci } 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci body = storedBuffer[n]; 441cb0ef41Sopenharmony_ci } else { 451cb0ef41Sopenharmony_ci status = 404; 461cb0ef41Sopenharmony_ci body = 'not found\n'; 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci // example: https://localhost:port/bytes/512/4 501cb0ef41Sopenharmony_ci // sends a 512 byte body in 4 chunks of 128 bytes 511cb0ef41Sopenharmony_ci const len = body.length; 521cb0ef41Sopenharmony_ci if (chunkedEnc) { 531cb0ef41Sopenharmony_ci res.writeHead(status, { 541cb0ef41Sopenharmony_ci 'Content-Type': 'text/plain', 551cb0ef41Sopenharmony_ci 'Transfer-Encoding': 'chunked' 561cb0ef41Sopenharmony_ci }); 571cb0ef41Sopenharmony_ci } else { 581cb0ef41Sopenharmony_ci res.writeHead(status, { 591cb0ef41Sopenharmony_ci 'Content-Type': 'text/plain', 601cb0ef41Sopenharmony_ci 'Content-Length': len.toString() 611cb0ef41Sopenharmony_ci }); 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci // send body in chunks 641cb0ef41Sopenharmony_ci if (n_chunks > 1) { 651cb0ef41Sopenharmony_ci const step = Math.floor(len / n_chunks) || 1; 661cb0ef41Sopenharmony_ci for (i = 0, n = (n_chunks - 1); i < n; ++i) 671cb0ef41Sopenharmony_ci res.write(body.slice(i * step, i * step + step)); 681cb0ef41Sopenharmony_ci res.end(body.slice((n_chunks - 1) * step)); 691cb0ef41Sopenharmony_ci } else { 701cb0ef41Sopenharmony_ci res.end(body); 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci}); 73