use std::{
future::{ready, Future, Ready},
pin::Pin,
};
use actix_web::body::BoxBody;
use actix_web::{
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
http, Error, HttpResponse,
};
pub struct StripPathPrefix {
prefix: String,
}
impl StripPathPrefix {
pub fn new(prefix: impl Into<String>) -> Self {
let mut prefix = prefix.into();
if !prefix.starts_with('/') {
prefix.insert(0, '/');
}
if prefix.len() > 1 && prefix.ends_with('/') {
prefix.pop();
}
Self { prefix }
}
}
impl<S, B> Transform<S, ServiceRequest> for StripPathPrefix
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: actix_web::body::MessageBody,
B: 'static,
{
type Response = ServiceResponse<BoxBody>;
type Error = Error;
type InitError = ();
type Transform = StripPathPrefixMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(StripPathPrefixMiddleware {
service,
prefix: self.prefix.clone(),
}))
}
}
pub struct StripPathPrefixMiddleware<S> {
service: S,
prefix: String,
}
type LocalBoxFuture<T> = Pin<Box<dyn Future<Output = T> + 'static>>;
impl<S, B> Service<ServiceRequest> for StripPathPrefixMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
B: actix_web::body::MessageBody,
{
type Response = ServiceResponse<BoxBody>;
type Error = Error;
type Future = LocalBoxFuture<Result<Self::Response, Self::Error>>;
forward_ready!(service);
fn call(&self, mut req: ServiceRequest) -> Self::Future {
let prefix = self.prefix.clone();
if let Some(path) = req.uri().path_and_query() {
let old_uri = req.uri();
if let Some(stripped) = path.as_str().strip_prefix(&prefix) {
let new_path = if stripped.is_empty() { "/" } else { stripped };
let mut builder = http::Uri::builder();
if let Some(auth) = old_uri.authority() {
builder = builder.authority(auth.clone());
}
if let Some(s) = old_uri.scheme() {
builder = builder.scheme(s.clone());
}
builder = builder.path_and_query(new_path);
let new_uri = builder.build().unwrap();
let head = req.head_mut();
head.uri = new_uri.clone();
req.match_info_mut().get_mut().update(&new_uri);
let fut = self.service.call(req);
return Box::pin(async move {
let res = fut.await?;
Ok(res.map_into_boxed_body())
});
}
}
Box::pin(async move {
let (req, _pl) = req.into_parts();
let res = HttpResponse::NotFound().finish();
Ok(ServiceResponse::new(req, res))
})
}
}