From 430d4e1ccde9dcd7db2d71eaf23779da8bd8f4df Mon Sep 17 00:00:00 2001 From: Vincent Nagel Date: Sun, 15 Mar 2020 20:04:34 -0500 Subject: [PATCH] ageRestrictedContent cookie only sent for youtube Now the age restricted content cookie is only sent when sending a request to youtube. There's no need to remove the cookie when the service changes because whether to add the cookie is determined by looking at the url the request is being sent to. --- .../org/schabi/newpipe/DownloaderImpl.java | 42 ++++++++++++------- .../java/org/schabi/newpipe/MainActivity.java | 2 +- .../settings/ContentSettingsFragment.java | 2 +- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java b/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java index ac6e9ef59..9f1722fb3 100644 --- a/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java +++ b/app/src/main/java/org/schabi/newpipe/DownloaderImpl.java @@ -7,14 +7,12 @@ import android.preference.PreferenceManager; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Request; import org.schabi.newpipe.extractor.downloader.Response; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.util.CookieUtils; import org.schabi.newpipe.util.InfoCache; -import org.schabi.newpipe.util.ServiceHelper; import org.schabi.newpipe.util.TLSSocketFactoryCompat; import java.io.IOException; @@ -49,6 +47,7 @@ public final class DownloaderImpl extends Downloader { public static final String YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY = "youtube_age_restricted_content_cookie_key"; public static final String YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE = "PREF=f2=8000000"; + public static final String YOUTUBE_DOMAIN = "youtube.com"; private static DownloaderImpl instance; private Map mCookies; @@ -131,31 +130,42 @@ public final class DownloaderImpl extends Downloader { } } - public String getCookies() { - return CookieUtils.concatCookies(mCookies.values()); + public String getCookies(final String url) { + List resultCookies = new ArrayList<>(); + if (url.contains(YOUTUBE_DOMAIN)) { + String youtubeCookie = getCookie(YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY); + if (youtubeCookie != null) { + resultCookies.add(youtubeCookie); + } + } + // Recaptcha cookie is always added TODO: not sure if this is necessary + String recaptchaCookie = getCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY); + if (recaptchaCookie != null) { + resultCookies.add(recaptchaCookie); + } + return CookieUtils.concatCookies(resultCookies); } public String getCookie(final String key){ return mCookies.get(key); } - public void setCookie(final String key, final String value){ - mCookies.put(key, value); + public void setCookie(final String key, final String cookie){ + mCookies.put(key, cookie); } public void removeCookie(final String key){ mCookies.remove(key); } - public void updateAgeRestrictedContentCookies(Context context){ + public void updateAgeRestrictedContentCookies(final Context context){ String showAgeRestrictedContentKey = context.getString(R.string.show_age_restricted_content); - int currentServiceId = ServiceHelper.getSelectedServiceId(context); boolean showAgeRestrictedContent = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(showAgeRestrictedContentKey, false); - updateAgeRestrictedContentCookies(currentServiceId, showAgeRestrictedContent); + updateAgeRestrictedContentCookies(showAgeRestrictedContent); } - public void updateAgeRestrictedContentCookies(int currentServiceId, boolean showAgeRestrictedContent) { - if (currentServiceId == ServiceList.YouTube.getServiceId() && !showAgeRestrictedContent) { + public void updateAgeRestrictedContentCookies(boolean showAgeRestrictedContent) { + if (!showAgeRestrictedContent) { setCookie(YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY, YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE); } else { removeCookie(YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY); @@ -186,8 +196,9 @@ public final class DownloaderImpl extends Downloader { .method("GET", null).url(siteUrl) .addHeader("User-Agent", USER_AGENT); - if (!mCookies.isEmpty()) { - requestBuilder.addHeader("Cookie", getCookies()); + String cookies = getCookies(siteUrl); + if (!cookies.isEmpty()) { + requestBuilder.addHeader("Cookie", cookies); } final okhttp3.Request request = requestBuilder.build(); @@ -226,8 +237,9 @@ public final class DownloaderImpl extends Downloader { .method(httpMethod, requestBody).url(url) .addHeader("User-Agent", USER_AGENT); - if (!mCookies.isEmpty()) { - requestBuilder.addHeader("Cookie", getCookies()); + String cookies = getCookies(url); + if (!cookies.isEmpty()) { + requestBuilder.addHeader("Cookie", cookies); } for (Map.Entry> pair : headers.entrySet()) { diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index b36cdadb4..4b3dab0c9 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -242,9 +242,9 @@ public class MainActivity extends AppCompatActivity { drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)) .setChecked(false); ServiceHelper.setSelectedServiceId(this, item.getItemId()); + drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(true); drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)) .setChecked(true); - DownloaderImpl.getInstance().updateAgeRestrictedContentCookies(getApplicationContext()); } private void tabSelected(final MenuItem item) throws ExtractionException { diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index 1b0f7090c..79a1fe72a 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -92,7 +92,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { if (preference.getKey().equals(showAgeRestrictedContentKey)) { Context context = getContext(); - if(context != null){ + if (context != null) { DownloaderImpl.getInstance().updateAgeRestrictedContentCookies(context); } }