Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/tests/http_body_sink.rs

6.6 KiB, 5 runs

created by r154713437:79565, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#![cfg(feature = "async")]
2//! A reply's body written to a caller's sink as it arrives: whole when the framing says it is,
3//! never held by the client, and never past the caller's limit.
4
5use oxedyne_fe2o3_core::prelude::*;
6use oxedyne_fe2o3_net::http::{
7 client,
8 fields::HeaderName,
9 header::HttpMethod,
10 msg::ReadLimits,
11};
12
13use std::{
14 net::SocketAddr,
15 pin::Pin,
16 task::{
17 Context,
18 Poll,
19 },
20};
21
22use tokio::{
23 io::{
24 AsyncReadExt,
25 AsyncWrite,
26 AsyncWriteExt,
27 },
28 net::TcpListener,
29};
30
31
32/// A sink that keeps nothing: it counts, checks each byte against the pattern a body is made of,
33/// and notes its largest single write.
34#[derive(Default)]
35struct Tally {
36 n: u64,
37 widest: usize,
38 wrong: u64, // bytes not where the pattern puts them
39}
40
41impl AsyncWrite for Tally {
42 fn poll_write(mut self: Pin<&mut Self>, _: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
43 for (i, b) in buf.iter().enumerate() {
44 if *b != byte_at(self.n + i as u64) {
45 self.wrong += 1;
46 }
47 }
48 self.n += buf.len() as u64;
49 self.widest = self.widest.max(buf.len());
50 Poll::Ready(Ok(buf.len()))
51 }
52 fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<std::io::Result<()>> {
53 Poll::Ready(Ok(()))
54 }
55 fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<std::io::Result<()>> {
56 Poll::Ready(Ok(()))
57 }
58}
59
60fn byte_at(i: u64) -> u8 {
61 (i % 251) as u8 // fixed: a prime period, so a slip by any power of two shows
62}
63
64/// How a test server frames the body it sends.
65#[derive(Clone, Copy)]
66enum Framing {
67 Length, // a true Content-Length
68 LengthShort, // a Content-Length twice what is sent, then the close
69 Chunked(usize), // chunks of this many bytes
70 ToClose, // neither: the body ends with the connection
71}
72
73/// A loopback server that answers one request with a `len` byte body framed as asked, written
74/// in pieces. Returns its address.
75async fn serve(len: u64, framing: Framing) -> SocketAddr {
76 let l = TcpListener::bind("127.0.0.1:0").await.expect("bind");
77 let addr = l.local_addr().expect("addr");
78 tokio::spawn(async move {
79 let (mut s, _) = match l.accept().await {
80 Ok(c) => c,
81 Err(_) => return,
82 };
83 let mut b = [0u8; 4096];
84 let _ = s.read(&mut b).await;
85 let head = match framing {
86 Framing::Length => fmt!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", len),
87 Framing::LengthShort => fmt!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", 2 * len),
88 Framing::Chunked(_) => fmt!("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"),
89 Framing::ToClose => fmt!("HTTP/1.0 200 OK\r\n\r\n"),
90 };
91 if s.write_all(head.as_bytes()).await.is_err() {
92 return;
93 }
94 let piece = match framing {
95 Framing::Chunked(c) => c as u64,
96 _ => 64 * 1024,
97 };
98 let mut at = 0;
99 while at < len {
100 let k = piece.min(len - at);
101 let body: Vec<u8> = (at..at + k).map(byte_at).collect();
102 let sent = match framing {
103 Framing::Chunked(_) => {
104 let mut wire = fmt!("{:x}\r\n", k).into_bytes();
105 wire.extend_from_slice(&body);
106 wire.extend_from_slice(b"\r\n");
107 s.write_all(&wire).await
108 }
109 _ => s.write_all(&body).await,
110 };
111 if sent.is_err() {
112 return; // the client stopped reading, as a limit tells it to
113 }
114 at += k;
115 }
116 if let Framing::Chunked(_) = framing {
117 let _ = s.write_all(b"0\r\n\r\n").await;
118 }
119 let _ = s.shutdown().await;
120 });
121 addr
122}
123
124async fn fetch(addr: SocketAddr, limit: Option<usize>, sink: &mut Tally)
125 -> Outcome<(oxedyne_fe2o3_net::http::header::HttpHeader, u64)>
126{
127 let limits = ReadLimits { max_body_bytes: limit, ..ReadLimits::default() };
128 client::http_request_at_into(addr, "127.0.0.1", HttpMethod::GET, "/", &[], &[],
129 Some(&limits), |_| Ok(()), sink).await
130}
131
132const MIB: u64 = 1024 * 1024;
133
134#[tokio::test]
135async fn a_body_past_what_memory_should_hold_streams_through_whole() {
136 for framing in [Framing::Length, Framing::Chunked(4 * MIB as usize), Framing::ToClose] {
137 let mut sink = Tally::default();
138 let addr = serve(12 * MIB, framing).await;
139 let (header, n) = fetch(addr, Some(16 * MIB as usize), &mut sink).await.expect("fetched");
140 assert_eq!((n, sink.n, sink.wrong), (12 * MIB, 12 * MIB, 0));
141 // A 4 MiB chunk is passed on a read at a time, not gathered first.
142 assert!(sink.widest <= 64 * 1024, "one write carried {} bytes", sink.widest);
143 assert!(header.fields.get_one(&HeaderName::TransferEncoding).is_none());
144 }
145}
146
147#[tokio::test]
148async fn a_stated_length_past_the_limit_is_refused_before_a_byte_is_written() {
149 let mut sink = Tally::default();
150 let addr = serve(3 * MIB, Framing::Length).await;
151 let e = fetch(addr, Some(MIB as usize), &mut sink).await.expect_err("refused");
152 assert!(e.tags().contains(&ErrTag::TooBig), "{}", e);
153 assert_eq!(sink.n, 0);
154}
155
156#[tokio::test]
157async fn a_body_that_grows_past_the_limit_stops_at_it() {
158 for framing in [Framing::Chunked(100_000), Framing::ToClose] {
159 let mut sink = Tally::default();
160 let addr = serve(3 * MIB, framing).await;
161 let e = fetch(addr, Some(MIB as usize), &mut sink).await.expect_err("refused");
162 assert!(e.tags().contains(&ErrTag::TooBig), "{}", e);
163 assert!(sink.n <= MIB, "the sink took {} bytes past a limit of {}", sink.n, MIB);
164 }
165}
166
167#[tokio::test]
168async fn a_body_cut_short_is_an_error_not_a_whole_file() {
169 let mut sink = Tally::default();
170 let addr = serve(MIB, Framing::LengthShort).await;
171 let e = fetch(addr, None, &mut sink).await.expect_err("cut short");
172 assert!(e.tags().contains(&ErrTag::Missing), "{}", e);
173}
174
175#[tokio::test]
176async fn the_caller_sees_the_header_first_and_may_decline_the_body() {
177 let mut sink = Tally::default();
178 let mut stated = None;
179 let addr = serve(3 * MIB, Framing::Length).await;
180 let e = client::http_request_at_into(addr, "127.0.0.1", HttpMethod::GET, "/", &[], &[], None,
181 |h| {
182 stated = h.fields.get_one(&HeaderName::ContentLength).map(|v| fmt!("{}", v));
183 Err(err!("Declined by its stated length."; Test, TooBig))
184 },
185 &mut sink).await.expect_err("declined");
186 assert_eq!(stated.as_deref(), Some("3145728"), "{}", e);
187 assert_eq!(sink.n, 0);
188}