From a3726b08742df40674c4f9abdb1d5dc785e7abec Mon Sep 17 00:00:00 2001 From: Christopher Gubbin <44929740+cgubbin@users.noreply.github.com> Date: Sat, 29 Jan 2022 17:43:00 +0000 Subject: [PATCH] Updated basics/middleware-http-to-https to v4. (#487) --- basics/middleware-http-to-https/Cargo.toml | 5 +++-- basics/middleware-http-to-https/src/main.rs | 25 +++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/basics/middleware-http-to-https/Cargo.toml b/basics/middleware-http-to-https/Cargo.toml index 32b7c1da..1eb866de 100644 --- a/basics/middleware-http-to-https/Cargo.toml +++ b/basics/middleware-http-to-https/Cargo.toml @@ -6,8 +6,9 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -actix-web = {version = "3", features = ["rustls"]} -rustls = "0.18" +actix-web = {version = "4.0.0-beta.21", features = ["rustls"]} +rustls = "0.20.2" +rustls-pemfile = "0.2.1" # these are now in an external library futures = "0.3" diff --git a/basics/middleware-http-to-https/src/main.rs b/basics/middleware-http-to-https/src/main.rs index 012da6b9..fed852d5 100644 --- a/basics/middleware-http-to-https/src/main.rs +++ b/basics/middleware-http-to-https/src/main.rs @@ -8,8 +8,8 @@ use actix_web::{get, App, HttpServer}; use actix_web::{http, HttpResponse}; use futures::future; use futures::future::Either; -use rustls::internal::pemfile::{certs, pkcs8_private_keys}; -use rustls::{NoClientAuth, ServerConfig}; +use rustls::{Certificate, PrivateKey, ServerConfig}; +use rustls_pemfile::{certs, pkcs8_private_keys}; #[get("/")] async fn index() -> String { @@ -20,14 +20,25 @@ async fn index() -> String { #[actix_web::main] async fn main() -> std::io::Result<()> { - let mut config = ServerConfig::new(NoClientAuth::new()); let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap()); let key_file = &mut BufReader::new(File::open("key.pem").unwrap()); - let cert_chain = certs(cert_file).unwrap(); - let mut keys = pkcs8_private_keys(key_file).unwrap(); + let cert_chain: Vec = certs(cert_file) + .unwrap() + .into_iter() + .map(Certificate) + .collect(); + let mut keys: Vec = pkcs8_private_keys(key_file) + .unwrap() + .into_iter() + .map(PrivateKey) + .collect(); - config.set_single_cert(cert_chain, keys.remove(0)).unwrap(); + let config = ServerConfig::builder() + .with_safe_defaults() + .with_no_client_auth() + .with_single_cert(cert_chain, keys.remove(0)) + .unwrap(); HttpServer::new(|| { App::new() @@ -47,7 +58,7 @@ async fn main() -> std::io::Result<()> { println!("An http request has arrived here, i will redirect it to use https"); return Either::Right(future::ready(Ok(sreq.into_response( HttpResponse::MovedPermanently() - .header(http::header::LOCATION, url) + .append_header((http::header::LOCATION, url)) .finish(), )))); }