GeezerVison is showing github.com — http2: send error text in case of ALPN mismatch · nodejs/node@f137c18
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/internal/http2/core.js +11-2Lines changed: 11 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2489,8 +2489,17 @@ function connectionListener(socket) { |
2489 | 2489 | return httpConnectionListener.call(this, socket); |
2490 | 2490 | } |
2491 | 2491 | // Let event handler deal with the socket |
2492 | | - if (!this.emit('unknownProtocol', socket)) |
2493 | | - socket.destroy(); |
| 2492 | + debug(`Unknown protocol from ${socket.remoteAddress}:${socket.remotePort}`); |
| 2493 | + if (!this.emit('unknownProtocol', socket)) { |
| 2494 | + // We don't know what to do, so let's just tell the other side what's |
| 2495 | + // going on in a format that they *might* understand. |
| 2496 | + socket.end('HTTP/1.0 403 Forbidden\r\n' + |
| 2497 | + 'Content-Type: text/plain\r\n\r\n' + |
| 2498 | + 'Unknown ALPN Protocol, expected `h2` to be available.\n' + |
| 2499 | + 'If this is a HTTP request: The server was not ' + |
| 2500 | + 'configured with the `allowHTTP1` option or a ' + |
| 2501 | + 'listener for the `unknownProtocol` event.\n'); |
| 2502 | + } |
2494 | 2503 | return; |
2495 | 2504 | } |
2496 | 2505 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-http2-https-fallback.js +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,7 +6,7 @@ const fixtures = require('../common/fixtures'); |
6 | 6 | if (!common.hasCrypto) |
7 | 7 | common.skip('missing crypto'); |
8 | 8 |
|
9 | | -const { strictEqual } = require('assert'); |
| 9 | +const { strictEqual, ok } = require('assert'); |
10 | 10 | const { createSecureContext } = require('tls'); |
11 | 11 | const { createSecureServer, connect } = require('http2'); |
12 | 12 | const { get } = require('https'); |
@@ -131,10 +131,17 @@ function onSession(session) { |
131 | 131 |
|
132 | 132 | // HTTP/1.1 client |
133 | 133 | get(Object.assign(parse(origin), clientOptions), common.mustNotCall()) |
134 | | - .on('error', common.mustCall(cleanup)); |
| 134 | + .on('error', common.mustCall(cleanup)) |
| 135 | + .end(); |
135 | 136 |
|
136 | 137 | // Incompatible ALPN TLS client |
| 138 | + let text = ''; |
137 | 139 | tls(Object.assign({ port, ALPNProtocols: ['fake'] }, clientOptions)) |
138 | | - .on('error', common.mustCall(cleanup)); |
| 140 | + .setEncoding('utf8') |
| 141 | + .on('data', (chunk) => text += chunk) |
| 142 | + .on('end', common.mustCall(() => { |
| 143 | + ok(/Unknown ALPN Protocol, expected `h2` to be available/.test(text)); |
| 144 | + cleanup(); |
| 145 | + })); |
139 | 146 | })); |
140 | 147 | } |