fix: resolve redirects without encodedPath

This commit is contained in:
yumoqing 2026-05-18 23:36:41 +08:00
parent 1f577fd137
commit 388272b4ed

View File

@ -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