Skip to content

Commit 6bd22ce

Browse files
committed
Add support for following redirects in OPTIONS request
1 parent 494b979 commit 6bd22ce

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/main/kotlin/at/bitfire/dav4jvm/DavResource.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,30 @@ open class DavResource @JvmOverloads constructor(
151151

152152
/**
153153
* Sends an OPTIONS request to this resource without HTTP compression (because some servers have
154-
* broken compression for OPTIONS). Doesn't follow redirects.
154+
* broken compression for OPTIONS). Follows up to [MAX_REDIRECTS] redirects when set.
155155
*
156+
* @param followRedirects whether redirects should be followed (default: *false*)
156157
* @param callback called with server response unless an exception is thrown
157158
*
158159
* @throws IOException on I/O error
159160
* @throws HttpException on HTTP error
160161
* @throws DavException on HTTPS -> HTTP redirect
161162
*/
162163
@Throws(IOException::class, HttpException::class)
163-
fun options(callback: CapabilitiesCallback) {
164-
httpClient.newCall(Request.Builder()
164+
fun options(followRedirects: Boolean = false, callback: CapabilitiesCallback) {
165+
val callBlock = {
166+
httpClient.newCall(Request.Builder()
165167
.method("OPTIONS", null)
166168
.header("Content-Length", "0")
167169
.url(location)
168170
.header("Accept-Encoding", "identity") // disable compression
169-
.build()).execute().use { response ->
171+
.build()).execute()
172+
}
173+
val response = if (followRedirects)
174+
followRedirects(callBlock)
175+
else
176+
callBlock()
177+
response.use {
170178
checkStatus(response)
171179
callback.onCapabilities(
172180
HttpUtils.listHeader(response, "DAV").map { it.trim() }.toSet(),

0 commit comments

Comments
 (0)