in facebook-core/src/main/java/com/facebook/internal/UrlRedirectCache.kt [51:111]
fun getRedirectedUri(uri: Uri?): Uri? {
if (uri == null) {
return null
}
var uriString = uri.toString()
var reader: InputStreamReader? = null
val redirectChain = HashSet<String>()
redirectChain.add(uriString)
try {
val cache = getCache()
var redirectExists = false
var stream = cache.get(uriString, this.redirectContentTag)
while (stream != null) {
redirectExists = true
// Get the redirected url
reader = InputStreamReader(stream)
val buffer = CharArray(128)
val urlBuilder = StringBuilder()
var bufferLength = reader.read(buffer, 0, buffer.size)
while (bufferLength > 0) {
urlBuilder.append(buffer, 0, bufferLength)
bufferLength = reader.read(buffer, 0, buffer.size)
}
Utility.closeQuietly(reader)
// Iterate to the next url in the redirection
val redirectToUriString = urlBuilder.toString()
if (redirectChain.contains(redirectToUriString)) {
if (redirectToUriString == uriString) {
// uriString redirect to itself. Stop the loop
break
} else {
// A loop with more than 1 address is detected. It's unexpected.
// In this case, return null so that the caller can directly use the original address.
Logger.log(
LoggingBehavior.CACHE, Log.ERROR, this.tag, "A loop detected in UrlRedirectCache")
return null
}
}
uriString = redirectToUriString
redirectChain.add(uriString)
stream = cache.get(uriString, this.redirectContentTag)
}
if (redirectExists) {
return Uri.parse(uriString)
}
} catch (e: IOException) {
Logger.log(
LoggingBehavior.CACHE,
Log.INFO,
this.tag,
"IOException when accessing cache: " + e.message)
} finally {
Utility.closeQuietly(reader)
}
return null
}