aboutsummaryrefslogtreecommitdiffstats
path: root/src/errors.rs
diff options
context:
space:
mode:
authorSven-Hendrik Haase <svenstaro@gmail.com>2025-03-07 02:41:10 +0000
committerSven-Hendrik Haase <svenstaro@gmail.com>2025-03-07 02:41:10 +0000
commit83dcdf729465e3961a541a3375b0917401631c4a (patch)
treee72750534dce74e7f0fb66243785e3205561c1b2 /src/errors.rs
parentUpgrade to Rust 2024 edition (diff)
downloadminiserve-83dcdf729465e3961a541a3375b0917401631c4a.tar.gz
miniserve-83dcdf729465e3961a541a3375b0917401631c4a.zip
Modernize actix-web middleware usage
Diffstat (limited to 'src/errors.rs')
-rw-r--r--src/errors.rs64
1 files changed, 28 insertions, 36 deletions
diff --git a/src/errors.rs b/src/errors.rs
index f1e5b2d..e35e8a8 100644
--- a/src/errors.rs
+++ b/src/errors.rs
@@ -1,14 +1,15 @@
+use std::str::FromStr;
+
use actix_web::{
- body::{BoxBody, MessageBody},
- dev::{ResponseHead, Service, ServiceRequest, ServiceResponse},
- http::{header, StatusCode},
HttpRequest, HttpResponse, ResponseError,
+ body::{BoxBody, MessageBody},
+ dev::{ResponseHead, ServiceRequest, ServiceResponse},
+ http::{StatusCode, header},
+ middleware::Next,
};
-use futures::prelude::*;
-use std::str::FromStr;
use thiserror::Error;
-use crate::{renderer::render_error, MiniserveConfig};
+use crate::{MiniserveConfig, renderer::render_error};
#[derive(Debug, Error)]
pub enum StartupError {
@@ -122,37 +123,28 @@ impl ResponseError for RuntimeError {
}
/// Middleware to convert plain-text error responses to user-friendly web pages
-pub fn error_page_middleware<S, B>(
+pub async fn error_page_middleware(
req: ServiceRequest,
- srv: &S,
-) -> impl Future<Output = actix_web::Result<ServiceResponse>> + 'static + use<S, B>
-where
- S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
- B: MessageBody + 'static,
- S::Future: 'static,
-{
- let fut = srv.call(req);
-
- async {
- let res = fut.await?.map_into_boxed_body();
-
- if (res.status().is_client_error() || res.status().is_server_error())
- && res.request().path() != "/upload"
- && res
- .headers()
- .get(header::CONTENT_TYPE)
- .map(AsRef::as_ref)
- .and_then(|s| std::str::from_utf8(s).ok())
- .and_then(|s| mime::Mime::from_str(s).ok())
- .as_ref()
- .map(mime::Mime::essence_str)
- == Some(mime::TEXT_PLAIN.as_ref())
- {
- let req = res.request().clone();
- Ok(res.map_body(|head, body| map_error_page(&req, head, body)))
- } else {
- Ok(res)
- }
+ next: Next<impl MessageBody + 'static>,
+) -> Result<ServiceResponse<impl MessageBody>, actix_web::Error> {
+ let res = next.call(req).await?.map_into_boxed_body();
+
+ if (res.status().is_client_error() || res.status().is_server_error())
+ && res.request().path() != "/upload"
+ && res
+ .headers()
+ .get(header::CONTENT_TYPE)
+ .map(AsRef::as_ref)
+ .and_then(|s| std::str::from_utf8(s).ok())
+ .and_then(|s| mime::Mime::from_str(s).ok())
+ .as_ref()
+ .map(mime::Mime::essence_str)
+ == Some(mime::TEXT_PLAIN.as_ref())
+ {
+ let req = res.request().clone();
+ Ok(res.map_body(|head, body| map_error_page(&req, head, body)))
+ } else {
+ Ok(res)
}
}