11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci// http://groups.google.com/group/nodejs/browse_thread/thread/f66cd3c960406919 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciif (!common.hasCrypto) 51cb0ef41Sopenharmony_ci common.skip('missing crypto'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst assert = require('assert'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciif (process.argv[2] === 'request') { 101cb0ef41Sopenharmony_ci const http = require('http'); 111cb0ef41Sopenharmony_ci const options = { 121cb0ef41Sopenharmony_ci port: +process.argv[3], 131cb0ef41Sopenharmony_ci path: '/' 141cb0ef41Sopenharmony_ci }; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci http.get(options, (res) => { 171cb0ef41Sopenharmony_ci res.pipe(process.stdout); 181cb0ef41Sopenharmony_ci }); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci return; 211cb0ef41Sopenharmony_ci} 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciif (process.argv[2] === 'shasum') { 241cb0ef41Sopenharmony_ci const crypto = require('crypto'); 251cb0ef41Sopenharmony_ci const shasum = crypto.createHash('sha1'); 261cb0ef41Sopenharmony_ci process.stdin.on('data', (d) => { 271cb0ef41Sopenharmony_ci shasum.update(d); 281cb0ef41Sopenharmony_ci }); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci process.stdin.on('close', () => { 311cb0ef41Sopenharmony_ci process.stdout.write(shasum.digest('hex')); 321cb0ef41Sopenharmony_ci }); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci return; 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciconst http = require('http'); 381cb0ef41Sopenharmony_ciconst cp = require('child_process'); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciconst filename = require('path').join(tmpdir.path, 'big'); 431cb0ef41Sopenharmony_cilet server; 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_cifunction executeRequest(cb) { 461cb0ef41Sopenharmony_ci cp.exec([`"${process.execPath}"`, 471cb0ef41Sopenharmony_ci `"${__filename}"`, 481cb0ef41Sopenharmony_ci 'request', 491cb0ef41Sopenharmony_ci server.address().port, 501cb0ef41Sopenharmony_ci '|', 511cb0ef41Sopenharmony_ci `"${process.execPath}"`, 521cb0ef41Sopenharmony_ci `"${__filename}"`, 531cb0ef41Sopenharmony_ci 'shasum' ].join(' '), 541cb0ef41Sopenharmony_ci (err, stdout, stderr) => { 551cb0ef41Sopenharmony_ci if (stderr.trim() !== '') { 561cb0ef41Sopenharmony_ci console.log(stderr); 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci assert.ifError(err); 591cb0ef41Sopenharmony_ci assert.strictEqual(stdout.slice(0, 40), 601cb0ef41Sopenharmony_ci '8c206a1a87599f532ce68675536f0b1546900d7a'); 611cb0ef41Sopenharmony_ci cb(); 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci ); 641cb0ef41Sopenharmony_ci} 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_citmpdir.refresh(); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_cicommon.createZeroFilledFile(filename); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciserver = http.createServer(function(req, res) { 721cb0ef41Sopenharmony_ci res.writeHead(200); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci // Create the subprocess 751cb0ef41Sopenharmony_ci const cat = cp.spawn('cat', [filename]); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci // Stream the data through to the response as binary chunks 781cb0ef41Sopenharmony_ci cat.stdout.on('data', (data) => { 791cb0ef41Sopenharmony_ci res.write(data); 801cb0ef41Sopenharmony_ci }); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci cat.stdout.on('end', () => res.end()); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci // End the response on exit (and log errors) 851cb0ef41Sopenharmony_ci cat.on('exit', (code) => { 861cb0ef41Sopenharmony_ci if (code !== 0) { 871cb0ef41Sopenharmony_ci console.error(`subprocess exited with code ${code}`); 881cb0ef41Sopenharmony_ci process.exit(1); 891cb0ef41Sopenharmony_ci } 901cb0ef41Sopenharmony_ci }); 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci}); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciserver.listen(0, () => { 951cb0ef41Sopenharmony_ci executeRequest(() => server.close()); 961cb0ef41Sopenharmony_ci}); 97