2024-05-18 18:22:53 +00:00
|
|
|
#![cfg(feature = "rustls-0_23")]
|
2021-02-07 03:54:58 +00:00
|
|
|
|
2024-05-18 18:22:53 +00:00
|
|
|
extern crate tls_rustls_023 as rustls;
|
2021-02-07 03:54:58 +00:00
|
|
|
|
2021-06-17 16:57:58 +00:00
|
|
|
use std::{
|
2023-07-02 00:09:15 +00:00
|
|
|
convert::Infallible,
|
2021-06-17 16:57:58 +00:00
|
|
|
io::{self, BufReader, Write},
|
|
|
|
net::{SocketAddr, TcpStream as StdTcpStream},
|
|
|
|
sync::Arc,
|
2021-12-24 17:47:47 +00:00
|
|
|
task::Poll,
|
2022-06-27 02:57:21 +00:00
|
|
|
time::Duration,
|
2021-06-17 16:57:58 +00:00
|
|
|
};
|
|
|
|
|
2021-04-13 10:16:12 +00:00
|
|
|
use actix_http::{
|
2021-12-04 19:40:47 +00:00
|
|
|
body::{BodyStream, BoxBody, SizedStream},
|
2021-05-14 15:40:00 +00:00
|
|
|
error::PayloadError,
|
2021-12-05 14:37:20 +00:00
|
|
|
header::{self, HeaderName, HeaderValue},
|
2022-06-27 02:57:21 +00:00
|
|
|
Error, HttpService, Method, Request, Response, StatusCode, TlsAcceptorConfig, Version,
|
2021-04-13 10:16:12 +00:00
|
|
|
};
|
2019-12-12 17:08:38 +00:00
|
|
|
use actix_http_test::test_server;
|
2021-12-24 17:47:47 +00:00
|
|
|
use actix_rt::pin;
|
2019-12-08 13:25:24 +00:00
|
|
|
use actix_service::{fn_factory_with_config, fn_service};
|
2024-05-18 18:22:53 +00:00
|
|
|
use actix_tls::connect::rustls_0_23::webpki_roots_cert_store;
|
2021-12-24 17:47:47 +00:00
|
|
|
use actix_utils::future::{err, ok, poll_fn};
|
2019-11-19 12:54:19 +00:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2021-05-14 15:40:00 +00:00
|
|
|
use derive_more::{Display, Error};
|
2021-12-24 17:47:47 +00:00
|
|
|
use futures_core::{ready, Stream};
|
|
|
|
use futures_util::stream::once;
|
2024-02-03 23:55:01 +00:00
|
|
|
use rustls::{pki_types::ServerName, ServerConfig as RustlsServerConfig};
|
2021-10-20 01:00:11 +00:00
|
|
|
use rustls_pemfile::{certs, pkcs8_private_keys};
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2021-12-24 17:47:47 +00:00
|
|
|
async fn load_body<S>(stream: S) -> Result<BytesMut, PayloadError>
|
2019-11-19 12:54:19 +00:00
|
|
|
where
|
2021-12-24 17:47:47 +00:00
|
|
|
S: Stream<Item = Result<Bytes, PayloadError>>,
|
2019-11-19 12:54:19 +00:00
|
|
|
{
|
2021-12-24 17:47:47 +00:00
|
|
|
let mut buf = BytesMut::new();
|
|
|
|
|
|
|
|
pin!(stream);
|
|
|
|
|
|
|
|
poll_fn(|cx| loop {
|
|
|
|
let body = stream.as_mut();
|
|
|
|
|
|
|
|
match ready!(body.poll_next(cx)) {
|
2022-09-25 19:54:17 +00:00
|
|
|
Some(Ok(bytes)) => buf.extend_from_slice(&bytes),
|
2021-12-24 17:47:47 +00:00
|
|
|
None => return Poll::Ready(Ok(())),
|
|
|
|
Some(Err(err)) => return Poll::Ready(Err(err)),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(buf)
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 17:16:36 +00:00
|
|
|
fn tls_config() -> RustlsServerConfig {
|
2024-05-19 09:12:32 +00:00
|
|
|
let rcgen::CertifiedKey { cert, key_pair } =
|
|
|
|
rcgen::generate_simple_self_signed(["localhost".to_owned()]).unwrap();
|
|
|
|
let cert_file = cert.pem();
|
|
|
|
let key_file = key_pair.serialize_pem();
|
2021-02-13 17:16:36 +00:00
|
|
|
|
|
|
|
let cert_file = &mut BufReader::new(cert_file.as_bytes());
|
|
|
|
let key_file = &mut BufReader::new(key_file.as_bytes());
|
|
|
|
|
2024-02-03 23:55:01 +00:00
|
|
|
let cert_chain = certs(cert_file).collect::<Result<Vec<_>, _>>().unwrap();
|
|
|
|
let mut keys = pkcs8_private_keys(key_file)
|
|
|
|
.collect::<Result<Vec<_>, _>>()
|
|
|
|
.unwrap();
|
2021-10-20 01:00:11 +00:00
|
|
|
|
|
|
|
let mut config = RustlsServerConfig::builder()
|
|
|
|
.with_no_client_auth()
|
2024-02-03 23:55:01 +00:00
|
|
|
.with_single_cert(
|
|
|
|
cert_chain,
|
|
|
|
rustls::pki_types::PrivateKeyDer::Pkcs8(keys.remove(0)),
|
|
|
|
)
|
2021-10-20 01:00:11 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
config.alpn_protocols.push(HTTP1_1_ALPN_PROTOCOL.to_vec());
|
|
|
|
config.alpn_protocols.push(H2_ALPN_PROTOCOL.to_vec());
|
2021-02-13 17:16:36 +00:00
|
|
|
|
2019-12-05 17:35:43 +00:00
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2021-06-10 15:25:21 +00:00
|
|
|
pub fn get_negotiated_alpn_protocol(
|
|
|
|
addr: SocketAddr,
|
|
|
|
client_alpn_protocol: &[u8],
|
|
|
|
) -> Option<Vec<u8>> {
|
2021-10-20 01:00:11 +00:00
|
|
|
let mut config = rustls::ClientConfig::builder()
|
2021-10-20 21:32:05 +00:00
|
|
|
.with_root_certificates(webpki_roots_cert_store())
|
2021-10-20 01:00:11 +00:00
|
|
|
.with_no_client_auth();
|
|
|
|
|
2021-06-10 15:25:21 +00:00
|
|
|
config.alpn_protocols.push(client_alpn_protocol.to_vec());
|
2021-10-20 01:00:11 +00:00
|
|
|
|
2023-07-17 01:38:12 +00:00
|
|
|
let mut sess =
|
|
|
|
rustls::ClientConnection::new(Arc::new(config), ServerName::try_from("localhost").unwrap())
|
|
|
|
.unwrap();
|
2021-10-20 01:00:11 +00:00
|
|
|
|
2021-06-10 15:25:21 +00:00
|
|
|
let mut sock = StdTcpStream::connect(addr).unwrap();
|
|
|
|
let mut stream = rustls::Stream::new(&mut sess, &mut sock);
|
2021-10-20 01:00:11 +00:00
|
|
|
|
2021-06-10 15:25:21 +00:00
|
|
|
// The handshake will fails because the client will not be able to verify the server
|
|
|
|
// certificate, but it doesn't matter here as we are just interested in the negotiated ALPN
|
|
|
|
// protocol
|
|
|
|
let _ = stream.flush();
|
2021-10-20 01:00:11 +00:00
|
|
|
|
|
|
|
sess.alpn_protocol().map(|proto| proto.to_vec())
|
2021-06-10 15:25:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 17:35:43 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h1() -> io::Result<()> {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2021-04-14 01:00:14 +00:00
|
|
|
.h1(|_| ok::<_, Error>(Response::ok()))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-12-05 17:35:43 +00:00
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
Ok(())
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2() -> io::Result<()> {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2021-04-14 01:00:14 +00:00
|
|
|
.h2(|_| ok::<_, Error>(Response::ok()))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-12-05 17:35:43 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h1_1() -> io::Result<()> {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h1(|req: Request| {
|
|
|
|
assert!(req.peer_addr().is_some());
|
|
|
|
assert_eq!(req.version(), Version::HTTP_11);
|
2021-04-14 01:00:14 +00:00
|
|
|
ok::<_, Error>(Response::ok())
|
2019-12-05 17:35:43 +00:00
|
|
|
})
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
Ok(())
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_1() -> io::Result<()> {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
|
|
|
.finish(|req: Request| {
|
|
|
|
assert!(req.peer_addr().is_some());
|
|
|
|
assert_eq!(req.version(), Version::HTTP_2);
|
2021-04-14 01:00:14 +00:00
|
|
|
ok::<_, Error>(Response::ok())
|
2019-12-05 17:35:43 +00:00
|
|
|
})
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23_with_config(
|
2022-06-27 02:57:21 +00:00
|
|
|
tls_config(),
|
|
|
|
TlsAcceptorConfig::default().handshake_timeout(Duration::from_secs(5)),
|
|
|
|
)
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
Ok(())
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_body1() -> io::Result<()> {
|
2019-11-26 05:25:50 +00:00
|
|
|
let data = "HELLOWORLD".to_owned().repeat(64 * 1024);
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2020-02-27 02:10:55 +00:00
|
|
|
.h2(|mut req: Request<_>| async move {
|
|
|
|
let body = load_body(req.take_payload()).await?;
|
2021-04-14 01:00:14 +00:00
|
|
|
Ok::<_, Error>(Response::ok().set_body(body))
|
2019-12-05 17:35:43 +00:00
|
|
|
})
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send_body(data.clone()).await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
let body = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(&body, data.as_bytes());
|
|
|
|
Ok(())
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_content_length() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h2(|req: Request| {
|
|
|
|
let indx: usize = req.uri().path()[1..].parse().unwrap();
|
|
|
|
let statuses = [
|
|
|
|
StatusCode::CONTINUE,
|
2021-03-04 13:27:54 +00:00
|
|
|
StatusCode::NO_CONTENT,
|
2019-12-05 17:35:43 +00:00
|
|
|
StatusCode::OK,
|
|
|
|
StatusCode::NOT_FOUND,
|
|
|
|
];
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(Response::new(statuses[indx]))
|
2019-12-05 17:35:43 +00:00
|
|
|
})
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let header = HeaderName::from_static("content-length");
|
|
|
|
let value = HeaderValue::from_static("0");
|
2021-03-04 13:27:54 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
{
|
2022-03-10 03:12:29 +00:00
|
|
|
#[allow(clippy::single_element_loop)]
|
2021-03-04 13:27:54 +00:00
|
|
|
for &i in &[0] {
|
|
|
|
let req = srv
|
|
|
|
.request(Method::HEAD, srv.surl(&format!("/{}", i)))
|
|
|
|
.send();
|
|
|
|
let _response = req.await.expect_err("should timeout on recv 1xx frame");
|
|
|
|
// assert_eq!(response.headers().get(&header), None);
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let req = srv
|
|
|
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
|
|
|
.send();
|
2021-03-04 13:27:54 +00:00
|
|
|
let _response = req.await.expect_err("should timeout on recv 1xx frame");
|
|
|
|
// assert_eq!(response.headers().get(&header), None);
|
|
|
|
}
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2022-03-10 03:12:29 +00:00
|
|
|
#[allow(clippy::single_element_loop)]
|
2021-03-04 13:27:54 +00:00
|
|
|
for &i in &[1] {
|
2019-11-26 05:25:50 +00:00
|
|
|
let req = srv
|
2021-03-04 13:27:54 +00:00
|
|
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send();
|
|
|
|
let response = req.await.unwrap();
|
|
|
|
assert_eq!(response.headers().get(&header), None);
|
|
|
|
}
|
|
|
|
|
2021-03-04 13:27:54 +00:00
|
|
|
for &i in &[2, 3] {
|
2019-11-26 05:25:50 +00:00
|
|
|
let req = srv
|
|
|
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
|
|
|
.send();
|
|
|
|
let response = req.await.unwrap();
|
|
|
|
assert_eq!(response.headers().get(&header), Some(&value));
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
2019-11-26 05:25:50 +00:00
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_headers() {
|
2019-11-26 05:25:50 +00:00
|
|
|
let data = STR.repeat(10);
|
|
|
|
let data2 = data.clone();
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let data = data.clone();
|
2021-12-08 06:01:11 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h2(move |_| {
|
|
|
|
let mut config = Response::build(StatusCode::OK);
|
|
|
|
for idx in 0..90 {
|
|
|
|
config.insert_header((
|
2019-11-19 12:54:19 +00:00
|
|
|
format!("X-TEST-{}", idx).as_str(),
|
|
|
|
"TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
|
|
|
|
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST ",
|
2021-01-15 02:11:10 +00:00
|
|
|
));
|
2021-12-08 06:01:11 +00:00
|
|
|
}
|
|
|
|
ok::<_, Infallible>(config.body(data.clone()))
|
|
|
|
})
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2021-12-08 06:01:11 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data2));
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World \
|
|
|
|
Hello World Hello World Hello World Hello World Hello World";
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_body2() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h2(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_head_empty() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.finish(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.shead("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert_eq!(response.version(), Version::HTTP_2);
|
|
|
|
|
|
|
|
{
|
|
|
|
let len = response
|
|
|
|
.headers()
|
|
|
|
.get(http::header::CONTENT_LENGTH)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert!(bytes.is_empty());
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_head_binary() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h2(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.shead("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
|
|
|
let len = response
|
|
|
|
.headers()
|
|
|
|
.get(http::header::CONTENT_LENGTH)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert!(bytes.is_empty());
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_head_binary2() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h2(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.shead("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
|
|
|
let len = response
|
|
|
|
.headers()
|
|
|
|
.get(http::header::CONTENT_LENGTH)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_body_length() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h2(|_| {
|
2021-06-17 16:57:58 +00:00
|
|
|
let body = once(ok::<_, Infallible>(Bytes::from_static(STR.as_ref())));
|
|
|
|
ok::<_, Infallible>(
|
2021-04-14 01:00:14 +00:00
|
|
|
Response::ok().set_body(SizedStream::new(STR.len() as u64, body)),
|
2019-12-05 17:35:43 +00:00
|
|
|
)
|
|
|
|
})
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_body_chunked_explicit() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h2(|_| {
|
|
|
|
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(
|
2021-04-14 01:00:14 +00:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
2021-12-04 19:40:47 +00:00
|
|
|
.body(BodyStream::new(body)),
|
2019-12-05 17:35:43 +00:00
|
|
|
)
|
|
|
|
})
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert!(!response.headers().contains_key(header::TRANSFER_ENCODING));
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
|
|
|
|
// decode
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_response_http_error_handling() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2019-12-08 13:25:24 +00:00
|
|
|
.h2(fn_factory_with_config(|_: ()| {
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(fn_service(|_| {
|
2019-12-05 17:35:43 +00:00
|
|
|
let broken_header = Bytes::from_static(b"\0\0\0");
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(
|
2021-04-14 01:00:14 +00:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header((http::header::CONTENT_TYPE, broken_header))
|
2019-12-05 17:35:43 +00:00
|
|
|
.body(STR),
|
|
|
|
)
|
|
|
|
}))
|
|
|
|
}))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
2021-06-17 16:57:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
bytes,
|
|
|
|
Bytes::from_static(b"error processing HTTP: failed to parse header value")
|
|
|
|
);
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 15:40:00 +00:00
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "error")]
|
|
|
|
struct BadRequest;
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
impl From<BadRequest> for Response<BoxBody> {
|
2021-06-17 16:57:58 +00:00
|
|
|
fn from(_: BadRequest) -> Self {
|
2021-12-04 19:40:47 +00:00
|
|
|
Response::bad_request().set_body(BoxBody::new("error"))
|
2021-05-14 15:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h2_service_error() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2021-12-04 19:40:47 +00:00
|
|
|
.h2(|_| err::<Response<BoxBody>, _>(BadRequest))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2019-12-05 17:35:43 +00:00
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"error"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn h1_service_error() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-05 17:35:43 +00:00
|
|
|
HttpService::build()
|
2021-12-04 19:40:47 +00:00
|
|
|
.h1(|_| err::<Response<BoxBody>, _>(BadRequest))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(tls_config())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"error"));
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
2021-06-10 15:25:21 +00:00
|
|
|
|
|
|
|
const H2_ALPN_PROTOCOL: &[u8] = b"h2";
|
|
|
|
const HTTP1_1_ALPN_PROTOCOL: &[u8] = b"http/1.1";
|
|
|
|
const CUSTOM_ALPN_PROTOCOL: &[u8] = b"custom";
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn alpn_h1() -> io::Result<()> {
|
2021-06-10 15:25:21 +00:00
|
|
|
let srv = test_server(move || {
|
|
|
|
let mut config = tls_config();
|
|
|
|
config.alpn_protocols.push(CUSTOM_ALPN_PROTOCOL.to_vec());
|
|
|
|
HttpService::build()
|
|
|
|
.h1(|_| ok::<_, Error>(Response::ok()))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(config)
|
2021-06-10 15:25:21 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
get_negotiated_alpn_protocol(srv.addr(), CUSTOM_ALPN_PROTOCOL),
|
|
|
|
Some(CUSTOM_ALPN_PROTOCOL.to_vec())
|
|
|
|
);
|
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn alpn_h2() -> io::Result<()> {
|
2021-06-10 15:25:21 +00:00
|
|
|
let srv = test_server(move || {
|
|
|
|
let mut config = tls_config();
|
|
|
|
config.alpn_protocols.push(CUSTOM_ALPN_PROTOCOL.to_vec());
|
|
|
|
HttpService::build()
|
|
|
|
.h2(|_| ok::<_, Error>(Response::ok()))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(config)
|
2021-06-10 15:25:21 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
get_negotiated_alpn_protocol(srv.addr(), H2_ALPN_PROTOCOL),
|
|
|
|
Some(H2_ALPN_PROTOCOL.to_vec())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_negotiated_alpn_protocol(srv.addr(), CUSTOM_ALPN_PROTOCOL),
|
|
|
|
Some(CUSTOM_ALPN_PROTOCOL.to_vec())
|
|
|
|
);
|
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-01 14:15:30 +00:00
|
|
|
async fn alpn_h2_1() -> io::Result<()> {
|
2021-06-10 15:25:21 +00:00
|
|
|
let srv = test_server(move || {
|
|
|
|
let mut config = tls_config();
|
|
|
|
config.alpn_protocols.push(CUSTOM_ALPN_PROTOCOL.to_vec());
|
|
|
|
HttpService::build()
|
|
|
|
.finish(|_| ok::<_, Error>(Response::ok()))
|
2024-05-18 18:22:53 +00:00
|
|
|
.rustls_0_23(config)
|
2021-06-10 15:25:21 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
get_negotiated_alpn_protocol(srv.addr(), H2_ALPN_PROTOCOL),
|
|
|
|
Some(H2_ALPN_PROTOCOL.to_vec())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_negotiated_alpn_protocol(srv.addr(), HTTP1_1_ALPN_PROTOCOL),
|
|
|
|
Some(HTTP1_1_ALPN_PROTOCOL.to_vec())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_negotiated_alpn_protocol(srv.addr(), CUSTOM_ALPN_PROTOCOL),
|
|
|
|
Some(CUSTOM_ALPN_PROTOCOL.to_vec())
|
|
|
|
);
|
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|