我在用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)时,会出现以下错误:
这个页面不工作了 本地主机没有发送任何数据。 ERR_EMPTY_RESPONSE
即使我尝试使用curl,它也给了我错误:
curl:(52)服务器的空回复
唯一起作用的是curl http://localhost:8080 --http2-prior-knowledge。
有什么解决办法吗?
发布于 2021-07-12 12:21:52
看起来您的浏览器拒绝在未加密的连接上执行HTTP/2。维基百科页面有以下是要说的
尽管标准本身不需要使用加密,51所有主要的客户端实现(Firefox、52 Chrome、Safari、Opera、IE、Edge)都表示,它们将只支持TLS上的HTTP/2,这使得加密成为事实上的任务。
cURL有一个不同的问题:默认为HTTP/1,而您的HTTP/2服务器不理解这一点。添加标志使其直接使用HTTP/2二进制协议。或者,连接到HTTPS端点将自动打开HTTP/2。
有关如何使用加密服务的示例,请参见asio文档:
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;
}
}https://stackoverflow.com/questions/68346874
复制相似问题