扩展的 CONNECT 协议


¥The extended CONNECT protocol

RFC 8441 定义了 HTTP/2 的 "扩展连接协议" 扩展,可用于使用 CONNECT 方法引导使用 Http2Stream 作为其他通信协议(例如 WebSockets)的隧道。

¥RFC 8441 defines an "Extended CONNECT Protocol" extension to HTTP/2 that may be used to bootstrap the use of an Http2Stream using the CONNECT method as a tunnel for other communication protocols (such as WebSockets).

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

¥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 });

一旦客户端从服务器收到指示可以使用扩展 CONNECT 的 SETTINGS 帧,它可能会发送使用 ':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' });
    // ...
  }
});