Foundations(iOS Sourceset): Provide implementation for Rfc3986 on iOS, using the Swift Rfc3986UriBridge.
This commit is contained in:
@@ -20,12 +20,33 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.quartz.utils
|
package com.vitorpamplona.quartz.utils
|
||||||
|
|
||||||
|
import kotlinx.cinterop.ExperimentalForeignApi
|
||||||
|
import platform.Foundation.NSURLComponents
|
||||||
|
import swiftbridge.Rfc3986UriBridge
|
||||||
|
|
||||||
|
@OptIn(ExperimentalForeignApi::class)
|
||||||
actual object Rfc3986 {
|
actual object Rfc3986 {
|
||||||
actual fun normalize(uri: String): String = TODO()
|
private val rfc3986UriBridge = Rfc3986UriBridge()
|
||||||
|
|
||||||
actual fun isValidUrl(url: String): Boolean = TODO()
|
actual fun normalize(uri: String): String = rfc3986UriBridge.normalizeUrlWithUrl(uri, null) ?: throw Exception("Could not normalize URL.")
|
||||||
|
|
||||||
actual fun normalizeAndRemoveFragment(url: String): String = TODO()
|
actual fun isValidUrl(url: String): Boolean = rfc3986UriBridge.isUrlValidWithUrl(url)
|
||||||
|
|
||||||
actual fun host(url: String): String = TODO()
|
actual fun normalizeAndRemoveFragment(url: String): String =
|
||||||
|
NSURLComponents(url)
|
||||||
|
.toStringNoFragment()
|
||||||
|
.internIfPossible()
|
||||||
|
|
||||||
|
actual fun host(url: String): String = rfc3986UriBridge.hostFromUriWithUrl(url, null) ?: throw Exception("Could not retrieve host from URL.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun NSURLComponents.toStringNoFragment(): String {
|
||||||
|
val sb = StringBuilder()
|
||||||
|
|
||||||
|
if (scheme != null) sb.append(scheme).append(":")
|
||||||
|
if (host != null) sb.append("//").append(host.toString())
|
||||||
|
if (path != null) sb.append(path)
|
||||||
|
if (query != null) sb.append("?").append(query)
|
||||||
|
|
||||||
|
return sb.toString()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
//
|
||||||
|
// Created by NullDev on 31/12/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import RFC_3986
|
||||||
|
|
||||||
|
@objcMembers public class Rfc3986UriBridge: NSObject {
|
||||||
|
public func normalizeUrl(url: String) throws -> String {
|
||||||
|
let uri = try RFC_3986.URI(url)
|
||||||
|
let normalized = uri.normalized()
|
||||||
|
return normalized.value
|
||||||
|
}
|
||||||
|
|
||||||
|
public func isUrlValid(url: String) -> Bool {
|
||||||
|
return RFC_3986.isValidURI(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func hostFromUri(url: String) throws -> String {
|
||||||
|
let actualUri = try RFC_3986.URI(url)
|
||||||
|
return actualUri.host!
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user