michael@0: var fs = require('fs'); michael@0: var path = require('path'); michael@0: var parse_url = require('url').parse; michael@0: var http2 = require('..'); michael@0: var tls = require('tls'); michael@0: michael@0: // Advertised protocol version michael@0: var implementedVersion = http2.ImplementedVersion; michael@0: michael@0: // Bunyan logger michael@0: var log = require('../test/util').createLogger('client'); michael@0: michael@0: // Parsing the URL michael@0: var url = parse_url(process.argv.pop()) michael@0: michael@0: // Connecting to the server michael@0: var socket = tls.connect(url.port, url.hostname, { michael@0: rejectUnauthorized: false, michael@0: ALPNProtocols: [implementedVersion], michael@0: NPNProtocols: [implementedVersion], michael@0: servername: url.hostname michael@0: }, onConnection); michael@0: michael@0: // Handling the connection michael@0: function onConnection() { michael@0: var endpoint = new http2.Endpoint(log, 'CLIENT', {}); michael@0: endpoint.pipe(socket).pipe(endpoint); michael@0: michael@0: // Sending request michael@0: var stream = endpoint.createStream(); michael@0: stream.headers({ michael@0: ':method': 'GET', michael@0: ':scheme': url.protocol.slice(0, url.protocol.length - 1), michael@0: ':authority': url.hostname, michael@0: ':path': url.path + (url.hash || '') michael@0: }); michael@0: michael@0: // Receiving push streams michael@0: stream.on('promise', function(push_stream, req_headers) { michael@0: var filename = path.join(__dirname, '/push-' + push_count); michael@0: push_count += 1; michael@0: console.error('Receiving pushed resource: ' + req_headers[':path'] + ' -> ' + filename); michael@0: push_stream.pipe(fs.createWriteStream(filename)).on('finish', finish); michael@0: }); michael@0: michael@0: // Receiving the response body michael@0: stream.pipe(process.stdout); michael@0: stream.on('end', finish); michael@0: } michael@0: michael@0: // Quitting after both the response and the associated pushed resources have arrived michael@0: var push_count = 0; michael@0: var finished = 0; michael@0: function finish() { michael@0: finished += 1; michael@0: if (finished === (1 + push_count)) { michael@0: process.exit(); michael@0: } michael@0: }