A now defunct web server engine that is notable for its overuse of unsafe code, the memory safety bugs caused by that, and its deletion by its creator.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.2 KiB

  1. #![cfg(feature = "rustls")]
  2. use std::sync::atomic::{AtomicUsize, Ordering};
  3. use std::sync::Arc;
  4. use actix_http::HttpService;
  5. use actix_http_test::test_server;
  6. use actix_service::{map_config, pipeline_factory, IntoServiceFactory, ServiceFactory};
  7. use actix_web::http::Version;
  8. use actix_web::{dev::AppConfig, web, App, HttpResponse};
  9. use futures::future::ok;
  10. use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod, SslVerifyMode};
  11. use rust_tls::ClientConfig;
  12. fn ssl_acceptor() -> SslAcceptor {
  13. // load ssl keys
  14. let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
  15. builder.set_verify_callback(SslVerifyMode::NONE, |_, _| true);
  16. builder
  17. .set_private_key_file("../tests/key.pem", SslFiletype::PEM)
  18. .unwrap();
  19. builder
  20. .set_certificate_chain_file("../tests/cert.pem")
  21. .unwrap();
  22. builder.set_alpn_select_callback(|_, protos| {
  23. const H2: &[u8] = b"\x02h2";
  24. if protos.windows(3).any(|window| window == H2) {
  25. Ok(b"h2")
  26. } else {
  27. Err(open_ssl::ssl::AlpnError::NOACK)
  28. }
  29. });
  30. builder.set_alpn_protos(b"\x02h2").unwrap();
  31. builder.build()
  32. }
  33. mod danger {
  34. pub struct NoCertificateVerification {}
  35. impl rust_tls::ServerCertVerifier for NoCertificateVerification {
  36. fn verify_server_cert(
  37. &self,
  38. _roots: &rust_tls::RootCertStore,
  39. _presented_certs: &[rust_tls::Certificate],
  40. _dns_name: webpki::DNSNameRef<'_>,
  41. _ocsp: &[u8],
  42. ) -> Result<rust_tls::ServerCertVerified, rust_tls::TLSError> {
  43. Ok(rust_tls::ServerCertVerified::assertion())
  44. }
  45. }
  46. }
  47. // #[actix_rt::test]
  48. async fn _test_connection_reuse_h2() {
  49. let num = Arc::new(AtomicUsize::new(0));
  50. let num2 = num.clone();
  51. let srv = test_server(move || {
  52. let num2 = num2.clone();
  53. pipeline_factory(move |io| {
  54. num2.fetch_add(1, Ordering::Relaxed);
  55. ok(io)
  56. })
  57. .and_then(
  58. HttpService::build()
  59. .h2(map_config(
  60. App::new().service(
  61. web::resource("/").route(web::to(|| HttpResponse::Ok())),
  62. ),
  63. |_| AppConfig::default(),
  64. ))
  65. .openssl(ssl_acceptor())
  66. .map_err(|_| ()),
  67. )
  68. });
  69. // disable ssl verification
  70. let mut config = ClientConfig::new();
  71. let protos = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
  72. config.set_protocols(&protos);
  73. config
  74. .dangerous()
  75. .set_certificate_verifier(Arc::new(danger::NoCertificateVerification {}));
  76. let client = awc::Client::build()
  77. .connector(awc::Connector::new().rustls(Arc::new(config)).finish())
  78. .finish();
  79. // req 1
  80. let request = client.get(srv.surl("/")).send();
  81. let response = request.await.unwrap();
  82. assert!(response.status().is_success());
  83. // req 2
  84. let req = client.post(srv.surl("/"));
  85. let response = req.send().await.unwrap();
  86. assert!(response.status().is_success());
  87. assert_eq!(response.version(), Version::HTTP_2);
  88. // one connection
  89. assert_eq!(num.load(Ordering::Relaxed), 1);
  90. }