From fad8968d0ab7805fc57e9325f0cc2142e1122707 Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Sun, 22 Dec 2024 15:52:29 -0500 Subject: [PATCH] Revert "Improve dummy-lock handling" This reverts commit addb9066729fdda5be277dd72b99859973226c5b. --- .../info/mechyrdia/route/ResourceWebDav.kt | 152 ++---------------- 1 file changed, 16 insertions(+), 136 deletions(-) diff --git a/src/main/kotlin/info/mechyrdia/route/ResourceWebDav.kt b/src/main/kotlin/info/mechyrdia/route/ResourceWebDav.kt index 46c5795..c33a6df 100644 --- a/src/main/kotlin/info/mechyrdia/route/ResourceWebDav.kt +++ b/src/main/kotlin/info/mechyrdia/route/ResourceWebDav.kt @@ -34,13 +34,7 @@ import io.ktor.server.routing.Route import io.ktor.server.routing.method import io.ktor.server.routing.route import io.ktor.util.AttributeKey -import kotlinx.html.a -import kotlinx.html.body -import kotlinx.html.h1 -import kotlinx.html.head -import kotlinx.html.li -import kotlinx.html.title -import kotlinx.html.ul +import kotlinx.html.* import java.net.URI import java.time.Instant import java.time.ZoneOffset @@ -75,10 +69,7 @@ sealed class WebDavProperties : XmlInsertable { resourceProps() "supportedlock" { "lockentry" { - "lockscope" { - "shared"() - "exclusive"() - } + "lockscope" { "shared"() } "locktype" { "write"() } } } @@ -365,139 +356,28 @@ suspend fun ApplicationCall.webDavDelete(path: StoragePath) { respond(HttpStatusCode.NotFound) } -private sealed interface WebDavLockInfo { - @JvmInline - value class Scalar(val value: String?) : WebDavLockInfo - - @JvmInline - value class Complex(val subTags: Map) : WebDavLockInfo -} - -private fun XmlTag.webDavLockInfo(info: WebDavLockInfo, tag: String) { - when (info) { - is WebDavLockInfo.Scalar -> { - info.value?.let { - tag { +it } - } ?: tag() - } - - is WebDavLockInfo.Complex -> { - tag { - for ((k, v) in info.subTags) - webDavLockInfo(v, k) - } - } - } -} - -private fun getXmlTagPrefix(line: String): String { - val xmlnsIndex = line.indexOf("xmlns:") - if (xmlnsIndex < 0) - return "" - - val namespace = line.substring(xmlnsIndex + 6).substringBefore('=') - return "$namespace:" -} - -private enum class XmlTagType { - SCALAR, OPEN, CLOSE -} - -private fun getXmlTagType(line: String, prefix: String): Triple? { - val (isClose, lineMinusAngleBracket) = if (line.startsWith("') - if (isClose) - return Triple(tag, XmlTagType.CLOSE, null) - - if (tag.endsWith("/")) - return Triple(tag.substring(0, tag.length - 1), XmlTagType.SCALAR, null) - - if (lineMinusPrefix.endsWith("")) - return Triple(tag, XmlTagType.SCALAR, lineMinusPrefix.substring(tag.length + 1, tag.length + prefix.length + 3)) - - return Triple(tag, XmlTagType.OPEN, null) -} - -private fun Iterator.nextOrNull(): T? = if (hasNext()) next() else null - -private fun Iterator.buildLockInfoTree(xmlTagPrefix: String? = null): Pair?, Boolean> { - val line = nextOrNull()?.trim() ?: return Pair(null, false) - if (line.isBlank() || line.startsWith(" return Pair(Pair(tagName, WebDavLockInfo.Scalar(tagValue)), true) - XmlTagType.OPEN -> { - val subTrees = buildMap { - do { - val (subTree, hasNext) = buildLockInfoTree(prefix) - subTree?.let { (k, v) -> put(k, v) } - } while (hasNext) - } - - return Pair(Pair(tagName, WebDavLockInfo.Complex(subTrees)), true) - } - - XmlTagType.CLOSE -> return Pair(null, false) - } -} - -private val defaultLockInfo: WebDavLockInfo.Complex = WebDavLockInfo.Complex( - mapOf( - "lockscope" to WebDavLockInfo.Complex( - mapOf( - "shared" to WebDavLockInfo.Scalar(null) - ) - ), - "locktype" to WebDavLockInfo.Complex( - mapOf( - "write" to WebDavLockInfo.Scalar(null) - ) - ), - "owner" to WebDavLockInfo.Scalar(null), - ) -) - -private const val InfiniteTimeout: String = "Second-31556925000" - -private suspend fun ApplicationCall.parseRequestLockInfo(): WebDavLockInfo.Complex { - val depth = request.header(HttpHeaders.Depth) ?: "Infinity" - val (requestLockInfoTag, _) = receiveText().lineSequence().iterator().buildLockInfoTree() - val requestLockInfo = requestLockInfoTag?.takeIf { it.first == "lockinfo" }?.second as? WebDavLockInfo.Complex ?: defaultLockInfo - - return WebDavLockInfo.Complex( - requestLockInfo.subTags + mapOf( - "depth" to WebDavLockInfo.Scalar(depth), - "timeout" to WebDavLockInfo.Scalar(InfiniteTimeout), - "locktoken" to WebDavLockInfo.Complex( - mapOf( - "href" to WebDavLockInfo.Scalar("opaquelocktoken:${UUID.randomUUID()}") - ) - ) - ) - ) -} - suspend fun ApplicationCall.webDavLock(path: StoragePath) { beforeWebDav() - val info = parseRequestLockInfo() + if (request.header(HttpHeaders.ContentType) != null) + receiveText() + + val depth = request.header(HttpHeaders.Depth) ?: "Infinity" respondXml { declaration() .root("prop", namespace = "DAV:") { "lockdiscovery" { - webDavLockInfo(info, "activeLock") + "activelock" { + "lockscope" { "shared"() } + "locktype" { "write"() } + "depth" { +depth } + "owner"() + "timeout" { +"Second-86400" } + "locktoken" { + "href" { +"opaquelocktoken:${UUID.randomUUID()}" } + } + } } } } -- 2.25.1