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.

147 lines
4.0 KiB

  1. use net2::TcpBuilder;
  2. use std::sync::mpsc;
  3. use std::{net, thread, time::Duration};
  4. #[cfg(feature = "openssl")]
  5. use open_ssl::ssl::SslAcceptorBuilder;
  6. use actix_web::{web, App, HttpResponse, HttpServer};
  7. fn unused_addr() -> net::SocketAddr {
  8. let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
  9. let socket = TcpBuilder::new_v4().unwrap();
  10. socket.bind(&addr).unwrap();
  11. socket.reuse_address(true).unwrap();
  12. let tcp = socket.to_tcp_listener().unwrap();
  13. tcp.local_addr().unwrap()
  14. }
  15. #[cfg(unix)]
  16. #[actix_rt::test]
  17. async fn test_start() {
  18. let addr = unused_addr();
  19. let (tx, rx) = mpsc::channel();
  20. thread::spawn(move || {
  21. let sys = actix_rt::System::new("test");
  22. let srv = HttpServer::new(|| {
  23. App::new().service(
  24. web::resource("/").route(web::to(|| HttpResponse::Ok().body("test"))),
  25. )
  26. })
  27. .workers(1)
  28. .backlog(1)
  29. .maxconn(10)
  30. .maxconnrate(10)
  31. .keep_alive(10)
  32. .client_timeout(5000)
  33. .client_shutdown(0)
  34. .server_hostname("localhost")
  35. .system_exit()
  36. .disable_signals()
  37. .bind(format!("{}", addr))
  38. .unwrap()
  39. .run();
  40. let _ = tx.send((srv, actix_rt::System::current()));
  41. let _ = sys.run();
  42. });
  43. let (srv, sys) = rx.recv().unwrap();
  44. #[cfg(feature = "client")]
  45. {
  46. use actix_http::client;
  47. let client = awc::Client::build()
  48. .connector(
  49. client::Connector::new()
  50. .timeout(Duration::from_millis(100))
  51. .finish(),
  52. )
  53. .finish();
  54. let host = format!("http://{}", addr);
  55. let response = client.get(host.clone()).send().await.unwrap();
  56. assert!(response.status().is_success());
  57. }
  58. // stop
  59. let _ = srv.stop(false);
  60. thread::sleep(Duration::from_millis(100));
  61. let _ = sys.stop();
  62. }
  63. #[cfg(feature = "openssl")]
  64. fn ssl_acceptor() -> std::io::Result<SslAcceptorBuilder> {
  65. use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
  66. // load ssl keys
  67. let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
  68. builder
  69. .set_private_key_file("tests/key.pem", SslFiletype::PEM)
  70. .unwrap();
  71. builder
  72. .set_certificate_chain_file("tests/cert.pem")
  73. .unwrap();
  74. Ok(builder)
  75. }
  76. #[actix_rt::test]
  77. #[cfg(feature = "openssl")]
  78. async fn test_start_ssl() {
  79. use actix_web::HttpRequest;
  80. let addr = unused_addr();
  81. let (tx, rx) = mpsc::channel();
  82. thread::spawn(move || {
  83. let sys = actix_rt::System::new("test");
  84. let builder = ssl_acceptor().unwrap();
  85. let srv = HttpServer::new(|| {
  86. App::new().service(web::resource("/").route(web::to(|req: HttpRequest| {
  87. assert!(req.app_config().secure());
  88. HttpResponse::Ok().body("test")
  89. })))
  90. })
  91. .workers(1)
  92. .shutdown_timeout(1)
  93. .system_exit()
  94. .disable_signals()
  95. .bind_openssl(format!("{}", addr), builder)
  96. .unwrap()
  97. .run();
  98. let _ = tx.send((srv, actix_rt::System::current()));
  99. let _ = sys.run();
  100. });
  101. let (srv, sys) = rx.recv().unwrap();
  102. use open_ssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
  103. let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
  104. builder.set_verify(SslVerifyMode::NONE);
  105. let _ = builder
  106. .set_alpn_protos(b"\x02h2\x08http/1.1")
  107. .map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
  108. let client = awc::Client::build()
  109. .connector(
  110. awc::Connector::new()
  111. .ssl(builder.build())
  112. .timeout(Duration::from_millis(100))
  113. .finish(),
  114. )
  115. .finish();
  116. let host = format!("https://{}", addr);
  117. let response = client.get(host.clone()).send().await.unwrap();
  118. assert!(response.status().is_success());
  119. // stop
  120. let _ = srv.stop(false);
  121. thread::sleep(Duration::from_millis(100));
  122. let _ = sys.stop();
  123. }