mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
Put OPTIONS handler into dedicated function
This commit is contained in:
@@ -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(
|
||||
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(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>())
|
||||
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(
|
||||
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>())
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user