2019-11-20 18:35:07 +00:00
|
|
|
#![cfg(feature = "openssl")]
|
2021-02-07 03:54:58 +00:00
|
|
|
|
|
|
|
extern crate tls_openssl as openssl;
|
|
|
|
|
2019-07-31 20:02:56 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use actix_http::HttpService;
|
2019-12-12 17:08:38 +00:00
|
|
|
use actix_http_test::test_server;
|
2021-01-03 23:47:04 +00:00
|
|
|
use actix_service::{map_config, pipeline_factory, ServiceFactoryExt};
|
2021-04-01 14:26:13 +00:00
|
|
|
use actix_utils::future::ok;
|
2019-07-31 20:02:56 +00:00
|
|
|
use actix_web::http::Version;
|
2019-12-20 11:27:32 +00:00
|
|
|
use actix_web::{dev::AppConfig, web, App, HttpResponse};
|
2021-02-13 17:16:36 +00:00
|
|
|
use openssl::{
|
|
|
|
pkey::PKey,
|
|
|
|
ssl::{SslAcceptor, SslConnector, SslMethod, SslVerifyMode},
|
|
|
|
x509::X509,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn tls_config() -> SslAcceptor {
|
|
|
|
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-07-31 20:02:56 +00:00
|
|
|
|
|
|
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
2021-02-13 17:16:36 +00:00
|
|
|
builder.set_certificate(&cert).unwrap();
|
|
|
|
builder.set_private_key(&key).unwrap();
|
|
|
|
|
2019-07-31 20:02:56 +00:00
|
|
|
builder.set_alpn_select_callback(|_, protos| {
|
|
|
|
const H2: &[u8] = b"\x02h2";
|
|
|
|
if protos.windows(3).any(|window| window == H2) {
|
|
|
|
Ok(b"h2")
|
|
|
|
} else {
|
2021-02-07 03:54:58 +00:00
|
|
|
Err(openssl::ssl::AlpnError::NOACK)
|
2019-07-31 20:02:56 +00:00
|
|
|
}
|
|
|
|
});
|
2019-12-02 11:33:11 +00:00
|
|
|
builder.set_alpn_protos(b"\x02h2").unwrap();
|
2021-02-13 17:16:36 +00:00
|
|
|
|
2019-12-02 11:33:11 +00:00
|
|
|
builder.build()
|
2019-07-31 20:02:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_connection_reuse_h2() {
|
|
|
|
let num = Arc::new(AtomicUsize::new(0));
|
|
|
|
let num2 = num.clone();
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let num2 = num2.clone();
|
|
|
|
pipeline_factory(move |io| {
|
|
|
|
num2.fetch_add(1, Ordering::Relaxed);
|
|
|
|
ok(io)
|
|
|
|
})
|
|
|
|
.and_then(
|
|
|
|
HttpService::build()
|
2019-12-20 11:27:32 +00:00
|
|
|
.h2(map_config(
|
2021-02-11 23:03:17 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok))),
|
2019-12-20 11:27:32 +00:00
|
|
|
|_| AppConfig::default(),
|
|
|
|
))
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-11-26 05:25:50 +00:00
|
|
|
.map_err(|_| ()),
|
|
|
|
)
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// disable ssl verification
|
|
|
|
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));
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2020-09-11 08:24:39 +00:00
|
|
|
let client = awc::Client::builder()
|
2021-02-18 12:30:09 +00:00
|
|
|
.connector(awc::Connector::new().ssl(builder.build()))
|
2019-11-26 05:25:50 +00:00
|
|
|
.finish();
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// req 1
|
|
|
|
let request = client.get(srv.surl("/")).send();
|
|
|
|
let response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// req 2
|
|
|
|
let req = client.post(srv.surl("/"));
|
|
|
|
let response = req.send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert_eq!(response.version(), Version::HTTP_2);
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// one connection
|
|
|
|
assert_eq!(num.load(Ordering::Relaxed), 1);
|
2019-07-31 20:02:56 +00:00
|
|
|
}
|