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.

892 lines
28 KiB

  1. use std::io::{Read, Write};
  2. use std::pin::Pin;
  3. use std::task::{Context, Poll};
  4. use actix_http::http::header::{
  5. ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH,
  6. TRANSFER_ENCODING,
  7. };
  8. use brotli2::write::{BrotliDecoder, BrotliEncoder};
  9. use bytes::Bytes;
  10. use flate2::read::GzDecoder;
  11. use flate2::write::{GzEncoder, ZlibDecoder, ZlibEncoder};
  12. use flate2::Compression;
  13. use futures::{ready, Future};
  14. use rand::{distributions::Alphanumeric, Rng};
  15. use actix_web::dev::BodyEncoding;
  16. use actix_web::middleware::Compress;
  17. use actix_web::{dev, test, web, App, Error, HttpResponse};
  18. const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
  19. Hello World Hello World Hello World Hello World Hello World \
  20. Hello World Hello World Hello World Hello World Hello World \
  21. Hello World Hello World Hello World Hello World Hello World \
  22. Hello World Hello World Hello World Hello World Hello World \
  23. Hello World Hello World Hello World Hello World Hello World \
  24. Hello World Hello World Hello World Hello World Hello World \
  25. Hello World Hello World Hello World Hello World Hello World \
  26. Hello World Hello World Hello World Hello World Hello World \
  27. Hello World Hello World Hello World Hello World Hello World \
  28. Hello World Hello World Hello World Hello World Hello World \
  29. Hello World Hello World Hello World Hello World Hello World \
  30. Hello World Hello World Hello World Hello World Hello World \
  31. Hello World Hello World Hello World Hello World Hello World \
  32. Hello World Hello World Hello World Hello World Hello World \
  33. Hello World Hello World Hello World Hello World Hello World \
  34. Hello World Hello World Hello World Hello World Hello World \
  35. Hello World Hello World Hello World Hello World Hello World \
  36. Hello World Hello World Hello World Hello World Hello World \
  37. Hello World Hello World Hello World Hello World Hello World \
  38. Hello World Hello World Hello World Hello World Hello World";
  39. struct TestBody {
  40. data: Bytes,
  41. chunk_size: usize,
  42. delay: actix_rt::time::Delay,
  43. }
  44. impl TestBody {
  45. fn new(data: Bytes, chunk_size: usize) -> Self {
  46. TestBody {
  47. data,
  48. chunk_size,
  49. delay: actix_rt::time::delay_for(std::time::Duration::from_millis(10)),
  50. }
  51. }
  52. }
  53. impl futures::Stream for TestBody {
  54. type Item = Result<Bytes, Error>;
  55. fn poll_next(
  56. mut self: Pin<&mut Self>,
  57. cx: &mut Context<'_>,
  58. ) -> Poll<Option<Self::Item>> {
  59. ready!(Pin::new(&mut self.delay).poll(cx));
  60. self.delay = actix_rt::time::delay_for(std::time::Duration::from_millis(10));
  61. let chunk_size = std::cmp::min(self.chunk_size, self.data.len());
  62. let chunk = self.data.split_to(chunk_size);
  63. if chunk.is_empty() {
  64. Poll::Ready(None)
  65. } else {
  66. Poll::Ready(Some(Ok(chunk)))
  67. }
  68. }
  69. }
  70. #[actix_rt::test]
  71. async fn test_body() {
  72. let srv = test::start(|| {
  73. App::new()
  74. .service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
  75. });
  76. let mut response = srv.get("/").send().await.unwrap();
  77. assert!(response.status().is_success());
  78. // read response
  79. let bytes = response.body().await.unwrap();
  80. assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
  81. }
  82. #[actix_rt::test]
  83. async fn test_body_gzip() {
  84. let srv = test::start_with(test::config().h1(), || {
  85. App::new()
  86. .wrap(Compress::new(ContentEncoding::Gzip))
  87. .service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
  88. });
  89. let mut response = srv
  90. .get("/")
  91. .no_decompress()
  92. .header(ACCEPT_ENCODING, "gzip")
  93. .send()
  94. .await
  95. .unwrap();
  96. assert!(response.status().is_success());
  97. // read response
  98. let bytes = response.body().await.unwrap();
  99. // decode
  100. let mut e = GzDecoder::new(&bytes[..]);
  101. let mut dec = Vec::new();
  102. e.read_to_end(&mut dec).unwrap();
  103. assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
  104. }
  105. #[actix_rt::test]
  106. async fn test_body_gzip2() {
  107. let srv = test::start_with(test::config().h1(), || {
  108. App::new()
  109. .wrap(Compress::new(ContentEncoding::Gzip))
  110. .service(web::resource("/").route(web::to(|| {
  111. HttpResponse::Ok().body(STR).into_body::<dev::Body>()
  112. })))
  113. });
  114. let mut response = srv
  115. .get("/")
  116. .no_decompress()
  117. .header(ACCEPT_ENCODING, "gzip")
  118. .send()
  119. .await
  120. .unwrap();
  121. assert!(response.status().is_success());
  122. // read response
  123. let bytes = response.body().await.unwrap();
  124. // decode
  125. let mut e = GzDecoder::new(&bytes[..]);
  126. let mut dec = Vec::new();
  127. e.read_to_end(&mut dec).unwrap();
  128. assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
  129. }
  130. #[actix_rt::test]
  131. async fn test_body_encoding_override() {
  132. let srv = test::start_with(test::config().h1(), || {
  133. App::new()
  134. .wrap(Compress::new(ContentEncoding::Gzip))
  135. .service(web::resource("/").route(web::to(|| {
  136. HttpResponse::Ok()
  137. .encoding(ContentEncoding::Deflate)
  138. .body(STR)
  139. })))
  140. .service(web::resource("/raw").route(web::to(|| {
  141. let body = actix_web::dev::Body::Bytes(STR.into());
  142. let mut response =
  143. HttpResponse::with_body(actix_web::http::StatusCode::OK, body);
  144. response.encoding(ContentEncoding::Deflate);
  145. response
  146. })))
  147. });
  148. // Builder
  149. let mut response = srv
  150. .get("/")
  151. .no_decompress()
  152. .header(ACCEPT_ENCODING, "deflate")
  153. .send()
  154. .await
  155. .unwrap();
  156. assert!(response.status().is_success());
  157. // read response
  158. let bytes = response.body().await.unwrap();
  159. // decode
  160. let mut e = ZlibDecoder::new(Vec::new());
  161. e.write_all(bytes.as_ref()).unwrap();
  162. let dec = e.finish().unwrap();
  163. assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
  164. // Raw Response
  165. let mut response = srv
  166. .request(actix_web::http::Method::GET, srv.url("/raw"))
  167. .no_decompress()
  168. .header(ACCEPT_ENCODING, "deflate")
  169. .send()
  170. .await
  171. .unwrap();
  172. assert!(response.status().is_success());
  173. // read response
  174. let bytes = response.body().await.unwrap();
  175. // decode
  176. let mut e = ZlibDecoder::new(Vec::new());
  177. e.write_all(bytes.as_ref()).unwrap();
  178. let dec = e.finish().unwrap();
  179. assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
  180. }
  181. #[actix_rt::test]
  182. async fn test_body_gzip_large() {
  183. let data = STR.repeat(10);
  184. let srv_data = data.clone();
  185. let srv = test::start_with(test::config().h1(), move || {
  186. let data = srv_data.clone();
  187. App::new()
  188. .wrap(Compress::new(ContentEncoding::Gzip))
  189. .service(
  190. web::resource("/")
  191. .route(web::to(move || HttpResponse::Ok().body(data.clone()))),
  192. )
  193. });
  194. let mut response = srv
  195. .get("/")
  196. .no_decompress()
  197. .header(ACCEPT_ENCODING, "gzip")
  198. .send()
  199. .await
  200. .unwrap();
  201. assert!(response.status().is_success());
  202. // read response
  203. let bytes = response.body().await.unwrap();
  204. // decode
  205. let mut e = GzDecoder::new(&bytes[..]);
  206. let mut dec = Vec::new();
  207. e.read_to_end(&mut dec).unwrap();
  208. assert_eq!(Bytes::from(dec), Bytes::from(data));
  209. }
  210. #[actix_rt::test]
  211. async fn test_body_gzip_large_random() {
  212. let data = rand::thread_rng()
  213. .sample_iter(&Alphanumeric)
  214. .take(70_000)
  215. .collect::<String>();
  216. let srv_data = data.clone();
  217. let srv = test::start_with(test::config().h1(), move || {
  218. let data = srv_data.clone();
  219. App::new()
  220. .wrap(Compress::new(ContentEncoding::Gzip))
  221. .service(
  222. web::resource("/")
  223. .route(web::to(move || HttpResponse::Ok().body(data.clone()))),
  224. )
  225. });
  226. let mut response = srv
  227. .get("/")
  228. .no_decompress()
  229. .header(ACCEPT_ENCODING, "gzip")
  230. .send()
  231. .await
  232. .unwrap();
  233. assert!(response.status().is_success());
  234. // read response
  235. let bytes = response.body().await.unwrap();
  236. // decode
  237. let mut e = GzDecoder::new(&bytes[..]);
  238. let mut dec = Vec::new();
  239. e.read_to_end(&mut dec).unwrap();
  240. assert_eq!(dec.len(), data.len());
  241. assert_eq!(Bytes::from(dec), Bytes::from(data));
  242. }
  243. #[actix_rt::test]
  244. async fn test_body_chunked_implicit() {
  245. let srv = test::start_with(test::config().h1(), || {
  246. App::new()
  247. .wrap(Compress::new(ContentEncoding::Gzip))
  248. .service(web::resource("/").route(web::get().to(move || {
  249. HttpResponse::Ok()
  250. .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
  251. })))
  252. });
  253. let mut response = srv
  254. .get("/")
  255. .no_decompress()
  256. .header(ACCEPT_ENCODING, "gzip")
  257. .send()
  258. .await
  259. .unwrap();
  260. assert!(response.status().is_success());
  261. assert_eq!(
  262. response.headers().get(TRANSFER_ENCODING).unwrap(),
  263. &b"chunked"[..]
  264. );
  265. // read response
  266. let bytes = response.body().await.unwrap();
  267. // decode
  268. let mut e = GzDecoder::new(&bytes[..]);
  269. let mut dec = Vec::new();
  270. e.read_to_end(&mut dec).unwrap();
  271. assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
  272. }
  273. #[actix_rt::test]
  274. async fn test_body_br_streaming() {
  275. let srv = test::start_with(test::config().h1(), || {
  276. App::new().wrap(Compress::new(ContentEncoding::Br)).service(
  277. web::resource("/").route(web::to(move || {
  278. HttpResponse::Ok()
  279. .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
  280. })),
  281. )
  282. });
  283. let mut response = srv
  284. .get("/")
  285. .header(ACCEPT_ENCODING, "br")
  286. .no_decompress()
  287. .send()
  288. .await
  289. .unwrap();
  290. assert!(response.status().is_success());
  291. // read response
  292. let bytes = response.body().await.unwrap();
  293. println!("TEST: {:?}", bytes.len());
  294. // decode br
  295. let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
  296. e.write_all(bytes.as_ref()).unwrap();
  297. let dec = e.finish().unwrap();
  298. println!("T: {:?}", Bytes::copy_from_slice(&dec));
  299. assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
  300. }
  301. #[actix_rt::test]
  302. async fn test_head_binary() {
  303. let srv = test::start_with(test::config().h1(), || {
  304. App::new().service(web::resource("/").route(
  305. web::head().to(move || HttpResponse::Ok().content_length(100).body(STR)),
  306. ))
  307. });
  308. let mut response = srv.head("/").send().await.unwrap();
  309. assert!(response.status().is_success());
  310. {
  311. let len = response.headers().get(CONTENT_LENGTH).unwrap();
  312. assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
  313. }
  314. // read response
  315. let bytes = response.body().await.unwrap();
  316. assert!(bytes.is_empty());
  317. }
  318. #[actix_rt::test]
  319. async fn test_no_chunking() {
  320. let srv = test::start_with(test::config().h1(), || {
  321. App::new().service(web::resource("/").route(web::to(move || {
  322. HttpResponse::Ok()
  323. .no_chunking()
  324. .content_length(STR.len() as u64)
  325. .streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
  326. })))
  327. });
  328. let mut response = srv.get("/").send().await.unwrap();
  329. assert!(response.status().is_success());
  330. assert!(!response.headers().contains_key(TRANSFER_ENCODING));
  331. // read response
  332. let bytes = response.body().await.unwrap();
  333. assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
  334. }
  335. #[actix_rt::test]
  336. async fn test_body_deflate() {
  337. let srv = test::start_with(test::config().h1(), || {
  338. App::new()
  339. .wrap(Compress::new(ContentEncoding::Deflate))
  340. .service(
  341. web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))),
  342. )
  343. });
  344. // client request
  345. let mut response = srv
  346. .get("/")
  347. .header(ACCEPT_ENCODING, "deflate")
  348. .no_decompress()
  349. .send()
  350. .await
  351. .unwrap();
  352. assert!(response.status().is_success());
  353. // read response
  354. let bytes = response.body().await.unwrap();
  355. let mut e = ZlibDecoder::new(Vec::new());
  356. e.write_all(bytes.as_ref()).unwrap();
  357. let dec = e.finish().unwrap();
  358. assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
  359. }
  360. #[actix_rt::test]
  361. async fn test_body_brotli() {
  362. let srv = test::start_with(test::config().h1(), || {
  363. App::new().wrap(Compress::new(ContentEncoding::Br)).service(
  364. web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))),
  365. )
  366. });
  367. // client request
  368. let mut response = srv
  369. .get("/")
  370. .header(ACCEPT_ENCODING, "br")
  371. .no_decompress()
  372. .send()
  373. .await
  374. .unwrap();
  375. assert!(response.status().is_success());
  376. // read response
  377. let bytes = response.body().await.unwrap();
  378. // decode brotli
  379. let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
  380. e.write_all(bytes.as_ref()).unwrap();
  381. let dec = e.finish().unwrap();
  382. assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
  383. }
  384. #[actix_rt::test]
  385. async fn test_encoding() {
  386. let srv = test::start_with(test::config().h1(), || {
  387. App::new().wrap(Compress::default()).service(
  388. web::resource("/")
  389. .route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
  390. )
  391. });
  392. // client request
  393. let mut e = GzEncoder::new(Vec::new(), Compression::default());
  394. e.write_all(STR.as_ref()).unwrap();
  395. let enc = e.finish().unwrap();
  396. let request = srv
  397. .post("/")
  398. .header(CONTENT_ENCODING, "gzip")
  399. .send_body(enc.clone());
  400. let mut response = request.await.unwrap();
  401. assert!(response.status().is_success());
  402. // read response
  403. let bytes = response.body().await.unwrap();
  404. assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
  405. }
  406. #[actix_rt::test]
  407. async fn test_gzip_encoding() {
  408. let srv = test::start_with(test::config().h1(), || {
  409. App::new().service(
  410. web::resource("/")
  411. .route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
  412. )
  413. });
  414. // client request
  415. let mut e = GzEncoder::new(Vec::new(), Compression::default());
  416. e.write_all(STR.as_ref()).unwrap();
  417. let enc = e.finish().unwrap();
  418. let request = srv
  419. .post("/")
  420. .header(CONTENT_ENCODING, "gzip")
  421. .send_body(enc.clone());
  422. let mut response = request.await.unwrap();
  423. assert!(response.status().is_success());
  424. // read response
  425. let bytes = response.body().await.unwrap();
  426. assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
  427. }
  428. #[actix_rt::test]
  429. async fn test_gzip_encoding_large() {
  430. let data = STR.repeat(10);
  431. let srv = test::start_with(test::config().h1(), || {
  432. App::new().service(
  433. web::resource("/")
  434. .route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
  435. )
  436. });
  437. // client request
  438. let mut e = GzEncoder::new(Vec::new(), Compression::default());
  439. e.write_all(data.as_ref()).unwrap();
  440. let enc = e.finish().unwrap();
  441. let request = srv
  442. .post("/")
  443. .header(CONTENT_ENCODING, "gzip")
  444. .send_body(enc.clone());
  445. let mut response = request.await.unwrap();
  446. assert!(response.status().is_success());
  447. // read response
  448. let bytes = response.body().await.unwrap();
  449. assert_eq!(bytes, Bytes::from(data));
  450. }
  451. #[actix_rt::test]
  452. async fn test_reading_gzip_encoding_large_random() {
  453. let data = rand::thread_rng()
  454. .sample_iter(&Alphanumeric)
  455. .take(60_000)
  456. .collect::<String>();
  457. let srv = test::start_with(test::config().h1(), || {
  458. App::new().service(
  459. web::resource("/")
  460. .route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
  461. )
  462. });
  463. // client request
  464. let mut e = GzEncoder::new(Vec::new(), Compression::default());
  465. e.write_all(data.as_ref()).unwrap();
  466. let enc = e.finish().unwrap();
  467. let request = srv
  468. .post("/")
  469. .header(CONTENT_ENCODING, "gzip")
  470. .send_body(enc.clone());
  471. let mut response = request.await.unwrap();
  472. assert!(response.status().is_success());
  473. // read response
  474. let bytes = response.body().await.unwrap();
  475. assert_eq!(bytes.len(), data.len());
  476. assert_eq!(bytes, Bytes::from(data));
  477. }
  478. #[actix_rt::test]
  479. async fn test_reading_deflate_encoding() {
  480. let srv = test::start_with(test::config().h1(), || {
  481. App::new().service(
  482. web::resource("/")
  483. .route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
  484. )
  485. });
  486. let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
  487. e.write_all(STR.as_ref()).unwrap();
  488. let enc = e.finish().unwrap();
  489. // client request
  490. let request = srv
  491. .post("/")
  492. .header(CONTENT_ENCODING, "deflate")
  493. .send_body(enc.clone());
  494. let mut response = request.await.unwrap();
  495. assert!(response.status().is_success());
  496. // read response
  497. let bytes = response.body().await.unwrap();
  498. assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
  499. }
  500. #[actix_rt::test]
  501. async fn test_reading_deflate_encoding_large() {
  502. let data = STR.repeat(10);
  503. let srv = test::start_with(test::config().h1(), || {
  504. App::new().service(
  505. web::resource("/")
  506. .route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
  507. )
  508. });
  509. let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
  510. e.write_all(data.as_ref()).unwrap();
  511. let enc = e.finish().unwrap();
  512. // client request
  513. let request = srv
  514. .post("/")
  515. .header(CONTENT_ENCODING, "deflate")
  516. .send_body(enc.clone());
  517. let mut response = request.await.unwrap();
  518. assert!(response.status().is_success());
  519. // read response
  520. let bytes = response.body().await.unwrap();
  521. assert_eq!(bytes, Bytes::from(data));
  522. }
  523. #[actix_rt::test]
  524. async fn test_reading_deflate_encoding_large_random() {
  525. let data = rand::thread_rng()
  526. .sample_iter(&Alphanumeric)
  527. .take(160_000)
  528. .collect::<String>();
  529. let srv = test::start_with(test::config().h1(), || {
  530. App::new().service(
  531. web::resource("/")
  532. .route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
  533. )
  534. });
  535. let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
  536. e.write_all(data.as_ref()).unwrap();
  537. let enc = e.finish().unwrap();
  538. // client request
  539. let request = srv
  540. .post("/")
  541. .header(CONTENT_ENCODING, "deflate")
  542. .send_body(enc.clone());
  543. let mut response = request.await.unwrap();
  544. assert!(response.status().is_success());
  545. // read response
  546. let bytes = response.body().await.unwrap();
  547. assert_eq!(bytes.len(), data.len());
  548. assert_eq!(bytes, Bytes::from(data));
  549. }
  550. #[actix_rt::test]
  551. async fn test_brotli_encoding() {
  552. let srv = test::start_with(test::config().h1(), || {
  553. App::new().service(
  554. web::resource("/")
  555. .route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
  556. )
  557. });
  558. let mut e = BrotliEncoder::new(Vec::new(), 5);
  559. e.write_all(STR.as_ref()).unwrap();
  560. let enc = e.finish().unwrap();
  561. // client request
  562. let request = srv
  563. .post("/")
  564. .header(CONTENT_ENCODING, "br")
  565. .send_body(enc.clone());
  566. let mut response = request.await.unwrap();
  567. assert!(response.status().is_success());
  568. // read response
  569. let bytes = response.body().await.unwrap();
  570. assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
  571. }
  572. #[actix_rt::test]
  573. async fn test_brotli_encoding_large() {
  574. let data = rand::thread_rng()
  575. .sample_iter(&Alphanumeric)
  576. .take(320_000)
  577. .collect::<String>();
  578. let srv = test::start_with(test::config().h1(), || {
  579. App::new().service(
  580. web::resource("/")
  581. .app_data(web::PayloadConfig::new(320_000))
  582. .route(web::to(move |body: Bytes| {
  583. HttpResponse::Ok().streaming(TestBody::new(body, 10240))
  584. })),
  585. )
  586. });
  587. let mut e = BrotliEncoder::new(Vec::new(), 5);
  588. e.write_all(data.as_ref()).unwrap();
  589. let enc = e.finish().unwrap();
  590. // client request
  591. let request = srv
  592. .post("/")
  593. .header(CONTENT_ENCODING, "br")
  594. .send_body(enc.clone());
  595. let mut response = request.await.unwrap();
  596. assert!(response.status().is_success());
  597. // read response
  598. let bytes = response.body().limit(320_000).await.unwrap();
  599. assert_eq!(bytes, Bytes::from(data));
  600. }
  601. #[cfg(feature = "openssl")]
  602. #[actix_rt::test]
  603. async fn test_brotli_encoding_large_openssl() {
  604. // load ssl keys
  605. use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
  606. let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
  607. builder
  608. .set_private_key_file("tests/key.pem", SslFiletype::PEM)
  609. .unwrap();
  610. builder
  611. .set_certificate_chain_file("tests/cert.pem")
  612. .unwrap();
  613. let data = STR.repeat(10);
  614. let srv = test::start_with(test::config().openssl(builder.build()), move || {
  615. App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
  616. HttpResponse::Ok()
  617. .encoding(actix_web::http::ContentEncoding::Identity)
  618. .body(bytes)
  619. })))
  620. });
  621. // body
  622. let mut e = BrotliEncoder::new(Vec::new(), 3);
  623. e.write_all(data.as_ref()).unwrap();
  624. let enc = e.finish().unwrap();
  625. // client request
  626. let mut response = srv
  627. .post("/")
  628. .header(actix_web::http::header::CONTENT_ENCODING, "br")
  629. .send_body(enc)
  630. .await
  631. .unwrap();
  632. assert!(response.status().is_success());
  633. // read response
  634. let bytes = response.body().await.unwrap();
  635. assert_eq!(bytes, Bytes::from(data));
  636. }
  637. #[cfg(all(feature = "rustls", feature = "openssl"))]
  638. #[actix_rt::test]
  639. async fn test_reading_deflate_encoding_large_random_rustls() {
  640. use rust_tls::internal::pemfile::{certs, pkcs8_private_keys};
  641. use rust_tls::{NoClientAuth, ServerConfig};
  642. use std::fs::File;
  643. use std::io::BufReader;
  644. let data = rand::thread_rng()
  645. .sample_iter(&Alphanumeric)
  646. .take(160_000)
  647. .collect::<String>();
  648. // load ssl keys
  649. let mut config = ServerConfig::new(NoClientAuth::new());
  650. let cert_file = &mut BufReader::new(File::open("tests/cert.pem").unwrap());
  651. let key_file = &mut BufReader::new(File::open("tests/key.pem").unwrap());
  652. let cert_chain = certs(cert_file).unwrap();
  653. let mut keys = pkcs8_private_keys(key_file).unwrap();
  654. config.set_single_cert(cert_chain, keys.remove(0)).unwrap();
  655. let srv = test::start_with(test::config().rustls(config), || {
  656. App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
  657. HttpResponse::Ok()
  658. .encoding(actix_web::http::ContentEncoding::Identity)
  659. .body(bytes)
  660. })))
  661. });
  662. // encode data
  663. let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
  664. e.write_all(data.as_ref()).unwrap();
  665. let enc = e.finish().unwrap();
  666. // client request
  667. let req = srv
  668. .post("/")
  669. .header(actix_web::http::header::CONTENT_ENCODING, "deflate")
  670. .send_stream(TestBody::new(Bytes::from(enc), 1024));
  671. let mut response = req.await.unwrap();
  672. assert!(response.status().is_success());
  673. // read response
  674. let bytes = response.body().await.unwrap();
  675. assert_eq!(bytes.len(), data.len());
  676. assert_eq!(bytes, Bytes::from(data));
  677. }
  678. // #[test]
  679. // fn test_server_cookies() {
  680. // use actix_web::http;
  681. // let srv = test::TestServer::with_factory(|| {
  682. // App::new().resource("/", |r| {
  683. // r.f(|_| {
  684. // HttpResponse::Ok()
  685. // .cookie(
  686. // http::CookieBuilder::new("first", "first_value")
  687. // .http_only(true)
  688. // .finish(),
  689. // )
  690. // .cookie(http::Cookie::new("second", "first_value"))
  691. // .cookie(http::Cookie::new("second", "second_value"))
  692. // .finish()
  693. // })
  694. // })
  695. // });
  696. // let first_cookie = http::CookieBuilder::new("first", "first_value")
  697. // .http_only(true)
  698. // .finish();
  699. // let second_cookie = http::Cookie::new("second", "second_value");
  700. // let request = srv.get("/").finish().unwrap();
  701. // let response = srv.execute(request.send()).unwrap();
  702. // assert!(response.status().is_success());
  703. // let cookies = response.cookies().expect("To have cookies");
  704. // assert_eq!(cookies.len(), 2);
  705. // if cookies[0] == first_cookie {
  706. // assert_eq!(cookies[1], second_cookie);
  707. // } else {
  708. // assert_eq!(cookies[0], second_cookie);
  709. // assert_eq!(cookies[1], first_cookie);
  710. // }
  711. // let first_cookie = first_cookie.to_string();
  712. // let second_cookie = second_cookie.to_string();
  713. // //Check that we have exactly two instances of raw cookie headers
  714. // let cookies = response
  715. // .headers()
  716. // .get_all(http::header::SET_COOKIE)
  717. // .iter()
  718. // .map(|header| header.to_str().expect("To str").to_string())
  719. // .collect::<Vec<_>>();
  720. // assert_eq!(cookies.len(), 2);
  721. // if cookies[0] == first_cookie {
  722. // assert_eq!(cookies[1], second_cookie);
  723. // } else {
  724. // assert_eq!(cookies[0], second_cookie);
  725. // assert_eq!(cookies[1], first_cookie);
  726. // }
  727. // }
  728. #[actix_rt::test]
  729. async fn test_slow_request() {
  730. use std::net;
  731. let srv = test::start_with(test::config().client_timeout(200), || {
  732. App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok())))
  733. });
  734. let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
  735. let mut data = String::new();
  736. let _ = stream.read_to_string(&mut data);
  737. assert!(data.starts_with("HTTP/1.1 408 Request Timeout"));
  738. let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
  739. let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n");
  740. let mut data = String::new();
  741. let _ = stream.read_to_string(&mut data);
  742. assert!(data.starts_with("HTTP/1.1 408 Request Timeout"));
  743. }
  744. // #[cfg(feature = "openssl")]
  745. // #[actix_rt::test]
  746. // async fn test_ssl_handshake_timeout() {
  747. // use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
  748. // use std::net;
  749. // // load ssl keys
  750. // let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
  751. // builder
  752. // .set_private_key_file("tests/key.pem", SslFiletype::PEM)
  753. // .unwrap();
  754. // builder
  755. // .set_certificate_chain_file("tests/cert.pem")
  756. // .unwrap();
  757. // let srv = test::start_with(test::config().openssl(builder.build()), || {
  758. // App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok())))
  759. // });
  760. // let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
  761. // let mut data = String::new();
  762. // let _ = stream.read_to_string(&mut data);
  763. // assert!(data.is_empty());
  764. // }