Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: createSecureServer support ALPNCallback option #56187

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3136,9 +3136,13 @@ function initializeOptions(options) {

function initializeTLSOptions(options, servername) {
options = initializeOptions(options);
options.ALPNProtocols = ['h2'];
if (options.allowHTTP1 === true)
options.ALPNProtocols.push('http/1.1');

if (!options.ALPNCallback) {
options.ALPNProtocols = ['h2'];
if (options.allowHTTP1 === true)
options.ALPNProtocols.push('http/1.1');
}

if (servername !== undefined && !options.servername)
options.servername = servername;
return options;
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-http2-alpn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');

// This test verifies that http2 server support ALPNCallback option.

if (!common.hasCrypto) common.skip('missing crypto');

const assert = require('assert');
const h2 = require('http2');
const tls = require('tls');

{
// Server sets two incompatible ALPN options:
assert.throws(() => h2.createSecureServer({
ALPNCallback: () => 'a',
ALPNProtocols: ['b', 'c']
}), (error) => error.code === 'ERR_TLS_ALPN_CALLBACK_WITH_PROTOCOLS');
}

{
const server = h2.createSecureServer({
key: fixtures.readKey('rsa_private.pem'),
cert: fixtures.readKey('rsa_cert.crt'),
ALPNCallback: () => 'a',
});

server.on(
'secureConnection',
common.mustCall((socket) => {
assert.strictEqual(socket.alpnProtocol, 'a');
socket.end();
server.close();
})
);

server.listen(0, function() {
const client = tls.connect({
port: server.address().port,
rejectUnauthorized: false,
ALPNProtocols: ['a'],
}, common.mustCall(() => {
assert.strictEqual(client.alpnProtocol, 'a');
client.end();
}));
});
}
Loading