blob: cb737041ddd9c9497a3c4f99c2556edc1d34bc2a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package org.jetbrains.dokka.base.resolvers.shared
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLConnection
internal fun URL.doOpenConnectionToReadContent(timeout: Int = 10000, redirectsAllowed: Int = 16): URLConnection {
val connection = this.openConnection().apply {
connectTimeout = timeout
readTimeout = timeout
}
when (connection) {
is HttpURLConnection -> {
return when (connection.responseCode) {
in 200..299 -> {
connection
}
HttpURLConnection.HTTP_MOVED_PERM,
HttpURLConnection.HTTP_MOVED_TEMP,
HttpURLConnection.HTTP_SEE_OTHER -> {
if (redirectsAllowed > 0) {
val newUrl = connection.getHeaderField("Location")
URL(newUrl).doOpenConnectionToReadContent(timeout, redirectsAllowed - 1)
} else {
throw RuntimeException("Too many redirects")
}
}
else -> {
throw RuntimeException("Unhandled http code: ${connection.responseCode}")
}
}
}
else -> return connection
}
}
|