HTTP/2.0

如果可能,actix-web会自动将连接升级到*HTTP/2.0*。

Negotiation 协商

在没有先验知识的情况下,基于tls的*HTTP/2.0*协议需要tls alpn

目前,只有rust-openssl支持。

alpn协商需要启用该功能。启用后,HttpServer将提供bind_openssl方法。

[dependencies]
actix-web = { version = "2.0", features = ["openssl"] }
actix-rt = "1.0.0"
openssl = { version = "0.10", features = ["v110"] }
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};

async fn index(_req: HttpRequest) -> impl Responder {
    "Hello."
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    // load ssl keys
    // to create a self-signed temporary cert for testing:
    // `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'`
    let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
    builder
        .set_private_key_file("key.pem", SslFiletype::PEM)
        .unwrap();
    builder.set_certificate_chain_file("cert.pem").unwrap();

    HttpServer::new(|| App::new().route("/", web::get().to(index)))
        .bind_openssl("127.0.0.1:8088", builder)?
        .run()
        .await
}

不支持升级到rfc section 3.2中描述的*HTTP/2.0*模式。明文连接和tls连接都支持使用prior knowledge(先验知识)启动*HTTP/2*。 rfc section 3.4

请查看examples/tls以获取具体示例。

接下来: 热更新