fix regression (issue #96): fix broken URLs in redirection instead of failing
This commit is contained in:
parent
ec1992f36c
commit
0d8bdf6d23
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue