diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-03-11 14:13:53 +0100 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-03-11 14:13:53 +0100 |
commit | 35f53c74ebcd8a7c970c048f7ec39f9e7de1a552 (patch) | |
tree | 967c1cca8e45183d64fa6991c3e61343eed8e699 /src | |
parent | 437cdabadfd6324f87ce367fdaf5aed9036e5ae0 (diff) | |
parent | 0d8bdf6d23843874d0f61ebbca6acc9ffdd3ae13 (diff) | |
download | AntennaPod-35f53c74ebcd8a7c970c048f7ec39f9e7de1a552.zip |
Merge branch 'master' of git://github.com/ortylp/AntennaPod into ortylp-master
Diffstat (limited to 'src')
-rw-r--r-- | src/de/danoeh/antennapod/service/download/APRedirectHandler.java | 55 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/service/download/HttpDownloader.java | 15 |
2 files changed, 64 insertions, 6 deletions
diff --git a/src/de/danoeh/antennapod/service/download/APRedirectHandler.java b/src/de/danoeh/antennapod/service/download/APRedirectHandler.java new file mode 100644 index 000000000..9416ef9d7 --- /dev/null +++ b/src/de/danoeh/antennapod/service/download/APRedirectHandler.java @@ -0,0 +1,55 @@ +package de.danoeh.antennapod.service.download; + +import java.net.URI; + +import org.apache.http.Header; +import org.apache.http.HttpResponse; +import org.apache.http.impl.client.DefaultRedirectHandler; +import org.apache.http.protocol.HttpContext; + +import de.danoeh.antennapod.AppConfig; +import android.util.Log; + +public class APRedirectHandler extends DefaultRedirectHandler { + // Identifier for logger + private static final String TAG = "APRedirectHandler"; + // Header field, which has to be potentially fixed + private static final String LOC = "Location"; + // Regular expressions for character strings, which should not appear in URLs + private static final String CHi[] = { "\\{", "\\}", "\\|", "\\\\", "\\^", "~", "\\[", "\\]", "\\`"}; + private static final String CHo[] = { "%7B", "%7D", "%7C", "%5C", "%5E", "%7E", "%5B", "%5D", "%60"}; + + /** + * Workaround for broken URLs in redirection. + * Proper solution involves LaxRedirectStrategy() which is not available in + * current API yet. + */ + @Override + public URI getLocationURI(HttpResponse response, HttpContext context) + throws org.apache.http.ProtocolException { + + Header h[] = response.getHeaders(LOC); + if (h.length>0) { + String s = h[0].getValue(); + + // Fix broken URL + for(int i=0; i<CHi.length;i++) + s = s.replaceAll(CHi[i], CHo[i]); + + // If anything had to be fixed, then replace the header + if (!s.equals(h[0].getValue())) + { + if (AppConfig.DEBUG) + Log.d(TAG, "Original URL: " + h[0].getValue()); + + response.setHeader(LOC, s); + + if (AppConfig.DEBUG) + Log.d(TAG, "Fixed URL: " + s); + } + } + + // call DefaultRedirectHandler with fixed URL + return super.getLocationURI(response, context); + } +} diff --git a/src/de/danoeh/antennapod/service/download/HttpDownloader.java b/src/de/danoeh/antennapod/service/download/HttpDownloader.java index 7cb487c1a..ec2b991b4 100644 --- a/src/de/danoeh/antennapod/service/download/HttpDownloader.java +++ b/src/de/danoeh/antennapod/service/download/HttpDownloader.java @@ -16,6 +16,8 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.params.HttpClientParams; +import org.apache.http.impl.client.AbstractHttpClient; +import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; @@ -25,6 +27,7 @@ import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.DownloadStatus; +import de.danoeh.antennapod.service.download.APRedirectHandler; import de.danoeh.antennapod.util.DownloadError; import de.danoeh.antennapod.util.StorageUtils; @@ -42,8 +45,8 @@ public class HttpDownloader extends Downloader { super(downloaderCallback, status); } - private AndroidHttpClient createHttpClient() { - AndroidHttpClient httpClient = AndroidHttpClient.newInstance(""); + private DefaultHttpClient createHttpClient() { + DefaultHttpClient httpClient = new DefaultHttpClient(); HttpParams params = httpClient.getParams(); params.setIntParameter("http.protocol.max-redirects", MAX_REDIRECTS); params.setBooleanParameter("http.protocol.reject-relative-redirect", @@ -51,12 +54,15 @@ public class HttpDownloader extends Downloader { HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT); HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT); HttpClientParams.setRedirecting(params, true); + + // Workaround for broken URLs in redirection + ((AbstractHttpClient)httpClient).setRedirectHandler(new APRedirectHandler()); return httpClient; } @Override protected void download() { - AndroidHttpClient httpClient = null; + DefaultHttpClient httpClient = null; OutputStream out = null; InputStream connection = null; try { @@ -142,9 +148,6 @@ public class HttpDownloader extends Downloader { } finally { IOUtils.closeQuietly(connection); IOUtils.closeQuietly(out); - if (httpClient != null) { - httpClient.close(); - } } } |