1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! [`Future`] types.

use crate::Cookies;
use futures_util::ready;
use http::{header, HeaderValue, Response};
use pin_project_lite::pin_project;
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

pin_project! {
    /// Response future for [`CookieManager`].
    #[derive(Debug)]
    pub struct ResponseFuture<F> {
        #[pin]
        pub(crate) future: F,
        pub(crate) cookies: Cookies,
    }
}

impl<F, ResBody, E> Future for ResponseFuture<F>
where
    F: Future<Output = Result<Response<ResBody>, E>>,
{
    type Output = F::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        let mut res = ready!(this.future.poll(cx)?);

        let mut cookies = this.cookies.inner.lock();
        if cookies.changed {
            let values: Vec<_> = cookies
                .jar()
                .delta()
                .filter_map(|c| HeaderValue::from_str(&c.to_string()).ok())
                .collect();
            let headers = res.headers_mut();
            for value in values {
                headers.append(header::SET_COOKIE, value);
            }
        }

        Poll::Ready(Ok(res))
    }
}