summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordaniel oeh <daniel.oeh@gmail.com>2014-03-19 13:24:28 +0100
committerdaniel oeh <daniel.oeh@gmail.com>2014-03-19 13:24:28 +0100
commitb454ab09740dc8557a670c9c1d4bc8cd19ea129c (patch)
tree412b61fb6a1787269d634be91c4c5573fd977bef /src
parent361e56385ba8fe445b2400cd5bf83958945b9293 (diff)
downloadAntennaPod-b454ab09740dc8557a670c9c1d4bc8cd19ea129c.zip
Don't encode already encoded URLs. fixes #376
Diffstat (limited to 'src')
-rw-r--r--src/de/danoeh/antennapod/service/download/HttpDownloader.java14
-rw-r--r--src/de/danoeh/antennapod/util/URIUtil.java35
-rw-r--r--src/instrumentationTest/de/test/antennapod/util/URIUtilTest.java21
3 files changed, 58 insertions, 12 deletions
diff --git a/src/de/danoeh/antennapod/service/download/HttpDownloader.java b/src/de/danoeh/antennapod/service/download/HttpDownloader.java
index 442b57c0b..273c98b0d 100644
--- a/src/de/danoeh/antennapod/service/download/HttpDownloader.java
+++ b/src/de/danoeh/antennapod/service/download/HttpDownloader.java
@@ -7,6 +7,7 @@ import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.StorageUtils;
+import de.danoeh.antennapod.util.URIUtil;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
@@ -29,24 +30,13 @@ public class HttpDownloader extends Downloader {
super(request);
}
- private URI getURIFromRequestUrl(String source) {
- try {
- URL url = new URL(source);
- return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
- } catch (MalformedURLException e) {
- throw new IllegalArgumentException(e);
- } catch (URISyntaxException e) {
- throw new IllegalArgumentException(e);
- }
- }
-
@Override
protected void download() {
HttpClient httpClient = AntennapodHttpClient.getHttpClient();
BufferedOutputStream out = null;
InputStream connection = null;
try {
- HttpGet httpGet = new HttpGet(getURIFromRequestUrl(request.getSource()));
+ HttpGet httpGet = new HttpGet(URIUtil.getURIFromRequestUrl(request.getSource()));
String userInfo = httpGet.getURI().getUserInfo();
if (userInfo != null) {
String[] parts = userInfo.split(":");
diff --git a/src/de/danoeh/antennapod/util/URIUtil.java b/src/de/danoeh/antennapod/util/URIUtil.java
new file mode 100644
index 000000000..ed68a01d7
--- /dev/null
+++ b/src/de/danoeh/antennapod/util/URIUtil.java
@@ -0,0 +1,35 @@
+package de.danoeh.antennapod.util;
+
+import android.util.Log;
+import de.danoeh.antennapod.AppConfig;
+
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+/**
+ * Utility methods for dealing with URL encoding.
+ */
+public class URIUtil {
+ private static final String TAG = "URIUtil";
+
+ private URIUtil() {}
+
+ public static URI getURIFromRequestUrl(String source) {
+ // try without encoding the URI
+ try {
+ return new URI(source);
+ } catch (URISyntaxException e) {
+ if (AppConfig.DEBUG) Log.d(TAG, "Source is not encoded, encoding now");
+ }
+ try {
+ URL url = new URL(source);
+ return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
+ } catch (MalformedURLException e) {
+ throw new IllegalArgumentException(e);
+ } catch (URISyntaxException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+}
diff --git a/src/instrumentationTest/de/test/antennapod/util/URIUtilTest.java b/src/instrumentationTest/de/test/antennapod/util/URIUtilTest.java
new file mode 100644
index 000000000..a7cba4c03
--- /dev/null
+++ b/src/instrumentationTest/de/test/antennapod/util/URIUtilTest.java
@@ -0,0 +1,21 @@
+package instrumentationTest.de.test.antennapod.util;
+
+import android.test.AndroidTestCase;
+import de.danoeh.antennapod.util.URIUtil;
+
+/**
+ * Test class for URIUtil
+ */
+public class URIUtilTest extends AndroidTestCase {
+
+ public void testGetURIFromRequestUrlShouldNotEncode() {
+ final String testUrl = "http://example.com/this%20is%20encoded";
+ assertEquals(testUrl, URIUtil.getURIFromRequestUrl(testUrl).toString());
+ }
+
+ public void testGetURIFromRequestUrlShouldEncode() {
+ final String testUrl = "http://example.com/this is not encoded";
+ final String expected = "http://example.com/this%20is%20not%20encoded";
+ assertEquals(expected, URIUtil.getURIFromRequestUrl(testUrl).toString());
+ }
+}