fix: resolve redirects without encodedPath
This commit is contained in:
parent
1f577fd137
commit
388272b4ed
@ -1,8 +1,6 @@
|
|||||||
package com.bricks.mp.actions
|
package com.bricks.mp.actions
|
||||||
|
|
||||||
import com.bricks.mp.core.*
|
import com.bricks.mp.core.*
|
||||||
import io.ktor.http.URLBuilder
|
|
||||||
import io.ktor.http.takeFrom
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.serialization.json.JsonPrimitive
|
import kotlinx.serialization.json.JsonPrimitive
|
||||||
@ -136,15 +134,33 @@ class ActionDispatcher(
|
|||||||
|
|
||||||
private fun resolveRedirectUrl(requestUrl: String, location: String): String {
|
private fun resolveRedirectUrl(requestUrl: String, location: String): String {
|
||||||
if (location.startsWith("http://") || location.startsWith("https://")) return location
|
if (location.startsWith("http://") || location.startsWith("https://")) return location
|
||||||
val base = URLBuilder().takeFrom(requestUrl)
|
val origin = requestUrl.originPart()
|
||||||
return if (location.startsWith("/")) {
|
return if (location.startsWith("/")) {
|
||||||
"${base.protocol.name}://${base.host}${if (base.port == base.protocol.defaultPort) "" else ":${base.port}"}$location"
|
origin + location
|
||||||
} else {
|
} else {
|
||||||
val parentPath = base.encodedPath.substringBeforeLast('/', missingDelimiterValue = "")
|
val parentPath = requestUrl.pathPart().substringBeforeLast('/', missingDelimiterValue = "")
|
||||||
"${base.protocol.name}://${base.host}${if (base.port == base.protocol.defaultPort) "" else ":${base.port}"}$parentPath/$location"
|
"$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) {
|
private fun handleMethod(bind: BricksBind) {
|
||||||
val methodName = bind.methodname ?: bind.target ?: return
|
val methodName = bind.methodname ?: bind.target ?: return
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user