Skip to content

Commit

Permalink
MKCOL: append trailing slash, allow other MKCOL HTTP methods (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 authored Dec 7, 2023
1 parent 1ed89c1 commit 4669474
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4669474

Please sign in to comment.