它必须是一个简单的问题,但是我对流的了解有限。

HTTP / 1 80到HTTP / 2 h2c代理

脚本(不起作用):

const net = require('net');
const http = require('http');
const http2 = require('http2');
const socketPath = `/tmp/socket.test.${Date.now()}`;

// front http 80 server.
http.createServer((req, res) => {
    const socket = net.createConnection(socketPath)
    req.pipe(socket).pipe(res);
}).listen(80);

// private http2 socket server.
http2.createServer(function(socket) {
    socket.write(`Echo from http2 server\r\n`);
    socket.pipe(socket);
}).listen(socketPath);


HTTP / 2 H2C到HTTP / 2 H2C代理

cli命令启动请求:

curl --http2-prior-knowledge -v http://localhost:3333/ --output -


脚本(不起作用):

const net = require('net');
const http = require('http');
const http2 = require('http2');
const socketPath = `/tmp/socket.test.${Date.now()}`;
const port = 3333;

const private = http2.createServer({allowHTTP1: true});
private.on('stream', (stream, headers) => {
    console.log('private http2 request');
    stream.end('HTTP/2');
});
private.listen(socketPath, () => console.log('private http2 server is listening', socketPath));

const public = http2.createServer({allowHTTP1: true});
public.on('stream', (stream, headers) => {
    console.log('public http2 request');
    const socket = net.connect(socketPath);
    stream.pipe(socket).pipe(stream);
});
public.listen(port, () => console.log('public http2 server is listening port', port));

最佳答案

最后,http2(h2c)到http2(h2c)(带有unix套接字)代理有效!

const net = require('net');
const http2 = require('http2');
const socketPath = `/tmp/socket.test.${Date.now()}`;
const port = 4444;

const priv = http2.createServer({});
priv.on('stream', (stream, headers) => {
    console.log('private http2 request');
    stream.end('HTTP/2');
});
priv.listen(socketPath, () => console.log('private http2 server is listening', socketPath));

const pub = http2.createServer({});
pub.on('stream', (stream, headers) => {
    const clientSession = http2.connect('http://0.0.0.0', {
        createConnection: () => net.connect({path: socketPath})
    });
    const req = clientSession.request({
      ':path': `/`,
    });
    req.pipe(stream).pipe(req);
});
pub.listen(port, () => console.log('public http2 server is listening port', port));

关于node.js - 如何使用套接字将请求代理到H2C HTTP/2服务器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51181463/

10-10 04:32