Hyper v1 middleware: early abort?

⚓ Rust    📅 2025-09-02    👤 surdeus    👁️ 4      

surdeus

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?

1 post - 1 participant

Read full topic

🏷️ Rust_feed