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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 var fs = require('fs');
     2 var path = require('path');
     3 var parse_url = require('url').parse;
     4 var http2 = require('..');
     5 var tls = require('tls');
     7 // Advertised protocol version
     8 var implementedVersion = http2.ImplementedVersion;
    10 // Bunyan logger
    11 var log = require('../test/util').createLogger('client');
    13 // Parsing the URL
    14 var url = parse_url(process.argv.pop())
    16 // Connecting to the server
    17 var socket = tls.connect(url.port, url.hostname, {
    18   rejectUnauthorized: false,
    19   ALPNProtocols: [implementedVersion],
    20   NPNProtocols: [implementedVersion],
    21   servername: url.hostname
    22 }, onConnection);
    24 // Handling the connection
    25 function onConnection() {
    26   var endpoint = new http2.Endpoint(log, 'CLIENT', {});
    27   endpoint.pipe(socket).pipe(endpoint);
    29   // Sending request
    30   var stream = endpoint.createStream();
    31   stream.headers({
    32     ':method': 'GET',
    33     ':scheme': url.protocol.slice(0, url.protocol.length - 1),
    34     ':authority': url.hostname,
    35     ':path': url.path + (url.hash || '')
    36   });
    38   // Receiving push streams
    39   stream.on('promise', function(push_stream, req_headers) {
    40     var filename = path.join(__dirname, '/push-' + push_count);
    41     push_count += 1;
    42     console.error('Receiving pushed resource: ' + req_headers[':path'] + ' -> ' + filename);
    43     push_stream.pipe(fs.createWriteStream(filename)).on('finish', finish);
    44   });
    46   // Receiving the response body
    47   stream.pipe(process.stdout);
    48   stream.on('end', finish);
    49 }
    51 // Quitting after both the response and the associated pushed resources have arrived
    52 var push_count = 0;
    53 var finished = 0;
    54 function finish() {
    55   finished += 1;
    56   if (finished === (1 + push_count)) {
    57     process.exit();
    58   }
    59 }

mercurial