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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:5608a1351e84
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');
6
7 // Advertised protocol version
8 var implementedVersion = http2.ImplementedVersion;
9
10 // Bunyan logger
11 var log = require('../test/util').createLogger('client');
12
13 // Parsing the URL
14 var url = parse_url(process.argv.pop())
15
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);
23
24 // Handling the connection
25 function onConnection() {
26 var endpoint = new http2.Endpoint(log, 'CLIENT', {});
27 endpoint.pipe(socket).pipe(endpoint);
28
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 });
37
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 });
45
46 // Receiving the response body
47 stream.pipe(process.stdout);
48 stream.on('end', finish);
49 }
50
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