Fix streaming password protected media with http redirect

This commit is contained in:
ByteHamster 2022-01-04 16:22:05 +01:00
parent 814cd0f88d
commit dee8d4f410
1 changed files with 18 additions and 7 deletions

View File

@ -9,12 +9,15 @@ import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.URIUtil; import de.danoeh.antennapod.core.util.URIUtil;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.List;
import okhttp3.Interceptor; import okhttp3.Interceptor;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
public class BasicAuthorizationInterceptor implements Interceptor { public class BasicAuthorizationInterceptor implements Interceptor {
private static final String TAG = "BasicAuthInterceptor"; private static final String TAG = "BasicAuthInterceptor";
private static final String HEADER_AUTHORIZATION = "Authorization";
@Override @Override
@NonNull @NonNull
@ -27,6 +30,19 @@ public class BasicAuthorizationInterceptor implements Interceptor {
return response; return response;
} }
Request.Builder newRequest = request.newBuilder();
if (!TextUtils.equals(response.request().url().toString(), request.url().toString())) {
// Redirect detected. OkHTTP does not re-add the headers on redirect, so calling the new location directly.
newRequest.url(response.request().url());
List<String> authorizationHeaders = request.headers().values(HEADER_AUTHORIZATION);
if (!authorizationHeaders.isEmpty() && !TextUtils.isEmpty(authorizationHeaders.get(0))) {
// Call already had authorization headers. Try again with the same credentials.
newRequest.header(HEADER_AUTHORIZATION, authorizationHeaders.get(0));
return chain.proceed(newRequest.build());
}
}
String userInfo; String userInfo;
if (request.tag() instanceof DownloadRequest) { if (request.tag() instanceof DownloadRequest) {
DownloadRequest downloadRequest = (DownloadRequest) request.tag(); DownloadRequest downloadRequest = (DownloadRequest) request.tag();
@ -49,14 +65,9 @@ public class BasicAuthorizationInterceptor implements Interceptor {
return response; return response;
} }
Request.Builder newRequest = request.newBuilder();
if (!TextUtils.equals(response.request().url().toString(), request.url().toString())) {
newRequest.url(response.request().url());
}
Log.d(TAG, "Authorization failed, re-trying with ISO-8859-1 encoded credentials"); Log.d(TAG, "Authorization failed, re-trying with ISO-8859-1 encoded credentials");
String credentials = HttpDownloader.encodeCredentials(parts[0], parts[1], "ISO-8859-1"); String credentials = HttpDownloader.encodeCredentials(parts[0], parts[1], "ISO-8859-1");
newRequest.header("Authorization", credentials); newRequest.header(HEADER_AUTHORIZATION, credentials);
response = chain.proceed(newRequest.build()); response = chain.proceed(newRequest.build());
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) { if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
@ -65,7 +76,7 @@ public class BasicAuthorizationInterceptor implements Interceptor {
Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoded credentials"); Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoded credentials");
credentials = HttpDownloader.encodeCredentials(parts[0], parts[1], "UTF-8"); credentials = HttpDownloader.encodeCredentials(parts[0], parts[1], "UTF-8");
newRequest.header("Authorization", credentials); newRequest.header(HEADER_AUTHORIZATION, credentials);
return chain.proceed(newRequest.build()); return chain.proceed(newRequest.build());
} }
} }