mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 08:12:24 +00:00
Put OPTIONS handler into dedicated function
This commit is contained in:
@@ -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,22 +25,9 @@ mod subscription;
|
|||||||
|
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
|
|
||||||
pub fn caldav_service<
|
/// Quite a janky implementation but the default METHOD_NOT_ALLOWED response gives us the allowed
|
||||||
AP: AuthenticationProvider,
|
/// methods of a resource
|
||||||
AS: AddressbookStore,
|
fn options_handler() -> ErrorHandlers<BoxBody> {
|
||||||
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(
|
|
||||||
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| {
|
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| {
|
||||||
Ok(ErrorHandlerResponse::Response(
|
Ok(ErrorHandlerResponse::Response(
|
||||||
if res.request().method() == Method::OPTIONS {
|
if res.request().method() == Method::OPTIONS {
|
||||||
@@ -60,8 +48,25 @@ pub fn caldav_service<
|
|||||||
res.map_into_left_body()
|
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(store.clone()))
|
||||||
.app_data(Data::from(birthday_store.clone()))
|
.app_data(Data::from(birthday_store.clone()))
|
||||||
.app_data(Data::from(subscription_store))
|
.app_data(Data::from(subscription_store))
|
||||||
@@ -69,31 +74,56 @@ pub fn caldav_service<
|
|||||||
.service(
|
.service(
|
||||||
web::scope("/principal").service(
|
web::scope("/principal").service(
|
||||||
web::scope("/{principal}")
|
web::scope("/{principal}")
|
||||||
.service(PrincipalResourceService{auth_provider, home_set: &[
|
.service(
|
||||||
("calendar", false), ("birthdays", true)
|
PrincipalResourceService {
|
||||||
]}.actix_resource().name(PrincipalResource::route_name()))
|
auth_provider,
|
||||||
.service(web::scope("/calendar")
|
home_set: &[("calendar", false), ("birthdays", true)],
|
||||||
.service(CalendarSetResourceService::new(store.clone()).actix_resource())
|
}
|
||||||
|
.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("/birthdays")
|
|
||||||
.service(CalendarSetResourceService::new(birthday_store.clone()).actix_resource())
|
|
||||||
.service(
|
.service(
|
||||||
web::scope("/{calendar_id}")
|
web::scope("/{object_id}.ics").service(
|
||||||
.service(
|
CalendarObjectResourceService::new(store.clone())
|
||||||
ResourceServiceRoute(CalendarResourceService::<_, S>::new(birthday_store.clone()))
|
.actix_resource(),
|
||||||
)
|
|
||||||
.service(web::scope("/{object_id}.ics").service(CalendarObjectResourceService::new(birthday_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>())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,14 +26,9 @@ pub mod addressbook;
|
|||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod principal;
|
pub mod principal;
|
||||||
|
|
||||||
pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: SubscriptionStore>(
|
/// Quite a janky implementation but the default METHOD_NOT_ALLOWED response gives us the allowed
|
||||||
auth_provider: Arc<AP>,
|
/// methods of a resource
|
||||||
store: Arc<A>,
|
fn options_handler() -> ErrorHandlers<BoxBody> {
|
||||||
subscription_store: Arc<S>,
|
|
||||||
) -> impl HttpServiceFactory {
|
|
||||||
web::scope("")
|
|
||||||
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
|
|
||||||
.wrap(
|
|
||||||
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| {
|
ErrorHandlers::new().handler(StatusCode::METHOD_NOT_ALLOWED, |res| {
|
||||||
Ok(ErrorHandlerResponse::Response(
|
Ok(ErrorHandlerResponse::Response(
|
||||||
if res.request().method() == Method::OPTIONS {
|
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) {
|
if let Some(allow) = res.headers().get(header::ALLOW) {
|
||||||
response.insert_header((header::ALLOW, allow.to_owned()));
|
response.insert_header((header::ALLOW, allow.to_owned()));
|
||||||
}
|
}
|
||||||
ServiceResponse::new(res.into_parts().0, response.finish())
|
ServiceResponse::new(res.into_parts().0, response.finish()).map_into_right_body()
|
||||||
.map_into_right_body()
|
|
||||||
} else {
|
} else {
|
||||||
res.map_into_left_body()
|
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(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())
|
||||||
|
|||||||
Reference in New Issue
Block a user