【问题标题】:How can I solve the "empty response" error using nghttp2?如何使用 nghttp2 解决“空响应”错误?
【发布时间】:2021-09-21 14:11:05
【问题描述】:

我正在使用nghttp2_asio。我使用./configure --enable-asio-lib 编译它。然后,我将/usr/local/lib 添加到/etc/ld.so.conf 文件中。代码如下:

#include "bits/stdc++.h"
#include "nghttp2/asio_http2_server.h"

using namespace std;
using namespace nghttp2::asio_http2;
using namespace nghttp2::asio_http2::server;

int main(int argc, char **argv) {
  http2 srv;
  srv.num_threads(4);
  srv.handle("/", [](const request &req, const response &res) {
    cout << req.uri().path << endl;
    header_map headers;
    headers.emplace("content-type", header_value{ "text/html", false });
    res.write_head(200, headers);
    res.end(file_generator("index.html"));
  });
  boost::system::error_code ec;
  if (srv.listen_and_serve(ec, "localhost", "8080")) cerr << ec.message() << endl;
  return 0;
}

当我尝试在http://localhost:8080 上打开浏览器(Chrome 或 Firefox)时,出现以下错误:

此页面无法正常工作

localhost 没有发送任何数据。

ERR_EMPTY_RESPONSE

即使我尝试使用curl,它也会给我错误:

curl: (52) 来自服务器的空回复

唯一有效的是curl http://localhost:8080 --http2-prior-knowledge

有解决办法吗?

【问题讨论】:

    标签: c++ http http2 nghttp2


    【解决方案1】:

    您的浏览器似乎拒绝通过未加密的连接执行 HTTP/2。维基百科页面有the following to say:

    虽然标准本身并不要求使用加密,[51] 所有主要客户端实现(Firefox、[52] Chrome、Safari、Opera、IE、Edge)都表示它们将仅支持基于 TLS 的 HTTP/2,这使得加密成为事实上的强制性。[53]

    cURL 有一个不同的问题:它默认为 HTTP/1,您的 HTTP/2 服务器无法理解。添加标志使其直接使用 HTTP/2 二进制协议。或者,连接到 HTTPS 端点会自动开启 HTTP/2。

    请参阅the libnghttp2_asio documentation 以获取有关如何使用 加密服务的示例:

    int main(int argc, char *argv[]) {
      boost::system::error_code ec;
      boost::asio::ssl::context tls(boost::asio::ssl::context::sslv23);
    
      tls.use_private_key_file("server.key", boost::asio::ssl::context::pem);
      tls.use_certificate_chain_file("server.crt");
    
      configure_tls_context_easy(ec, tls);
    
      http2 server;
    
      // add server handlers here
    
      if (server.listen_and_serve(ec, tls, "localhost", "3000")) {
        std::cerr << "error: " << ec.message() << std::endl;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 2021-11-30
      • 2018-12-26
      • 2013-06-28
      • 1970-01-01
      • 2018-05-26
      • 2012-03-20
      相关资源
      最近更新 更多