扩展的 CONNECT 协议


【The extended CONNECT protocol】

RFC 8441 定义了一个“扩展 CONNECT 协议”扩展,用于 HTTP/2,可用于通过将 CONNECT 方法作为其他通信协议(如 WebSockets)的隧道来引导使用 Http2Stream

HTTP/2 服务器通过使用 enableConnectProtocol 设置来启用扩展 CONNECT 协议的使用:

【The use of the Extended CONNECT Protocol is enabled by HTTP/2 servers by using the enableConnectProtocol setting:】

import { createServer } from 'node:http2';
const settings = { enableConnectProtocol: true };
const server = createServer({ settings });const http2 = require('node:http2');
const settings = { enableConnectProtocol: true };
const server = http2.createServer({ settings });

一旦客户端收到服务器发送的 SETTINGS 帧,表明可以使用扩展的 CONNECT,它就可以发送使用 ':protocol' HTTP/2 伪头的 CONNECT 请求:

【Once the client receives the SETTINGS frame from the server indicating that the extended CONNECT may be used, it may send CONNECT requests that use the ':protocol' HTTP/2 pseudo-header:】

import { connect } from 'node:http2';
const client = connect('http://localhost:8080');
client.on('remoteSettings', (settings) => {
  if (settings.enableConnectProtocol) {
    const req = client.request({ ':method': 'CONNECT', ':protocol': 'foo' });
    // ...
  }
});const http2 = require('node:http2');
const client = http2.connect('http://localhost:8080');
client.on('remoteSettings', (settings) => {
  if (settings.enableConnectProtocol) {
    const req = client.request({ ':method': 'CONNECT', ':protocol': 'foo' });
    // ...
  }
});