Keep the OPTIONS answer when the home set's type can't be read (#180)

This commit is contained in:
makiolaj committed 2026-11-25 12:31:13 +01:00
1 parent 5f308b47c5
commit 74d6be61f2
2 files changed
+20 -11

No files matched your search

@@ -2,6 +2,7 @@ package de.jeanlucmakiola.caldav
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.Response
import at.bitfire.dav4jvm.XmlUtils
import at.bitfire.dav4jvm.XmlUtils.insertTag
import at.bitfire.dav4jvm.property.ResourceType
@@ -69,7 +70,10 @@ sealed interface CollectionOutcome {
*/
interface CollectionAdmin {
/** What [homeSet] supports, as an OPTIONS rather than a guess. */
/**
* What [homeSet] supports, as an OPTIONS rather than a guess, plus a
* PROPFIND ruling out a home set that is itself a calendar.
*/
fun support(homeSet: HttpUrl): CollectionSupport
/**
@@ -123,7 +127,7 @@ class DavCollectionAdmin(
mkCalendar = "MKCALENDAR" in allow,
extendedMkCol = "EXTENDED-MKCOL" in dav,
)
}.takeIf { !it.canCreate || isCalendar(homeSet) == false } ?: CollectionSupport.NONE
}.takeIf { !it.canCreate || !isCalendar(homeSet) } ?: CollectionSupport.NONE
} catch (_: IOException) {
// Unknown reads as "no", which costs the user an affordance they can
// reach again on the next attempt — the other way round costs them a
@@ -133,16 +137,21 @@ class DavCollectionAdmin(
/**
* A home set that is itself a calendar, as on all-inkl.com (#180), cannot
* hold another. Null when the server did not say, which reads as no.
* hold another. A probe that fails leaves the OPTIONS answer standing, so
* other servers behave as before.
*/
private fun isCalendar(homeSet: HttpUrl): Boolean? {
var types: Set<Property.Name>? = null
private fun isCalendar(homeSet: HttpUrl): Boolean {
var calendar = false
runCatching {
DavResource(httpClient, homeSet).propfind(0, ResourceType.NAME) { response, _ ->
response[ResourceType::class.java]?.let { types = it.types }
DavResource(httpClient, homeSet).propfind(0, ResourceType.NAME) { response, relation ->
if (relation == Response.HrefRelation.SELF &&
response[ResourceType::class.java]?.types?.contains(ResourceType.CALENDAR) == true
) {
calendar = true
}
}
}.onFailure { return null }
return types?.contains(ResourceType.CALENDAR)
}
return calendar
}
/**
@@ -64,7 +64,7 @@ class CollectionAdminTest {
}
@Test
fun `a home set whose type cannot be read offers nothing`() {
fun `a home set whose type cannot be read keeps the OPTIONS answer`() {
server.enqueue(
MockResponse()
.setResponseCode(200)
@@ -73,7 +73,7 @@ class CollectionAdminTest {
)
server.enqueue(MockResponse().setResponseCode(500))
assertThat(admin.support(homeSet())).isEqualTo(CollectionSupport.NONE)
assertThat(admin.support(homeSet()).mkCalendar).isTrue()
}
private fun resourceType(types: String) = MockResponse()