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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/xpcshell/node-http2/node_modules/http2-protocol/example/client.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,59 @@
     1.4 +var fs = require('fs');
     1.5 +var path = require('path');
     1.6 +var parse_url = require('url').parse;
     1.7 +var http2 = require('..');
     1.8 +var tls = require('tls');
     1.9 +
    1.10 +// Advertised protocol version
    1.11 +var implementedVersion = http2.ImplementedVersion;
    1.12 +
    1.13 +// Bunyan logger
    1.14 +var log = require('../test/util').createLogger('client');
    1.15 +
    1.16 +// Parsing the URL
    1.17 +var url = parse_url(process.argv.pop())
    1.18 +
    1.19 +// Connecting to the server
    1.20 +var socket = tls.connect(url.port, url.hostname, {
    1.21 +  rejectUnauthorized: false,
    1.22 +  ALPNProtocols: [implementedVersion],
    1.23 +  NPNProtocols: [implementedVersion],
    1.24 +  servername: url.hostname
    1.25 +}, onConnection);
    1.26 +
    1.27 +// Handling the connection
    1.28 +function onConnection() {
    1.29 +  var endpoint = new http2.Endpoint(log, 'CLIENT', {});
    1.30 +  endpoint.pipe(socket).pipe(endpoint);
    1.31 +
    1.32 +  // Sending request
    1.33 +  var stream = endpoint.createStream();
    1.34 +  stream.headers({
    1.35 +    ':method': 'GET',
    1.36 +    ':scheme': url.protocol.slice(0, url.protocol.length - 1),
    1.37 +    ':authority': url.hostname,
    1.38 +    ':path': url.path + (url.hash || '')
    1.39 +  });
    1.40 +
    1.41 +  // Receiving push streams
    1.42 +  stream.on('promise', function(push_stream, req_headers) {
    1.43 +    var filename = path.join(__dirname, '/push-' + push_count);
    1.44 +    push_count += 1;
    1.45 +    console.error('Receiving pushed resource: ' + req_headers[':path'] + ' -> ' + filename);
    1.46 +    push_stream.pipe(fs.createWriteStream(filename)).on('finish', finish);
    1.47 +  });
    1.48 +
    1.49 +  // Receiving the response body
    1.50 +  stream.pipe(process.stdout);
    1.51 +  stream.on('end', finish);
    1.52 +}
    1.53 +
    1.54 +// Quitting after both the response and the associated pushed resources have arrived
    1.55 +var push_count = 0;
    1.56 +var finished = 0;
    1.57 +function finish() {
    1.58 +  finished += 1;
    1.59 +  if (finished === (1 + push_count)) {
    1.60 +    process.exit();
    1.61 +  }
    1.62 +}

mercurial