node-http2模块中的HTTP2_Plain不起作用?

时间:2016-04-21 09:00:44

标签: http2

我想使用不带TLS的node-http2模块创建一个http2服务器。我的代码如下:

http2 = require('http2');
const bunyan = require('bunyan');

var log = bunyan.createLogger({name: "HTTP2 server without TLS!"});

var options = {
    log: log
}

var server = http2.raw.createServer(options, function(request, response) {
    console.log("Receiving HTTP2 request!");
    // response.writeHead(200);
    response.end('Hello world from HTTP2!');
});

server.listen(8000);

然而,它不起作用。从chrome连接到此服务器时,它显示下载内容。当我关闭服务器时,下载完成了空白文件(26个字节)。

有谁知道这里有什么问题吗?我需要配置浏览器吗?提前谢谢!

2 个答案:

答案 0 :(得分:2)

Chrome and all other browsers only support HTTP/2 over TLS (h2) and not plain HTTP/2 (h2c). So your browser does not understand what is returned from the server and apparently node-http2 does not send a proper error response when it receives a non-http2 request.

答案 1 :(得分:0)

问题似乎不仅来自浏览器。在http:// URL上使用支持http2的curl也不起作用。以下是卷曲的输出:

$ curl -I --http2 http://54.208.83.136:8000/ -v -k
*   Trying 54.208.83.136...
* Connected to 54.208.83.136 (54.208.83.136) port 8000 (#0)
> HEAD / HTTP/1.1
> Host: 54.208.83.136:8000
> User-Agent: curl/7.47.1
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAAQAAP__
> 

正如我们从卷曲输出中看到的那样。它根据http2 rfc发送http / 1.1升级请求,并设置正确的标头。

在服务器端,日志很长,所以我在这里只显示相关三个日志中的msg内容。

New incoming HTTP/2 connection

Client connection header prelude does not match

PROTOCOL ERROR, Fatal error, closing connection

所以基本上服务器关闭了连接,因为客户端连接头部前缀不匹配。通过检查代码,我发现错误源自endpoint.js的readPrelude函数。这是一个读取客户端标题的功能,但我不知道客户端标题中有什么问题:(。

因此我可以说node-http2模块不支持明文上的http2。

更新:事实证明我错了。 node-http2模块通过直接连接支持http2 over plaintext,不支持 HTTP / 2服务器,从HTTP / 1.1升级。问题是客户端使用升级机制连接到不支持升级的服务器。使用nghttp客户端连接服务器与先前的知识如下工作。

$ nghttp http://127.0.0.1:8000/ 
Hello world from HTTP2!

nghttpd服务器也支持不带TLS的HTTP2,即使它不支持HTTP升级。

$ nghttpd -d /Documents/Proxy 8080 --no-tls -v

所以我强烈建议在没有TLS的情况下测试HTTP2时使用nghttp。