Hyper v1 middleware: early abort?

Hello,
I would like to be able to abort early in a Hyper v1 middleware. Below is a very simplified skeleton of my issue:

#[derive(Debug, Clone)]

pub struct Auth<S> {
    inner: S,
}

impl<S> Auth<S> {
    pub fn new(inner: S) -> Self {
        Auth { inner }
    }
}

type Req = Request<Incoming>;

impl<S> Service<Req> for Auth<S>
where
    S: Service<Req>,
{
    type Error = S::Error;
    type Future = S::Future;
    type Response = S::Response;

    fn call(&self, mut req: Req) -> Self::Future {
        match req.headers_mut().entry("Auth") {
            http::header::Entry::Occupied(occupied_entry) => {
                // Do things and keep going
                self.inner.call(req)
            }
            http::header::Entry::Vacant(vacant_entry) => {
                // Early return with a reponse. i don't want to redirect the user to a login
                // page. My client handles this situation already and present the user with a floating window.

                let response = Response::builder()
                    .status(StatusCode::FORBIDDEN)
                    .body(Full::new(Bytes::from("Forbidden: No credentials given.")))
                    .unwrap();

                // What is the type to return here?
            }
        }
    }
}

But as you can see in the code above, I don't know which type or how to return my answer. Any help?

Currently it's "whatever type S uses", which is not specific enough to support your early return workflow. You'd need to add some constraints on S::Response, like maybe where S: Service<Req, Response = Response<Full>>.

1 Like

Got it right!

impl<S> Service<Req> for Auth<S>
where
        S: Service<Req, Response = Response<Full<Bytes>>, Error = hyper::Error, Future = Ready<Result<Response<Full<Bytes>>, hyper::Error>>>

Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.