Loading src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt +44 −17 Original line number Diff line number Diff line Loading @@ -32,6 +32,9 @@ import at.bitfire.dav4jvm.Response as DavResponse * callback will be called. Otherwise, an exception is thrown. *These callbacks * don't need to close the response.* * * To cancel a request, interrupt the thread. This will cause the requests to * throw `InterruptedException` or `InterruptedIOException`. * * @param httpClient [OkHttpClient] to access this object (must not follow redirects) * @param location location of the WebDAV resource * @param log will be used for logging Loading Loading @@ -229,22 +232,45 @@ open class DavResource @JvmOverloads constructor( * * Follows up to [MAX_REDIRECTS] redirects. * * @param accept value of Accept header (must not be null, but may be */*) * @param accept value of `Accept` header (always sent for clarity; use */* if you don't care) * @param callback called with server response unless an exception is thrown * * @throws IOException on I/O error * @throws HttpException on HTTP error * @throws DavException on HTTPS -> HTTP redirect */ @Deprecated("Use get(accept, headers, callback) with explicit Accept-Encoding instead") @Throws(IOException::class, HttpException::class) fun get(accept: String, callback: (response: Response) -> Unit) { fun get(accept: String, callback: (response: Response) -> Unit) = get(accept, Headers.headersOf("Accept-Encoding", "identity"), callback) /** * 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 * @param callback called with server response unless an exception is thrown * * @throws IOException on I/O error * @throws HttpException on HTTP error * @throws DavException on HTTPS -> HTTP redirect */ fun get(accept: String, headers: Headers?, callback: (response: Response) -> Unit) { followRedirects { httpClient.newCall(Request.Builder() val request = Request.Builder() .get() .url(location) .header("Accept", accept) .header("Accept-Encoding", "identity") // disable compression because it can change the ETag .build()).execute() if (headers != null) request.headers(headers) // always Accept header request.header("Accept", accept) httpClient.newCall(request.build()).execute() }.use { response -> checkStatus(response) callback(response) Loading @@ -252,12 +278,15 @@ open class DavResource @JvmOverloads constructor( } /** * Sends a GET request to the resource for a specific byte range. * Sends a GET request to the resource for a specific byte range. Make sure to check the * response code: servers may return the whole resource with 200 or partials with 206. * * Follows up to [MAX_REDIRECTS] redirects. * * @param accept value of `Accept` header (always sent for clarity; use */* if you don't care) * @param offset zero-based index of first byte to request * @param size number of bytes to request * @param headers additional headers to send with the request * @param callback called with server response unless an exception is thrown * * @throws IOException on I/O error Loading @@ -265,7 +294,7 @@ open class DavResource @JvmOverloads constructor( * @throws DavException on high-level errors */ @Throws(IOException::class, HttpException::class) fun getRange(offset: Long, size: Int, headers: Headers? = null, callback: (response: Response) -> Unit) { fun getRange(accept: String, offset: Long, size: Int, headers: Headers? = null, callback: (response: Response) -> Unit) { followRedirects { val request = Request.Builder() .get() Loading @@ -275,15 +304,13 @@ open class DavResource @JvmOverloads constructor( request.headers(headers) val lastIndex = offset + size - 1 request.header("Range", "bytes=$offset-$lastIndex") request .header("Accept", accept) .header("Range", "bytes=$offset-$lastIndex") httpClient.newCall(request.build()).execute() }.use { response -> checkStatus(response) if (response.code != HttpURLConnection.HTTP_PARTIAL) throw DavException("Expected 206 Partial Content, got ${response.code} ${response.message}") callback(response) } } Loading src/test/kotlin/at/bitfire/dav4jvm/DavResourceTest.kt +4 −4 Original line number Diff line number Diff line Loading @@ -214,7 +214,7 @@ class DavResourceTest { .setHeader("Content-Type", "application/x-test-result") .setBody(sampleText)) var called = false dav.get("*/*") { response -> dav.get("*/*", null) { response -> called = true assertEquals(sampleText, response.body!!.string()) Loading @@ -238,7 +238,7 @@ class DavResourceTest { .setHeader("ETag", "\"StrongETag\"") .setBody(sampleText)) called = false dav.get("*/*") { response -> dav.get("*/*", null) { response -> called = true assertEquals(sampleText, response.body!!.string()) assertEquals("StrongETag", GetETag(response.header("ETag")).eTag) Loading @@ -255,7 +255,7 @@ class DavResourceTest { .setResponseCode(HttpURLConnection.HTTP_OK) .setBody(sampleText)) called = false dav.get("*/*") { response -> dav.get("*/*", null) { response -> called = true assertNull(response.header("ETag")) } Loading @@ -270,7 +270,7 @@ class DavResourceTest { mockServer.enqueue(MockResponse() .setResponseCode(HttpURLConnection.HTTP_PARTIAL)) var called = false dav.getRange(100, 342) { response -> dav.getRange("*/*", 100, 342) { response -> assertEquals("bytes=100-441", response.request.header("Range")) called = true } Loading Loading
src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt +44 −17 Original line number Diff line number Diff line Loading @@ -32,6 +32,9 @@ import at.bitfire.dav4jvm.Response as DavResponse * callback will be called. Otherwise, an exception is thrown. *These callbacks * don't need to close the response.* * * To cancel a request, interrupt the thread. This will cause the requests to * throw `InterruptedException` or `InterruptedIOException`. * * @param httpClient [OkHttpClient] to access this object (must not follow redirects) * @param location location of the WebDAV resource * @param log will be used for logging Loading Loading @@ -229,22 +232,45 @@ open class DavResource @JvmOverloads constructor( * * Follows up to [MAX_REDIRECTS] redirects. * * @param accept value of Accept header (must not be null, but may be */*) * @param accept value of `Accept` header (always sent for clarity; use */* if you don't care) * @param callback called with server response unless an exception is thrown * * @throws IOException on I/O error * @throws HttpException on HTTP error * @throws DavException on HTTPS -> HTTP redirect */ @Deprecated("Use get(accept, headers, callback) with explicit Accept-Encoding instead") @Throws(IOException::class, HttpException::class) fun get(accept: String, callback: (response: Response) -> Unit) { fun get(accept: String, callback: (response: Response) -> Unit) = get(accept, Headers.headersOf("Accept-Encoding", "identity"), callback) /** * 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 * @param callback called with server response unless an exception is thrown * * @throws IOException on I/O error * @throws HttpException on HTTP error * @throws DavException on HTTPS -> HTTP redirect */ fun get(accept: String, headers: Headers?, callback: (response: Response) -> Unit) { followRedirects { httpClient.newCall(Request.Builder() val request = Request.Builder() .get() .url(location) .header("Accept", accept) .header("Accept-Encoding", "identity") // disable compression because it can change the ETag .build()).execute() if (headers != null) request.headers(headers) // always Accept header request.header("Accept", accept) httpClient.newCall(request.build()).execute() }.use { response -> checkStatus(response) callback(response) Loading @@ -252,12 +278,15 @@ open class DavResource @JvmOverloads constructor( } /** * Sends a GET request to the resource for a specific byte range. * Sends a GET request to the resource for a specific byte range. Make sure to check the * response code: servers may return the whole resource with 200 or partials with 206. * * Follows up to [MAX_REDIRECTS] redirects. * * @param accept value of `Accept` header (always sent for clarity; use */* if you don't care) * @param offset zero-based index of first byte to request * @param size number of bytes to request * @param headers additional headers to send with the request * @param callback called with server response unless an exception is thrown * * @throws IOException on I/O error Loading @@ -265,7 +294,7 @@ open class DavResource @JvmOverloads constructor( * @throws DavException on high-level errors */ @Throws(IOException::class, HttpException::class) fun getRange(offset: Long, size: Int, headers: Headers? = null, callback: (response: Response) -> Unit) { fun getRange(accept: String, offset: Long, size: Int, headers: Headers? = null, callback: (response: Response) -> Unit) { followRedirects { val request = Request.Builder() .get() Loading @@ -275,15 +304,13 @@ open class DavResource @JvmOverloads constructor( request.headers(headers) val lastIndex = offset + size - 1 request.header("Range", "bytes=$offset-$lastIndex") request .header("Accept", accept) .header("Range", "bytes=$offset-$lastIndex") httpClient.newCall(request.build()).execute() }.use { response -> checkStatus(response) if (response.code != HttpURLConnection.HTTP_PARTIAL) throw DavException("Expected 206 Partial Content, got ${response.code} ${response.message}") callback(response) } } Loading
src/test/kotlin/at/bitfire/dav4jvm/DavResourceTest.kt +4 −4 Original line number Diff line number Diff line Loading @@ -214,7 +214,7 @@ class DavResourceTest { .setHeader("Content-Type", "application/x-test-result") .setBody(sampleText)) var called = false dav.get("*/*") { response -> dav.get("*/*", null) { response -> called = true assertEquals(sampleText, response.body!!.string()) Loading @@ -238,7 +238,7 @@ class DavResourceTest { .setHeader("ETag", "\"StrongETag\"") .setBody(sampleText)) called = false dav.get("*/*") { response -> dav.get("*/*", null) { response -> called = true assertEquals(sampleText, response.body!!.string()) assertEquals("StrongETag", GetETag(response.header("ETag")).eTag) Loading @@ -255,7 +255,7 @@ class DavResourceTest { .setResponseCode(HttpURLConnection.HTTP_OK) .setBody(sampleText)) called = false dav.get("*/*") { response -> dav.get("*/*", null) { response -> called = true assertNull(response.header("ETag")) } Loading @@ -270,7 +270,7 @@ class DavResourceTest { mockServer.enqueue(MockResponse() .setResponseCode(HttpURLConnection.HTTP_PARTIAL)) var called = false dav.getRange(100, 342) { response -> dav.getRange("*/*", 100, 342) { response -> assertEquals("bytes=100-441", response.request.header("Range")) called = true } Loading