Skip to content

Commit 913f0a8

Browse files
authored
Merge pull request #430 from Microsoft/fix/http-resource-leak
Fixed http response leak
2 parents d744815 + fe809b7 commit 913f0a8

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected void onCreate(Bundle savedInstanceState) {
6565

6666
sSharedPreferences = getSharedPreferences("Sasquatch", Context.MODE_PRIVATE);
6767
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().build());
68+
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().build());
6869

6970
/* Set custom log URL if one was configured in settings. */
7071
String logUrl = sSharedPreferences.getString(LOG_URL_KEY, getString(R.string.log_url));

sdk/mobile-center/src/main/java/com/microsoft/azure/mobile/http/DefaultHttpClient.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,16 @@ private static String dump(HttpURLConnection urlConnection) throws IOException {
8484
stream = urlConnection.getInputStream();
8585
else
8686
stream = urlConnection.getErrorStream();
87-
InputStreamReader in = new InputStreamReader(stream, CHARSET_NAME);
88-
char[] buffer = new char[READ_BUFFER_SIZE];
89-
int len;
90-
while ((len = in.read(buffer)) > 0)
91-
builder.append(buffer, 0, len);
92-
return builder.toString();
87+
try {
88+
InputStreamReader in = new InputStreamReader(stream, CHARSET_NAME);
89+
char[] buffer = new char[READ_BUFFER_SIZE];
90+
int len;
91+
while ((len = in.read(buffer)) > 0)
92+
builder.append(buffer, 0, len);
93+
return builder.toString();
94+
} finally {
95+
stream.close();
96+
}
9397
}
9498

9599
private static String doCall(String urlString, String method, Map<String, String> headers, CallTemplate callTemplate) throws Exception {

sdk/mobile-center/src/test/java/com/microsoft/azure/mobile/http/DefaultHttpClientTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.io.ByteArrayInputStream;
1616
import java.io.ByteArrayOutputStream;
1717
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.io.InputStreamReader;
1820
import java.net.HttpURLConnection;
1921
import java.net.URL;
2022
import java.util.HashMap;
@@ -186,7 +188,8 @@ public void get200() throws Exception {
186188
when(urlConnection.getResponseCode()).thenReturn(200);
187189
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
188190
when(urlConnection.getOutputStream()).thenReturn(buffer);
189-
when(urlConnection.getInputStream()).thenReturn(new ByteArrayInputStream("OK".getBytes()));
191+
ByteArrayInputStream inputStream = spy(new ByteArrayInputStream("OK".getBytes()));
192+
when(urlConnection.getInputStream()).thenReturn(inputStream);
190193

191194
/* Configure API client. */
192195
HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class);
@@ -208,6 +211,7 @@ public void get200() throws Exception {
208211
verify(urlConnection).setRequestProperty("Install-ID", installId.toString());
209212
verify(urlConnection, never()).setDoOutput(true);
210213
verify(urlConnection).disconnect();
214+
verify(inputStream).close();
211215
verify(callTemplate).onBeforeCalling(eq(url), any(Map.class));
212216
verify(callTemplate, never()).buildRequestBody();
213217
httpClient.close();
@@ -324,6 +328,29 @@ public void failedConnection() throws Exception {
324328
verifyZeroInteractions(serviceCallback);
325329
}
326330

331+
@Test
332+
public void failedToReadResponse() throws Exception {
333+
URL url = mock(URL.class);
334+
whenNew(URL.class).withAnyArguments().thenReturn(url);
335+
IOException exception = new IOException("mock");
336+
HttpURLConnection urlConnection = mock(HttpURLConnection.class);
337+
when(url.openConnection()).thenReturn(urlConnection);
338+
when(urlConnection.getResponseCode()).thenReturn(200);
339+
InputStream inputStream = mock(InputStream.class);
340+
when(urlConnection.getInputStream()).thenReturn(inputStream);
341+
InputStreamReader inputStreamReader = mock(InputStreamReader.class);
342+
whenNew(InputStreamReader.class).withAnyArguments().thenReturn(inputStreamReader);
343+
when(inputStreamReader.read(any(char[].class))).thenThrow(exception);
344+
HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class);
345+
ServiceCallback serviceCallback = mock(ServiceCallback.class);
346+
DefaultHttpClient httpClient = new DefaultHttpClient();
347+
mockCall();
348+
httpClient.callAsync("", "", new HashMap<String, String>(), callTemplate, serviceCallback);
349+
verify(serviceCallback).onCallFailed(exception);
350+
verifyZeroInteractions(serviceCallback);
351+
verify(inputStream).close();
352+
}
353+
327354
@Test
328355
public void failedSerialization() throws Exception {
329356

0 commit comments

Comments
 (0)