Skip to content

Commit

Permalink
fix dividing by zero while loading empty file (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
danikula committed Nov 6, 2015
1 parent a270460 commit 0aad13f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private HttpProxyCacheServer(Config config) {

private void makeSureServerWorks() {
int maxPingAttempts = 3;
int delay = 200;
int delay = 300;
int pingAttempts = 0;
while (pingAttempts < maxPingAttempts) {
try {
Expand All @@ -107,7 +107,7 @@ private void makeSureServerWorks() {
pingAttempts++;
delay *= 2;
}
Log.e(LOG_TAG, "Shutdown server… Error pinging server [attempt: " + pingAttempts + ", timeout: " + delay + "]. " +
Log.e(LOG_TAG, "Shutdown server… Error pinging server [attempts: " + pingAttempts + ", max timeout: " + delay / 2 + "]. " +
"If you see this message, please, email me danikula@gmail.com");
shutdown();
}
Expand Down
7 changes: 4 additions & 3 deletions library/src/main/java/com/danikula/videocache/ProxyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ private void notifyNewCacheDataAvailable(long cacheAvailable, long sourceAvailab
}
}

protected void onCacheAvailable(long cacheAvailable, long sourceAvailable) {
int percents = (int) (cacheAvailable * 100 / sourceAvailable);
protected void onCacheAvailable(long cacheAvailable, long sourceLength) {
boolean zeroLengthSource = sourceLength == 0;
int percents = zeroLengthSource ? 100 : (int) (cacheAvailable * 100 / sourceLength);
boolean percentsChanged = percents != percentsAvailable;
boolean sourceLengthKnown = sourceAvailable >= 0;
boolean sourceLengthKnown = sourceLength >= 0;
if (sourceLengthKnown && percentsChanged) {
onCachePercentsAvailableChanged(percents);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ public void testMaxFileCacheLimit() throws Exception {
}

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

Response response = readProxyResponse(proxy, url, offset);
proxy.shutdown();
Expand Down
22 changes: 22 additions & 0 deletions test/src/test/java/com/danikula/videocache/HttpProxyCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.robolectric.annotation.Config;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.Socket;

import static com.danikula.videocache.support.ProxyCacheTestUtils.HTTP_DATA_URL;
Expand All @@ -20,6 +21,7 @@
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -73,4 +75,24 @@ public void testProcessPartialRequestWithoutCache() throws Exception {
assertThat(response.data).isEqualTo(partialData);
assertThat(response.code).isEqualTo(206);
}

@Test
public void testLoadEmptyFile() throws Exception {
String zeroSizeUrl = "https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/empty.txt";
HttpUrlSource source = new HttpUrlSource(zeroSizeUrl);
HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(ProxyCacheTestUtils.newCacheFile()));
GetRequest request = new GetRequest("GET /" + HTTP_DATA_URL + " HTTP/1.1");
ByteArrayOutputStream out = new ByteArrayOutputStream();
Socket socket = mock(Socket.class);
when(socket.getOutputStream()).thenReturn(out);

CacheListener listener = Mockito.mock(CacheListener.class);
proxyCache.registerCacheListener(listener);
proxyCache.processRequest(request, socket);
proxyCache.registerCacheListener(null);
Response response = new Response(out.toByteArray());

Mockito.verify(listener).onCacheAvailable(Mockito.<File>any(), eq(zeroSizeUrl), eq(100));
assertThat(response.data).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public static Response readProxyResponse(HttpProxyCacheServer proxy, String url)
}

public static Response readProxyResponse(HttpProxyCacheServer proxy, String url, int offset) throws IOException {
URL proxiedUrl = new URL(proxy.getProxyUrl(url));
String proxyUrl = proxy.getProxyUrl(url);
if (!proxyUrl.startsWith("http://127.0.0.1")) {
throw new IllegalStateException("Url " + url + " is not proxied!");
}
URL proxiedUrl = new URL(proxyUrl);
HttpURLConnection connection = (HttpURLConnection) proxiedUrl.openConnection();
try {
if (offset >= 0) {
Expand Down

0 comments on commit 0aad13f

Please sign in to comment.