Skip to content

Commit 0aad13f

Browse files
committed
fix dividing by zero while loading empty file (#26)
1 parent a270460 commit 0aad13f

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

library/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private HttpProxyCacheServer(Config config) {
9191

9292
private void makeSureServerWorks() {
9393
int maxPingAttempts = 3;
94-
int delay = 200;
94+
int delay = 300;
9595
int pingAttempts = 0;
9696
while (pingAttempts < maxPingAttempts) {
9797
try {
@@ -107,7 +107,7 @@ private void makeSureServerWorks() {
107107
pingAttempts++;
108108
delay *= 2;
109109
}
110-
Log.e(LOG_TAG, "Shutdown server… Error pinging server [attempt: " + pingAttempts + ", timeout: " + delay + "]. " +
110+
Log.e(LOG_TAG, "Shutdown server… Error pinging server [attempts: " + pingAttempts + ", max timeout: " + delay / 2 + "]. " +
111111
"If you see this message, please, email me [email protected]");
112112
shutdown();
113113
}

library/src/main/java/com/danikula/videocache/ProxyCache.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ private void notifyNewCacheDataAvailable(long cacheAvailable, long sourceAvailab
100100
}
101101
}
102102

103-
protected void onCacheAvailable(long cacheAvailable, long sourceAvailable) {
104-
int percents = (int) (cacheAvailable * 100 / sourceAvailable);
103+
protected void onCacheAvailable(long cacheAvailable, long sourceLength) {
104+
boolean zeroLengthSource = sourceLength == 0;
105+
int percents = zeroLengthSource ? 100 : (int) (cacheAvailable * 100 / sourceLength);
105106
boolean percentsChanged = percents != percentsAvailable;
106-
boolean sourceLengthKnown = sourceAvailable >= 0;
107+
boolean sourceLengthKnown = sourceLength >= 0;
107108
if (sourceLengthKnown && percentsChanged) {
108109
onCachePercentsAvailableChanged(percents);
109110
}

test/src/test/java/com/danikula/videocache/HttpProxyCacheServerTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ public void testMaxFileCacheLimit() throws Exception {
211211
}
212212

213213
private Pair<File, Response> readProxyData(String url, int offset) throws IOException {
214-
File externalCacheDir = RuntimeEnvironment.application.getExternalCacheDir();
215-
File file = file(externalCacheDir, url);
216-
HttpProxyCacheServer proxy = newProxy(externalCacheDir);
214+
File file = file(cacheFolder, url);
215+
HttpProxyCacheServer proxy = newProxy(cacheFolder);
217216

218217
Response response = readProxyResponse(proxy, url, offset);
219218
proxy.shutdown();

test/src/test/java/com/danikula/videocache/HttpProxyCacheTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.robolectric.annotation.Config;
1313

1414
import java.io.ByteArrayOutputStream;
15+
import java.io.File;
1516
import java.net.Socket;
1617

1718
import static com.danikula.videocache.support.ProxyCacheTestUtils.HTTP_DATA_URL;
@@ -20,6 +21,7 @@
2021
import static org.mockito.Matchers.any;
2122
import static org.mockito.Matchers.anyInt;
2223
import static org.mockito.Matchers.anyLong;
24+
import static org.mockito.Matchers.eq;
2325
import static org.mockito.Mockito.doThrow;
2426
import static org.mockito.Mockito.mock;
2527
import static org.mockito.Mockito.when;
@@ -73,4 +75,24 @@ public void testProcessPartialRequestWithoutCache() throws Exception {
7375
assertThat(response.data).isEqualTo(partialData);
7476
assertThat(response.code).isEqualTo(206);
7577
}
78+
79+
@Test
80+
public void testLoadEmptyFile() throws Exception {
81+
String zeroSizeUrl = "https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/empty.txt";
82+
HttpUrlSource source = new HttpUrlSource(zeroSizeUrl);
83+
HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(ProxyCacheTestUtils.newCacheFile()));
84+
GetRequest request = new GetRequest("GET /" + HTTP_DATA_URL + " HTTP/1.1");
85+
ByteArrayOutputStream out = new ByteArrayOutputStream();
86+
Socket socket = mock(Socket.class);
87+
when(socket.getOutputStream()).thenReturn(out);
88+
89+
CacheListener listener = Mockito.mock(CacheListener.class);
90+
proxyCache.registerCacheListener(listener);
91+
proxyCache.processRequest(request, socket);
92+
proxyCache.registerCacheListener(null);
93+
Response response = new Response(out.toByteArray());
94+
95+
Mockito.verify(listener).onCacheAvailable(Mockito.<File>any(), eq(zeroSizeUrl), eq(100));
96+
assertThat(response.data).isEmpty();
97+
}
7698
}

test/src/test/java/com/danikula/videocache/support/ProxyCacheTestUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public static Response readProxyResponse(HttpProxyCacheServer proxy, String url)
4040
}
4141

4242
public static Response readProxyResponse(HttpProxyCacheServer proxy, String url, int offset) throws IOException {
43-
URL proxiedUrl = new URL(proxy.getProxyUrl(url));
43+
String proxyUrl = proxy.getProxyUrl(url);
44+
if (!proxyUrl.startsWith("http://127.0.0.1")) {
45+
throw new IllegalStateException("Url " + url + " is not proxied!");
46+
}
47+
URL proxiedUrl = new URL(proxyUrl);
4448
HttpURLConnection connection = (HttpURLConnection) proxiedUrl.openConnection();
4549
try {
4650
if (offset >= 0) {

0 commit comments

Comments
 (0)