mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
refactor OPTIONS handling
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
use actix_web::{
|
||||
dev::Service,
|
||||
dev::ServiceResponse,
|
||||
http::{
|
||||
header::{HeaderName, HeaderValue},
|
||||
Method, StatusCode,
|
||||
},
|
||||
middleware::{ErrorHandlerResponse, ErrorHandlers},
|
||||
web::{self, Data},
|
||||
HttpResponse,
|
||||
};
|
||||
use address_object::resource::AddressObjectResourceService;
|
||||
use addressbook::resource::AddressbookResourceService;
|
||||
pub use error::Error;
|
||||
use futures_util::FutureExt;
|
||||
use principal::{PrincipalResource, PrincipalResourceService};
|
||||
use rustical_dav::resource::ResourceService;
|
||||
use rustical_dav::resources::RootResourceService;
|
||||
@@ -36,28 +37,25 @@ pub fn configure_dav<AP: AuthenticationProvider, A: AddressbookStore + ?Sized>(
|
||||
cfg.service(
|
||||
web::scope("")
|
||||
.wrap(AuthenticationMiddleware::new(auth_provider))
|
||||
.wrap_fn(|req, srv| {
|
||||
// Middleware to set the DAV header
|
||||
// Could be more elegant if actix_web::guard::RegisteredMethods was public :(
|
||||
let method = req.method().clone();
|
||||
srv.call(req).map(move |res| {
|
||||
if method == Method::OPTIONS {
|
||||
return res.map(|mut response| {
|
||||
if response.status() == StatusCode::METHOD_NOT_ALLOWED {
|
||||
response.headers_mut().insert(
|
||||
.wrap(
|
||||
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| {
|
||||
Ok(ErrorHandlerResponse::Response(
|
||||
if res.request().method() == Method::OPTIONS {
|
||||
let response = HttpResponse::Ok()
|
||||
.insert_header((
|
||||
HeaderName::from_static("dav"),
|
||||
HeaderValue::from_static(
|
||||
"1, 2, 3, access-control, addressbook, extended-mkcol",
|
||||
),
|
||||
);
|
||||
*response.response_mut().status_mut() = StatusCode::OK;
|
||||
}
|
||||
response
|
||||
});
|
||||
}
|
||||
res
|
||||
})
|
||||
})
|
||||
))
|
||||
.finish();
|
||||
ServiceResponse::new(res.into_parts().0, response).map_into_right_body()
|
||||
} else {
|
||||
res.map_into_left_body()
|
||||
},
|
||||
))
|
||||
}),
|
||||
)
|
||||
.app_data(Data::from(store.clone()))
|
||||
.service(RootResourceService::<PrincipalResource>::actix_resource())
|
||||
.service(
|
||||
|
||||
Reference in New Issue
Block a user