testing/xpcshell/node-http2/example/client.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:bda7f88a82b1
1 var fs = require('fs');
2 var path = require('path');
3 var http2 = require('..');
4
5 http2.globalAgent = new http2.Agent({
6 log: require('../test/util').createLogger('client')
7 });
8
9 process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
10
11 // Sending the request
12 // It would be `var request = http2.get(process.argv.pop());` if we wouldn't care about plain mode
13 var options = require('url').parse(process.argv.pop());
14 options.plain = Boolean(process.env.HTTP2_PLAIN);
15 var request = http2.request(options);
16 request.end();
17
18 // Receiving the response
19 request.on('response', function(response) {
20 response.pipe(process.stdout);
21 response.on('end', finish);
22 });
23
24 // Receiving push streams
25 request.on('push', function(pushRequest) {
26 var filename = path.join(__dirname, '/push-' + push_count);
27 push_count += 1;
28 console.error('Receiving pushed resource: ' + pushRequest.url + ' -> ' + filename);
29 pushRequest.on('response', function(pushResponse) {
30 pushResponse.pipe(fs.createWriteStream(filename)).on('finish', finish);
31 });
32 });
33
34 // Quitting after both the response and the associated pushed resources have arrived
35 var push_count = 0;
36 var finished = 0;
37 function finish() {
38 finished += 1;
39 if (finished === (1 + push_count)) {
40 process.exit();
41 }
42 }

mercurial