-
Notifications
You must be signed in to change notification settings - Fork 18
/
unknown-over-h2.js
72 lines (60 loc) · 1.58 KB
/
unknown-over-h2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const http2 = require('../../source/index.js'); // Note: using the local version
const H1Agent = new http2.proxies.HttpOverHttp2({
proxyOptions: {
url: 'https://username:password@localhost:8000',
// For demo purposes only!
rejectUnauthorized: false
}
});
const H1SAgent = new http2.proxies.HttpsOverHttp2({
proxyOptions: {
url: 'https://username:password@localhost:8000',
// For demo purposes only!
rejectUnauthorized: false
}
});
const H2Agent = new http2.proxies.Http2OverHttp2({
proxyOptions: {
url: 'https://username:password@localhost:8000',
// For demo purposes only!
rejectUnauthorized: false
}
});
const agent = {
http: H1Agent,
https: H1SAgent,
http2: H2Agent
};
(async () => {
try {
const request = await http2.auto({
// Try changing this to: http/1.1
ALPNProtocols: ['h2'],
hostname: 'httpbin.org',
// Try changing this to: http:
protocol: 'https:',
path: '/anything',
method: 'POST',
agent
}, response => {
const isSecure = response.req.agent.protocol === 'https:';
const protocol = isSecure ? `http/${response.httpVersion} with TLS` : 'http/1.1 without TLS';
console.log('statusCode:', response.statusCode);
console.log('headers:', response.headers);
console.log('protocol:', protocol);
const body = [];
response.on('data', chunk => {
body.push(chunk);
});
response.on('end', () => {
console.log('body:', Buffer.concat(body).toString());
});
});
request.on('error', console.error);
request.write('123');
request.end('456');
} catch (error) {
console.error(error);
}
})();