【问题标题】:HTTP/2 issue with Jetty - invalid prefaceJetty 的 HTTP/2 问题 - 前言无效
【发布时间】:2017-10-29 20:06:44
【问题描述】:

我正在试验 HTTP/2。我在this repo 中找到了一些示例。这是构造函数(跳过样板代码):

public WebServer(String path, HttpServlet servlet, int port) {
    Server server = new Server(new QueuedThreadPool());
    ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(servlet), path);
    ServerConnector connector = new ServerConnector(server, 1,1, new HTTP2ServerConnectionFactory(new HttpConfiguration()));
    connector.setPort(port);
    server.addConnector(connector);
}

然后我start()服务器,从上面提到的repo运行java http2客户端,它连接没有错误。但后来我尝试对 curl 做同样的事情:

# curl --http2 http://localhost:8080
invalid_preface

浏览器(Chrome)说:

GET http://localhost:8080/ net::ERR_INVALID_HTTP_RESPONSE

服务器怎么了?

【问题讨论】:

    标签: java curl jetty http2


    【解决方案1】:

    服务器没有问题。

    您已将 Jetty 配置为仅侦听 HTTP/2(没有 HTTP/1.1),然后您要求 curl 和 Chrome 发出 HTTP/1.1 请求,Jetty 回复了 invalid_preface 因为它无法理解它。

    请记住,浏览器只能通过 TLS(即 SSL)使用 HTTP/2,而 curl 也可以使用明文 HTTP/2。

    This example 向您展示了如何设置支持明文 HTTP/1.1 和 HTTP/2 的服务器。

    This example 向您展示了如何设置支持明文和 TLS HTTP/1.1 和 HTTP/2 的服务器。

    请参阅 here 了解如何将 curl 与 HTTP/2 一起使用。

    【讨论】:

    • 感谢您对浏览器限制的解释。不过,如果我将 --http2 标志与 curl 一起使用,它会失败并显示相同的消息。
    • curl 对我来说不会失败。也许你有一个旧版本的curl,或者一个旧版本的openssl,等等。你需要确保你的curl 版本在HTTP/2 上工作正常,如果你仍然有错误,发布完整的服务器日志和客户端日志。
    猜你喜欢
    • 2017-01-17
    • 2016-04-20
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 2015-10-16
    • 2017-12-07
    • 2016-08-21
    相关资源
    最近更新 更多