fix redirect infinite loop (#4857)
This commit is contained in:
@@ -12,7 +12,9 @@ import java.net.IDN
|
|||||||
import java.net.Inet6Address
|
import java.net.Inet6Address
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
|
import java.net.MalformedURLException
|
||||||
import java.net.Proxy
|
import java.net.Proxy
|
||||||
|
import java.net.URI
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
object HttpUtil {
|
object HttpUtil {
|
||||||
@@ -140,7 +142,7 @@ object HttpUtil {
|
|||||||
val responseCode = conn.responseCode
|
val responseCode = conn.responseCode
|
||||||
when (responseCode) {
|
when (responseCode) {
|
||||||
in 300..399 -> {
|
in 300..399 -> {
|
||||||
val location = conn.getHeaderField("Location")
|
val location = resolveLocation(conn)
|
||||||
conn.disconnect()
|
conn.disconnect()
|
||||||
if (location.isNullOrEmpty()) {
|
if (location.isNullOrEmpty()) {
|
||||||
throw IOException("Redirect location not found")
|
throw IOException("Redirect location not found")
|
||||||
@@ -219,5 +221,29 @@ object HttpUtil {
|
|||||||
}
|
}
|
||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns absolute URL string location header sets
|
||||||
|
fun resolveLocation(conn: HttpURLConnection): String? {
|
||||||
|
val raw = conn.getHeaderField("Location")?.trim()?.takeIf { it.isNotEmpty() } ?: return null
|
||||||
|
|
||||||
|
// Try check url is relative or absolute
|
||||||
|
return try {
|
||||||
|
val locUri = URI(raw)
|
||||||
|
val baseUri = conn.url.toURI()
|
||||||
|
val resolved = if (locUri.isAbsolute) locUri else baseUri.resolve(locUri)
|
||||||
|
resolved.toURL().toString()
|
||||||
|
} catch (_: Exception) {
|
||||||
|
// Fallback: url resolver, also should handles //host/...
|
||||||
|
try {
|
||||||
|
URL(raw).toString() // absolute with protocol
|
||||||
|
} catch (_: MalformedURLException) {
|
||||||
|
try {
|
||||||
|
URL(conn.url, raw).toString()
|
||||||
|
} catch (_: MalformedURLException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user