Skip to content

Commit

Permalink
Merge pull request #1568 from bemusementpark/fix-body
Browse files Browse the repository at this point in the history
Fix leak in Response#body
  • Loading branch information
bemusementpark authored Jul 25, 2024
2 parents 631d93c + 0a08eb6 commit 64c7224
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions libsignal/src/main/java/org/session/libsignal/utilities/HTTP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,26 @@ object HTTP {
}
Verb.DELETE -> request.delete()
}
lateinit var response: Response
try {
val connection: OkHttpClient = if (timeout != HTTP.timeout) { // Custom timeout
if (useSeedNodeConnection) {
throw IllegalStateException("Setting a custom timeout is only allowed for requests to snodes.")
return try {
when {
// Custom timeout
timeout != HTTP.timeout -> {
if (useSeedNodeConnection) {
throw IllegalStateException("Setting a custom timeout is only allowed for requests to snodes.")
}
getDefaultConnection(timeout)
}
useSeedNodeConnection -> seedNodeConnection
else -> defaultConnection
}.newCall(request.build()).execute().use { response ->
when (val statusCode = response.code) {
200 -> response.body!!.bytes()
else -> {
Log.d("Loki", "${verb.rawValue} request to $url failed with status code: $statusCode.")
throw HTTPRequestFailedException(statusCode, null)
}
}
getDefaultConnection(timeout)
} else {
if (useSeedNodeConnection) seedNodeConnection else defaultConnection
}

response = connection.newCall(request.build()).execute()
} catch (exception: Exception) {
Log.d("Loki", "${verb.rawValue} request to $url failed due to error: ${exception.localizedMessage}.")

Expand All @@ -135,14 +143,5 @@ object HTTP {
// Override the actual error so that we can correctly catch failed requests in OnionRequestAPI
throw HTTPRequestFailedException(0, null, "HTTP request failed due to: ${exception.message}")
}
return when (val statusCode = response.code) {
200 -> {
response.body!!.bytes()
}
else -> {
Log.d("Loki", "${verb.rawValue} request to $url failed with status code: $statusCode.")
throw HTTPRequestFailedException(statusCode, null)
}
}
}
}

0 comments on commit 64c7224

Please sign in to comment.