1use std::{io, time::Duration};
2use std::sync::Arc;
3use std::sync::atomic::{AtomicBool, Ordering};
4use std::task::{Poll, Context};
5use std::pin::Pin;
6
7use bytes::{Bytes, BytesMut};
8use pin_project_lite::pin_project;
9use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
10use tokio::time::{sleep, Sleep};
11
12use futures::stream::Stream;
13use futures::future::{self, Future, FutureExt};
14
15pin_project! {
16 pub struct ReaderStream<R> {
17 #[pin]
18 reader: Option<R>,
19 buf: BytesMut,
20 cap: usize,
21 }
22}
23
24impl<R: AsyncRead> Stream for ReaderStream<R> {
25 type Item = std::io::Result<Bytes>;
26
27 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
28 use tokio_util::io::poll_read_buf;
29
30 let mut this = self.as_mut().project();
31
32 let reader = match this.reader.as_pin_mut() {
33 Some(r) => r,
34 None => return Poll::Ready(None),
35 };
36
37 if this.buf.capacity() == 0 {
38 this.buf.reserve(*this.cap);
39 }
40
41 match poll_read_buf(reader, cx, &mut this.buf) {
42 Poll::Pending => Poll::Pending,
43 Poll::Ready(Err(err)) => {
44 self.project().reader.set(None);
45 Poll::Ready(Some(Err(err)))
46 }
47 Poll::Ready(Ok(0)) => {
48 self.project().reader.set(None);
49 Poll::Ready(None)
50 }
51 Poll::Ready(Ok(_)) => {
52 let chunk = this.buf.split();
53 Poll::Ready(Some(Ok(chunk.freeze())))
54 }
55 }
56 }
57}
58
59pub trait AsyncReadExt: AsyncRead + Sized {
60 fn into_bytes_stream(self, cap: usize) -> ReaderStream<Self> {
61 ReaderStream { reader: Some(self), cap, buf: BytesMut::with_capacity(cap) }
62 }
63}
64
65impl<T: AsyncRead> AsyncReadExt for T { }
66
67pub trait PollExt<T, E> {
68 fn map_err_ext<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
69 where F: FnOnce(E) -> U;
70}
71
72impl<T, E> PollExt<T, E> for Poll<Option<Result<T, E>>> {
73 fn map_err_ext<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
75 where F: FnOnce(E) -> U
76 {
77 match self {
78 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(t))),
79 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(f(e)))),
80 Poll::Ready(None) => Poll::Ready(None),
81 Poll::Pending => Poll::Pending,
82 }
83 }
84}
85
86pin_project! {
87 #[must_use = "streams do nothing unless polled"]
89 pub struct Chain<T, U> {
90 #[pin]
91 first: T,
92 #[pin]
93 second: U,
94 done_first: bool,
95 }
96}
97
98impl<T: AsyncRead, U: AsyncRead> Chain<T, U> {
99 pub(crate) fn new(first: T, second: U) -> Self {
100 Self { first, second, done_first: false }
101 }
102}
103
104impl<T: AsyncRead, U: AsyncRead> Chain<T, U> {
105 pub fn get_ref(&self) -> (&T, &U) {
107 (&self.first, &self.second)
108 }
109}
110
111impl<T: AsyncRead, U: AsyncRead> AsyncRead for Chain<T, U> {
112 fn poll_read(
113 self: Pin<&mut Self>,
114 cx: &mut Context<'_>,
115 buf: &mut ReadBuf<'_>,
116 ) -> Poll<io::Result<()>> {
117 let me = self.project();
118
119 if !*me.done_first {
120 let init_rem = buf.remaining();
121 futures::ready!(me.first.poll_read(cx, buf))?;
122 if buf.remaining() == init_rem {
123 *me.done_first = true;
124 } else {
125 return Poll::Ready(Ok(()));
126 }
127 }
128 me.second.poll_read(cx, buf)
129 }
130}
131
132enum State {
133 Active,
135 Grace(Pin<Box<Sleep>>),
137 Mercy(Pin<Box<Sleep>>),
140}
141
142pin_project! {
143 #[must_use = "futures do nothing unless polled"]
145 pub struct CancellableIo<F, I> {
146 #[pin]
147 io: Option<I>,
148 #[pin]
149 trigger: future::Fuse<F>,
150 state: State,
151 grace: Duration,
152 mercy: Duration,
153 }
154}
155
156impl<F: Future, I: AsyncWrite> CancellableIo<F, I> {
157 pub fn new(trigger: F, io: I, grace: Duration, mercy: Duration) -> Self {
158 CancellableIo {
159 grace, mercy,
160 io: Some(io),
161 trigger: trigger.fuse(),
162 state: State::Active,
163 }
164 }
165
166 pub fn io(&self) -> Option<&I> {
167 self.io.as_ref()
168 }
169
170 fn poll_trigger_then<T>(
172 mut self: Pin<&mut Self>,
173 cx: &mut Context<'_>,
174 do_io: impl FnOnce(Pin<&mut I>, &mut Context<'_>) -> Poll<io::Result<T>>,
175 ) -> Poll<io::Result<T>> {
176 let mut me = self.as_mut().project();
177 let io = match me.io.as_pin_mut() {
178 Some(io) => io,
179 None => return Poll::Ready(Err(gone())),
180 };
181
182 loop {
183 match me.state {
184 State::Active => {
185 if me.trigger.as_mut().poll(cx).is_ready() {
186 *me.state = State::Grace(Box::pin(sleep(*me.grace)));
187 } else {
188 return do_io(io, cx);
189 }
190 }
191 State::Grace(timer) => {
192 if timer.as_mut().poll(cx).is_ready() {
193 *me.state = State::Mercy(Box::pin(sleep(*me.mercy)));
194 } else {
195 return do_io(io, cx);
196 }
197 }
198 State::Mercy(timer) => {
199 if timer.as_mut().poll(cx).is_ready() {
200 self.project().io.set(None);
201 return Poll::Ready(Err(time_out()));
202 } else {
203 let result = futures::ready!(io.poll_shutdown(cx));
204 self.project().io.set(None);
205 return match result {
206 Err(e) => Poll::Ready(Err(e)),
207 Ok(()) => Poll::Ready(Err(gone()))
208 };
209 }
210 },
211 }
212 }
213 }
214}
215
216fn time_out() -> io::Error {
217 io::Error::new(io::ErrorKind::TimedOut, "Shutdown grace timed out")
218}
219
220fn gone() -> io::Error {
221 io::Error::new(io::ErrorKind::BrokenPipe, "IO driver has terminated")
222}
223
224impl<F: Future, I: AsyncRead + AsyncWrite> AsyncRead for CancellableIo<F, I> {
225 fn poll_read(
226 mut self: Pin<&mut Self>,
227 cx: &mut Context<'_>,
228 buf: &mut ReadBuf<'_>,
229 ) -> Poll<io::Result<()>> {
230 self.as_mut().poll_trigger_then(cx, |io, cx| io.poll_read(cx, buf))
231 }
232}
233
234impl<F: Future, I: AsyncWrite> AsyncWrite for CancellableIo<F, I> {
235 fn poll_write(
236 mut self: Pin<&mut Self>,
237 cx: &mut Context<'_>,
238 buf: &[u8],
239 ) -> Poll<io::Result<usize>> {
240 self.as_mut().poll_trigger_then(cx, |io, cx| io.poll_write(cx, buf))
241 }
242
243 fn poll_flush(
244 mut self: Pin<&mut Self>,
245 cx: &mut Context<'_>
246 ) -> Poll<io::Result<()>> {
247 self.as_mut().poll_trigger_then(cx, |io, cx| io.poll_flush(cx))
248 }
249
250 fn poll_shutdown(
251 mut self: Pin<&mut Self>,
252 cx: &mut Context<'_>
253 ) -> Poll<io::Result<()>> {
254 self.as_mut().poll_trigger_then(cx, |io, cx| io.poll_shutdown(cx))
255 }
256
257 fn poll_write_vectored(
258 mut self: Pin<&mut Self>,
259 cx: &mut Context<'_>,
260 bufs: &[io::IoSlice<'_>],
261 ) -> Poll<io::Result<usize>> {
262 self.as_mut().poll_trigger_then(cx, |io, cx| io.poll_write_vectored(cx, bufs))
263 }
264
265 fn is_write_vectored(&self) -> bool {
266 self.io().map(|io| io.is_write_vectored()).unwrap_or(false)
267 }
268}
269
270pin_project! {
271 #[must_use = "futures do nothing unless polled"]
284 pub struct LingeringIo<I> {
285 #[pin]
286 io: I,
287 linger: Duration,
288 upgraded: Arc<AtomicBool>,
289 lingering: Option<Pin<Box<Sleep>>>,
290 }
291}
292
293impl<I> LingeringIo<I> {
294 pub fn new(io: I, linger: Duration, upgraded: Arc<AtomicBool>) -> Self {
295 LingeringIo { io, linger, upgraded, lingering: None }
296 }
297}
298
299impl<I: AsyncRead> AsyncRead for LingeringIo<I> {
300 fn poll_read(
301 self: Pin<&mut Self>,
302 cx: &mut Context<'_>,
303 buf: &mut ReadBuf<'_>,
304 ) -> Poll<io::Result<()>> {
305 self.project().io.poll_read(cx, buf)
306 }
307}
308
309impl<I: AsyncRead + AsyncWrite> AsyncWrite for LingeringIo<I> {
310 fn poll_write(
311 self: Pin<&mut Self>,
312 cx: &mut Context<'_>,
313 buf: &[u8],
314 ) -> Poll<io::Result<usize>> {
315 self.project().io.poll_write(cx, buf)
316 }
317
318 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
319 if self.lingering.is_some() {
323 return Poll::Ready(Ok(()));
324 }
325
326 self.project().io.poll_flush(cx)
327 }
328
329 fn poll_shutdown(
330 mut self: Pin<&mut Self>,
331 cx: &mut Context<'_>
332 ) -> Poll<io::Result<()>> {
333 if self.lingering.is_none() {
334 let me = self.as_mut().project();
335 futures::ready!(me.io.poll_shutdown(cx))?;
336 if me.linger.is_zero() || me.upgraded.load(Ordering::Acquire) {
337 return Poll::Ready(Ok(()));
338 }
339
340 *me.lingering = Some(Box::pin(sleep(*me.linger)));
341 }
342
343 let mut buf = [0; 4096];
344 loop {
345 let me = self.as_mut().project();
346 if me.lingering.as_mut().map_or(true, |timer| timer.as_mut().poll(cx).is_ready()) {
347 return Poll::Ready(Ok(()));
348 }
349
350 let mut buf = ReadBuf::new(&mut buf);
351 match futures::ready!(me.io.poll_read(cx, &mut buf)) {
352 Ok(()) if !buf.filled().is_empty() => continue,
353 _ => return Poll::Ready(Ok(())),
354 }
355 }
356 }
357
358 fn poll_write_vectored(
359 self: Pin<&mut Self>,
360 cx: &mut Context<'_>,
361 bufs: &[io::IoSlice<'_>],
362 ) -> Poll<io::Result<usize>> {
363 self.project().io.poll_write_vectored(cx, bufs)
364 }
365
366 fn is_write_vectored(&self) -> bool {
367 self.io.is_write_vectored()
368 }
369}
370
371use crate::http::private::{Listener, Connection, Certificates};
372
373impl<F: Future, C: Connection> Connection for CancellableIo<F, C> {
374 fn peer_address(&self) -> Option<std::net::SocketAddr> {
375 self.io().and_then(|io| io.peer_address())
376 }
377
378 fn peer_certificates(&self) -> Option<Certificates> {
379 self.io().and_then(|io| io.peer_certificates())
380 }
381
382 fn enable_nodelay(&self) -> io::Result<()> {
383 match self.io() {
384 Some(io) => io.enable_nodelay(),
385 None => Ok(())
386 }
387 }
388}
389
390pin_project! {
391 pub struct CancellableListener<F, L> {
392 pub trigger: F,
393 #[pin]
394 pub listener: L,
395 pub grace: Duration,
396 pub mercy: Duration,
397 }
398}
399
400impl<F, L> CancellableListener<F, L> {
401 pub fn new(trigger: F, listener: L, grace: u64, mercy: u64) -> Self {
402 let (grace, mercy) = (Duration::from_secs(grace), Duration::from_secs(mercy));
403 CancellableListener { trigger, listener, grace, mercy }
404 }
405}
406
407impl<L: Listener, F: Future + Clone> Listener for CancellableListener<F, L> {
408 type Connection = CancellableIo<F, L::Connection>;
409
410 fn local_addr(&self) -> Option<std::net::SocketAddr> {
411 self.listener.local_addr()
412 }
413
414 fn poll_accept(
415 mut self: Pin<&mut Self>,
416 cx: &mut Context<'_>
417 ) -> Poll<io::Result<Self::Connection>> {
418 self.as_mut().project().listener
419 .poll_accept(cx)
420 .map(|res| res.map(|conn| {
421 CancellableIo::new(self.trigger.clone(), conn, self.grace, self.mercy)
422 }))
423 }
424}
425
426pub trait StreamExt: Sized + Stream {
427 fn join<U>(self, other: U) -> Join<Self, U>
428 where U: Stream<Item = Self::Item>;
429}
430
431impl<S: Stream> StreamExt for S {
432 fn join<U>(self, other: U) -> Join<Self, U>
433 where U: Stream<Item = Self::Item>
434 {
435 Join::new(self, other)
436 }
437}
438
439pin_project! {
440 pub struct Join<T, U> {
442 #[pin]
443 a: T,
444 #[pin]
445 b: U,
446 toggle: bool,
448 done: bool,
450 }
451}
452
453impl<T, U> Join<T, U> {
454 pub(super) fn new(a: T, b: U) -> Join<T, U>
455 where T: Stream, U: Stream,
456 {
457 Join { a, b, toggle: false, done: false, }
458 }
459
460 fn poll_next<A: Stream, B: Stream<Item = A::Item>>(
461 first: Pin<&mut A>,
462 second: Pin<&mut B>,
463 done: &mut bool,
464 cx: &mut Context<'_>,
465 ) -> Poll<Option<A::Item>> {
466 match first.poll_next(cx) {
467 Poll::Ready(opt) => { *done = opt.is_none(); Poll::Ready(opt) }
468 Poll::Pending => match second.poll_next(cx) {
469 Poll::Ready(opt) => { *done = opt.is_none(); Poll::Ready(opt) }
470 Poll::Pending => Poll::Pending
471 }
472 }
473 }
474}
475
476impl<T, U> Stream for Join<T, U>
477 where T: Stream,
478 U: Stream<Item = T::Item>,
479{
480 type Item = T::Item;
481
482 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T::Item>> {
483 if self.done {
484 return Poll::Ready(None);
485 }
486
487 let me = self.project();
488 *me.toggle = !*me.toggle;
489 match *me.toggle {
490 true => Self::poll_next(me.a, me.b, me.done, cx),
491 false => Self::poll_next(me.b, me.a, me.done, cx),
492 }
493 }
494
495 fn size_hint(&self) -> (usize, Option<usize>) {
496 let (left_low, left_high) = self.a.size_hint();
497 let (right_low, right_high) = self.b.size_hint();
498
499 let low = left_low.saturating_add(right_low);
500 let high = match (left_high, right_high) {
501 (Some(h1), Some(h2)) => h1.checked_add(h2),
502 _ => None,
503 };
504
505 (low, high)
506 }
507}