blob: 0c3f249506da94c338e2b8e8272ed95f82d924bf (
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
37
|
package org.jetbrains.dokka.base.resolvers.shared
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLConnection
internal fun URL.readContent(timeout: Int = 10000, redirectsAllowed: Int = 16): InputStream {
fun URL.doOpenConnection(timeout: Int, redirectsAllowed: Int): 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).doOpenConnection(timeout, redirectsAllowed - 1)
} else {
throw RuntimeException("Too many redirects")
}
}
else -> throw RuntimeException("Unhandled HTTP code: ${connection.responseCode}")
}
else -> return connection
}
}
return doOpenConnection(timeout, redirectsAllowed).getInputStream()
}
|