|
| 1 | +'use strict'; |
| 2 | +// http://groups.google.com/group/nodejs/browse_thread/thread/f66cd3c960406919 |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +if (!common.hasCrypto) { |
| 6 | + console.log('1..0 # Skipped: missing crypto'); |
| 7 | + return; |
| 8 | +} |
| 9 | + |
| 10 | +if (process.argv[2] === 'request') { |
| 11 | + const http = require('http'); |
| 12 | + const options = { |
| 13 | + port: common.PORT, |
| 14 | + path : '/' |
| 15 | + }; |
| 16 | + |
| 17 | + http.get(options, (res) => { |
| 18 | + res.pipe(process.stdout); |
| 19 | + }); |
| 20 | + |
| 21 | + return; |
| 22 | +} |
| 23 | + |
| 24 | +if (process.argv[2] === 'shasum') { |
| 25 | + const crypto = require('crypto'); |
| 26 | + const shasum = crypto.createHash('sha1'); |
| 27 | + process.stdin.on('data', (d) => { |
| 28 | + shasum.update(d); |
| 29 | + }); |
| 30 | + |
| 31 | + process.stdin.on('close', () => { |
| 32 | + process.stdout.write(shasum.digest('hex')); |
| 33 | + }); |
| 34 | + |
| 35 | + return; |
| 36 | +} |
| 37 | + |
| 38 | +const http = require('http'); |
| 39 | +const cp = require('child_process'); |
| 40 | + |
| 41 | +const filename = require('path').join(common.tmpDir, 'big'); |
| 42 | + |
| 43 | +function executeRequest(cb) { |
| 44 | + cp.exec([process.execPath, |
| 45 | + __filename, |
| 46 | + 'request', |
| 47 | + '|', |
| 48 | + process.execPath, |
| 49 | + __filename, |
| 50 | + 'shasum' ].join(' '), |
| 51 | + (err, stdout, stderr) => { |
| 52 | + if (err) throw err; |
| 53 | + assert.equal('8c206a1a87599f532ce68675536f0b1546900d7a', |
| 54 | + stdout.slice(0, 40)); |
| 55 | + cb(); |
| 56 | + } |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +common.refreshTmpDir(); |
| 62 | + |
| 63 | +const ddcmd = common.ddCommand(filename, 10240); |
| 64 | + |
| 65 | +cp.exec(ddcmd, function(err, stdout, stderr) { |
| 66 | + if (err) throw err; |
| 67 | + const server = http.createServer(function(req, res) { |
| 68 | + res.writeHead(200); |
| 69 | + |
| 70 | + // Create the subprocess |
| 71 | + const cat = cp.spawn('cat', [filename]); |
| 72 | + |
| 73 | + // Stream the data through to the response as binary chunks |
| 74 | + cat.stdout.on('data', (data) => { |
| 75 | + res.write(data); |
| 76 | + }); |
| 77 | + |
| 78 | + cat.stdout.on('end', () => res.end()); |
| 79 | + |
| 80 | + // End the response on exit (and log errors) |
| 81 | + cat.on('exit', (code) => { |
| 82 | + if (code !== 0) { |
| 83 | + console.error('subprocess exited with code ' + code); |
| 84 | + process.exit(1); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + }); |
| 89 | + |
| 90 | + server.listen(common.PORT, () => { |
| 91 | + executeRequest(() => server.close()); |
| 92 | + }); |
| 93 | +}); |
0 commit comments