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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial