Skip to content

Commit d011b94

Browse files
authored
Merge pull request #406 from rocketraman/issue-284
Fix some OkHttp response body leaks
2 parents 4714613 + cd0296d commit d011b94

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public Request authenticateRequest(Request request) {
269269
.tag(RetryOptions.class, retryOptions);
270270

271271
String contenttype = null;
272+
Response response = null;
272273

273274
try {
274275
logger.logDebug("Request Method " + request.getHttpMethod().toString());
@@ -353,7 +354,7 @@ public MediaType contentType() {
353354
coreHttpRequest = corehttpRequestBuilder.build();
354355

355356
// Call being executed
356-
Response response = corehttpClient.newCall(coreHttpRequest).execute();
357+
response = corehttpClient.newCall(coreHttpRequest).execute();
357358

358359
if (handler != null) {
359360
handler.configConnection(response);
@@ -401,12 +402,13 @@ public MediaType contentType() {
401402
return (Result) handleBinaryStream(in);
402403
}
403404
} finally {
404-
if (!isBinaryStreamInput && in != null) {
405+
if (!isBinaryStreamInput) {
405406
try{
406-
in.close();
407+
if (in != null) in.close();
407408
}catch(IOException e) {
408409
logger.logError(e.getMessage(), e);
409410
}
411+
if (response != null) response.close();
410412
}
411413
}
412414
} catch (final GraphServiceException ex) {

src/main/java/com/microsoft/graph/http/GraphServiceException.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package com.microsoft.graph.http;
2424

2525
import java.io.IOException;
26+
import java.io.InputStream;
2627
import java.util.LinkedList;
2728
import java.util.List;
2829
import java.util.Locale;
@@ -38,6 +39,7 @@
3839
import com.microsoft.graph.serializer.ISerializer;
3940

4041
import okhttp3.Response;
42+
import static okhttp3.internal.Util.closeQuietly;
4143

4244
/**
4345
* An exception from the Graph service
@@ -416,8 +418,12 @@ public static <T> GraphServiceException createFromConnection(final IHttpRequest
416418

417419
final String responseMessage = response.message();
418420
String rawOutput = "{}";
419-
if(response.body().byteStream() != null) {
420-
rawOutput = DefaultHttpProvider.streamToString(response.body().byteStream());
421+
422+
InputStream is = response.body().byteStream();
423+
try {
424+
rawOutput = DefaultHttpProvider.streamToString(is);
425+
} finally {
426+
closeQuietly(is);
421427
}
422428
GraphErrorResponse error;
423429
try {

0 commit comments

Comments
 (0)