2019-11-20 18:33:22 +01:00
|
|
|
#[cfg(feature = "openssl")]
|
2021-02-07 04:54:58 +01:00
|
|
|
extern crate tls_openssl as openssl;
|
2019-04-01 05:43:00 +02:00
|
|
|
|
2021-03-19 12:25:35 +01:00
|
|
|
#[cfg(any(unix, feature = "openssl"))]
|
|
|
|
use {
|
2021-04-02 09:26:59 +02:00
|
|
|
actix_web::{web, App, HttpResponse, HttpServer},
|
2021-03-19 12:25:35 +01:00
|
|
|
std::{sync::mpsc, thread, time::Duration},
|
|
|
|
};
|
2019-03-13 06:57:09 +01:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_start() {
|
2021-04-02 09:26:59 +02:00
|
|
|
let addr = actix_test::unused_addr();
|
2019-03-13 06:57:09 +01:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
thread::spawn(move || {
|
2021-11-15 05:03:33 +01:00
|
|
|
actix_rt::System::new()
|
|
|
|
.block_on(async {
|
|
|
|
let srv = HttpServer::new(|| {
|
|
|
|
App::new().service(
|
2022-01-24 12:56:01 +01:00
|
|
|
web::resource("/")
|
|
|
|
.route(web::to(|| async { HttpResponse::Ok().body("test") })),
|
2021-11-15 05:03:33 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.workers(1)
|
|
|
|
.backlog(1)
|
|
|
|
.max_connections(10)
|
|
|
|
.max_connection_rate(10)
|
|
|
|
.keep_alive(10)
|
|
|
|
.client_timeout(5000)
|
|
|
|
.client_shutdown(0)
|
|
|
|
.server_hostname("localhost")
|
|
|
|
.system_exit()
|
|
|
|
.disable_signals()
|
|
|
|
.bind(format!("{}", addr))
|
|
|
|
.unwrap()
|
|
|
|
.run();
|
|
|
|
|
|
|
|
tx.send(srv.handle()).unwrap();
|
|
|
|
|
|
|
|
srv.await
|
2021-01-04 00:47:04 +01:00
|
|
|
})
|
2021-11-15 05:03:33 +01:00
|
|
|
.unwrap();
|
|
|
|
});
|
2021-01-04 00:47:04 +01:00
|
|
|
|
2021-11-15 05:03:33 +01:00
|
|
|
let srv = rx.recv().unwrap();
|
2019-03-13 06:57:09 +01:00
|
|
|
|
2021-11-15 05:03:33 +01:00
|
|
|
let client = awc::Client::builder()
|
|
|
|
.connector(awc::Connector::new().timeout(Duration::from_millis(100)))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
let host = format!("http://{}", addr);
|
|
|
|
let response = client.get(host.clone()).send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
srv.stop(false).await;
|
2019-03-13 06:57:09 +01:00
|
|
|
}
|
2019-04-01 05:43:00 +02:00
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
#[cfg(feature = "openssl")]
|
2021-03-19 12:25:35 +01:00
|
|
|
fn ssl_acceptor() -> openssl::ssl::SslAcceptorBuilder {
|
2021-02-13 18:16:36 +01:00
|
|
|
use openssl::{
|
|
|
|
pkey::PKey,
|
|
|
|
ssl::{SslAcceptor, SslMethod},
|
|
|
|
x509::X509,
|
|
|
|
};
|
|
|
|
|
|
|
|
let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()]).unwrap();
|
|
|
|
let cert_file = cert.serialize_pem().unwrap();
|
|
|
|
let key_file = cert.serialize_private_key_pem();
|
|
|
|
let cert = X509::from_pem(cert_file.as_bytes()).unwrap();
|
|
|
|
let key = PKey::private_key_from_pem(key_file.as_bytes()).unwrap();
|
|
|
|
|
2019-04-01 05:43:00 +02:00
|
|
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
2021-02-13 18:16:36 +01:00
|
|
|
builder.set_certificate(&cert).unwrap();
|
|
|
|
builder.set_private_key(&key).unwrap();
|
|
|
|
|
2021-03-08 21:51:50 +01:00
|
|
|
builder
|
2019-04-01 05:43:00 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-20 18:33:22 +01:00
|
|
|
#[cfg(feature = "openssl")]
|
2019-11-26 06:25:50 +01:00
|
|
|
async fn test_start_ssl() {
|
2019-12-20 11:11:51 +01:00
|
|
|
use actix_web::HttpRequest;
|
2021-11-15 05:03:33 +01:00
|
|
|
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
|
2019-12-20 11:11:51 +01:00
|
|
|
|
2021-04-02 09:26:59 +02:00
|
|
|
let addr = actix_test::unused_addr();
|
2019-04-01 05:43:00 +02:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
thread::spawn(move || {
|
2021-11-15 05:03:33 +01:00
|
|
|
actix_rt::System::new()
|
|
|
|
.block_on(async {
|
|
|
|
let builder = ssl_acceptor();
|
|
|
|
|
|
|
|
let srv = HttpServer::new(|| {
|
|
|
|
App::new().service(web::resource("/").route(web::to(|req: HttpRequest| {
|
|
|
|
assert!(req.app_config().secure());
|
2022-01-24 12:56:01 +01:00
|
|
|
async { HttpResponse::Ok().body("test") }
|
2021-11-15 05:03:33 +01:00
|
|
|
})))
|
|
|
|
})
|
|
|
|
.workers(1)
|
|
|
|
.shutdown_timeout(1)
|
|
|
|
.system_exit()
|
|
|
|
.disable_signals()
|
|
|
|
.bind_openssl(format!("{}", addr), builder)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let srv = srv.run();
|
|
|
|
tx.send(srv.handle()).unwrap();
|
|
|
|
|
|
|
|
srv.await
|
|
|
|
})
|
|
|
|
.unwrap()
|
2019-04-01 05:43:00 +02:00
|
|
|
});
|
2021-11-15 05:03:33 +01:00
|
|
|
let srv = rx.recv().unwrap();
|
2019-04-01 05:43:00 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
|
|
|
builder.set_verify(SslVerifyMode::NONE);
|
|
|
|
let _ = builder
|
|
|
|
.set_alpn_protos(b"\x02h2\x08http/1.1")
|
|
|
|
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
|
|
|
|
|
2020-09-11 10:24:39 +02:00
|
|
|
let client = awc::Client::builder()
|
2019-11-26 06:25:50 +01:00
|
|
|
.connector(
|
|
|
|
awc::Connector::new()
|
2021-12-18 17:44:30 +01:00
|
|
|
.openssl(builder.build())
|
2021-02-18 13:30:09 +01:00
|
|
|
.timeout(Duration::from_millis(100)),
|
2019-11-26 06:25:50 +01:00
|
|
|
)
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
let host = format!("https://{}", addr);
|
|
|
|
let response = client.get(host.clone()).send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-04-01 05:43:00 +02:00
|
|
|
|
2021-11-15 05:03:33 +01:00
|
|
|
srv.stop(false).await;
|
2019-04-01 05:43:00 +02:00
|
|
|
}
|