Close response body for all storage requests and for unsuccessful requests.

fork-5.53.8
AsamK 2021-11-08 21:49:52 +01:00 zatwierdzone przez Cody Henthorne
rodzic 7ccc7ec856
commit 1ad0b0e6ae
1 zmienionych plików z 78 dodań i 45 usunięć

Wyświetl plik

@ -521,7 +521,7 @@ public class PushServiceSocket {
} }
public SignalServiceMessagesResult getMessages() throws IOException { public SignalServiceMessagesResult getMessages() throws IOException {
Response response = makeServiceRequest(String.format(MESSAGE_PATH, ""), "GET", (RequestBody) null, NO_HEADERS, NO_HANDLER, Optional.absent()); try (Response response = makeServiceRequest(String.format(MESSAGE_PATH, ""), "GET", (RequestBody) null, NO_HEADERS, NO_HANDLER, Optional.absent())) {
validateServiceResponse(response); validateServiceResponse(response);
List<SignalServiceEnvelopeEntity> envelopes = readBodyJson(response.body(), SignalServiceEnvelopeEntityList.class).getMessages(); List<SignalServiceEnvelopeEntity> envelopes = readBodyJson(response.body(), SignalServiceEnvelopeEntityList.class).getMessages();
@ -538,6 +538,7 @@ public class PushServiceSocket {
return new SignalServiceMessagesResult(envelopes, serverDeliveredTimestamp); return new SignalServiceMessagesResult(envelopes, serverDeliveredTimestamp);
} }
}
public void acknowledgeMessage(String sender, long timestamp) throws IOException { public void acknowledgeMessage(String sender, long timestamp) throws IOException {
makeServiceRequest(String.format(Locale.US, SENDER_ACK_MESSAGE_PATH, sender, timestamp), "DELETE", null); makeServiceRequest(String.format(Locale.US, SENDER_ACK_MESSAGE_PATH, sender, timestamp), "DELETE", null);
@ -1087,7 +1088,7 @@ public class PushServiceSocket {
public Optional<StorageManifest> writeStorageContacts(String authToken, WriteOperation writeOperation) throws IOException { public Optional<StorageManifest> writeStorageContacts(String authToken, WriteOperation writeOperation) throws IOException {
try { try {
makeStorageRequest(authToken, "/v1/storage", "PUT", protobufRequestBody(writeOperation)); makeAndCloseStorageRequest(authToken, "/v1/storage", "PUT", protobufRequestBody(writeOperation));
return Optional.absent(); return Optional.absent();
} catch (ContactManifestMismatchException e) { } catch (ContactManifestMismatchException e) {
return Optional.of(StorageManifest.parseFrom(e.getResponseBody())); return Optional.of(StorageManifest.parseFrom(e.getResponseBody()));
@ -1095,7 +1096,7 @@ public class PushServiceSocket {
} }
public void pingStorageService() throws IOException { public void pingStorageService() throws IOException {
makeStorageRequest(null, "/ping", "GET", null); makeAndCloseStorageRequest(null, "/ping", "GET", null);
} }
public RemoteConfigResponse getRemoteConfig() throws IOException { public RemoteConfigResponse getRemoteConfig() throws IOException {
@ -1616,7 +1617,8 @@ public class PushServiceSocket {
call.enqueue(new Callback() { call.enqueue(new Callback() {
@Override @Override
public void onResponse(Call call, Response response) { public void onResponse(Call call, Response response) {
try (ResponseBody body = validateServiceResponse(response).body()) { try (ResponseBody body = response.body()) {
validateServiceResponse(response);
bodyFuture.set(readBodyString(body)); bodyFuture.set(readBodyString(body));
} catch (IOException e) { } catch (IOException e) {
bodyFuture.setException(e); bodyFuture.setException(e);
@ -1665,9 +1667,17 @@ public class PushServiceSocket {
{ {
Response response = getServiceConnection(urlFragment, method, body, headers, unidentifiedAccessKey, doNotAddAuthenticationOrUnidentifiedAccessKey); Response response = getServiceConnection(urlFragment, method, body, headers, unidentifiedAccessKey, doNotAddAuthenticationOrUnidentifiedAccessKey);
responseCodeHandler.handle(response.code(), response.body()); ResponseBody responseBody = response.body();
try {
responseCodeHandler.handle(response.code(), responseBody);
return validateServiceResponse(response); return validateServiceResponse(response);
} catch (NonSuccessfulResponseCodeException | PushNetworkException | MalformedResponseException e) {
if (responseBody != null) {
responseBody.close();
}
throw e;
}
} }
private Response validateServiceResponse(Response response) private Response validateServiceResponse(Response response)
@ -1888,6 +1898,21 @@ public class PushServiceSocket {
throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response); throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response);
} }
private void makeAndCloseStorageRequest(String authorization, String path, String method, RequestBody body)
throws PushNetworkException, NonSuccessfulResponseCodeException
{
makeAndCloseStorageRequest(authorization, path, method, body, NO_HANDLER);
}
private void makeAndCloseStorageRequest(String authorization, String path, String method, RequestBody body, ResponseCodeHandler responseCodeHandler)
throws PushNetworkException, NonSuccessfulResponseCodeException
{
ResponseBody responseBody = makeStorageRequest(authorization, path, method, body, responseCodeHandler);
if (responseBody != null) {
responseBody.close();
}
}
private ResponseBody makeStorageRequest(String authorization, String path, String method, RequestBody body) private ResponseBody makeStorageRequest(String authorization, String path, String method, RequestBody body)
throws PushNetworkException, NonSuccessfulResponseCodeException throws PushNetworkException, NonSuccessfulResponseCodeException
{ {
@ -1945,7 +1970,9 @@ public class PushServiceSocket {
} }
} }
responseCodeHandler.handle(response.code(), response.body()); ResponseBody responseBody = response.body();
try {
responseCodeHandler.handle(response.code(), responseBody);
switch (response.code()) { switch (response.code()) {
case 204: case 204:
@ -1956,8 +1983,8 @@ public class PushServiceSocket {
case 404: case 404:
throw new NotFoundException("Not found"); throw new NotFoundException("Not found");
case 409: case 409:
if (response.body() != null) { if (responseBody != null) {
throw new ContactManifestMismatchException(readBodyBytes(response.body())); throw new ContactManifestMismatchException(readBodyBytes(responseBody));
} else { } else {
throw new ConflictException(); throw new ConflictException();
} }
@ -1968,6 +1995,12 @@ public class PushServiceSocket {
} }
throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response); throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response);
} catch (NonSuccessfulResponseCodeException | PushNetworkException e) {
if (responseBody != null) {
responseBody.close();
}
throw e;
}
} }
public CallingResponse makeCallingRequest(long requestId, String url, String httpMethod, List<Pair<String, String>> headers, byte[] body) { public CallingResponse makeCallingRequest(long requestId, String url, String httpMethod, List<Pair<String, String>> headers, byte[] body) {
@ -2303,7 +2336,7 @@ public class PushServiceSocket {
public void putNewGroupsV2Group(Group group, GroupsV2AuthorizationString authorization) public void putNewGroupsV2Group(Group group, GroupsV2AuthorizationString authorization)
throws NonSuccessfulResponseCodeException, PushNetworkException throws NonSuccessfulResponseCodeException, PushNetworkException
{ {
makeStorageRequest(authorization.toString(), makeAndCloseStorageRequest(authorization.toString(),
GROUPSV2_GROUP, GROUPSV2_GROUP,
"PUT", "PUT",
protobufRequestBody(group), protobufRequestBody(group),
@ -2368,8 +2401,8 @@ public class PushServiceSocket {
} }
GroupChanges groupChanges; GroupChanges groupChanges;
try { try (InputStream input = response.body().byteStream()) {
groupChanges = GroupChanges.parseFrom(response.body().byteStream()); groupChanges = GroupChanges.parseFrom(input);
} catch (IOException e) { } catch (IOException e) {
throw new PushNetworkException(e); throw new PushNetworkException(e);
} }