Reconfigured authentication for downloading files - fixing s3 problems
This commit is contained in:
parent
8e281890dd
commit
bc7c319a01
|
@ -2,13 +2,21 @@ package de.danoeh.antennapod.core.service.download;
|
||||||
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import com.squareup.okhttp.Interceptor;
|
||||||
import com.squareup.okhttp.OkHttpClient;
|
import com.squareup.okhttp.OkHttpClient;
|
||||||
import com.squareup.okhttp.Protocol;
|
import com.squareup.okhttp.Protocol;
|
||||||
import com.squareup.okhttp.Request;
|
import com.squareup.okhttp.Request;
|
||||||
import com.squareup.okhttp.Response;
|
import com.squareup.okhttp.Response;
|
||||||
import com.squareup.okhttp.ResponseBody;
|
import com.squareup.okhttp.ResponseBody;
|
||||||
|
import de.danoeh.antennapod.core.ClientConfig;
|
||||||
|
import de.danoeh.antennapod.core.R;
|
||||||
|
import de.danoeh.antennapod.core.feed.FeedImage;
|
||||||
|
import de.danoeh.antennapod.core.feed.FeedMedia;
|
||||||
|
import de.danoeh.antennapod.core.util.DateUtils;
|
||||||
|
import de.danoeh.antennapod.core.util.DownloadError;
|
||||||
|
import de.danoeh.antennapod.core.util.StorageUtils;
|
||||||
|
import de.danoeh.antennapod.core.util.URIUtil;
|
||||||
|
import okio.ByteString;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
|
@ -24,16 +32,6 @@ import java.net.UnknownHostException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import de.danoeh.antennapod.core.ClientConfig;
|
|
||||||
import de.danoeh.antennapod.core.R;
|
|
||||||
import de.danoeh.antennapod.core.feed.FeedImage;
|
|
||||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
|
||||||
import de.danoeh.antennapod.core.util.DateUtils;
|
|
||||||
import de.danoeh.antennapod.core.util.DownloadError;
|
|
||||||
import de.danoeh.antennapod.core.util.StorageUtils;
|
|
||||||
import de.danoeh.antennapod.core.util.URIUtil;
|
|
||||||
import okio.ByteString;
|
|
||||||
|
|
||||||
public class HttpDownloader extends Downloader {
|
public class HttpDownloader extends Downloader {
|
||||||
private static final String TAG = "HttpDownloader";
|
private static final String TAG = "HttpDownloader";
|
||||||
|
|
||||||
|
@ -59,7 +57,8 @@ public class HttpDownloader extends Downloader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OkHttpClient httpClient = AntennapodHttpClient.getHttpClient();
|
OkHttpClient httpClient = AntennapodHttpClient.newHttpClient();
|
||||||
|
httpClient.interceptors().add(new BasicAuthorizationInterceptor(request));
|
||||||
RandomAccessFile out = null;
|
RandomAccessFile out = null;
|
||||||
InputStream connection;
|
InputStream connection;
|
||||||
ResponseBody responseBody = null;
|
ResponseBody responseBody = null;
|
||||||
|
@ -89,19 +88,6 @@ public class HttpDownloader extends Downloader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add authentication information
|
|
||||||
String userInfo = uri.getUserInfo();
|
|
||||||
if (userInfo != null) {
|
|
||||||
String[] parts = userInfo.split(":");
|
|
||||||
if (parts.length == 2) {
|
|
||||||
String credentials = encodeCredentials(parts[0], parts[1], "ISO-8859-1");
|
|
||||||
httpReq.header("Authorization", credentials);
|
|
||||||
}
|
|
||||||
} else if (!TextUtils.isEmpty(request.getUsername()) && request.getPassword() != null) {
|
|
||||||
String credentials = encodeCredentials(request.getUsername(), request.getPassword(),
|
|
||||||
"ISO-8859-1");
|
|
||||||
httpReq.header("Authorization", credentials);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add range header if necessary
|
// add range header if necessary
|
||||||
if (fileExists) {
|
if (fileExists) {
|
||||||
|
@ -111,6 +97,7 @@ public class HttpDownloader extends Downloader {
|
||||||
}
|
}
|
||||||
|
|
||||||
Response response;
|
Response response;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = httpClient.newCall(httpReq.build()).execute();
|
response = httpClient.newCall(httpReq.build()).execute();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -118,8 +105,7 @@ public class HttpDownloader extends Downloader {
|
||||||
if (e.getMessage().contains("PROTOCOL_ERROR")) {
|
if (e.getMessage().contains("PROTOCOL_ERROR")) {
|
||||||
httpClient.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
|
httpClient.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
|
||||||
response = httpClient.newCall(httpReq.build()).execute();
|
response = httpClient.newCall(httpReq.build()).execute();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,27 +119,6 @@ public class HttpDownloader extends Downloader {
|
||||||
|
|
||||||
Log.d(TAG, "Response code is " + response.code());
|
Log.d(TAG, "Response code is " + response.code());
|
||||||
|
|
||||||
if(!response.isSuccessful() && response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
|
|
||||||
Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoding");
|
|
||||||
if (userInfo != null) {
|
|
||||||
String[] parts = userInfo.split(":");
|
|
||||||
if (parts.length == 2) {
|
|
||||||
String credentials = encodeCredentials(parts[0], parts[1], "UTF-8");
|
|
||||||
httpReq.header("Authorization", credentials);
|
|
||||||
}
|
|
||||||
} else if (!TextUtils.isEmpty(request.getUsername()) && request.getPassword() != null) {
|
|
||||||
String credentials = encodeCredentials(request.getUsername(), request.getPassword(),
|
|
||||||
"UTF-8");
|
|
||||||
httpReq.header("Authorization", credentials);
|
|
||||||
}
|
|
||||||
response = httpClient.newCall(httpReq.build()).execute();
|
|
||||||
responseBody = response.body();
|
|
||||||
contentEncodingHeader = response.header("Content-Encoding");
|
|
||||||
if(!TextUtils.isEmpty(contentEncodingHeader)) {
|
|
||||||
isGzip = TextUtils.equals(contentEncodingHeader.toLowerCase(), "gzip");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.isSuccessful() && response.code() == HttpURLConnection.HTTP_NOT_MODIFIED) {
|
if (!response.isSuccessful() && response.code() == HttpURLConnection.HTTP_NOT_MODIFIED) {
|
||||||
Log.d(TAG, "Feed '" + request.getSource() + "' not modified since last update, Download canceled");
|
Log.d(TAG, "Feed '" + request.getSource() + "' not modified since last update, Download canceled");
|
||||||
onCancelled();
|
onCancelled();
|
||||||
|
@ -190,7 +155,8 @@ public class HttpDownloader extends Downloader {
|
||||||
if (contentLen != null) {
|
if (contentLen != null) {
|
||||||
try {
|
try {
|
||||||
contentLength = Integer.parseInt(contentLen);
|
contentLength = Integer.parseInt(contentLen);
|
||||||
} catch(NumberFormatException e) {}
|
} catch (NumberFormatException e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Log.d(TAG, "content length: " + contentLength);
|
Log.d(TAG, "content length: " + contentLength);
|
||||||
String contentType = response.header("Content-Type");
|
String contentType = response.header("Content-Type");
|
||||||
|
@ -343,4 +309,62 @@ public class HttpDownloader extends Downloader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class BasicAuthorizationInterceptor implements Interceptor {
|
||||||
|
|
||||||
|
private DownloadRequest downloadRequest;
|
||||||
|
|
||||||
|
public BasicAuthorizationInterceptor(DownloadRequest downloadRequest) {
|
||||||
|
this.downloadRequest = downloadRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response intercept(Chain chain) throws IOException {
|
||||||
|
Request request = chain.request();
|
||||||
|
String userInfo = URIUtil.getURIFromRequestUrl(downloadRequest.getSource()).getUserInfo();
|
||||||
|
|
||||||
|
Response response = chain.proceed(request);
|
||||||
|
|
||||||
|
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
Request.Builder newRequest = request.newBuilder();
|
||||||
|
|
||||||
|
Log.d(TAG, "Authorization failed, re-trying with ISO-8859-1 encoded credentials");
|
||||||
|
if (userInfo != null) {
|
||||||
|
String[] parts = userInfo.split(":");
|
||||||
|
if (parts.length == 2) {
|
||||||
|
String credentials = encodeCredentials(parts[0], parts[1], "ISO-8859-1");
|
||||||
|
newRequest.header("Authorization", credentials);
|
||||||
|
}
|
||||||
|
} else if (!TextUtils.isEmpty(downloadRequest.getUsername()) && downloadRequest.getPassword() != null) {
|
||||||
|
String credentials = encodeCredentials(downloadRequest.getUsername(), downloadRequest.getPassword(),
|
||||||
|
"ISO-8859-1");
|
||||||
|
newRequest.header("Authorization", credentials);
|
||||||
|
}
|
||||||
|
|
||||||
|
response = chain.proceed(newRequest.build());
|
||||||
|
|
||||||
|
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoded credentials");
|
||||||
|
if (userInfo != null) {
|
||||||
|
String[] parts = userInfo.split(":");
|
||||||
|
if (parts.length == 2) {
|
||||||
|
String credentials = encodeCredentials(parts[0], parts[1], "UTF-8");
|
||||||
|
newRequest.header("Authorization", credentials);
|
||||||
|
}
|
||||||
|
} else if (!TextUtils.isEmpty(downloadRequest.getUsername()) && downloadRequest.getPassword() != null) {
|
||||||
|
String credentials = encodeCredentials(downloadRequest.getUsername(), downloadRequest.getPassword(),
|
||||||
|
"UTF-8");
|
||||||
|
newRequest.header("Authorization", credentials);
|
||||||
|
}
|
||||||
|
|
||||||
|
return chain.proceed(newRequest.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue