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::HttpResponse;
use actix_web::body::BoxBody;
use actix_web::dev::{HttpServiceFactory, ServiceResponse}; use actix_web::dev::{HttpServiceFactory, ServiceResponse};
use actix_web::http::header::{self, HeaderName, HeaderValue}; use actix_web::http::header::{self, HeaderName, HeaderValue};
use actix_web::http::{Method, StatusCode}; use actix_web::http::{Method, StatusCode};
@@ -24,6 +25,32 @@ mod subscription;
pub use error::Error; pub use error::Error;
/// 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 {
let mut response = HttpResponse::Ok();
response.insert_header((
HeaderName::from_static("dav"),
// https://datatracker.ietf.org/doc/html/rfc4918#section-18
HeaderValue::from_static(
"1, 3, access-control, calendar-access, extended-mkcol, calendar-no-timezone, webdav-push",
),
));
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()
} else {
res.map_into_left_body()
},
))
})
}
pub fn caldav_service< pub fn caldav_service<
AP: AuthenticationProvider, AP: AuthenticationProvider,
AS: AddressbookStore, AS: AddressbookStore,
@@ -38,62 +65,65 @@ pub fn caldav_service<
let birthday_store = Arc::new(ContactBirthdayStore::new(addr_store)); let birthday_store = Arc::new(ContactBirthdayStore::new(addr_store));
web::scope("") web::scope("")
.wrap(AuthenticationMiddleware::new(auth_provider.clone())) .wrap(AuthenticationMiddleware::new(auth_provider.clone()))
.wrap( .wrap(options_handler())
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| { .app_data(Data::from(store.clone()))
Ok(ErrorHandlerResponse::Response( .app_data(Data::from(birthday_store.clone()))
if res.request().method() == Method::OPTIONS { .app_data(Data::from(subscription_store))
let mut response = HttpResponse::Ok(); .service(RootResourceService::<PrincipalResource, User>::default().actix_resource())
response.insert_header(( .service(
HeaderName::from_static("dav"), web::scope("/principal").service(
// https://datatracker.ietf.org/doc/html/rfc4918#section-18 web::scope("/{principal}")
HeaderValue::from_static( .service(
"1, 3, access-control, calendar-access, extended-mkcol, calendar-no-timezone, webdav-push", PrincipalResourceService {
), auth_provider,
)); home_set: &[("calendar", false), ("birthdays", true)],
}
if let Some(allow) = res.headers().get(header::ALLOW) { .actix_resource()
response.insert_header((header::ALLOW, allow.to_owned())); .name(PrincipalResource::route_name()),
} )
ServiceResponse::new(res.into_parts().0, response.finish()).map_into_right_body() .service(
} else { web::scope("/calendar")
res.map_into_left_body() .service(
}, CalendarSetResourceService::new(store.clone()).actix_resource(),
)) )
}),
)
.app_data(Data::from(store.clone()))
.app_data(Data::from(birthday_store.clone()))
.app_data(Data::from(subscription_store))
.service(RootResourceService::<PrincipalResource, User>::default().actix_resource())
.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( .service(
web::scope("/{calendar_id}") web::scope("/{calendar_id}")
.service( .service(ResourceServiceRoute(
ResourceServiceRoute(CalendarResourceService::<_, S>::new(store.clone())) CalendarResourceService::<_, S>::new(store.clone()),
)
.service(web::scope("/{object_id}.ics").service(CalendarObjectResourceService::new(store.clone()).actix_resource()
)) ))
.service(
web::scope("/{object_id}.ics").service(
CalendarObjectResourceService::new(store.clone())
.actix_resource(),
),
),
),
)
.service(
web::scope("/birthdays")
.service(
CalendarSetResourceService::new(birthday_store.clone())
.actix_resource(),
) )
)
.service(web::scope("/birthdays")
.service(CalendarSetResourceService::new(birthday_store.clone()).actix_resource())
.service( .service(
web::scope("/{calendar_id}") web::scope("/{calendar_id}")
.service( .service(ResourceServiceRoute(
ResourceServiceRoute(CalendarResourceService::<_, S>::new(birthday_store.clone())) CalendarResourceService::<_, S>::new(
) birthday_store.clone(),
.service(web::scope("/{object_id}.ics").service(CalendarObjectResourceService::new(birthday_store.clone()).actix_resource() ),
)) ))
) .service(
) web::scope("/{object_id}.ics").service(
), CalendarObjectResourceService::new(
).service(subscription_resource::<S>()) birthday_store.clone(),
)
.actix_resource(),
),
),
),
),
),
)
.service(subscription_resource::<S>())
} }

View File

@@ -1,5 +1,6 @@
use actix_web::{ use actix_web::{
HttpResponse, HttpResponse,
body::BoxBody,
dev::{HttpServiceFactory, ServiceResponse}, dev::{HttpServiceFactory, ServiceResponse},
http::{ http::{
Method, StatusCode, Method, StatusCode,
@@ -25,6 +26,32 @@ pub mod addressbook;
pub mod error; pub mod error;
pub mod principal; pub mod principal;
/// 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 {
let mut response = HttpResponse::Ok();
response.insert_header((
HeaderName::from_static("dav"),
// https://datatracker.ietf.org/doc/html/rfc4918#section-18
HeaderValue::from_static(
"1, 3, access-control, addressbook, extended-mkcol, webdav-push",
),
));
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()
} else {
res.map_into_left_body()
},
))
})
}
pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: SubscriptionStore>( pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: SubscriptionStore>(
auth_provider: Arc<AP>, auth_provider: Arc<AP>,
store: Arc<A>, store: Arc<A>,
@@ -32,30 +59,7 @@ pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: Subsc
) -> impl HttpServiceFactory { ) -> impl HttpServiceFactory {
web::scope("") web::scope("")
.wrap(AuthenticationMiddleware::new(auth_provider.clone())) .wrap(AuthenticationMiddleware::new(auth_provider.clone()))
.wrap( .wrap(options_handler())
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| {
Ok(ErrorHandlerResponse::Response(
if res.request().method() == Method::OPTIONS {
let mut response = HttpResponse::Ok();
response.insert_header((
HeaderName::from_static("dav"),
// https://datatracker.ietf.org/doc/html/rfc4918#section-18
HeaderValue::from_static(
"1, 3, access-control, addressbook, extended-mkcol, webdav-push",
),
));
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()
} else {
res.map_into_left_body()
},
))
}),
)
.app_data(Data::from(store.clone())) .app_data(Data::from(store.clone()))
.app_data(Data::from(subscription_store)) .app_data(Data::from(subscription_store))
.service(RootResourceService::<PrincipalResource, User>::default().actix_resource()) .service(RootResourceService::<PrincipalResource, User>::default().actix_resource())