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.

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

mercurial