Put OPTIONS handler into dedicated function

This commit is contained in:
Lennart
2025-05-10 11:37:28 +02:00
parent de6ccdc37b
commit d14ded7179
2 changed files with 109 additions and 75 deletions

View File

@@ -1,4 +1,5 @@
use actix_web::HttpResponse;
use actix_web::body::BoxBody;
use actix_web::dev::{HttpServiceFactory, ServiceResponse};
use actix_web::http::header::{self, HeaderName, HeaderValue};
use actix_web::http::{Method, StatusCode};
@@ -24,22 +25,9 @@ mod subscription;
pub use error::Error;
pub fn caldav_service<
AP: AuthenticationProvider,
AS: AddressbookStore,
C: CalendarStore,
S: SubscriptionStore,
>(
auth_provider: Arc<AP>,
store: Arc<C>,
addr_store: Arc<AS>,
subscription_store: Arc<S>,
) -> impl HttpServiceFactory {
let birthday_store = Arc::new(ContactBirthdayStore::new(addr_store));
web::scope("")
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
.wrap(
/// Quite a janky implementation but the default METHOD_NOT_ALLOWED response gives us the allowed
/// methods of a resource
fn options_handler() -> ErrorHandlers<BoxBody> {
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| {
Ok(ErrorHandlerResponse::Response(
if res.request().method() == Method::OPTIONS {
@@ -60,8 +48,25 @@ pub fn caldav_service<
res.map_into_left_body()
},
))
}),
)
})
}
pub fn caldav_service<
AP: AuthenticationProvider,
AS: AddressbookStore,
C: CalendarStore,
S: SubscriptionStore,
>(
auth_provider: Arc<AP>,
store: Arc<C>,
addr_store: Arc<AS>,
subscription_store: Arc<S>,
) -> impl HttpServiceFactory {
let birthday_store = Arc::new(ContactBirthdayStore::new(addr_store));
web::scope("")
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
.wrap(options_handler())
.app_data(Data::from(store.clone()))
.app_data(Data::from(birthday_store.clone()))
.app_data(Data::from(subscription_store))
@@ -69,31 +74,56 @@ pub fn caldav_service<
.service(
web::scope("/principal").service(
web::scope("/{principal}")
.service(PrincipalResourceService{auth_provider, home_set: &[
("calendar", false), ("birthdays", true)
]}.actix_resource().name(PrincipalResource::route_name()))
.service(web::scope("/calendar")
.service(CalendarSetResourceService::new(store.clone()).actix_resource())
.service(
PrincipalResourceService {
auth_provider,
home_set: &[("calendar", false), ("birthdays", true)],
}
.actix_resource()
.name(PrincipalResource::route_name()),
)
.service(
web::scope("/calendar")
.service(
CalendarSetResourceService::new(store.clone()).actix_resource(),
)
.service(
web::scope("/{calendar_id}")
.service(
ResourceServiceRoute(CalendarResourceService::<_, S>::new(store.clone()))
)
.service(web::scope("/{object_id}.ics").service(CalendarObjectResourceService::new(store.clone()).actix_resource()
.service(ResourceServiceRoute(
CalendarResourceService::<_, S>::new(store.clone()),
))
)
)
.service(web::scope("/birthdays")
.service(CalendarSetResourceService::new(birthday_store.clone()).actix_resource())
.service(
web::scope("/{calendar_id}")
.service(
ResourceServiceRoute(CalendarResourceService::<_, S>::new(birthday_store.clone()))
)
.service(web::scope("/{object_id}.ics").service(CalendarObjectResourceService::new(birthday_store.clone()).actix_resource()
))
)
)
web::scope("/{object_id}.ics").service(
CalendarObjectResourceService::new(store.clone())
.actix_resource(),
),
).service(subscription_resource::<S>())
),
),
)
.service(
web::scope("/birthdays")
.service(
CalendarSetResourceService::new(birthday_store.clone())
.actix_resource(),
)
.service(
web::scope("/{calendar_id}")
.service(ResourceServiceRoute(
CalendarResourceService::<_, S>::new(
birthday_store.clone(),
),
))
.service(
web::scope("/{object_id}.ics").service(
CalendarObjectResourceService::new(
birthday_store.clone(),
)
.actix_resource(),
),
),
),
),
),
)
.service(subscription_resource::<S>())
}

View File

@@ -1,5 +1,6 @@
use actix_web::{
HttpResponse,
body::BoxBody,
dev::{HttpServiceFactory, ServiceResponse},
http::{
Method, StatusCode,
@@ -25,14 +26,9 @@ pub mod addressbook;
pub mod error;
pub mod principal;
pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: SubscriptionStore>(
auth_provider: Arc<AP>,
store: Arc<A>,
subscription_store: Arc<S>,
) -> impl HttpServiceFactory {
web::scope("")
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
.wrap(
/// Quite a janky implementation but the default METHOD_NOT_ALLOWED response gives us the allowed
/// methods of a resource
fn options_handler() -> ErrorHandlers<BoxBody> {
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| {
Ok(ErrorHandlerResponse::Response(
if res.request().method() == Method::OPTIONS {
@@ -48,14 +44,22 @@ pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: Subsc
if let Some(allow) = res.headers().get(header::ALLOW) {
response.insert_header((header::ALLOW, allow.to_owned()));
}
ServiceResponse::new(res.into_parts().0, response.finish())
.map_into_right_body()
ServiceResponse::new(res.into_parts().0, response.finish()).map_into_right_body()
} else {
res.map_into_left_body()
},
))
}),
)
})
}
pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: SubscriptionStore>(
auth_provider: Arc<AP>,
store: Arc<A>,
subscription_store: Arc<S>,
) -> impl HttpServiceFactory {
web::scope("")
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
.wrap(options_handler())
.app_data(Data::from(store.clone()))
.app_data(Data::from(subscription_store))
.service(RootResourceService::<PrincipalResource, User>::default().actix_resource())