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.

83 lines
2.6 KiB

  1. #![cfg(feature = "openssl")]
  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, SslConnector, SslFiletype, SslMethod, SslVerifyMode};
  11. fn ssl_acceptor() -> SslAcceptor {
  12. // load ssl keys
  13. let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
  14. builder
  15. .set_private_key_file("../tests/key.pem", SslFiletype::PEM)
  16. .unwrap();
  17. builder
  18. .set_certificate_chain_file("../tests/cert.pem")
  19. .unwrap();
  20. builder.set_alpn_select_callback(|_, protos| {
  21. const H2: &[u8] = b"\x02h2";
  22. if protos.windows(3).any(|window| window == H2) {
  23. Ok(b"h2")
  24. } else {
  25. Err(open_ssl::ssl::AlpnError::NOACK)
  26. }
  27. });
  28. builder.set_alpn_protos(b"\x02h2").unwrap();
  29. builder.build()
  30. }
  31. #[actix_rt::test]
  32. async fn test_connection_reuse_h2() {
  33. let num = Arc::new(AtomicUsize::new(0));
  34. let num2 = num.clone();
  35. let srv = test_server(move || {
  36. let num2 = num2.clone();
  37. pipeline_factory(move |io| {
  38. num2.fetch_add(1, Ordering::Relaxed);
  39. ok(io)
  40. })
  41. .and_then(
  42. HttpService::build()
  43. .h2(map_config(
  44. App::new().service(
  45. web::resource("/").route(web::to(|| HttpResponse::Ok())),
  46. ),
  47. |_| AppConfig::default(),
  48. ))
  49. .openssl(ssl_acceptor())
  50. .map_err(|_| ()),
  51. )
  52. });
  53. // disable ssl verification
  54. let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
  55. builder.set_verify(SslVerifyMode::NONE);
  56. let _ = builder
  57. .set_alpn_protos(b"\x02h2\x08http/1.1")
  58. .map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
  59. let client = awc::Client::build()
  60. .connector(awc::Connector::new().ssl(builder.build()).finish())
  61. .finish();
  62. // req 1
  63. let request = client.get(srv.surl("/")).send();
  64. let response = request.await.unwrap();
  65. assert!(response.status().is_success());
  66. // req 2
  67. let req = client.post(srv.surl("/"));
  68. let response = req.send().await.unwrap();
  69. assert!(response.status().is_success());
  70. assert_eq!(response.version(), Version::HTTP_2);
  71. // one connection
  72. assert_eq!(num.load(Ordering::Relaxed), 1);
  73. }