Fix OkHttp connection leaks on connection error
When request fails, entire response needs to be wrapped in try with resources in order to close the connection properly and not only response body. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
@@ -26,14 +26,17 @@ package net.runelite.http.service.hiscore;
|
||||
|
||||
import java.io.IOException;
|
||||
import net.runelite.http.api.RuneliteAPI;
|
||||
import net.runelite.http.api.hiscore.*;
|
||||
import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
||||
import net.runelite.http.api.hiscore.HiscoreResult;
|
||||
import net.runelite.http.api.hiscore.HiscoreSkill;
|
||||
import net.runelite.http.api.hiscore.SingleHiscoreSkillResult;
|
||||
import net.runelite.http.api.hiscore.Skill;
|
||||
import net.runelite.http.service.util.HiscoreEndpointEditor;
|
||||
import net.runelite.http.service.util.exception.InternalServerErrorException;
|
||||
import net.runelite.http.service.util.exception.NotFoundException;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.apache.commons.csv.CSVFormat;
|
||||
import org.apache.commons.csv.CSVParser;
|
||||
import org.apache.commons.csv.CSVRecord;
|
||||
@@ -41,7 +44,11 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/hiscore")
|
||||
@@ -66,24 +73,22 @@ public class HiscoreService
|
||||
.url(url)
|
||||
.build();
|
||||
|
||||
Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute();
|
||||
|
||||
if (!okresponse.isSuccessful())
|
||||
{
|
||||
switch (HttpStatus.valueOf(okresponse.code()))
|
||||
{
|
||||
case NOT_FOUND:
|
||||
throw new NotFoundException();
|
||||
default:
|
||||
throw new InternalServerErrorException("Error retrieving data from Jagex Hiscores: " + okresponse.message());
|
||||
}
|
||||
}
|
||||
|
||||
String responseStr;
|
||||
|
||||
try (ResponseBody body = okresponse.body())
|
||||
try (Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute())
|
||||
{
|
||||
responseStr = body.string();
|
||||
if (!okresponse.isSuccessful())
|
||||
{
|
||||
switch (HttpStatus.valueOf(okresponse.code()))
|
||||
{
|
||||
case NOT_FOUND:
|
||||
throw new NotFoundException();
|
||||
default:
|
||||
throw new InternalServerErrorException("Error retrieving data from Jagex Hiscores: " + okresponse.message());
|
||||
}
|
||||
}
|
||||
|
||||
responseStr = okresponse.body().string();
|
||||
}
|
||||
|
||||
CSVParser parser = CSVParser.parse(responseStr, CSVFormat.DEFAULT);
|
||||
|
||||
@@ -45,7 +45,6 @@ import net.runelite.http.api.item.SearchResult;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -487,16 +486,14 @@ public class ItemService
|
||||
|
||||
private <T> T fetchJson(Request request, Class<T> clazz) throws IOException
|
||||
{
|
||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
||||
|
||||
if (!response.isSuccessful())
|
||||
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||
{
|
||||
throw new IOException("Unsuccessful http response: " + response.message());
|
||||
}
|
||||
if (!response.isSuccessful())
|
||||
{
|
||||
throw new IOException("Unsuccessful http response: " + response.message());
|
||||
}
|
||||
|
||||
try (ResponseBody body = response.body())
|
||||
{
|
||||
InputStream in = body.byteStream();
|
||||
InputStream in = response.body().byteStream();
|
||||
return RuneliteAPI.GSON.fromJson(new InputStreamReader(in), clazz);
|
||||
}
|
||||
catch (JsonParseException ex)
|
||||
@@ -513,16 +510,14 @@ public class ItemService
|
||||
.url(httpUrl)
|
||||
.build();
|
||||
|
||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
||||
|
||||
if (!response.isSuccessful())
|
||||
try (Response response = RuneliteAPI.CLIENT.newCall(request).execute())
|
||||
{
|
||||
throw new IOException("Unsuccessful http response: " + response.message());
|
||||
}
|
||||
if (!response.isSuccessful())
|
||||
{
|
||||
throw new IOException("Unsuccessful http response: " + response.message());
|
||||
}
|
||||
|
||||
try (ResponseBody body = response.body())
|
||||
{
|
||||
return body.bytes();
|
||||
return response.body().bytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ import net.runelite.http.api.worlds.WorldResult;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -53,12 +52,11 @@ public class WorldsService
|
||||
.url(url)
|
||||
.build();
|
||||
|
||||
Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute();
|
||||
byte[] b;
|
||||
|
||||
try (ResponseBody body = okresponse.body())
|
||||
try (Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute())
|
||||
{
|
||||
b = body.bytes();
|
||||
b = okresponse.body().bytes();
|
||||
}
|
||||
|
||||
List<World> worlds = new ArrayList<>();
|
||||
|
||||
Reference in New Issue
Block a user