From 890c4d32d1ef5e957137c3357cb2d52d8fb9f2be Mon Sep 17 00:00:00 2001 From: Anderson Mesquita Date: Fri, 18 Sep 2020 13:40:36 -0400 Subject: [PATCH 1/2] Extract method to get longest cookie This reduces duplication and makes it easier to know what `getCookie` is doing, since all domains are now clustered together and it's easy to see the difference between them.. --- .../awais/instagrabber/utils/CookieUtils.java | 97 ++++++------------- 1 file changed, 30 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/CookieUtils.java b/app/src/main/java/awais/instagrabber/utils/CookieUtils.java index e3e4f800..207f2600 100644 --- a/app/src/main/java/awais/instagrabber/utils/CookieUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/CookieUtils.java @@ -10,6 +10,8 @@ import java.net.CookieStore; import java.net.HttpCookie; import java.net.URI; import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.List; import awais.instagrabber.BuildConfig; import awaisomereport.LogCollector; @@ -74,79 +76,40 @@ public final class CookieUtils { @Nullable public static String getCookie(@Nullable final String webViewUrl) { - int lastLongestCookieLength = 0; - String mainCookie = null; + final List domains = Arrays.asList( + "https://instagram.com", + "https://instagram.com/", + "http://instagram.com", + "http://instagram.com", + "https://www.instagram.com", + "https://www.instagram.com/", + "http://www.instagram.com", + "http://www.instagram.com/" + ); - String cookie; if (!TextUtils.isEmpty(webViewUrl)) { - cookie = COOKIE_MANAGER.getCookie(webViewUrl); + domains.add(0, webViewUrl); + } + + return getLongestCookie(domains); + } + + @Nullable + private static String getLongestCookie(final List domains) { + int longestLength = 0; + String longestCookie = null; + + for (final String domain : domains) { + final String cookie = COOKIE_MANAGER.getCookie(domain); if (cookie != null) { - final int cookieLen = cookie.length(); - if (cookieLen > lastLongestCookieLength) { - mainCookie = cookie; - lastLongestCookieLength = cookieLen; + final int cookieLength = cookie.length(); + if (cookieLength > longestLength) { + longestCookie = cookie; + longestLength = cookieLength; } } } - cookie = COOKIE_MANAGER.getCookie("https://instagram.com"); - if (cookie != null) { - final int cookieLen = cookie.length(); - if (cookieLen > lastLongestCookieLength) { - mainCookie = cookie; - lastLongestCookieLength = cookieLen; - } - } - cookie = COOKIE_MANAGER.getCookie("https://instagram.com/"); - if (cookie != null) { - final int cookieLen = cookie.length(); - if (cookieLen > lastLongestCookieLength) { - mainCookie = cookie; - lastLongestCookieLength = cookieLen; - } - } - cookie = COOKIE_MANAGER.getCookie("http://instagram.com"); - if (cookie != null) { - final int cookieLen = cookie.length(); - if (cookieLen > lastLongestCookieLength) { - mainCookie = cookie; - lastLongestCookieLength = cookieLen; - } - } - cookie = COOKIE_MANAGER.getCookie("http://instagram.com/"); - if (cookie != null) { - final int cookieLen = cookie.length(); - if (cookieLen > lastLongestCookieLength) { - mainCookie = cookie; - lastLongestCookieLength = cookieLen; - } - } - cookie = COOKIE_MANAGER.getCookie("https://www.instagram.com"); - if (cookie != null) { - final int cookieLen = cookie.length(); - if (cookieLen > lastLongestCookieLength) { - mainCookie = cookie; - lastLongestCookieLength = cookieLen; - } - } - cookie = COOKIE_MANAGER.getCookie("https://www.instagram.com/"); - if (cookie != null) { - final int cookieLen = cookie.length(); - if (cookieLen > lastLongestCookieLength) { - mainCookie = cookie; - lastLongestCookieLength = cookieLen; - } - } - cookie = COOKIE_MANAGER.getCookie("http://www.instagram.com"); - if (cookie != null) { - final int cookieLen = cookie.length(); - if (cookieLen > lastLongestCookieLength) { - mainCookie = cookie; - lastLongestCookieLength = cookieLen; - } - } - cookie = COOKIE_MANAGER.getCookie("http://www.instagram.com/"); - if (cookie != null && cookie.length() > lastLongestCookieLength) mainCookie = cookie; - return mainCookie; + return longestCookie; } } From dd0f1e397c849cf06a2a61c2967bb0a71ca0833d Mon Sep 17 00:00:00 2001 From: Anderson Mesquita Date: Fri, 18 Sep 2020 13:48:40 -0400 Subject: [PATCH 2/2] Extract method to get cookie value Currently, trying to get a `csrftoken` from an empty cookie string will crash the application. This creates a helper method to extract a cookie value given a cookie name by pattern matching on the cookie string and returning the value if one is found. Fixes: #167 --- .../awais/instagrabber/utils/CookieUtils.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/CookieUtils.java b/app/src/main/java/awais/instagrabber/utils/CookieUtils.java index 207f2600..17c43e2b 100644 --- a/app/src/main/java/awais/instagrabber/utils/CookieUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/CookieUtils.java @@ -12,6 +12,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import awais.instagrabber.BuildConfig; import awaisomereport.LogCollector; @@ -56,22 +58,23 @@ public final class CookieUtils { } @Nullable - public static String getUserIdFromCookie(final String cookie) { - if (!TextUtils.isEmpty(cookie)) { - final int uidIndex = cookie.indexOf("ds_user_id="); - if (uidIndex > 0) { - String uid = cookie.split("ds_user_id=")[1].split(";")[0]; - return !TextUtils.isEmpty(uid) ? uid : null; - } - } - return null; + public static String getUserIdFromCookie(final String cookies) { + return getCookieValue(cookies, "ds_user_id"); + } + + @Nullable + public static String getCsrfTokenFromCookie(final String cookies) { + return getCookieValue(cookies, "csrftoken"); } - public static String getCsrfTokenFromCookie(final String cookie) { - if (cookie == null) { - return null; + @Nullable + private static String getCookieValue(final String cookies, final String name) { + final Pattern pattern = Pattern.compile(name + "=(.+?);"); + final Matcher matcher = pattern.matcher(cookies); + if (matcher.find()) { + return matcher.group(1); } - return cookie.split("csrftoken=")[1].split(";")[0]; + return null; } @Nullable