Skip to content
This repository was archived by the owner on Jul 6, 2018. It is now read-only.

Commit ad8bb9d

Browse files
committed
http2: Emit unknownProtocol event or silently destroy the socket (Squash)
1 parent b900d2d commit ad8bb9d

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

doc/api/http2.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,13 @@ The `'socketError'` event is emitted when an `'error'` event is emitted by
685685
a `Socket` associated with the server. If no listener is registered for this
686686
event, an `'error'` event is emitted on the `Socket` instance instead.
687687

688+
#### Event: 'unknownProtocol'
689+
690+
The `'unknownProtocol'` event is emitted when a connecting client fails to
691+
negotiate an allowed protocol (i.e. HTTP/2 or HTTP/1.1). The event handler
692+
receives the socket for handling. If no listener is registered for this event,
693+
the connection is terminated. See the
694+
688695
#### Event: 'stream'
689696

690697
The `'stream'` event is emitted when a `'stream'` event has been emitted by
@@ -799,9 +806,7 @@ server.listen(80);
799806
* `options` {Object}
800807
* `allowHTTP1` {boolean} Incoming client connections that do not support
801808
HTTP/2 will be downgraded to HTTP/1.x when set to `true`. The default value
802-
is `false`, which rejects non-HTTP/2 client connections by either emitting
803-
a `'socketError'` event, if a handler is registered, or else disconnecing
804-
the remote peer and emitting an `'error'` event on the socket.
809+
is `false`. See the [`'unknownProtocol'`][] event.
805810
* `maxDefaultDynamicTableSize` {number} (TODO: Add detail)
806811
* `maxReservedRemoteStreams` {number} (TODO: Add detail)
807812
* `maxSendHeaderBlockLength` {number} (TODO: Add detail)
@@ -1014,3 +1019,4 @@ TBD
10141019
[ServerHttp2Stream]: #http2_class_serverhttp2stream
10151020
[Settings Object]: #http2_settings_object
10161021
[Using options.selectPadding]: #http2_using_options_selectpadding
1022+
[`'unknownProtocol'`]: #http2_event_unknownprotocol

lib/internal/errors.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ E('ERR_SOCKET_BAD_TYPE',
181181
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
182182
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
183183
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
184-
E('ERR_SOCKET_REJECT_HTTP1', 'HTTP/1 not allowed');
185184
// Add new errors from here...
186185

187186
function invalidArgType(name, expected, actual) {

lib/internal/http2/core.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,12 +1627,12 @@ function connectionListener(socket) {
16271627
if (options.allowHTTP1 === true) {
16281628
// Fallback to HTTP/1.1
16291629
return httpConnectionListener.call(this, socket);
1630+
} else if (this.emit('unknownProtocol', socket)) {
1631+
// Let event handler deal with the socket
1632+
return;
16301633
} else {
16311634
// Reject non-HTTP/2 client
1632-
return process.nextTick(
1633-
socketOnError.bind(socket),
1634-
new errors.TypeError('ERR_SOCKET_REJECT_HTTP1')
1635-
);
1635+
return socket.destroy();
16361636
}
16371637
}
16381638

test/parallel/test-http2-https-fallback.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,9 @@ function onSession(session) {
128128
mustCall(onRequest)
129129
);
130130

131-
const expectedRejection = mustCall(expectsError({
132-
code: 'ERR_SOCKET_REJECT_HTTP1',
133-
type: TypeError,
134-
message: 'HTTP/1 not allowed'
135-
}), 2);
136-
server.on('secureConnection', mustCall((socket) => {
137-
socket.on('error', expectedRejection);
138-
}, 3));
131+
server.on('unknownProtocol', mustCall((socket) => {
132+
socket.destroy();
133+
}, 2));
139134

140135
server.listen(0);
141136

0 commit comments

Comments
 (0)