client.js (1425B)
1 var fs = require('fs'); 2 var path = require('path'); 3 var http2 = require('..'); 4 var urlParse = require('url').parse; 5 6 // Setting the global logger (optional) 7 http2.globalAgent = new http2.Agent({ 8 rejectUnauthorized: true, 9 log: require('../test/util').createLogger('client') 10 }); 11 12 // Sending the request 13 var url = process.argv.pop(); 14 var options = urlParse(url); 15 16 // Optionally verify self-signed certificates. 17 if (options.hostname == 'localhost') { 18 options.key = fs.readFileSync(path.join(__dirname, '/localhost.key')); 19 options.ca = fs.readFileSync(path.join(__dirname, '/localhost.crt')); 20 } 21 22 var request = process.env.HTTP2_PLAIN ? http2.raw.get(options) : http2.get(options); 23 24 // Receiving the response 25 request.on('response', function(response) { 26 response.pipe(process.stdout); 27 response.on('end', finish); 28 }); 29 30 // Receiving push streams 31 request.on('push', function(pushRequest) { 32 var filename = path.join(__dirname, '/push-' + push_count); 33 push_count += 1; 34 console.error('Receiving pushed resource: ' + pushRequest.url + ' -> ' + filename); 35 pushRequest.on('response', function(pushResponse) { 36 pushResponse.pipe(fs.createWriteStream(filename)).on('finish', finish); 37 }); 38 }); 39 40 // Quitting after both the response and the associated pushed resources have arrived 41 var push_count = 0; 42 var finished = 0; 43 function finish() { 44 finished += 1; 45 if (finished === (1 + push_count)) { 46 process.exit(); 47 } 48 }