UrlUtils.kt 1.99 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright © Ricki Hirner (bitfire web engineering).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

9
package at.bitfire.dav4android
Ricki Hirner's avatar
Ricki Hirner committed
10 11 12 13 14

import okhttp3.HttpUrl
import java.net.URI
import java.net.URISyntaxException

Ricki Hirner's avatar
Ricki Hirner committed
15
object UrlUtils {
Ricki Hirner's avatar
Ricki Hirner committed
16

Ricki Hirner's avatar
Ricki Hirner committed
17 18 19 20 21
    fun equals(url1: HttpUrl, url2: HttpUrl): Boolean {
        // if okhttp thinks the two URLs are equal, they're in any case
        // (and it's a simple String comparison)
        if (url1 == url2)
            return true
Ricki Hirner's avatar
Ricki Hirner committed
22

Ricki Hirner's avatar
Ricki Hirner committed
23 24 25 26 27 28 29 30
        val uri1 = url1.uri()
        val uri2 = url2.uri()
        try {
            val decoded1 = URI(uri1.scheme, uri1.schemeSpecificPart, uri1.fragment)
            val decoded2 = URI (uri2.scheme, uri2.schemeSpecificPart, uri2.fragment)
            return decoded1 == decoded2
        } catch (e: URISyntaxException) {
            return false
Ricki Hirner's avatar
Ricki Hirner committed
31
        }
Ricki Hirner's avatar
Ricki Hirner committed
32
    }
Ricki Hirner's avatar
Ricki Hirner committed
33

Ricki Hirner's avatar
Ricki Hirner committed
34 35 36
    fun hostToDomain(host: String?): String? {
        if (host == null)
            return null
Ricki Hirner's avatar
Ricki Hirner committed
37

Ricki Hirner's avatar
Ricki Hirner committed
38 39 40
        // remove optional dot at end
        @Suppress("NAME_SHADOWING")
        val host = host.removeSuffix(".")
Ricki Hirner's avatar
Ricki Hirner committed
41

Ricki Hirner's avatar
Ricki Hirner committed
42 43 44 45 46 47 48
        // split into labels
        val labels = host.split('.')
        return if (labels.size >= 2) {
            labels[labels.size - 2] + "." + labels[labels.size - 1]
        } else
            host
    }
Ricki Hirner's avatar
Ricki Hirner committed
49

Ricki Hirner's avatar
Ricki Hirner committed
50 51 52
    fun omitTrailingSlash(url: HttpUrl): HttpUrl {
        val idxLast = url.pathSize () - 1
        val hasTrailingSlash = url.pathSegments()[idxLast] == ""
Ricki Hirner's avatar
Ricki Hirner committed
53

Ricki Hirner's avatar
Ricki Hirner committed
54 55 56 57 58
        return if (hasTrailingSlash)
            url.newBuilder().removePathSegment(idxLast).build()
        else
            url
    }
Ricki Hirner's avatar
Ricki Hirner committed
59

Ricki Hirner's avatar
Ricki Hirner committed
60 61 62
    fun withTrailingSlash(url: HttpUrl): HttpUrl {
        val idxLast = url.pathSize() - 1
        val hasTrailingSlash = url.pathSegments()[idxLast] == ""
Ricki Hirner's avatar
Ricki Hirner committed
63

Ricki Hirner's avatar
Ricki Hirner committed
64 65 66 67
        return if (hasTrailingSlash)
            url
        else
            url.newBuilder().addPathSegment("").build()
Ricki Hirner's avatar
Ricki Hirner committed
68
    }
Ricki Hirner's avatar
Ricki Hirner committed
69

Ricki Hirner's avatar
Ricki Hirner committed
70
}