Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 38b23777 authored by Sunik Kupfer's avatar Sunik Kupfer Committed by GitHub
Browse files

Follow redirects when adding webdav mount (#1582)



* Follow redirects when adding webdav mount

Signed-off-by: default avatarSunik Kupfer <kupfer@bitfire.at>

* Update dav4jvm version

* Remove unused null

* Fix tests

* Add Kdoc

* Add Kdoc

---------

Signed-off-by: default avatarSunik Kupfer <kupfer@bitfire.at>
Co-authored-by: default avatarRicki Hirner <hirner@bitfire.at>
parent 10f6356a
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -6,11 +6,11 @@ package at.bitfire.davdroid.webdav

import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@@ -36,7 +36,7 @@ class WebDavMountRepositoryTest {
    @Test
    fun testHasWebDav_NoDavHeader() = runTest {
        web.enqueue(MockResponse().setResponseCode(200))
        assertFalse(repository.hasWebDav(url, null))
        assertNull(repository.hasWebDav(url, null))
    }

    @Test
@@ -44,7 +44,7 @@ class WebDavMountRepositoryTest {
        web.enqueue(MockResponse()
            .setResponseCode(200)
            .addHeader("DAV: 1"))
        assertTrue(repository.hasWebDav(url, null))
        assertEquals(url, repository.hasWebDav(url, null))
    }

    @Test
@@ -52,7 +52,7 @@ class WebDavMountRepositoryTest {
        web.enqueue(MockResponse()
            .setResponseCode(200)
            .addHeader("DAV: 1, 2"))
        assertTrue(repository.hasWebDav(url, null))
        assertEquals(url,repository.hasWebDav(url, null))
    }

    @Test
@@ -60,7 +60,7 @@ class WebDavMountRepositoryTest {
        web.enqueue(MockResponse()
            .setResponseCode(200)
            .addHeader("DAV: 1, 3"))
        assertTrue(repository.hasWebDav(url, null))
        assertEquals(url,repository.hasWebDav(url, null))
    }

}
 No newline at end of file
+16 −7
Original line number Diff line number Diff line
@@ -50,12 +50,13 @@ class WebDavMountRepository @Inject constructor(
        displayName: String,
        credentials: Credentials?
    ): Boolean {
        if (!hasWebDav(url, credentials))
        val webdavUrl = hasWebDav(url, credentials)
        if (webdavUrl == null)
            return false

        // create in database
        val mount = WebDavMount(
            url = url,
            url = webdavUrl,
            name = displayName
        )
        val id = db.webDavMountDao().insert(mount)
@@ -110,11 +111,19 @@ class WebDavMountRepository @Inject constructor(

    // helpers

    /**
     * Checks whether WebDAV is supported at given URL with given credentials
     * and returns the resulting if following a few redirects.
     *
     * @param url The URL to check
     * @param credentials The credentials to use for the request
     * @return The URL at which WebDAV support was found
     */
    @VisibleForTesting
    internal suspend fun hasWebDav(
        url: HttpUrl,
        credentials: Credentials?
    ): Boolean = withContext(ioDispatcher) {
    ): HttpUrl? = withContext(ioDispatcher) {
        val validVersions = arrayOf("1", "2", "3")

        val builder = httpClientBuilder.get()
@@ -125,18 +134,18 @@ class WebDavMountRepository @Inject constructor(
                getCredentials = { credentials }
            )

        var supported = false
        var webdavUrl: HttpUrl? = null
        builder.build().use { httpClient ->
            val dav = DavResource(httpClient.okHttpClient, url)
            runInterruptible {
                dav.options { davCapabilities, _ ->
                dav.options(followRedirects = true) { davCapabilities, response ->
                    if (davCapabilities.any { it in validVersions })
                        supported = true
                        webdavUrl = dav.location
                }
            }
        }

        supported
        webdavUrl
    }

}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ androidx-test-rules = "1.6.1"
androidx-test-junit = "1.2.1"
androidx-work = "2.10.2"
bitfire-cert4android = "41009d48ed"
bitfire-dav4jvm = "fda822904a"
bitfire-dav4jvm = "e22831fc52"
bitfire-synctools = "b59ceecd96"
compose-accompanist = "0.37.3"
compose-bom = "2025.07.00"