From 4669474c328b4c59dd52c15a79ff089e9c54f971 Mon Sep 17 00:00:00 2001 From: Ricki Hirner Date: Thu, 7 Dec 2023 12:33:18 +0100 Subject: [PATCH] MKCOL: append trailing slash, allow other MKCOL HTTP methods (#31) --- .../kotlin/at/bitfire/dav4jvm/DavResource.kt | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt b/src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt index 9bba5d95..f59c6580 100644 --- a/src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt +++ b/src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt @@ -8,10 +8,21 @@ package at.bitfire.dav4jvm import at.bitfire.dav4jvm.XmlUtils.insertTag import at.bitfire.dav4jvm.XmlUtils.propertyName -import at.bitfire.dav4jvm.exception.* +import at.bitfire.dav4jvm.exception.ConflictException +import at.bitfire.dav4jvm.exception.DavException +import at.bitfire.dav4jvm.exception.ForbiddenException +import at.bitfire.dav4jvm.exception.HttpException +import at.bitfire.dav4jvm.exception.NotFoundException +import at.bitfire.dav4jvm.exception.PreconditionFailedException +import at.bitfire.dav4jvm.exception.ServiceUnavailableException +import at.bitfire.dav4jvm.exception.UnauthorizedException import at.bitfire.dav4jvm.property.SyncToken -import okhttp3.* +import okhttp3.Headers +import okhttp3.HttpUrl import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response import org.xmlpull.v1.XmlPullParser @@ -237,19 +248,25 @@ open class DavResource @JvmOverloads constructor( /** * Sends a MKCOL request to this resource. Follows up to [MAX_REDIRECTS] redirects. + * Because the target [location] is by definition a collection, a trailing slash + * is appended (unless [location] already has a trailing slash). + * + * @param xmlBody optional request body (used for MKCALENDAR or Extended MKCOL) + * @param method HTTP MKCOL method (`MKCOL` by default, may for instance be `MKCALENDAR`) + * @param callback called for the response * * @throws IOException on I/O error * @throws HttpException on HTTP error * @throws DavException on HTTPS -> HTTP redirect */ @Throws(IOException::class, HttpException::class) - fun mkCol(xmlBody: String?, callback: ResponseCallback) { + fun mkCol(xmlBody: String?, method: String = "MKCOL", callback: ResponseCallback) { val rqBody = xmlBody?.toRequestBody(MIME_XML) followRedirects { httpClient.newCall(Request.Builder() - .method("MKCOL", rqBody) - .url(location) + .method(method, rqBody) + .url(UrlUtils.withTrailingSlash(location)) .build()).execute() }.use { response -> checkStatus(response)