Skip to content

Commit

Permalink
Merge branch 'main' into push-draft1
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 authored Apr 8, 2024
2 parents 731ca44 + f428759 commit d3664ad
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
61 changes: 41 additions & 20 deletions src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,21 @@ open class DavResource @JvmOverloads constructor(
* Updates [location] on success.
*
* @param destination where the resource shall be moved to
* @param forceOverride whether resources are overwritten when they already exist in destination
* @param overwrite whether resources are overwritten when they already exist in destination
*
* @throws IOException on I/O error
* @throws HttpException on HTTP error
* @throws DavException on WebDAV error or HTTPS -> HTTP redirect
*/
@Throws(IOException::class, HttpException::class, DavException::class)
fun move(destination: HttpUrl, forceOverride: Boolean, callback: ResponseCallback) {
fun move(destination: HttpUrl, overwrite: Boolean, callback: ResponseCallback) {
val requestBuilder = Request.Builder()
.method("MOVE", null)
.header("Content-Length", "0")
.header("Destination", destination.toString())

if (forceOverride) requestBuilder.header("Overwrite", "F")
if (!overwrite) // RFC 4918 9.9.3 and 10.6, default value: T
requestBuilder.header("Overwrite", "F")

followRedirects {
requestBuilder.url(location)
Expand Down Expand Up @@ -216,20 +217,21 @@ open class DavResource @JvmOverloads constructor(
* Sends a COPY request for this resource. Follows up to [MAX_REDIRECTS] redirects.
*
* @param destination where the resource shall be copied to
* @param forceOverride whether resources are overwritten when they already exist in destination
* @param overwrite whether resources are overwritten when they already exist in destination
*
* @throws IOException on I/O error
* @throws HttpException on HTTP error
* @throws DavException on WebDAV error or HTTPS -> HTTP redirect
*/
@Throws(IOException::class, HttpException::class, DavException::class)
fun copy(destination:HttpUrl, forceOverride: Boolean, callback: ResponseCallback) {
fun copy(destination:HttpUrl, overwrite: Boolean, callback: ResponseCallback) {
val requestBuilder = Request.Builder()
.method("COPY", null)
.header("Content-Length", "0")
.header("Destination", destination.toString())

if (forceOverride) requestBuilder.header("Overwrite", "F")
if (!overwrite) // RFC 4918 9.9.3 and 10.6, default value: T
requestBuilder.header("Overwrite", "F")

followRedirects {
requestBuilder.url(location)
Expand Down Expand Up @@ -302,6 +304,36 @@ open class DavResource @JvmOverloads constructor(
}
}

/**
* Sends a GET request to the resource. Follows up to [MAX_REDIRECTS] redirects.
*
* Note: Add `Accept-Encoding: identity` to [headers] if you want to disable compression
* (compression might change the returned ETag).
*
* @param accept value of `Accept` header (always sent for clarity; use */* if you don't care)
* @param headers additional headers to send with the request
*
* @return okhttp Response – **caller is responsible for closing it!**
*
* @throws IOException on I/O error
* @throws HttpException on HTTP error
* @throws DavException on HTTPS -> HTTP redirect
*/
fun get(accept: String, headers: Headers?): Response =
followRedirects {
val request = Request.Builder()
.get()
.url(location)

if (headers != null)
request.headers(headers)

// always Accept header
request.header("Accept", accept)

httpClient.newCall(request.build()).execute()
}

/**
* Sends a GET request to the resource. Sends `Accept-Encoding: identity` to disable
* compression, because compression might change the ETag.
Expand All @@ -317,8 +349,9 @@ open class DavResource @JvmOverloads constructor(
*/
@Deprecated("Use get(accept, headers, callback) with explicit Accept-Encoding instead")
@Throws(IOException::class, HttpException::class)
fun get(accept: String, callback: ResponseCallback) =
fun get(accept: String, callback: ResponseCallback) {
get(accept, Headers.headersOf("Accept-Encoding", "identity"), callback)
}

/**
* Sends a GET request to the resource. Follows up to [MAX_REDIRECTS] redirects.
Expand All @@ -335,19 +368,7 @@ open class DavResource @JvmOverloads constructor(
* @throws DavException on HTTPS -> HTTP redirect
*/
fun get(accept: String, headers: Headers?, callback: ResponseCallback) {
followRedirects {
val request = Request.Builder()
.get()
.url(location)

if (headers != null)
request.headers(headers)

// always Accept header
request.header("Accept", accept)

httpClient.newCall(request.build()).execute()
}.use { response ->
get(accept, headers).use { response ->
checkStatus(response)
callback.onResponse(response)
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/at/bitfire/dav4jvm/DavResourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class DavResourceTest {
assertEquals("COPY", rq.method)
assertEquals(url.encodedPath, rq.path)
assertEquals(destination.toString(), rq.getHeader("Destination"))
assertNull(rq.getHeader("Overwrite"))
assertEquals("F", rq.getHeader("Overwrite"))

// no preconditions, 204 No content, resource successfully copied to a preexisting
// destination resource
Expand All @@ -95,7 +95,7 @@ class DavResourceTest {
assertEquals("COPY", rq.method)
assertEquals(url.encodedPath, rq.path)
assertEquals(destination.toString(), rq.getHeader("Destination"))
assertEquals("F", rq.getHeader("Overwrite"))
assertNull(rq.getHeader("Overwrite"))

/* NEGATIVE TEST CASES */

Expand Down Expand Up @@ -333,7 +333,7 @@ class DavResourceTest {
assertEquals("MOVE", rq.method)
assertEquals(url.encodedPath, rq.path)
assertEquals(destination.toString(), rq.getHeader("Destination"))
assertNull(rq.getHeader("Overwrite"))
assertEquals("F", rq.getHeader("Overwrite"))

// no preconditions, 204 No content, URL already mapped, overwrite
mockServer.enqueue(MockResponse()
Expand All @@ -351,7 +351,7 @@ class DavResourceTest {
assertEquals("MOVE", rq.method)
assertEquals(url.encodedPath, rq.path)
assertEquals(destination.toString(), rq.getHeader("Destination"))
assertEquals("F", rq.getHeader("Overwrite"))
assertNull(rq.getHeader("Overwrite"))

/* NEGATIVE TEST CASES */

Expand Down

0 comments on commit d3664ad

Please sign in to comment.