mirror of
https://github.com/DreamExposure/DisCal-Discord-Bot.git
synced 2026-01-24 12:58:27 -06:00
Stop leaking OkHTTP response bodies
Why they did it this way, I have no clue, but it should be fixed now
This commit is contained in:
@@ -31,6 +31,7 @@ object GoogleExternalAuthHandler {
|
||||
if (response.code == GlobalVal.STATUS_SUCCESS) {
|
||||
//Got code -- Send message with code, start auth poll
|
||||
val successJson = JSONObject(response.body!!.string())
|
||||
response.body?.close()
|
||||
|
||||
val embed = EmbedCreateSpec.builder()
|
||||
.author("DisCal", BotSettings.BASE_URL.get(), iconUrl)
|
||||
@@ -62,8 +63,10 @@ object GoogleExternalAuthHandler {
|
||||
}
|
||||
} else {
|
||||
//Bad response -- Log, send message
|
||||
val body = response.body?.string()
|
||||
response.body?.close()
|
||||
LOGGER.debug(DEFAULT, "Error request access token | Status code: ${response.code} | ${response
|
||||
.message} | ${response.body?.string()}")
|
||||
.message} | $body")
|
||||
|
||||
event.message.authorAsMember.flatMap {
|
||||
Messages.sendDirectMessage(
|
||||
@@ -85,6 +88,7 @@ object GoogleExternalAuthHandler {
|
||||
GlobalVal.STATUS_BAD_REQUEST, GlobalVal.STATUS_PRECONDITION_REQUIRED -> {
|
||||
//See if auth is pending, if so, just reschedule...
|
||||
val errorJson = JSONObject(response.body!!.string())
|
||||
response.body?.close()
|
||||
when {
|
||||
"authorization_pending".equals(errorJson.getString("error"), true) -> {
|
||||
//Response pending
|
||||
@@ -116,6 +120,7 @@ object GoogleExternalAuthHandler {
|
||||
GlobalVal.STATUS_SUCCESS -> {
|
||||
//Access granted -- Save creds, get calendars, list for user, cancel auth
|
||||
val successJson = JSONObject(response.body!!.string())
|
||||
response.body?.close()
|
||||
|
||||
//Save creds
|
||||
val calData = CalendarData.emptyExternal(poll.settings.guildID, CalendarHost.GOOGLE)
|
||||
@@ -158,6 +163,7 @@ object GoogleExternalAuthHandler {
|
||||
//Unknown error -- Log, send message, cancel poll
|
||||
LOGGER.debug(DEFAULT, "Network error | poll failure" +
|
||||
" | Status code: ${response.code} | ${response.message} | ${response.body?.string()}")
|
||||
response.body?.close()
|
||||
|
||||
Messages.sendDirectMessage(
|
||||
Messages.getMessage("Notification.Error.Network", poll.settings), poll.user)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
@file:Suppress("DEPRECATION")
|
||||
|
||||
package org.dreamexposure.discal.core.wrapper.google
|
||||
|
||||
import com.google.api.client.auth.oauth2.Credential
|
||||
@@ -93,8 +95,8 @@ object GoogleAuthWrapper {
|
||||
}.subscribeOn(Schedulers.boundedElastic()).flatMap { response ->
|
||||
when (response.code) {
|
||||
GlobalVal.STATUS_SUCCESS -> {
|
||||
response.body
|
||||
val responseJson = JSONObject(response.body!!.string())
|
||||
response.body?.close()
|
||||
|
||||
//Update Db data and return
|
||||
calData.encryptedAccessToken = encryption.encrypt(responseJson.getString("access_token"))
|
||||
@@ -104,6 +106,7 @@ object GoogleAuthWrapper {
|
||||
}
|
||||
GlobalVal.STATUS_BAD_REQUEST -> {
|
||||
val errorBody = JSONObject(response.body!!.string())
|
||||
response.body?.close()
|
||||
|
||||
if ("invalid_grant".equals(errorBody.getString("error"), true)) {
|
||||
//User revoked access to calendar, delete our reference as they need to re-auth it.
|
||||
@@ -124,6 +127,7 @@ object GoogleAuthWrapper {
|
||||
else -> {
|
||||
//Failed to get OK. Send debug info...
|
||||
LOGGER.debug(DEFAULT, "[!DGC!] Err requesting new access token. Code: ${response.code} | ${response.message} | ${response.body?.string()}")
|
||||
response.body?.close()
|
||||
|
||||
return@flatMap Mono.empty()
|
||||
}
|
||||
@@ -162,6 +166,7 @@ object GoogleAuthWrapper {
|
||||
when (response.code) {
|
||||
GlobalVal.STATUS_SUCCESS -> {
|
||||
val responseJson = JSONObject(response.body!!.string())
|
||||
response.body?.close()
|
||||
|
||||
//Update DB and return
|
||||
credential.setAccessToken(responseJson.getString("access_token"))
|
||||
@@ -172,6 +177,7 @@ object GoogleAuthWrapper {
|
||||
}
|
||||
GlobalVal.STATUS_BAD_REQUEST -> {
|
||||
val errorBody = JSONObject(response.body!!.string())
|
||||
response.body?.close()
|
||||
|
||||
if ("invalid_grant".equals(errorBody.getString("error"), true)) {
|
||||
//We revoked access to this account. Is this on purpose??
|
||||
@@ -187,6 +193,7 @@ object GoogleAuthWrapper {
|
||||
//Failed to get OK. Send debug info.
|
||||
LOGGER.debug(DEFAULT, "[!DGC!] Error requesting new access token. Status code: ${response.code} |" +
|
||||
" ${response.message} | ${response.body?.string()}")
|
||||
response.body?.close()
|
||||
return@flatMap Mono.empty()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ class UpdateDBotsData(private val networkInfo: NetworkInfo) : ApplicationRunner
|
||||
}.doOnNext { response ->
|
||||
if (response.code != GlobalVal.STATUS_SUCCESS) {
|
||||
LOGGER.debug("Failed to update DBots.gg stats | Body: ${response.body?.string()}")
|
||||
response.body?.close()
|
||||
}
|
||||
}.onErrorResume {
|
||||
Mono.empty()
|
||||
|
||||
@@ -19,6 +19,7 @@ object GoogleInternalAuthHandler {
|
||||
fun requestCode(credNumber: Int): Mono<Void> {
|
||||
return GoogleAuthWrapper.requestDeviceCode().flatMap { response ->
|
||||
val responseBody = response.body!!.string()
|
||||
response.body?.close()
|
||||
|
||||
if (response.code == GlobalVal.STATUS_SUCCESS) {
|
||||
val codeResponse = JSONObject(responseBody)
|
||||
@@ -48,6 +49,7 @@ object GoogleInternalAuthHandler {
|
||||
private fun pollForAuth(poll: InternalGoogleAuthPoll): Mono<Void> {
|
||||
return GoogleAuthWrapper.requestPollResponse(poll).flatMap { response ->
|
||||
val responseBody = response.body!!.string()
|
||||
response.body?.close()
|
||||
|
||||
when (response.code) {
|
||||
GlobalVal.STATUS_FORBIDDEN -> {
|
||||
|
||||
@@ -113,7 +113,7 @@ object DiscordAccountHandler {
|
||||
if (has) {
|
||||
swe.session.map {
|
||||
this.discordAccounts.remove(it.getRequiredAttribute("account"))
|
||||
it.attributes.remove("account")
|
||||
Mono.justOrEmpty(it.attributes.remove("account"))
|
||||
}.then()
|
||||
} else Mono.empty()
|
||||
}
|
||||
@@ -165,11 +165,13 @@ object DiscordAccountHandler {
|
||||
if (response.isSuccessful) {
|
||||
//TODO: Change to use kotlin serialization
|
||||
val body = JSONObject(response.body?.string())
|
||||
response.body?.close()
|
||||
|
||||
return@map body.optString("key", "internal_error")
|
||||
} else {
|
||||
//Something didn't work, log and add "key" embed page knows how to handle
|
||||
LOGGER.debug("Embed key fail ${response.body?.string()}")
|
||||
response.body?.close()
|
||||
return@map "internal_error"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,9 @@ object StatusHandler {
|
||||
return@fromCallable client.newCall(request).execute()
|
||||
}.map { response ->
|
||||
if (response.code == HttpStatusCodes.STATUS_CODE_OK) {
|
||||
return@map NetworkInfo().fromJson(JSONObject(response.body?.string()))
|
||||
val body = response.body?.string()
|
||||
response.body?.close()
|
||||
return@map NetworkInfo().fromJson(JSONObject(body))
|
||||
} else {
|
||||
return@map NetworkInfo() //Just return an empty object, it's fine.
|
||||
}
|
||||
|
||||
@@ -46,7 +46,12 @@ class DiscordLoginHandler {
|
||||
|
||||
return@fromCallable client.newCall(tokenRequest).execute()
|
||||
}.subscribeOn(Schedulers.boundedElastic()).flatMap {
|
||||
Mono.fromCallable { JSONObject(it.body?.string()) }
|
||||
Mono.fromCallable {
|
||||
val json = JSONObject(it.body?.string())
|
||||
it.body?.close()
|
||||
|
||||
json
|
||||
}
|
||||
}.flatMap { info ->
|
||||
if (info.has("access_token")) {
|
||||
//GET request for user info
|
||||
@@ -72,7 +77,9 @@ class DiscordLoginHandler {
|
||||
Mono.zip(dataResMono, guildResMono)
|
||||
.flatMap(TupleUtils.function { userDataResponse, userGuildsResponse ->
|
||||
val userInfo = JSONObject(userDataResponse.body?.string())
|
||||
userDataResponse.body?.close()
|
||||
val guildsInfo = JSONArray(userGuildsResponse.body?.string())
|
||||
userGuildsResponse.body?.close()
|
||||
|
||||
//Saving session and access info into memory...
|
||||
val model = DiscordAccountHandler.createDefaultModel()
|
||||
@@ -130,6 +137,7 @@ class DiscordLoginHandler {
|
||||
//Handle response
|
||||
if (keyGrantResponse.isSuccessful) {
|
||||
val keyGrantResponseBody = JSONObject(keyGrantResponse.body?.string())
|
||||
keyGrantResponse.body?.close()
|
||||
//API Key received
|
||||
model["key"] = keyGrantResponseBody.getString("key")
|
||||
|
||||
@@ -138,6 +146,7 @@ class DiscordLoginHandler {
|
||||
} else {
|
||||
//Something didn't work, just redirect back to login page
|
||||
LOGGER.debug("login issue | ${keyGrantResponse.body?.string()}")
|
||||
keyGrantResponse.body?.close()
|
||||
|
||||
Mono.just("redirect:/login")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user