Always add UserAgent to requests

This commit is contained in:
ByteHamster 2020-03-16 01:09:32 +01:00
parent c0a688ce54
commit 3167c7a5d2
7 changed files with 28 additions and 12 deletions

View File

@ -43,8 +43,7 @@ public class ItunesPodcastSearcher implements PodcastSearcher {
OkHttpClient client = AntennapodHttpClient.getHttpClient(); OkHttpClient client = AntennapodHttpClient.getHttpClient();
Request.Builder httpReq = new Request.Builder() Request.Builder httpReq = new Request.Builder()
.url(formattedUrl) .url(formattedUrl);
.header("User-Agent", ClientConfig.USER_AGENT);
List<PodcastSearchResult> podcasts = new ArrayList<>(); List<PodcastSearchResult> podcasts = new ArrayList<>();
try { try {
Response response = client.newCall(httpReq.build()).execute(); Response response = client.newCall(httpReq.build()).execute();

View File

@ -57,8 +57,7 @@ public class ItunesTopListLoader {
return Single.create((SingleOnSubscribe<String>) emitter -> { return Single.create((SingleOnSubscribe<String>) emitter -> {
OkHttpClient client = AntennapodHttpClient.getHttpClient(); OkHttpClient client = AntennapodHttpClient.getHttpClient();
Request.Builder httpReq = new Request.Builder() Request.Builder httpReq = new Request.Builder()
.url(podcast.feedUrl) .url(podcast.feedUrl);
.header("User-Agent", ClientConfig.USER_AGENT);
try { try {
Response response = client.newCall(httpReq.build()).execute(); Response response = client.newCall(httpReq.build()).execute();
if (response.isSuccessful()) { if (response.isSuccessful()) {
@ -84,7 +83,6 @@ public class ItunesTopListLoader {
Log.d(TAG, "Feed URL " + String.format(url, country)); Log.d(TAG, "Feed URL " + String.format(url, country));
Request.Builder httpReq = new Request.Builder() Request.Builder httpReq = new Request.Builder()
.cacheControl(new CacheControl.Builder().minFresh(1, TimeUnit.DAYS).build()) .cacheControl(new CacheControl.Builder().minFresh(1, TimeUnit.DAYS).build())
.header("User-Agent", ClientConfig.USER_AGENT)
.url(String.format(url, country)); .url(String.format(url, country));
try (Response response = client.newCall(httpReq.build()).execute()) { try (Response response = client.newCall(httpReq.build()).execute()) {

View File

@ -71,7 +71,6 @@ public final class ChapterImageModelLoader implements ModelLoader<EmbeddedChapte
callback.onDataReady(ByteBuffer.wrap(imageContent)); callback.onDataReady(ByteBuffer.wrap(imageContent));
} else { } else {
Request.Builder httpReq = new Request.Builder(); Request.Builder httpReq = new Request.Builder();
httpReq.header("User-Agent", ClientConfig.USER_AGENT);
// Skipping would download the whole file // Skipping would download the whole file
httpReq.header("Range", "bytes=" + image.getPosition() httpReq.header("Range", "bytes=" + image.getPosition()
+ "-" + (image.getPosition() + image.getLength())); + "-" + (image.getPosition() + image.getLength()));

View File

@ -554,9 +554,8 @@ public class GpodnetService {
}.start(); }.start();
} }
private String executeRequest(@NonNull Request.Builder requestB) private String executeRequest(@NonNull Request.Builder requestB) throws GpodnetServiceException {
throws GpodnetServiceException { Request request = requestB.build();
Request request = requestB.header("User-Agent", ClientConfig.USER_AGENT).build();
String responseString = null; String responseString = null;
Response response = null; Response response = null;
ResponseBody body = null; ResponseBody body = null;
@ -584,7 +583,7 @@ public class GpodnetService {
"request and credentials must not be null"); "request and credentials must not be null");
} }
Request request = requestB.header("User-Agent", ClientConfig.USER_AGENT).build(); Request request = requestB.build();
String result = null; String result = null;
ResponseBody body = null; ResponseBody body = null;
try { try {

View File

@ -0,0 +1,20 @@
package de.danoeh.antennapod.core.service;
import de.danoeh.antennapod.core.ClientConfig;
import okhttp3.Interceptor;
import okhttp3.Response;
import java.io.IOException;
public class UserAgentInterceptor implements Interceptor {
public UserAgentInterceptor() {
}
@Override
public Response intercept(Chain chain) throws IOException {
return chain.proceed(chain.request().newBuilder()
.header("User-Agent", ClientConfig.USER_AGENT)
.build());
}
}

View File

@ -32,6 +32,7 @@ import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.UserAgentInterceptor;
import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.DBWriter;
import okhttp3.Cache; import okhttp3.Cache;
import okhttp3.CipherSuite; import okhttp3.CipherSuite;
@ -116,6 +117,7 @@ public class AntennapodHttpClient {
return response; return response;
}); });
builder.interceptors().add(new BasicAuthorizationInterceptor()); builder.interceptors().add(new BasicAuthorizationInterceptor());
builder.networkInterceptors().add(new UserAgentInterceptor());
// set cookie handler // set cookie handler
CookieManager cm = new CookieManager(); CookieManager cm = new CookieManager();

View File

@ -63,8 +63,7 @@ public class HttpDownloader extends Downloader {
try { try {
final URI uri = URIUtil.getURIFromRequestUrl(request.getSource()); final URI uri = URIUtil.getURIFromRequestUrl(request.getSource());
Request.Builder httpReq = new Request.Builder().url(uri.toURL()) Request.Builder httpReq = new Request.Builder().url(uri.toURL());
.header("User-Agent", ClientConfig.USER_AGENT);
httpReq.tag(request); httpReq.tag(request);
if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
// set header explicitly so that okhttp doesn't do transparent gzip // set header explicitly so that okhttp doesn't do transparent gzip