diff --git a/shared/src/commonMain/kotlin/com/bricks/mp/actions/ActionDispatcher.kt b/shared/src/commonMain/kotlin/com/bricks/mp/actions/ActionDispatcher.kt index 8084a54..7b588c1 100644 --- a/shared/src/commonMain/kotlin/com/bricks/mp/actions/ActionDispatcher.kt +++ b/shared/src/commonMain/kotlin/com/bricks/mp/actions/ActionDispatcher.kt @@ -1,8 +1,6 @@ package com.bricks.mp.actions import com.bricks.mp.core.* -import io.ktor.http.URLBuilder -import io.ktor.http.takeFrom import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import kotlinx.serialization.json.JsonPrimitive @@ -136,15 +134,33 @@ class ActionDispatcher( private fun resolveRedirectUrl(requestUrl: String, location: String): String { if (location.startsWith("http://") || location.startsWith("https://")) return location - val base = URLBuilder().takeFrom(requestUrl) + val origin = requestUrl.originPart() return if (location.startsWith("/")) { - "${base.protocol.name}://${base.host}${if (base.port == base.protocol.defaultPort) "" else ":${base.port}"}$location" + origin + location } else { - val parentPath = base.encodedPath.substringBeforeLast('/', missingDelimiterValue = "") - "${base.protocol.name}://${base.host}${if (base.port == base.protocol.defaultPort) "" else ":${base.port}"}$parentPath/$location" + val parentPath = requestUrl.pathPart().substringBeforeLast('/', missingDelimiterValue = "") + "$origin$parentPath/$location" } } + private fun String.originPart(): String { + val schemeEnd = indexOf("://") + if (schemeEnd < 0) return "" + val authorityStart = schemeEnd + 3 + val authorityEnd = indexOf('/', startIndex = authorityStart).let { if (it < 0) length else it } + return substring(0, authorityEnd) + } + + private fun String.pathPart(): String { + val schemeEnd = indexOf("://") + val pathStart = if (schemeEnd >= 0) { + indexOf('/', startIndex = schemeEnd + 3).let { if (it < 0) return "" else it } + } else { + 0 + } + return substring(pathStart).substringBefore('?').substringBefore('#') + } + private fun handleMethod(bind: BricksBind) { val methodName = bind.methodname ?: bind.target ?: return