1use std::sync::Arc;
23#[derive(Clone)]
4pub(crate) struct OnInformational(Arc<dyn OnInformationalCallback + Send + Sync>);
56/// Add a callback for 1xx informational responses.
7///
8/// # Example
9///
10/// ```
11/// # let some_body = ();
12/// let mut req = hyper::Request::new(some_body);
13///
14/// hyper::ext::on_informational(&mut req, |res| {
15/// println!("informational: {:?}", res.status());
16/// });
17///
18/// // send request on a client connection...
19/// ```
20pub fn on_informational<B, F>(req: &mut http::Request<B>, callback: F)
21where
22F: Fn(Response<'_>) + Send + Sync + 'static,
23{
24 on_informational_raw(req, OnInformationalClosure(callback));
25}
2627pub(crate) fn on_informational_raw<B, C>(req: &mut http::Request<B>, callback: C)
28where
29C: OnInformationalCallback + Send + Sync + 'static,
30{
31 req.extensions_mut()
32 .insert(OnInformational(Arc::new(callback)));
33}
3435// Sealed, not actually nameable bounds
36pub(crate) trait OnInformationalCallback {
37fn on_informational(&self, res: http::Response<()>);
38}
3940impl OnInformational {
41pub(crate) fn call(&self, res: http::Response<()>) {
42self.0.on_informational(res);
43 }
44}
4546struct OnInformationalClosure<F>(F);
4748impl<F> OnInformationalCallback for OnInformationalClosure<F>
49where
50F: Fn(Response<'_>) + Send + Sync + 'static,
51{
52fn on_informational(&self, res: http::Response<()>) {
53let res = Response(&res);
54 (self.0)(res);
55 }
56}
5758// A facade over http::Response.
59//
60// It purposefully hides being able to move the response out of the closure,
61// while also not being able to expect it to be a reference `&Response`.
62// (Otherwise, a closure can be written as `|res: &_|`, and then be broken if
63// we make the closure take ownership.)
64//
65// With the type not being nameable, we could change from being a facade to
66// being either a real reference, or moving the http::Response into the closure,
67// in a backwards-compatible change in the future.
68#[derive(Debug)]
69pub struct Response<'a>(&'a http::Response<()>);
7071impl Response<'_> {
72#[inline]
73pub fn status(&self) -> http::StatusCode {
74self.0.status()
75 }
7677#[inline]
78pub fn version(&self) -> http::Version {
79self.0.version()
80 }
8182#[inline]
83pub fn headers(&self) -> &http::HeaderMap {
84self.0.headers()
85 }
86}