Skip to content

Commit

Permalink
Handle relative redirects in HttpUrlFetcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Sep 9, 2014
1 parent 2bd356b commit 32f4c3c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -87,6 +86,19 @@ public void testHandlesRedirect302s() throws Exception {
assertThat(isToString(is), equalTo(expected));
}

@Test
public void testHandlesRelativeRedirects() throws Exception {
String expected = "fakedata";
mockWebServer.enqueue(new MockResponse()
.setResponseCode(301)
.setHeader("Location", "/redirect"));
mockWebServer.enqueue(new MockResponse()
.setResponseCode(200)
.setBody(expected));
InputStream is = getFetcher().loadData(Priority.NORMAL);
assertThat(isToString(is), equalTo(expected));
}

@Test
public void testHandlesUpToFiveRedirects() throws Exception {
int numRedirects = 4;
Expand Down Expand Up @@ -127,14 +139,27 @@ public void testThrowsOnRedirectLoops() throws Exception {
}

@Test
public void testThrowsIfRedirectLocationIsEmpty() throws Exception {
public void testThrowsIfRedirectLocationIsNotPresent() throws Exception {
mockWebServer.enqueue(new MockResponse().setResponseCode(301));

try {
getFetcher().loadData(Priority.NORMAL);
fail("Didn't get expected IOException");
} catch (MalformedURLException e) {
// Expected
} catch (IOException e) {
// Expected.
}
}

@Test
public void testThrowsIfRedirectLocationIsPresentAndEmpty() throws Exception {
mockWebServer.enqueue(new MockResponse()
.setResponseCode(301).setHeader("Location", ""));

try {
getFetcher().loadData(Priority.NORMAL);
fail("Didn't get expected IOException");
} catch (IOException e) {
// Expected.
}
}

Expand All @@ -154,7 +179,6 @@ public void testThrowsAfterTooManyRedirects() throws Exception {
getFetcher().loadData(Priority.NORMAL);
}


@Test(expected = IOException.class)
public void testThrowsIfStatusCodeIs500() throws Exception {
mockWebServer.enqueue(new MockResponse().setResponseCode(500));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.data;

import android.text.TextUtils;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.model.GlideUrl;

Expand Down Expand Up @@ -56,7 +57,10 @@ private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl) t
return urlConnection.getInputStream();
} else if (statusCode / 100 == 3) {
String redirectUrlString = urlConnection.getHeaderField("Location");
URL redirectUrl = new URL(redirectUrlString);
if (TextUtils.isEmpty(redirectUrlString)) {
throw new IOException("Received empty or null redirect url");
}
URL redirectUrl = new URL(url, redirectUrlString);
return loadDataWithRedirects(redirectUrl, redirects + 1, url);
} else {
if (statusCode == -1) {
Expand Down

0 comments on commit 32f4c3c

Please sign in to comment.