From 32f4c3cf7b4ee11954ad088981869cd4dbc9c7ef Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Tue, 9 Sep 2014 07:44:14 -0700 Subject: [PATCH] Handle relative redirects in HttpUrlFetcher. Fixes #119 --- .../load/data/HttpUrlFetcherServerTest.java | 34 ++++++++++++++++--- .../glide/load/data/HttpUrlFetcher.java | 6 +++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherServerTest.java b/library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherServerTest.java index bbc3374a12..0a05e38e21 100644 --- a/library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherServerTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherServerTest.java @@ -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; @@ -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; @@ -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. } } @@ -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)); diff --git a/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java b/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java index 4917d91b30..325eb11359 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java +++ b/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java @@ -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; @@ -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) {